blob: d2125d3a3579697b36a98eb383f0f213a11eb8f8 [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 */
230void k_object_grant_access(void *object, struct k_thread *thread);
231
232#else
233static inline int _k_object_validate(void *obj, enum k_objects otype, int init)
234{
235 ARG_UNUSED(obj);
236 ARG_UNUSED(otype);
237 ARG_UNUSED(init);
238
239 return 0;
240}
241
242static inline void _k_object_init(void *obj)
243{
244 ARG_UNUSED(obj);
245}
246
247static inline void k_object_grant_access(void *object, struct k_thread *thread)
248{
249 ARG_UNUSED(object);
250 ARG_UNUSED(thread);
251}
252#endif /* CONFIG_USERSPACE */
253
Andrew Boie73abd322017-04-04 13:19:13 -0700254/* timeouts */
255
256struct _timeout;
257typedef void (*_timeout_func_t)(struct _timeout *t);
258
259struct _timeout {
260 sys_dnode_t node;
261 struct k_thread *thread;
262 sys_dlist_t *wait_q;
263 s32_t delta_ticks_from_prev;
264 _timeout_func_t func;
265};
266
267extern s32_t _timeout_remaining_get(struct _timeout *timeout);
268
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700269/**
270 * @typedef k_thread_entry_t
271 * @brief Thread entry point function type.
272 *
273 * A thread's entry point function is invoked when the thread starts executing.
274 * Up to 3 argument values can be passed to the function.
275 *
276 * The thread terminates execution permanently if the entry point function
277 * returns. The thread is responsible for releasing any shared resources
278 * it may own (such as mutexes and dynamically allocated memory), prior to
279 * returning.
280 *
281 * @param p1 First argument.
282 * @param p2 Second argument.
283 * @param p3 Third argument.
284 *
285 * @return N/A
286 */
287typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700288
289#ifdef CONFIG_THREAD_MONITOR
290struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700291 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700292 void *parameter1;
293 void *parameter2;
294 void *parameter3;
295};
296#endif
297
298/* can be used for creating 'dummy' threads, e.g. for pending on objects */
299struct _thread_base {
300
301 /* this thread's entry in a ready/wait queue */
302 sys_dnode_t k_q_node;
303
304 /* user facing 'thread options'; values defined in include/kernel.h */
305 u8_t user_options;
306
307 /* thread state */
308 u8_t thread_state;
309
310 /*
311 * scheduler lock count and thread priority
312 *
313 * These two fields control the preemptibility of a thread.
314 *
315 * When the scheduler is locked, sched_locked is decremented, which
316 * means that the scheduler is locked for values from 0xff to 0x01. A
317 * thread is coop if its prio is negative, thus 0x80 to 0xff when
318 * looked at the value as unsigned.
319 *
320 * By putting them end-to-end, this means that a thread is
321 * non-preemptible if the bundled value is greater than or equal to
322 * 0x0080.
323 */
324 union {
325 struct {
326#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
327 u8_t sched_locked;
328 s8_t prio;
329#else /* LITTLE and PDP */
330 s8_t prio;
331 u8_t sched_locked;
332#endif
333 };
334 u16_t preempt;
335 };
336
337 /* data returned by APIs */
338 void *swap_data;
339
340#ifdef CONFIG_SYS_CLOCK_EXISTS
341 /* this thread's entry in a timeout queue */
342 struct _timeout timeout;
343#endif
Andrew Boie2acfcd62017-08-30 14:31:03 -0700344
345#ifdef CONFIG_USERSPACE
346 /* Bit position in kernel object permissions bitfield for this thread */
347 unsigned int perm_index;
348#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700349};
350
351typedef struct _thread_base _thread_base_t;
352
353#if defined(CONFIG_THREAD_STACK_INFO)
354/* Contains the stack information of a thread */
355struct _thread_stack_info {
356 /* Stack Start */
357 u32_t start;
358 /* Stack Size */
359 u32_t size;
360};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700361
362typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700363#endif /* CONFIG_THREAD_STACK_INFO */
364
Chunlin Hane9c97022017-07-07 20:29:30 +0800365#if defined(CONFIG_USERSPACE)
366struct _mem_domain_info {
367 /* memory domain queue node */
368 sys_dnode_t mem_domain_q_node;
369 /* memory domain of the thread */
370 struct k_mem_domain *mem_domain;
371};
372
373#endif /* CONFIG_USERSPACE */
374
Andrew Boie73abd322017-04-04 13:19:13 -0700375struct k_thread {
376
377 struct _thread_base base;
378
379 /* defined by the architecture, but all archs need these */
380 struct _caller_saved caller_saved;
381 struct _callee_saved callee_saved;
382
383 /* static thread init data */
384 void *init_data;
385
386 /* abort function */
387 void (*fn_abort)(void);
388
389#if defined(CONFIG_THREAD_MONITOR)
390 /* thread entry and parameters description */
391 struct __thread_entry *entry;
392
393 /* next item in list of all threads */
394 struct k_thread *next_thread;
395#endif
396
397#ifdef CONFIG_THREAD_CUSTOM_DATA
398 /* crude thread-local storage */
399 void *custom_data;
400#endif
401
402#ifdef CONFIG_ERRNO
403 /* per-thread errno variable */
404 int errno_var;
405#endif
406
407#if defined(CONFIG_THREAD_STACK_INFO)
408 /* Stack Info */
409 struct _thread_stack_info stack_info;
410#endif /* CONFIG_THREAD_STACK_INFO */
411
Chunlin Hane9c97022017-07-07 20:29:30 +0800412#if defined(CONFIG_USERSPACE)
413 /* memory domain info of the thread */
414 struct _mem_domain_info mem_domain_info;
415#endif /* CONFIG_USERSPACE */
416
Andrew Boie73abd322017-04-04 13:19:13 -0700417 /* arch-specifics: must always be at the end */
418 struct _thread_arch arch;
419};
420
421typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400422typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700423#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400424
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400425enum execution_context_types {
426 K_ISR = 0,
427 K_COOP_THREAD,
428 K_PREEMPT_THREAD,
429};
430
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400431/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100432 * @defgroup profiling_apis Profiling APIs
433 * @ingroup kernel_apis
434 * @{
435 */
436
437/**
438 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
439 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700440 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100441 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
442 *
443 * CONFIG_MAIN_STACK_SIZE
444 * CONFIG_IDLE_STACK_SIZE
445 * CONFIG_ISR_STACK_SIZE
446 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
447 *
448 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
449 * produce output.
450 *
451 * @return N/A
452 */
453extern void k_call_stacks_analyze(void);
454
455/**
456 * @} end defgroup profiling_apis
457 */
458
459/**
Allan Stephensc98da842016-11-11 15:45:03 -0500460 * @defgroup thread_apis Thread APIs
461 * @ingroup kernel_apis
462 * @{
463 */
464
Benjamin Walshed240f22017-01-22 13:05:08 -0500465#endif /* !_ASMLANGUAGE */
466
467
468/*
469 * Thread user options. May be needed by assembly code. Common part uses low
470 * bits, arch-specific use high bits.
471 */
472
473/* system thread that must not abort */
474#define K_ESSENTIAL (1 << 0)
475
476#if defined(CONFIG_FP_SHARING)
477/* thread uses floating point registers */
478#define K_FP_REGS (1 << 1)
479#endif
480
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700481/* This thread has dropped from supervisor mode to user mode and consequently
482 * has additional restrictions
483 */
484#define K_USER (1 << 2)
485
Benjamin Walshed240f22017-01-22 13:05:08 -0500486#ifdef CONFIG_X86
487/* x86 Bitmask definitions for threads user options */
488
489#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
490/* thread uses SSEx (and also FP) registers */
491#define K_SSE_REGS (1 << 7)
492#endif
493#endif
494
495/* end - thread options */
496
497#if !defined(_ASMLANGUAGE)
498
Andrew Boie507852a2017-07-25 18:47:07 -0700499/* Using typedef deliberately here, this is quite intended to be an opaque
500 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
501 *
502 * The purpose of this data type is to clearly distinguish between the
503 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
504 * buffer which composes the stack data actually used by the underlying
505 * thread; they cannot be used interchangably as some arches precede the
506 * stack buffer region with guard areas that trigger a MPU or MMU fault
507 * if written to.
508 *
509 * APIs that want to work with the buffer inside should continue to use
510 * char *.
511 *
512 * Stacks should always be created with K_THREAD_STACK_DEFINE().
513 */
514struct __packed _k_thread_stack_element {
515 char data;
516};
517typedef struct _k_thread_stack_element *k_thread_stack_t;
518
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400519
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400520/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700521 * @brief Create a thread.
522 *
523 * This routine initializes a thread, then schedules it for execution.
524 *
525 * The new thread may be scheduled for immediate execution or a delayed start.
526 * If the newly spawned thread does not have a delayed start the kernel
527 * scheduler may preempt the current thread to allow the new thread to
528 * execute.
529 *
530 * Thread options are architecture-specific, and can include K_ESSENTIAL,
531 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
532 * them using "|" (the logical OR operator).
533 *
534 * Historically, users often would use the beginning of the stack memory region
535 * to store the struct k_thread data, although corruption will occur if the
536 * stack overflows this region and stack protection features may not detect this
537 * situation.
538 *
539 * @param new_thread Pointer to uninitialized struct k_thread
540 * @param stack Pointer to the stack space.
541 * @param stack_size Stack size in bytes.
542 * @param entry Thread entry function.
543 * @param p1 1st entry point parameter.
544 * @param p2 2nd entry point parameter.
545 * @param p3 3rd entry point parameter.
546 * @param prio Thread priority.
547 * @param options Thread options.
548 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
549 *
550 * @return ID of new thread.
551 */
Andrew Boie507852a2017-07-25 18:47:07 -0700552extern k_tid_t k_thread_create(struct k_thread *new_thread,
553 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700554 size_t stack_size,
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700555 k_thread_entry_t entry,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700556 void *p1, void *p2, void *p3,
557 int prio, u32_t options, s32_t delay);
558
Andrew Boie3f091b52017-08-30 14:34:14 -0700559#ifdef CONFIG_USERSPACE
560/**
561 * @brief Drop a thread's privileges permanently to user mode
562 *
563 * @param entry Function to start executing from
564 * @param p1 1st entry point parameter
565 * @param p2 2nd entry point parameter
566 * @param p3 3rd entry point parameter
567 */
568extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
569 void *p1, void *p2,
570 void *p3);
571#endif
572
Andrew Boied26cf2d2017-03-30 13:07:02 -0700573/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500574 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400575 *
Allan Stephensc98da842016-11-11 15:45:03 -0500576 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500577 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500579 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400580 *
581 * @return N/A
582 */
Kumar Galacc334c72017-04-21 10:55:34 -0500583extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400584
585/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500586 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400587 *
588 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500589 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400590 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400591 * @return N/A
592 */
Kumar Galacc334c72017-04-21 10:55:34 -0500593extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400594
595/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500596 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500598 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500600 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400601 *
602 * @return N/A
603 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400604extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400605
606/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500607 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500609 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500611 * If @a thread is not currently sleeping, the routine has no effect.
612 *
613 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400614 *
615 * @return N/A
616 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400617extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400618
619/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500620 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500622 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400623 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400624extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400625
626/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500627 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500629 * This routine prevents @a thread from executing if it has not yet started
630 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500632 * @param thread ID of thread to cancel.
633 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700634 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500635 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400636 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400637extern int k_thread_cancel(k_tid_t thread);
638
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400639/**
Allan Stephensc98da842016-11-11 15:45:03 -0500640 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500642 * This routine permanently stops execution of @a thread. The thread is taken
643 * off all kernel queues it is part of (i.e. the ready queue, the timeout
644 * queue, or a kernel object wait queue). However, any kernel resources the
645 * thread might currently own (such as mutexes or memory blocks) are not
646 * released. It is the responsibility of the caller of this routine to ensure
647 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500649 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400650 *
651 * @return N/A
652 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400653extern void k_thread_abort(k_tid_t thread);
654
Andrew Boie7d627c52017-08-30 11:01:56 -0700655
656/**
657 * @brief Start an inactive thread
658 *
659 * If a thread was created with K_FOREVER in the delay parameter, it will
660 * not be added to the scheduling queue until this function is called
661 * on it.
662 *
663 * @param thread thread to start
664 */
665extern void k_thread_start(k_tid_t thread);
666
Allan Stephensc98da842016-11-11 15:45:03 -0500667/**
668 * @cond INTERNAL_HIDDEN
669 */
670
Benjamin Walshd211a522016-12-06 11:44:01 -0500671/* timeout has timed out and is not on _timeout_q anymore */
672#define _EXPIRED (-2)
673
674/* timeout is not in use */
675#define _INACTIVE (-1)
676
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400677struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700678 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700679 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400680 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700681 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500682 void *init_p1;
683 void *init_p2;
684 void *init_p3;
685 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500686 u32_t init_options;
687 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500688 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500689 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400690};
691
Andrew Boied26cf2d2017-03-30 13:07:02 -0700692#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400693 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500694 prio, options, delay, abort, groups) \
695 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700696 .init_thread = (thread), \
697 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500698 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700699 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400700 .init_p1 = (void *)p1, \
701 .init_p2 = (void *)p2, \
702 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500703 .init_prio = (prio), \
704 .init_options = (options), \
705 .init_delay = (delay), \
706 .init_abort = (abort), \
707 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400708 }
709
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400710/**
Allan Stephensc98da842016-11-11 15:45:03 -0500711 * INTERNAL_HIDDEN @endcond
712 */
713
714/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500715 * @brief Statically define and initialize a thread.
716 *
717 * The thread may be scheduled for immediate execution or a delayed start.
718 *
719 * Thread options are architecture-specific, and can include K_ESSENTIAL,
720 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
721 * them using "|" (the logical OR operator).
722 *
723 * The ID of the thread can be accessed using:
724 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500725 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500726 *
727 * @param name Name of the thread.
728 * @param stack_size Stack size in bytes.
729 * @param entry Thread entry function.
730 * @param p1 1st entry point parameter.
731 * @param p2 2nd entry point parameter.
732 * @param p3 3rd entry point parameter.
733 * @param prio Thread priority.
734 * @param options Thread options.
735 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400736 *
737 * @internal It has been observed that the x86 compiler by default aligns
738 * these _static_thread_data structures to 32-byte boundaries, thereby
739 * wasting space. To work around this, force a 4-byte alignment.
740 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500741#define K_THREAD_DEFINE(name, stack_size, \
742 entry, p1, p2, p3, \
743 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700744 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700745 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500746 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500747 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700748 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
749 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500750 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700751 NULL, 0); \
752 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400753
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400754/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500755 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500757 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500759 * @param thread ID of thread whose priority is needed.
760 *
761 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400762 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500763extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400764
765/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500766 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500768 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400769 *
770 * Rescheduling can occur immediately depending on the priority @a thread is
771 * set to:
772 *
773 * - If its priority is raised above the priority of the caller of this
774 * function, and the caller is preemptible, @a thread will be scheduled in.
775 *
776 * - If the caller operates on itself, it lowers its priority below that of
777 * other threads in the system, and the caller is preemptible, the thread of
778 * highest priority will be scheduled in.
779 *
780 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
781 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
782 * highest priority.
783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500784 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400785 * @param prio New priority.
786 *
787 * @warning Changing the priority of a thread currently involved in mutex
788 * priority inheritance may result in undefined behavior.
789 *
790 * @return N/A
791 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400792extern void k_thread_priority_set(k_tid_t thread, int prio);
793
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400794/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500795 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500797 * This routine prevents the kernel scheduler from making @a thread the
798 * current thread. All other internal operations on @a thread are still
799 * performed; for example, any timeout it is waiting on keeps ticking,
800 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500802 * If @a thread is already suspended, the routine has no effect.
803 *
804 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400805 *
806 * @return N/A
807 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400808extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400809
810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * This routine allows the kernel scheduler to make @a thread the current
814 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500816 * If @a thread is not currently suspended, the routine has no effect.
817 *
818 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
820 * @return N/A
821 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400822extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400823
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500825 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * This routine specifies how the scheduler will perform time slicing of
828 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500830 * To enable time slicing, @a slice must be non-zero. The scheduler
831 * ensures that no thread runs for more than the specified time limit
832 * before other threads of that priority are given a chance to execute.
833 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700834 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500836 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 * execute. Once the scheduler selects a thread for execution, there is no
838 * minimum guaranteed time the thread will execute before threads of greater or
839 * equal priority are scheduled.
840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 * for execution, this routine has no effect; the thread is immediately
843 * rescheduled after the slice period expires.
844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * To disable timeslicing, set both @a slice and @a prio to zero.
846 *
847 * @param slice Maximum time slice length (in milliseconds).
848 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849 *
850 * @return N/A
851 */
Kumar Galacc334c72017-04-21 10:55:34 -0500852extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400853
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854/**
Allan Stephensc98da842016-11-11 15:45:03 -0500855 * @} end defgroup thread_apis
856 */
857
858/**
859 * @addtogroup isr_apis
860 * @{
861 */
862
863/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500864 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 *
Allan Stephensc98da842016-11-11 15:45:03 -0500866 * This routine allows the caller to customize its actions, depending on
867 * whether it is a thread or an ISR.
868 *
869 * @note Can be called by ISRs.
870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500871 * @return 0 if invoked by a thread.
872 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400873 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500874extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400875
Benjamin Walsh445830d2016-11-10 15:54:27 -0500876/**
877 * @brief Determine if code is running in a preemptible thread.
878 *
Allan Stephensc98da842016-11-11 15:45:03 -0500879 * This routine allows the caller to customize its actions, depending on
880 * whether it can be preempted by another thread. The routine returns a 'true'
881 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500882 *
Allan Stephensc98da842016-11-11 15:45:03 -0500883 * - The code is running in a thread, not at ISR.
884 * - The thread's priority is in the preemptible range.
885 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500886 *
Allan Stephensc98da842016-11-11 15:45:03 -0500887 * @note Can be called by ISRs.
888 *
889 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500890 * @return Non-zero if invoked by a preemptible thread.
891 */
892extern int k_is_preempt_thread(void);
893
Allan Stephensc98da842016-11-11 15:45:03 -0500894/**
895 * @} end addtogroup isr_apis
896 */
897
898/**
899 * @addtogroup thread_apis
900 * @{
901 */
902
903/**
904 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500905 *
Allan Stephensc98da842016-11-11 15:45:03 -0500906 * This routine prevents the current thread from being preempted by another
907 * thread by instructing the scheduler to treat it as a cooperative thread.
908 * If the thread subsequently performs an operation that makes it unready,
909 * it will be context switched out in the normal manner. When the thread
910 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500911 *
Allan Stephensc98da842016-11-11 15:45:03 -0500912 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500913 *
Allan Stephensc98da842016-11-11 15:45:03 -0500914 * @note k_sched_lock() and k_sched_unlock() should normally be used
915 * when the operation being performed can be safely interrupted by ISRs.
916 * However, if the amount of processing involved is very small, better
917 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500918 *
919 * @return N/A
920 */
921extern void k_sched_lock(void);
922
Allan Stephensc98da842016-11-11 15:45:03 -0500923/**
924 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500925 *
Allan Stephensc98da842016-11-11 15:45:03 -0500926 * This routine reverses the effect of a previous call to k_sched_lock().
927 * A thread must call the routine once for each time it called k_sched_lock()
928 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500929 *
930 * @return N/A
931 */
932extern void k_sched_unlock(void);
933
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400934/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500935 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500937 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500939 * Custom data is not used by the kernel itself, and is freely available
940 * for a thread to use as it sees fit. It can be used as a framework
941 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500943 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400944 *
945 * @return N/A
946 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400947extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400948
949/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500950 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500952 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500954 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400955 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400956extern void *k_thread_custom_data_get(void);
957
958/**
Allan Stephensc98da842016-11-11 15:45:03 -0500959 * @} end addtogroup thread_apis
960 */
961
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400962#include <sys_clock.h>
963
Allan Stephensc2f15a42016-11-17 12:24:22 -0500964/**
965 * @addtogroup clock_apis
966 * @{
967 */
968
969/**
970 * @brief Generate null timeout delay.
971 *
972 * This macro generates a timeout delay that that instructs a kernel API
973 * not to wait if the requested operation cannot be performed immediately.
974 *
975 * @return Timeout delay value.
976 */
977#define K_NO_WAIT 0
978
979/**
980 * @brief Generate timeout delay from milliseconds.
981 *
982 * This macro generates a timeout delay that that instructs a kernel API
983 * to wait up to @a ms milliseconds to perform the requested operation.
984 *
985 * @param ms Duration in milliseconds.
986 *
987 * @return Timeout delay value.
988 */
Johan Hedberg14471692016-11-13 10:52:15 +0200989#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500990
991/**
992 * @brief Generate timeout delay from seconds.
993 *
994 * This macro generates a timeout delay that that instructs a kernel API
995 * to wait up to @a s seconds to perform the requested operation.
996 *
997 * @param s Duration in seconds.
998 *
999 * @return Timeout delay value.
1000 */
Johan Hedberg14471692016-11-13 10:52:15 +02001001#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001002
1003/**
1004 * @brief Generate timeout delay from minutes.
1005 *
1006 * This macro generates a timeout delay that that instructs a kernel API
1007 * to wait up to @a m minutes to perform the requested operation.
1008 *
1009 * @param m Duration in minutes.
1010 *
1011 * @return Timeout delay value.
1012 */
Johan Hedberg14471692016-11-13 10:52:15 +02001013#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001014
1015/**
1016 * @brief Generate timeout delay from hours.
1017 *
1018 * This macro generates a timeout delay that that instructs a kernel API
1019 * to wait up to @a h hours to perform the requested operation.
1020 *
1021 * @param h Duration in hours.
1022 *
1023 * @return Timeout delay value.
1024 */
Johan Hedberg14471692016-11-13 10:52:15 +02001025#define K_HOURS(h) K_MINUTES((h) * 60)
1026
Allan Stephensc98da842016-11-11 15:45:03 -05001027/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001028 * @brief Generate infinite timeout delay.
1029 *
1030 * This macro generates a timeout delay that that instructs a kernel API
1031 * to wait as long as necessary to perform the requested operation.
1032 *
1033 * @return Timeout delay value.
1034 */
1035#define K_FOREVER (-1)
1036
1037/**
1038 * @} end addtogroup clock_apis
1039 */
1040
1041/**
Allan Stephensc98da842016-11-11 15:45:03 -05001042 * @cond INTERNAL_HIDDEN
1043 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001044
Benjamin Walsh62092182016-12-20 14:39:08 -05001045/* kernel clocks */
1046
1047#if (sys_clock_ticks_per_sec == 1000) || \
1048 (sys_clock_ticks_per_sec == 500) || \
1049 (sys_clock_ticks_per_sec == 250) || \
1050 (sys_clock_ticks_per_sec == 125) || \
1051 (sys_clock_ticks_per_sec == 100) || \
1052 (sys_clock_ticks_per_sec == 50) || \
1053 (sys_clock_ticks_per_sec == 25) || \
1054 (sys_clock_ticks_per_sec == 20) || \
1055 (sys_clock_ticks_per_sec == 10) || \
1056 (sys_clock_ticks_per_sec == 1)
1057
1058 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1059#else
1060 /* yields horrible 64-bit math on many architectures: try to avoid */
1061 #define _NON_OPTIMIZED_TICKS_PER_SEC
1062#endif
1063
1064#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001065extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001066#else
Kumar Galacc334c72017-04-21 10:55:34 -05001067static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001068{
Kumar Galacc334c72017-04-21 10:55:34 -05001069 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001070}
1071#endif
1072
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001073/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001074#ifdef CONFIG_TICKLESS_KERNEL
1075#define _TICK_ALIGN 0
1076#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001077#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001078#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001079
Kumar Galacc334c72017-04-21 10:55:34 -05001080static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001081{
Benjamin Walsh62092182016-12-20 14:39:08 -05001082#ifdef CONFIG_SYS_CLOCK_EXISTS
1083
1084#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001085 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001086#else
Kumar Galacc334c72017-04-21 10:55:34 -05001087 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001088#endif
1089
1090#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001091 __ASSERT(ticks == 0, "");
1092 return 0;
1093#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001094}
1095
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001096struct k_timer {
1097 /*
1098 * _timeout structure must be first here if we want to use
1099 * dynamic timer allocation. timeout.node is used in the double-linked
1100 * list of free timers
1101 */
1102 struct _timeout timeout;
1103
Allan Stephens45bfa372016-10-12 12:39:42 -05001104 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001105 _wait_q_t wait_q;
1106
1107 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001108 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001109
1110 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001111 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001112
1113 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001114 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001115
Allan Stephens45bfa372016-10-12 12:39:42 -05001116 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001117 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001118
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001119 /* user-specific data, also used to support legacy features */
1120 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001121
Anas Nashif2f203c22016-12-18 06:57:45 -05001122 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001123};
1124
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001125#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001126 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001127 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001128 .timeout.wait_q = NULL, \
1129 .timeout.thread = NULL, \
1130 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001131 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001132 .expiry_fn = expiry, \
1133 .stop_fn = stop, \
1134 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001135 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001136 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001137 }
1138
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001139#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1140
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001141/**
Allan Stephensc98da842016-11-11 15:45:03 -05001142 * INTERNAL_HIDDEN @endcond
1143 */
1144
1145/**
1146 * @defgroup timer_apis Timer APIs
1147 * @ingroup kernel_apis
1148 * @{
1149 */
1150
1151/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001152 * @typedef k_timer_expiry_t
1153 * @brief Timer expiry function type.
1154 *
1155 * A timer's expiry function is executed by the system clock interrupt handler
1156 * each time the timer expires. The expiry function is optional, and is only
1157 * invoked if the timer has been initialized with one.
1158 *
1159 * @param timer Address of timer.
1160 *
1161 * @return N/A
1162 */
1163typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1164
1165/**
1166 * @typedef k_timer_stop_t
1167 * @brief Timer stop function type.
1168 *
1169 * A timer's stop function is executed if the timer is stopped prematurely.
1170 * The function runs in the context of the thread that stops the timer.
1171 * The stop function is optional, and is only invoked if the timer has been
1172 * initialized with one.
1173 *
1174 * @param timer Address of timer.
1175 *
1176 * @return N/A
1177 */
1178typedef void (*k_timer_stop_t)(struct k_timer *timer);
1179
1180/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001181 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001183 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001184 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001185 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001186 *
1187 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001188 * @param expiry_fn Function to invoke each time the timer expires.
1189 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001190 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001191#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001192 struct k_timer name \
1193 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001194 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001195
Allan Stephens45bfa372016-10-12 12:39:42 -05001196/**
1197 * @brief Initialize a timer.
1198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001199 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001200 *
1201 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001202 * @param expiry_fn Function to invoke each time the timer expires.
1203 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001204 *
1205 * @return N/A
1206 */
1207extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001208 k_timer_expiry_t expiry_fn,
1209 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001210
Allan Stephens45bfa372016-10-12 12:39:42 -05001211/**
1212 * @brief Start a timer.
1213 *
1214 * This routine starts a timer, and resets its status to zero. The timer
1215 * begins counting down using the specified duration and period values.
1216 *
1217 * Attempting to start a timer that is already running is permitted.
1218 * The timer's status is reset to zero and the timer begins counting down
1219 * using the new duration and period values.
1220 *
1221 * @param timer Address of timer.
1222 * @param duration Initial timer duration (in milliseconds).
1223 * @param period Timer period (in milliseconds).
1224 *
1225 * @return N/A
1226 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001227extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001228 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001229
1230/**
1231 * @brief Stop a timer.
1232 *
1233 * This routine stops a running timer prematurely. The timer's stop function,
1234 * if one exists, is invoked by the caller.
1235 *
1236 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001237 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001238 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001239 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1240 * if @a k_timer_stop is to be called from ISRs.
1241 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001242 * @param timer Address of timer.
1243 *
1244 * @return N/A
1245 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001246extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001247
1248/**
1249 * @brief Read timer status.
1250 *
1251 * This routine reads the timer's status, which indicates the number of times
1252 * it has expired since its status was last read.
1253 *
1254 * Calling this routine resets the timer's status to zero.
1255 *
1256 * @param timer Address of timer.
1257 *
1258 * @return Timer status.
1259 */
Kumar Galacc334c72017-04-21 10:55:34 -05001260extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001261
1262/**
1263 * @brief Synchronize thread to timer expiration.
1264 *
1265 * This routine blocks the calling thread until the timer's status is non-zero
1266 * (indicating that it has expired at least once since it was last examined)
1267 * or the timer is stopped. If the timer status is already non-zero,
1268 * or the timer is already stopped, the caller continues without waiting.
1269 *
1270 * Calling this routine resets the timer's status to zero.
1271 *
1272 * This routine must not be used by interrupt handlers, since they are not
1273 * allowed to block.
1274 *
1275 * @param timer Address of timer.
1276 *
1277 * @return Timer status.
1278 */
Kumar Galacc334c72017-04-21 10:55:34 -05001279extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001280
1281/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001282 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001283 *
1284 * This routine computes the (approximate) time remaining before a running
1285 * timer next expires. If the timer is not running, it returns zero.
1286 *
1287 * @param timer Address of timer.
1288 *
1289 * @return Remaining time (in milliseconds).
1290 */
Kumar Galacc334c72017-04-21 10:55:34 -05001291static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001292{
1293 return _timeout_remaining_get(&timer->timeout);
1294}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001295
Allan Stephensc98da842016-11-11 15:45:03 -05001296/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001297 * @brief Associate user-specific data with a timer.
1298 *
1299 * This routine records the @a user_data with the @a timer, to be retrieved
1300 * later.
1301 *
1302 * It can be used e.g. in a timer handler shared across multiple subsystems to
1303 * retrieve data specific to the subsystem this timer is associated with.
1304 *
1305 * @param timer Address of timer.
1306 * @param user_data User data to associate with the timer.
1307 *
1308 * @return N/A
1309 */
1310static inline void k_timer_user_data_set(struct k_timer *timer,
1311 void *user_data)
1312{
1313 timer->user_data = user_data;
1314}
1315
1316/**
1317 * @brief Retrieve the user-specific data from a timer.
1318 *
1319 * @param timer Address of timer.
1320 *
1321 * @return The user data.
1322 */
1323static inline void *k_timer_user_data_get(struct k_timer *timer)
1324{
1325 return timer->user_data;
1326}
1327
1328/**
Allan Stephensc98da842016-11-11 15:45:03 -05001329 * @} end defgroup timer_apis
1330 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001331
Allan Stephensc98da842016-11-11 15:45:03 -05001332/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001333 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001334 * @{
1335 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001336
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001337/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001338 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001339 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001340 * This routine returns the elapsed time since the system booted,
1341 * in milliseconds.
1342 *
1343 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001344 */
Kumar Galacc334c72017-04-21 10:55:34 -05001345extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001346
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001347#ifdef CONFIG_TICKLESS_KERNEL
1348/**
1349 * @brief Enable clock always on in tickless kernel
1350 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001351 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001352 * there are no timer events programmed in tickless kernel
1353 * scheduling. This is necessary if the clock is used to track
1354 * passage of time.
1355 *
1356 * @retval prev_status Previous status of always on flag
1357 */
1358static inline int k_enable_sys_clock_always_on(void)
1359{
1360 int prev_status = _sys_clock_always_on;
1361
1362 _sys_clock_always_on = 1;
1363 _enable_sys_clock();
1364
1365 return prev_status;
1366}
1367
1368/**
1369 * @brief Disable clock always on in tickless kernel
1370 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001371 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001372 * there are no timer events programmed in tickless kernel
1373 * scheduling. To save power, this routine should be called
1374 * immediately when clock is not used to track time.
1375 */
1376static inline void k_disable_sys_clock_always_on(void)
1377{
1378 _sys_clock_always_on = 0;
1379}
1380#else
1381#define k_enable_sys_clock_always_on() do { } while ((0))
1382#define k_disable_sys_clock_always_on() do { } while ((0))
1383#endif
1384
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001385/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001386 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001388 * This routine returns the lower 32-bits of the elapsed time since the system
1389 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001391 * This routine can be more efficient than k_uptime_get(), as it reduces the
1392 * need for interrupt locking and 64-bit math. However, the 32-bit result
1393 * cannot hold a system uptime time larger than approximately 50 days, so the
1394 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001396 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001397 */
Kumar Galacc334c72017-04-21 10:55:34 -05001398extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001399
1400/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001401 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001403 * This routine computes the elapsed time between the current system uptime
1404 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001406 * @param reftime Pointer to a reference time, which is updated to the current
1407 * uptime upon return.
1408 *
1409 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001410 */
Kumar Galacc334c72017-04-21 10:55:34 -05001411extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001412
1413/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001414 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001416 * This routine computes the elapsed time between the current system uptime
1417 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001419 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1420 * need for interrupt locking and 64-bit math. However, the 32-bit result
1421 * cannot hold an elapsed time larger than approximately 50 days, so the
1422 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001424 * @param reftime Pointer to a reference time, which is updated to the current
1425 * uptime upon return.
1426 *
1427 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001428 */
Kumar Galacc334c72017-04-21 10:55:34 -05001429extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001430
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001431/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001432 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001434 * This routine returns the current time, as measured by the system's hardware
1435 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001436 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001437 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001438 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001439#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001440
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001441/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001442 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001443 */
1444
Allan Stephensc98da842016-11-11 15:45:03 -05001445/**
1446 * @cond INTERNAL_HIDDEN
1447 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001448
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001449struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001450 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001451 union {
1452 _wait_q_t wait_q;
1453
1454 _POLL_EVENT;
1455 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001456
1457 _OBJECT_TRACING_NEXT_PTR(k_queue);
1458};
1459
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001460#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001461 { \
1462 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1463 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001464 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001465 _OBJECT_TRACING_INIT \
1466 }
1467
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001468#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1469
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001470/**
1471 * INTERNAL_HIDDEN @endcond
1472 */
1473
1474/**
1475 * @defgroup queue_apis Queue APIs
1476 * @ingroup kernel_apis
1477 * @{
1478 */
1479
1480/**
1481 * @brief Initialize a queue.
1482 *
1483 * This routine initializes a queue object, prior to its first use.
1484 *
1485 * @param queue Address of the queue.
1486 *
1487 * @return N/A
1488 */
1489extern void k_queue_init(struct k_queue *queue);
1490
1491/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001492 * @brief Cancel waiting on a queue.
1493 *
1494 * This routine causes first thread pending on @a queue, if any, to
1495 * return from k_queue_get() call with NULL value (as if timeout expired).
1496 *
1497 * @note Can be called by ISRs.
1498 *
1499 * @param queue Address of the queue.
1500 *
1501 * @return N/A
1502 */
1503extern void k_queue_cancel_wait(struct k_queue *queue);
1504
1505/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001506 * @brief Append an element to the end of a queue.
1507 *
1508 * This routine appends a data item to @a queue. A queue data item must be
1509 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1510 * reserved for the kernel's use.
1511 *
1512 * @note Can be called by ISRs.
1513 *
1514 * @param queue Address of the queue.
1515 * @param data Address of the data item.
1516 *
1517 * @return N/A
1518 */
1519extern void k_queue_append(struct k_queue *queue, void *data);
1520
1521/**
1522 * @brief Prepend an element to a queue.
1523 *
1524 * This routine prepends a data item to @a queue. A queue data item must be
1525 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1526 * reserved for the kernel's use.
1527 *
1528 * @note Can be called by ISRs.
1529 *
1530 * @param queue Address of the queue.
1531 * @param data Address of the data item.
1532 *
1533 * @return N/A
1534 */
1535extern void k_queue_prepend(struct k_queue *queue, void *data);
1536
1537/**
1538 * @brief Inserts an element to a queue.
1539 *
1540 * This routine inserts a data item to @a queue after previous item. A queue
1541 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1542 * item are reserved for the kernel's use.
1543 *
1544 * @note Can be called by ISRs.
1545 *
1546 * @param queue Address of the queue.
1547 * @param prev Address of the previous data item.
1548 * @param data Address of the data item.
1549 *
1550 * @return N/A
1551 */
1552extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1553
1554/**
1555 * @brief Atomically append a list of elements to a queue.
1556 *
1557 * This routine adds a list of data items to @a queue in one operation.
1558 * The data items must be in a singly-linked list, with the first 32 bits
1559 * in each data item pointing to the next data item; the list must be
1560 * NULL-terminated.
1561 *
1562 * @note Can be called by ISRs.
1563 *
1564 * @param queue Address of the queue.
1565 * @param head Pointer to first node in singly-linked list.
1566 * @param tail Pointer to last node in singly-linked list.
1567 *
1568 * @return N/A
1569 */
1570extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1571
1572/**
1573 * @brief Atomically add a list of elements to a queue.
1574 *
1575 * This routine adds a list of data items to @a queue in one operation.
1576 * The data items must be in a singly-linked list implemented using a
1577 * sys_slist_t object. Upon completion, the original list is empty.
1578 *
1579 * @note Can be called by ISRs.
1580 *
1581 * @param queue Address of the queue.
1582 * @param list Pointer to sys_slist_t object.
1583 *
1584 * @return N/A
1585 */
1586extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1587
1588/**
1589 * @brief Get an element from a queue.
1590 *
1591 * This routine removes first data item from @a queue. The first 32 bits of the
1592 * data item are reserved for the kernel's use.
1593 *
1594 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1595 *
1596 * @param queue Address of the queue.
1597 * @param timeout Waiting period to obtain a data item (in milliseconds),
1598 * or one of the special values K_NO_WAIT and K_FOREVER.
1599 *
1600 * @return Address of the data item if successful; NULL if returned
1601 * without waiting, or waiting period timed out.
1602 */
Kumar Galacc334c72017-04-21 10:55:34 -05001603extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001604
1605/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001606 * @brief Remove an element from a queue.
1607 *
1608 * This routine removes data item from @a queue. The first 32 bits of the
1609 * data item are reserved for the kernel's use. Removing elements from k_queue
1610 * rely on sys_slist_find_and_remove which is not a constant time operation.
1611 *
1612 * @note Can be called by ISRs
1613 *
1614 * @param queue Address of the queue.
1615 * @param data Address of the data item.
1616 *
1617 * @return true if data item was removed
1618 */
1619static inline bool k_queue_remove(struct k_queue *queue, void *data)
1620{
1621 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1622}
1623
1624/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001625 * @brief Query a queue to see if it has data available.
1626 *
1627 * Note that the data might be already gone by the time this function returns
1628 * if other threads are also trying to read from the queue.
1629 *
1630 * @note Can be called by ISRs.
1631 *
1632 * @param queue Address of the queue.
1633 *
1634 * @return Non-zero if the queue is empty.
1635 * @return 0 if data is available.
1636 */
1637static inline int k_queue_is_empty(struct k_queue *queue)
1638{
1639 return (int)sys_slist_is_empty(&queue->data_q);
1640}
1641
1642/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001643 * @brief Peek element at the head of queue.
1644 *
1645 * Return element from the head of queue without removing it.
1646 *
1647 * @param queue Address of the queue.
1648 *
1649 * @return Head element, or NULL if queue is empty.
1650 */
1651static inline void *k_queue_peek_head(struct k_queue *queue)
1652{
1653 return sys_slist_peek_head(&queue->data_q);
1654}
1655
1656/**
1657 * @brief Peek element at the tail of queue.
1658 *
1659 * Return element from the tail of queue without removing it.
1660 *
1661 * @param queue Address of the queue.
1662 *
1663 * @return Tail element, or NULL if queue is empty.
1664 */
1665static inline void *k_queue_peek_tail(struct k_queue *queue)
1666{
1667 return sys_slist_peek_tail(&queue->data_q);
1668}
1669
1670/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001671 * @brief Statically define and initialize a queue.
1672 *
1673 * The queue can be accessed outside the module where it is defined using:
1674 *
1675 * @code extern struct k_queue <name>; @endcode
1676 *
1677 * @param name Name of the queue.
1678 */
1679#define K_QUEUE_DEFINE(name) \
1680 struct k_queue name \
1681 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001682 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001683
1684/**
1685 * @} end defgroup queue_apis
1686 */
1687
1688/**
1689 * @cond INTERNAL_HIDDEN
1690 */
1691
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001692struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001693 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001694};
1695
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001696#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001697 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001698 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001699 }
1700
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001701#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1702
Allan Stephensc98da842016-11-11 15:45:03 -05001703/**
1704 * INTERNAL_HIDDEN @endcond
1705 */
1706
1707/**
1708 * @defgroup fifo_apis Fifo APIs
1709 * @ingroup kernel_apis
1710 * @{
1711 */
1712
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001713/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001714 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001716 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001718 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001719 *
1720 * @return N/A
1721 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001722#define k_fifo_init(fifo) \
1723 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001724
1725/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001726 * @brief Cancel waiting on a fifo.
1727 *
1728 * This routine causes first thread pending on @a fifo, if any, to
1729 * return from k_fifo_get() call with NULL value (as if timeout
1730 * expired).
1731 *
1732 * @note Can be called by ISRs.
1733 *
1734 * @param fifo Address of the fifo.
1735 *
1736 * @return N/A
1737 */
1738#define k_fifo_cancel_wait(fifo) \
1739 k_queue_cancel_wait((struct k_queue *) fifo)
1740
1741/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001742 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001744 * This routine adds a data item to @a fifo. A fifo data item must be
1745 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1746 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001747 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001748 * @note Can be called by ISRs.
1749 *
1750 * @param fifo Address of the fifo.
1751 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001752 *
1753 * @return N/A
1754 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001755#define k_fifo_put(fifo, data) \
1756 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001757
1758/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001759 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001760 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001761 * This routine adds a list of data items to @a fifo in one operation.
1762 * The data items must be in a singly-linked list, with the first 32 bits
1763 * each data item pointing to the next data item; the list must be
1764 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001766 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001768 * @param fifo Address of the fifo.
1769 * @param head Pointer to first node in singly-linked list.
1770 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001771 *
1772 * @return N/A
1773 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001774#define k_fifo_put_list(fifo, head, tail) \
1775 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001776
1777/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001778 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001780 * This routine adds a list of data items to @a fifo in one operation.
1781 * The data items must be in a singly-linked list implemented using a
1782 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001783 * and must be re-initialized via sys_slist_init().
1784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001785 * @note Can be called by ISRs.
1786 *
1787 * @param fifo Address of the fifo.
1788 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001789 *
1790 * @return N/A
1791 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001792#define k_fifo_put_slist(fifo, list) \
1793 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001794
1795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001796 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001798 * This routine removes a data item from @a fifo in a "first in, first out"
1799 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001801 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1802 *
1803 * @param fifo Address of the fifo.
1804 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001805 * or one of the special values K_NO_WAIT and K_FOREVER.
1806 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001807 * @return Address of the data item if successful; NULL if returned
1808 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001809 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001810#define k_fifo_get(fifo, timeout) \
1811 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001812
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001813/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001814 * @brief Query a fifo to see if it has data available.
1815 *
1816 * Note that the data might be already gone by the time this function returns
1817 * if other threads is also trying to read from the fifo.
1818 *
1819 * @note Can be called by ISRs.
1820 *
1821 * @param fifo Address of the fifo.
1822 *
1823 * @return Non-zero if the fifo is empty.
1824 * @return 0 if data is available.
1825 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001826#define k_fifo_is_empty(fifo) \
1827 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001828
1829/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001830 * @brief Peek element at the head of fifo.
1831 *
1832 * Return element from the head of fifo without removing it. A usecase
1833 * for this is if elements of the fifo are themselves containers. Then
1834 * on each iteration of processing, a head container will be peeked,
1835 * and some data processed out of it, and only if the container is empty,
1836 * it will be completely remove from the fifo.
1837 *
1838 * @param fifo Address of the fifo.
1839 *
1840 * @return Head element, or NULL if the fifo is empty.
1841 */
1842#define k_fifo_peek_head(fifo) \
1843 k_queue_peek_head((struct k_queue *) fifo)
1844
1845/**
1846 * @brief Peek element at the tail of fifo.
1847 *
1848 * Return element from the tail of fifo (without removing it). A usecase
1849 * for this is if elements of the fifo are themselves containers. Then
1850 * it may be useful to add more data to the last container in fifo.
1851 *
1852 * @param fifo Address of the fifo.
1853 *
1854 * @return Tail element, or NULL if fifo is empty.
1855 */
1856#define k_fifo_peek_tail(fifo) \
1857 k_queue_peek_tail((struct k_queue *) fifo)
1858
1859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001860 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001862 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001863 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001864 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001866 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001867 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001868#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001869 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001870 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001871 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001872
Allan Stephensc98da842016-11-11 15:45:03 -05001873/**
1874 * @} end defgroup fifo_apis
1875 */
1876
1877/**
1878 * @cond INTERNAL_HIDDEN
1879 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001880
1881struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001882 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001883};
1884
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001885#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001886 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001887 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001888 }
1889
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001890#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1891
Allan Stephensc98da842016-11-11 15:45:03 -05001892/**
1893 * INTERNAL_HIDDEN @endcond
1894 */
1895
1896/**
1897 * @defgroup lifo_apis Lifo APIs
1898 * @ingroup kernel_apis
1899 * @{
1900 */
1901
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001902/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001903 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001905 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001907 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001908 *
1909 * @return N/A
1910 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001911#define k_lifo_init(lifo) \
1912 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001913
1914/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001915 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001917 * This routine adds a data item to @a lifo. A lifo data item must be
1918 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1919 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001921 * @note Can be called by ISRs.
1922 *
1923 * @param lifo Address of the lifo.
1924 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001925 *
1926 * @return N/A
1927 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001928#define k_lifo_put(lifo, data) \
1929 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001930
1931/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001932 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001934 * This routine removes a data item from @a lifo in a "last in, first out"
1935 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001937 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1938 *
1939 * @param lifo Address of the lifo.
1940 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001941 * or one of the special values K_NO_WAIT and K_FOREVER.
1942 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001943 * @return Address of the data item if successful; NULL if returned
1944 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001945 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001946#define k_lifo_get(lifo, timeout) \
1947 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001948
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001949/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001950 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001952 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001953 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001954 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001956 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001957 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001958#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001959 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001960 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001961 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001962
Allan Stephensc98da842016-11-11 15:45:03 -05001963/**
1964 * @} end defgroup lifo_apis
1965 */
1966
1967/**
1968 * @cond INTERNAL_HIDDEN
1969 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001970
1971struct k_stack {
1972 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001973 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001974
Anas Nashif2f203c22016-12-18 06:57:45 -05001975 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001976};
1977
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001978#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001979 { \
1980 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1981 .base = stack_buffer, \
1982 .next = stack_buffer, \
1983 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001984 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001985 }
1986
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001987#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1988
Allan Stephensc98da842016-11-11 15:45:03 -05001989/**
1990 * INTERNAL_HIDDEN @endcond
1991 */
1992
1993/**
1994 * @defgroup stack_apis Stack APIs
1995 * @ingroup kernel_apis
1996 * @{
1997 */
1998
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001999/**
2000 * @brief Initialize a stack.
2001 *
2002 * This routine initializes a stack object, prior to its first use.
2003 *
2004 * @param stack Address of the stack.
2005 * @param buffer Address of array used to hold stacked values.
2006 * @param num_entries Maximum number of values that can be stacked.
2007 *
2008 * @return N/A
2009 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05002010extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05002011 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002012
2013/**
2014 * @brief Push an element onto a stack.
2015 *
2016 * This routine adds a 32-bit value @a data to @a stack.
2017 *
2018 * @note Can be called by ISRs.
2019 *
2020 * @param stack Address of the stack.
2021 * @param data Value to push onto the stack.
2022 *
2023 * @return N/A
2024 */
Kumar Galacc334c72017-04-21 10:55:34 -05002025extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002026
2027/**
2028 * @brief Pop an element from a stack.
2029 *
2030 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2031 * manner and stores the value in @a data.
2032 *
2033 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2034 *
2035 * @param stack Address of the stack.
2036 * @param data Address of area to hold the value popped from the stack.
2037 * @param timeout Waiting period to obtain a value (in milliseconds),
2038 * or one of the special values K_NO_WAIT and K_FOREVER.
2039 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002040 * @retval 0 Element popped from stack.
2041 * @retval -EBUSY Returned without waiting.
2042 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002043 */
Kumar Galacc334c72017-04-21 10:55:34 -05002044extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002045
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002047 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002049 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002050 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002051 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002052 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002053 * @param name Name of the stack.
2054 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002055 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002056#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002057 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002058 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002059 struct k_stack name \
2060 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002061 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002062 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002063
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002064/**
Allan Stephensc98da842016-11-11 15:45:03 -05002065 * @} end defgroup stack_apis
2066 */
2067
Allan Stephens6bba9b02016-11-16 14:56:54 -05002068struct k_work;
2069
Allan Stephensc98da842016-11-11 15:45:03 -05002070/**
2071 * @defgroup workqueue_apis Workqueue Thread APIs
2072 * @ingroup kernel_apis
2073 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002074 */
2075
Allan Stephens6bba9b02016-11-16 14:56:54 -05002076/**
2077 * @typedef k_work_handler_t
2078 * @brief Work item handler function type.
2079 *
2080 * A work item's handler function is executed by a workqueue's thread
2081 * when the work item is processed by the workqueue.
2082 *
2083 * @param work Address of the work item.
2084 *
2085 * @return N/A
2086 */
2087typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002088
2089/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002090 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002091 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002092
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002093struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002094 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002095 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002096};
2097
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002098enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002099 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002100};
2101
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002102struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002103 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002104 k_work_handler_t handler;
2105 atomic_t flags[1];
2106};
2107
Allan Stephens6bba9b02016-11-16 14:56:54 -05002108struct k_delayed_work {
2109 struct k_work work;
2110 struct _timeout timeout;
2111 struct k_work_q *work_q;
2112};
2113
2114extern struct k_work_q k_sys_work_q;
2115
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002116/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002117 * INTERNAL_HIDDEN @endcond
2118 */
2119
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002120#define _K_WORK_INITIALIZER(work_handler) \
2121 { \
2122 ._reserved = NULL, \
2123 .handler = work_handler, \
2124 .flags = { 0 } \
2125 }
2126
2127#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2128
Allan Stephens6bba9b02016-11-16 14:56:54 -05002129/**
2130 * @brief Initialize a statically-defined work item.
2131 *
2132 * This macro can be used to initialize a statically-defined workqueue work
2133 * item, prior to its first use. For example,
2134 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002135 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002136 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002137 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002138 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002139 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002140#define K_WORK_DEFINE(work, work_handler) \
2141 struct k_work work \
2142 __in_section(_k_work, static, work) = \
2143 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002144
2145/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002146 * @brief Initialize a work item.
2147 *
2148 * This routine initializes a workqueue work item, prior to its first use.
2149 *
2150 * @param work Address of work item.
2151 * @param handler Function to invoke each time work item is processed.
2152 *
2153 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002154 */
2155static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2156{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002157 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002159 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002160}
2161
2162/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002163 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002164 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002165 * This routine submits work item @a work to be processed by workqueue
2166 * @a work_q. If the work item is already pending in the workqueue's queue
2167 * as a result of an earlier submission, this routine has no effect on the
2168 * work item. If the work item has already been processed, or is currently
2169 * being processed, its work is considered complete and the work item can be
2170 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002171 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002172 * @warning
2173 * A submitted work item must not be modified until it has been processed
2174 * by the workqueue.
2175 *
2176 * @note Can be called by ISRs.
2177 *
2178 * @param work_q Address of workqueue.
2179 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002180 *
2181 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002182 */
2183static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2184 struct k_work *work)
2185{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002186 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002187 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002188 }
2189}
2190
2191/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002192 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002193 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002194 * This routine indicates if work item @a work is pending in a workqueue's
2195 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002196 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002197 * @note Can be called by ISRs.
2198 *
2199 * @param work Address of work item.
2200 *
2201 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002202 */
2203static inline int k_work_pending(struct k_work *work)
2204{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002205 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002206}
2207
2208/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002209 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002210 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002211 * This routine starts workqueue @a work_q. The workqueue spawns its work
2212 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002213 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002214 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002215 * @param stack Pointer to work queue thread's stack space, as defined by
2216 * K_THREAD_STACK_DEFINE()
2217 * @param stack_size Size of the work queue thread's stack (in bytes), which
2218 * should either be the same constant passed to
2219 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002220 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002221 *
2222 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002223 */
Andrew Boie507852a2017-07-25 18:47:07 -07002224extern void k_work_q_start(struct k_work_q *work_q,
2225 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002226 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002227
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002228/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002229 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002230 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002231 * This routine initializes a workqueue delayed work item, prior to
2232 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002233 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002234 * @param work Address of delayed work item.
2235 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002236 *
2237 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002238 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002239extern void k_delayed_work_init(struct k_delayed_work *work,
2240 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002241
2242/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002243 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002244 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002245 * This routine schedules work item @a work to be processed by workqueue
2246 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002247 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002248 * Only when the countdown completes is the work item actually submitted to
2249 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002250 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002251 * Submitting a previously submitted delayed work item that is still
2252 * counting down cancels the existing submission and restarts the countdown
2253 * using the new delay. If the work item is currently pending on the
2254 * workqueue's queue because the countdown has completed it is too late to
2255 * resubmit the item, and resubmission fails without impacting the work item.
2256 * If the work item has already been processed, or is currently being processed,
2257 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002258 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002259 * @warning
2260 * A delayed work item must not be modified until it has been processed
2261 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002262 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002263 * @note Can be called by ISRs.
2264 *
2265 * @param work_q Address of workqueue.
2266 * @param work Address of delayed work item.
2267 * @param delay Delay before submitting the work item (in milliseconds).
2268 *
2269 * @retval 0 Work item countdown started.
2270 * @retval -EINPROGRESS Work item is already pending.
2271 * @retval -EINVAL Work item is being processed or has completed its work.
2272 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002273 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002274extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2275 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002276 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002277
2278/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002279 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002280 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002281 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002282 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002283 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002285 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002286 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002287 * @param work Address of delayed work item.
2288 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002289 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002290 * @retval -EINPROGRESS Work item is already pending.
2291 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002292 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002293extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002294
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002295/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002296 * @brief Submit a work item to the system workqueue.
2297 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002298 * This routine submits work item @a work to be processed by the system
2299 * workqueue. If the work item is already pending in the workqueue's queue
2300 * as a result of an earlier submission, this routine has no effect on the
2301 * work item. If the work item has already been processed, or is currently
2302 * being processed, its work is considered complete and the work item can be
2303 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002304 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002305 * @warning
2306 * Work items submitted to the system workqueue should avoid using handlers
2307 * that block or yield since this may prevent the system workqueue from
2308 * processing other work items in a timely manner.
2309 *
2310 * @note Can be called by ISRs.
2311 *
2312 * @param work Address of work item.
2313 *
2314 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002315 */
2316static inline void k_work_submit(struct k_work *work)
2317{
2318 k_work_submit_to_queue(&k_sys_work_q, work);
2319}
2320
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002321/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002322 * @brief Submit a delayed work item to the system workqueue.
2323 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002324 * This routine schedules work item @a work to be processed by the system
2325 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002326 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002327 * Only when the countdown completes is the work item actually submitted to
2328 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002329 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002330 * Submitting a previously submitted delayed work item that is still
2331 * counting down cancels the existing submission and restarts the countdown
2332 * using the new delay. If the work item is currently pending on the
2333 * workqueue's queue because the countdown has completed it is too late to
2334 * resubmit the item, and resubmission fails without impacting the work item.
2335 * If the work item has already been processed, or is currently being processed,
2336 * its work is considered complete and the work item can be resubmitted.
2337 *
2338 * @warning
2339 * Work items submitted to the system workqueue should avoid using handlers
2340 * that block or yield since this may prevent the system workqueue from
2341 * processing other work items in a timely manner.
2342 *
2343 * @note Can be called by ISRs.
2344 *
2345 * @param work Address of delayed work item.
2346 * @param delay Delay before submitting the work item (in milliseconds).
2347 *
2348 * @retval 0 Work item countdown started.
2349 * @retval -EINPROGRESS Work item is already pending.
2350 * @retval -EINVAL Work item is being processed or has completed its work.
2351 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002352 */
2353static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002354 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002355{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002356 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357}
2358
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002359/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002360 * @brief Get time remaining before a delayed work gets scheduled.
2361 *
2362 * This routine computes the (approximate) time remaining before a
2363 * delayed work gets executed. If the delayed work is not waiting to be
2364 * schedules, it returns zero.
2365 *
2366 * @param work Delayed work item.
2367 *
2368 * @return Remaining time (in milliseconds).
2369 */
Kumar Galacc334c72017-04-21 10:55:34 -05002370static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002371{
2372 return _timeout_remaining_get(&work->timeout);
2373}
2374
2375/**
Allan Stephensc98da842016-11-11 15:45:03 -05002376 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002377 */
2378
Allan Stephensc98da842016-11-11 15:45:03 -05002379/**
2380 * @cond INTERNAL_HIDDEN
2381 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002382
2383struct k_mutex {
2384 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002385 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002386 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002387 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002388
Anas Nashif2f203c22016-12-18 06:57:45 -05002389 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002390};
2391
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002392#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002393 { \
2394 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2395 .owner = NULL, \
2396 .lock_count = 0, \
2397 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002398 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002399 }
2400
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002401#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2402
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002403/**
Allan Stephensc98da842016-11-11 15:45:03 -05002404 * INTERNAL_HIDDEN @endcond
2405 */
2406
2407/**
2408 * @defgroup mutex_apis Mutex APIs
2409 * @ingroup kernel_apis
2410 * @{
2411 */
2412
2413/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002414 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002417 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002418 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002420 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002421 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002422#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002423 struct k_mutex name \
2424 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002425 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002426
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002427/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002428 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002430 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002432 * Upon completion, the mutex is available and does not have an owner.
2433 *
2434 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002435 *
2436 * @return N/A
2437 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002439
2440/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002441 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002442 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002443 * This routine locks @a mutex. If the mutex is locked by another thread,
2444 * the calling thread waits until the mutex becomes available or until
2445 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002447 * A thread is permitted to lock a mutex it has already locked. The operation
2448 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002450 * @param mutex Address of the mutex.
2451 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002452 * or one of the special values K_NO_WAIT and K_FOREVER.
2453 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002454 * @retval 0 Mutex locked.
2455 * @retval -EBUSY Returned without waiting.
2456 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002457 */
Kumar Galacc334c72017-04-21 10:55:34 -05002458extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002459
2460/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002461 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002463 * This routine unlocks @a mutex. The mutex must already be locked by the
2464 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002465 *
2466 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002467 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002468 * thread.
2469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002470 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002471 *
2472 * @return N/A
2473 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002474extern void k_mutex_unlock(struct k_mutex *mutex);
2475
Allan Stephensc98da842016-11-11 15:45:03 -05002476/**
2477 * @} end defgroup mutex_apis
2478 */
2479
2480/**
2481 * @cond INTERNAL_HIDDEN
2482 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002483
2484struct k_sem {
2485 _wait_q_t wait_q;
2486 unsigned int count;
2487 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002488 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002489
Anas Nashif2f203c22016-12-18 06:57:45 -05002490 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002491};
2492
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002493#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002494 { \
2495 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2496 .count = initial_count, \
2497 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002498 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002499 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002500 }
2501
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002502#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2503
Allan Stephensc98da842016-11-11 15:45:03 -05002504/**
2505 * INTERNAL_HIDDEN @endcond
2506 */
2507
2508/**
2509 * @defgroup semaphore_apis Semaphore APIs
2510 * @ingroup kernel_apis
2511 * @{
2512 */
2513
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002514/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002515 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002517 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * @param sem Address of the semaphore.
2520 * @param initial_count Initial semaphore count.
2521 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002522 *
2523 * @return N/A
2524 */
Andrew Boie99280232017-09-29 14:17:47 -07002525__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2526 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002527
2528/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002531 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002533 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2534 *
2535 * @param sem Address of the semaphore.
2536 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002537 * or one of the special values K_NO_WAIT and K_FOREVER.
2538 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002539 * @note When porting code from the nanokernel legacy API to the new API, be
2540 * careful with the return value of this function. The return value is the
2541 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2542 * non-zero means failure, while the nano_sem_take family returns 1 for success
2543 * and 0 for failure.
2544 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002545 * @retval 0 Semaphore taken.
2546 * @retval -EBUSY Returned without waiting.
2547 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002548 */
Andrew Boie99280232017-09-29 14:17:47 -07002549__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002550
2551/**
2552 * @brief Give a semaphore.
2553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002554 * This routine gives @a sem, unless the semaphore is already at its maximum
2555 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002557 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002559 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002560 *
2561 * @return N/A
2562 */
Andrew Boie99280232017-09-29 14:17:47 -07002563__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002564
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002565/**
2566 * @brief Reset a semaphore's count to zero.
2567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002568 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002570 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002571 *
2572 * @return N/A
2573 */
Andrew Boie99280232017-09-29 14:17:47 -07002574__syscall_inline void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002575
2576static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002577{
2578 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002579}
2580
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002581/**
2582 * @brief Get a semaphore's count.
2583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002584 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002585 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002586 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002588 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002589 */
Andrew Boie99280232017-09-29 14:17:47 -07002590__syscall_inline unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002591
2592static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002593{
2594 return sem->count;
2595}
2596
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002597/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002598 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002600 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002601 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002602 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002604 * @param name Name of the semaphore.
2605 * @param initial_count Initial semaphore count.
2606 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002607 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002608#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002609 struct k_sem name \
2610 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002611 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002612
Allan Stephensc98da842016-11-11 15:45:03 -05002613/**
2614 * @} end defgroup semaphore_apis
2615 */
2616
2617/**
2618 * @defgroup alert_apis Alert APIs
2619 * @ingroup kernel_apis
2620 * @{
2621 */
2622
Allan Stephens5eceb852016-11-16 10:16:30 -05002623/**
2624 * @typedef k_alert_handler_t
2625 * @brief Alert handler function type.
2626 *
2627 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002628 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002629 * and is only invoked if the alert has been initialized with one.
2630 *
2631 * @param alert Address of the alert.
2632 *
2633 * @return 0 if alert has been consumed; non-zero if alert should pend.
2634 */
2635typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002636
2637/**
2638 * @} end defgroup alert_apis
2639 */
2640
2641/**
2642 * @cond INTERNAL_HIDDEN
2643 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002644
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002645#define K_ALERT_DEFAULT NULL
2646#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002647
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002648struct k_alert {
2649 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002650 atomic_t send_count;
2651 struct k_work work_item;
2652 struct k_sem sem;
2653
Anas Nashif2f203c22016-12-18 06:57:45 -05002654 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002655};
2656
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002657extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002658
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002659#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002660 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002661 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002662 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002663 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2664 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002665 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002666 }
2667
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002668#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2669
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002670/**
Allan Stephensc98da842016-11-11 15:45:03 -05002671 * INTERNAL_HIDDEN @endcond
2672 */
2673
2674/**
2675 * @addtogroup alert_apis
2676 * @{
2677 */
2678
2679/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002680 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002681 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002682 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002683 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002684 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002686 * @param name Name of the alert.
2687 * @param alert_handler Action to take when alert is sent. Specify either
2688 * the address of a function to be invoked by the system workqueue
2689 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2690 * K_ALERT_DEFAULT (which causes the alert to pend).
2691 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002692 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002693#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002694 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002695 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002696 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002697 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002698
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002699/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002700 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002702 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002704 * @param alert Address of the alert.
2705 * @param handler Action to take when alert is sent. Specify either the address
2706 * of a function to be invoked by the system workqueue thread,
2707 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2708 * K_ALERT_DEFAULT (which causes the alert to pend).
2709 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002710 *
2711 * @return N/A
2712 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002713extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2714 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002715
2716/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002717 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002719 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002721 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2722 *
2723 * @param alert Address of the alert.
2724 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002725 * or one of the special values K_NO_WAIT and K_FOREVER.
2726 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002727 * @retval 0 Alert received.
2728 * @retval -EBUSY Returned without waiting.
2729 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002730 */
Kumar Galacc334c72017-04-21 10:55:34 -05002731extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002732
2733/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002734 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002736 * This routine signals @a alert. The action specified for @a alert will
2737 * be taken, which may trigger the execution of an alert handler function
2738 * and/or cause the alert to pend (assuming the alert has not reached its
2739 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002741 * @note Can be called by ISRs.
2742 *
2743 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002744 *
2745 * @return N/A
2746 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002747extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002748
2749/**
Allan Stephensc98da842016-11-11 15:45:03 -05002750 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751 */
2752
Allan Stephensc98da842016-11-11 15:45:03 -05002753/**
2754 * @cond INTERNAL_HIDDEN
2755 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002756
2757struct k_msgq {
2758 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002759 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002760 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002761 char *buffer_start;
2762 char *buffer_end;
2763 char *read_ptr;
2764 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002765 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002766
Anas Nashif2f203c22016-12-18 06:57:45 -05002767 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002768};
2769
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002770#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771 { \
2772 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002773 .max_msgs = q_max_msgs, \
2774 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002775 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002776 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002777 .read_ptr = q_buffer, \
2778 .write_ptr = q_buffer, \
2779 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002780 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002781 }
2782
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002783#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2784
Peter Mitsis1da807e2016-10-06 11:36:59 -04002785/**
Allan Stephensc98da842016-11-11 15:45:03 -05002786 * INTERNAL_HIDDEN @endcond
2787 */
2788
2789/**
2790 * @defgroup msgq_apis Message Queue APIs
2791 * @ingroup kernel_apis
2792 * @{
2793 */
2794
2795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002796 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002798 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2799 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002800 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2801 * message is similarly aligned to this boundary, @a q_msg_size must also be
2802 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002804 * The message queue can be accessed outside the module where it is defined
2805 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002806 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002807 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002809 * @param q_name Name of the message queue.
2810 * @param q_msg_size Message size (in bytes).
2811 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002812 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002813 */
2814#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2815 static char __noinit __aligned(q_align) \
2816 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002817 struct k_msgq q_name \
2818 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002819 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002820 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002821
Peter Mitsisd7a37502016-10-13 11:37:40 -04002822/**
2823 * @brief Initialize a message queue.
2824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002825 * This routine initializes a message queue object, prior to its first use.
2826 *
Allan Stephensda827222016-11-09 14:23:58 -06002827 * The message queue's ring buffer must contain space for @a max_msgs messages,
2828 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2829 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2830 * that each message is similarly aligned to this boundary, @a q_msg_size
2831 * must also be a multiple of N.
2832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002833 * @param q Address of the message queue.
2834 * @param buffer Pointer to ring buffer that holds queued messages.
2835 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002836 * @param max_msgs Maximum number of messages that can be queued.
2837 *
2838 * @return N/A
2839 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002840extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002841 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002842
2843/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002844 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002846 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002847 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002848 * @note Can be called by ISRs.
2849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002850 * @param q Address of the message queue.
2851 * @param data Pointer to the message.
2852 * @param timeout Waiting period to add the message (in milliseconds),
2853 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002854 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002855 * @retval 0 Message sent.
2856 * @retval -ENOMSG Returned without waiting or queue purged.
2857 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002858 */
Kumar Galacc334c72017-04-21 10:55:34 -05002859extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002860
2861/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002862 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002864 * This routine receives a message from message queue @a q in a "first in,
2865 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002866 *
Allan Stephensc98da842016-11-11 15:45:03 -05002867 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002869 * @param q Address of the message queue.
2870 * @param data Address of area to hold the received message.
2871 * @param timeout Waiting period to receive the message (in milliseconds),
2872 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002873 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002874 * @retval 0 Message received.
2875 * @retval -ENOMSG Returned without waiting.
2876 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002877 */
Kumar Galacc334c72017-04-21 10:55:34 -05002878extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002879
2880/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002881 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * This routine discards all unreceived messages in a message queue's ring
2884 * buffer. Any threads that are blocked waiting to send a message to the
2885 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002887 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002888 *
2889 * @return N/A
2890 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002891extern void k_msgq_purge(struct k_msgq *q);
2892
Peter Mitsis67be2492016-10-07 11:44:34 -04002893/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002894 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002896 * This routine returns the number of unused entries in a message queue's
2897 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002899 * @param q Address of the message queue.
2900 *
2901 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002902 */
Kumar Galacc334c72017-04-21 10:55:34 -05002903static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002904{
2905 return q->max_msgs - q->used_msgs;
2906}
2907
Peter Mitsisd7a37502016-10-13 11:37:40 -04002908/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002909 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002911 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * @param q Address of the message queue.
2914 *
2915 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002916 */
Kumar Galacc334c72017-04-21 10:55:34 -05002917static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002918{
2919 return q->used_msgs;
2920}
2921
Allan Stephensc98da842016-11-11 15:45:03 -05002922/**
2923 * @} end defgroup msgq_apis
2924 */
2925
2926/**
2927 * @defgroup mem_pool_apis Memory Pool APIs
2928 * @ingroup kernel_apis
2929 * @{
2930 */
2931
Andy Ross73cb9582017-05-09 10:42:39 -07002932/* Note on sizing: the use of a 20 bit field for block means that,
2933 * assuming a reasonable minimum block size of 16 bytes, we're limited
2934 * to 16M of memory managed by a single pool. Long term it would be
2935 * good to move to a variable bit size based on configuration.
2936 */
2937struct k_mem_block_id {
2938 u32_t pool : 8;
2939 u32_t level : 4;
2940 u32_t block : 20;
2941};
2942
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002943struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002945 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002946};
2947
Allan Stephensc98da842016-11-11 15:45:03 -05002948/**
2949 * @} end defgroup mem_pool_apis
2950 */
2951
2952/**
2953 * @defgroup mailbox_apis Mailbox APIs
2954 * @ingroup kernel_apis
2955 * @{
2956 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002957
2958struct k_mbox_msg {
2959 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002960 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002961 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002962 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002963 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002964 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002965 /** sender's message data buffer */
2966 void *tx_data;
2967 /** internal use only - needed for legacy API support */
2968 void *_rx_data;
2969 /** message data block descriptor */
2970 struct k_mem_block tx_block;
2971 /** source thread id */
2972 k_tid_t rx_source_thread;
2973 /** target thread id */
2974 k_tid_t tx_target_thread;
2975 /** internal use only - thread waiting on send (may be a dummy) */
2976 k_tid_t _syncing_thread;
2977#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2978 /** internal use only - semaphore used during asynchronous send */
2979 struct k_sem *_async_sem;
2980#endif
2981};
2982
Allan Stephensc98da842016-11-11 15:45:03 -05002983/**
2984 * @cond INTERNAL_HIDDEN
2985 */
2986
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002987struct k_mbox {
2988 _wait_q_t tx_msg_queue;
2989 _wait_q_t rx_msg_queue;
2990
Anas Nashif2f203c22016-12-18 06:57:45 -05002991 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002992};
2993
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002994#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002995 { \
2996 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2997 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002998 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002999 }
3000
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003001#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3002
Peter Mitsis12092702016-10-14 12:57:23 -04003003/**
Allan Stephensc98da842016-11-11 15:45:03 -05003004 * INTERNAL_HIDDEN @endcond
3005 */
3006
3007/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003008 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003010 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003011 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003012 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003014 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003015 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003016#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003017 struct k_mbox name \
3018 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003019 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003020
Peter Mitsis12092702016-10-14 12:57:23 -04003021/**
3022 * @brief Initialize a mailbox.
3023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003024 * This routine initializes a mailbox object, prior to its first use.
3025 *
3026 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003027 *
3028 * @return N/A
3029 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003030extern void k_mbox_init(struct k_mbox *mbox);
3031
Peter Mitsis12092702016-10-14 12:57:23 -04003032/**
3033 * @brief Send a mailbox message in a synchronous manner.
3034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003035 * This routine sends a message to @a mbox and waits for a receiver to both
3036 * receive and process it. The message data may be in a buffer, in a memory
3037 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003039 * @param mbox Address of the mailbox.
3040 * @param tx_msg Address of the transmit message descriptor.
3041 * @param timeout Waiting period for the message to be received (in
3042 * milliseconds), or one of the special values K_NO_WAIT
3043 * and K_FOREVER. Once the message has been received,
3044 * this routine waits as long as necessary for the message
3045 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003046 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003047 * @retval 0 Message sent.
3048 * @retval -ENOMSG Returned without waiting.
3049 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003050 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003051extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003052 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003053
Peter Mitsis12092702016-10-14 12:57:23 -04003054/**
3055 * @brief Send a mailbox message in an asynchronous manner.
3056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003057 * This routine sends a message to @a mbox without waiting for a receiver
3058 * to process it. The message data may be in a buffer, in a memory pool block,
3059 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3060 * will be given when the message has been both received and completely
3061 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003063 * @param mbox Address of the mailbox.
3064 * @param tx_msg Address of the transmit message descriptor.
3065 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003066 *
3067 * @return N/A
3068 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003069extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003070 struct k_sem *sem);
3071
Peter Mitsis12092702016-10-14 12:57:23 -04003072/**
3073 * @brief Receive a mailbox message.
3074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003075 * This routine receives a message from @a mbox, then optionally retrieves
3076 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003078 * @param mbox Address of the mailbox.
3079 * @param rx_msg Address of the receive message descriptor.
3080 * @param buffer Address of the buffer to receive data, or NULL to defer data
3081 * retrieval and message disposal until later.
3082 * @param timeout Waiting period for a message to be received (in
3083 * milliseconds), or one of the special values K_NO_WAIT
3084 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003085 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003086 * @retval 0 Message received.
3087 * @retval -ENOMSG Returned without waiting.
3088 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003089 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003090extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003091 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003092
3093/**
3094 * @brief Retrieve mailbox message data into a buffer.
3095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003096 * This routine completes the processing of a received message by retrieving
3097 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003098 *
3099 * Alternatively, this routine can be used to dispose of a received message
3100 * without retrieving its data.
3101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003102 * @param rx_msg Address of the receive message descriptor.
3103 * @param buffer Address of the buffer to receive data, or NULL to discard
3104 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003105 *
3106 * @return N/A
3107 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003108extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003109
3110/**
3111 * @brief Retrieve mailbox message data into a memory pool block.
3112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003113 * This routine completes the processing of a received message by retrieving
3114 * its data into a memory pool block, then disposing of the message.
3115 * The memory pool block that results from successful retrieval must be
3116 * returned to the pool once the data has been processed, even in cases
3117 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003118 *
3119 * Alternatively, this routine can be used to dispose of a received message
3120 * without retrieving its data. In this case there is no need to return a
3121 * memory pool block to the pool.
3122 *
3123 * This routine allocates a new memory pool block for the data only if the
3124 * data is not already in one. If a new block cannot be allocated, the routine
3125 * returns a failure code and the received message is left unchanged. This
3126 * permits the caller to reattempt data retrieval at a later time or to dispose
3127 * of the received message without retrieving its data.
3128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003129 * @param rx_msg Address of a receive message descriptor.
3130 * @param pool Address of memory pool, or NULL to discard data.
3131 * @param block Address of the area to hold memory pool block info.
3132 * @param timeout Waiting period to wait for a memory pool block (in
3133 * milliseconds), or one of the special values K_NO_WAIT
3134 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003135 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003136 * @retval 0 Data retrieved.
3137 * @retval -ENOMEM Returned without waiting.
3138 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003139 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003140extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003141 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003142 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003143
Allan Stephensc98da842016-11-11 15:45:03 -05003144/**
3145 * @} end defgroup mailbox_apis
3146 */
3147
3148/**
3149 * @cond INTERNAL_HIDDEN
3150 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003151
3152struct k_pipe {
3153 unsigned char *buffer; /* Pipe buffer: may be NULL */
3154 size_t size; /* Buffer size */
3155 size_t bytes_used; /* # bytes used in buffer */
3156 size_t read_index; /* Where in buffer to read from */
3157 size_t write_index; /* Where in buffer to write */
3158
3159 struct {
3160 _wait_q_t readers; /* Reader wait queue */
3161 _wait_q_t writers; /* Writer wait queue */
3162 } wait_q;
3163
Anas Nashif2f203c22016-12-18 06:57:45 -05003164 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003165};
3166
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003167#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003168 { \
3169 .buffer = pipe_buffer, \
3170 .size = pipe_buffer_size, \
3171 .bytes_used = 0, \
3172 .read_index = 0, \
3173 .write_index = 0, \
3174 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3175 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003176 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003177 }
3178
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003179#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3180
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003181/**
Allan Stephensc98da842016-11-11 15:45:03 -05003182 * INTERNAL_HIDDEN @endcond
3183 */
3184
3185/**
3186 * @defgroup pipe_apis Pipe APIs
3187 * @ingroup kernel_apis
3188 * @{
3189 */
3190
3191/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003192 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003193 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003194 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003195 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003196 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003198 * @param name Name of the pipe.
3199 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3200 * or zero if no ring buffer is used.
3201 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003202 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003203#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3204 static unsigned char __noinit __aligned(pipe_align) \
3205 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003206 struct k_pipe name \
3207 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003208 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003209
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003210/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003211 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003212 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003213 * This routine initializes a pipe object, prior to its first use.
3214 *
3215 * @param pipe Address of the pipe.
3216 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3217 * is used.
3218 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3219 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003220 *
3221 * @return N/A
3222 */
3223extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3224 size_t size);
3225
3226/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003227 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003229 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003231 * @param pipe Address of the pipe.
3232 * @param data Address of data to write.
3233 * @param bytes_to_write Size of data (in bytes).
3234 * @param bytes_written Address of area to hold the number of bytes written.
3235 * @param min_xfer Minimum number of bytes to write.
3236 * @param timeout Waiting period to wait for the data to be written (in
3237 * milliseconds), or one of the special values K_NO_WAIT
3238 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003239 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003240 * @retval 0 At least @a min_xfer bytes of data were written.
3241 * @retval -EIO Returned without waiting; zero data bytes were written.
3242 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003243 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003244 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003245extern int k_pipe_put(struct k_pipe *pipe, void *data,
3246 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003247 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003248
3249/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003250 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003252 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003254 * @param pipe Address of the pipe.
3255 * @param data Address to place the data read from pipe.
3256 * @param bytes_to_read Maximum number of data bytes to read.
3257 * @param bytes_read Address of area to hold the number of bytes read.
3258 * @param min_xfer Minimum number of data bytes to read.
3259 * @param timeout Waiting period to wait for the data to be read (in
3260 * milliseconds), or one of the special values K_NO_WAIT
3261 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003262 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003263 * @retval 0 At least @a min_xfer bytes of data were read.
3264 * @retval -EIO Returned without waiting; zero data bytes were read.
3265 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003266 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003267 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003268extern int k_pipe_get(struct k_pipe *pipe, void *data,
3269 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003270 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003271
3272/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003273 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003275 * This routine writes the data contained in a memory block to @a pipe.
3276 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003277 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003279 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003280 * @param block Memory block containing data to send
3281 * @param size Number of data bytes in memory block to send
3282 * @param sem Semaphore to signal upon completion (else NULL)
3283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003284 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285 */
3286extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3287 size_t size, struct k_sem *sem);
3288
3289/**
Allan Stephensc98da842016-11-11 15:45:03 -05003290 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003291 */
3292
Allan Stephensc98da842016-11-11 15:45:03 -05003293/**
3294 * @cond INTERNAL_HIDDEN
3295 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003296
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003297struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003298 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003299 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003300 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003301 char *buffer;
3302 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003303 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003304
Anas Nashif2f203c22016-12-18 06:57:45 -05003305 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003306};
3307
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003308#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003309 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003310 { \
3311 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003312 .num_blocks = slab_num_blocks, \
3313 .block_size = slab_block_size, \
3314 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003315 .free_list = NULL, \
3316 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003317 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003318 }
3319
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003320#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3321
3322
Peter Mitsis578f9112016-10-07 13:50:31 -04003323/**
Allan Stephensc98da842016-11-11 15:45:03 -05003324 * INTERNAL_HIDDEN @endcond
3325 */
3326
3327/**
3328 * @defgroup mem_slab_apis Memory Slab APIs
3329 * @ingroup kernel_apis
3330 * @{
3331 */
3332
3333/**
Allan Stephensda827222016-11-09 14:23:58 -06003334 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003335 *
Allan Stephensda827222016-11-09 14:23:58 -06003336 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003337 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003338 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3339 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003340 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003341 *
Allan Stephensda827222016-11-09 14:23:58 -06003342 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003344 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003345 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003347 * @param name Name of the memory slab.
3348 * @param slab_block_size Size of each memory block (in bytes).
3349 * @param slab_num_blocks Number memory blocks.
3350 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003351 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003352#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3353 char __noinit __aligned(slab_align) \
3354 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3355 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003356 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003357 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003358 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003359
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003360/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003361 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003364 *
Allan Stephensda827222016-11-09 14:23:58 -06003365 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3366 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3367 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3368 * To ensure that each memory block is similarly aligned to this boundary,
3369 * @a slab_block_size must also be a multiple of N.
3370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003371 * @param slab Address of the memory slab.
3372 * @param buffer Pointer to buffer used for the memory blocks.
3373 * @param block_size Size of each memory block (in bytes).
3374 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003375 *
3376 * @return N/A
3377 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003378extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003379 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003380
3381/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003382 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003384 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003386 * @param slab Address of the memory slab.
3387 * @param mem Pointer to block address area.
3388 * @param timeout Maximum time to wait for operation to complete
3389 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3390 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003391 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003392 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003393 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003394 * @retval -ENOMEM Returned without waiting.
3395 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003396 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003397extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003398 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003399
3400/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003401 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * This routine releases a previously allocated memory block back to its
3404 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003406 * @param slab Address of the memory slab.
3407 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003408 *
3409 * @return N/A
3410 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003411extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003412
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003413/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003414 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003416 * This routine gets the number of memory blocks that are currently
3417 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003419 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003421 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003422 */
Kumar Galacc334c72017-04-21 10:55:34 -05003423static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003424{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003425 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003426}
3427
Peter Mitsisc001aa82016-10-13 13:53:37 -04003428/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003429 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003431 * This routine gets the number of memory blocks that are currently
3432 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003434 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003436 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003437 */
Kumar Galacc334c72017-04-21 10:55:34 -05003438static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003439{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003440 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003441}
3442
Allan Stephensc98da842016-11-11 15:45:03 -05003443/**
3444 * @} end defgroup mem_slab_apis
3445 */
3446
3447/**
3448 * @cond INTERNAL_HIDDEN
3449 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003450
Andy Ross73cb9582017-05-09 10:42:39 -07003451struct k_mem_pool_lvl {
3452 union {
3453 u32_t *bits_p;
3454 u32_t bits;
3455 };
3456 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003457};
3458
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003459struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003460 void *buf;
3461 size_t max_sz;
3462 u16_t n_max;
3463 u8_t n_levels;
3464 u8_t max_inline_level;
3465 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003466 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003467};
3468
Andy Ross73cb9582017-05-09 10:42:39 -07003469#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003470
Andy Ross73cb9582017-05-09 10:42:39 -07003471#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3472
3473#define _MPOOL_LVLS(maxsz, minsz) \
3474 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3475 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3476 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3477 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3478 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3479 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3480 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3481 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3482 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3483 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3484 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3485 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3486 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3487 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3488 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3489 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3490
3491/* Rounds the needed bits up to integer multiples of u32_t */
3492#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3493 ((((n_max) << (2*(l))) + 31) / 32)
3494
3495/* One word gets stored free unioned with the pointer, otherwise the
3496 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003497 */
Andy Ross73cb9582017-05-09 10:42:39 -07003498#define _MPOOL_LBIT_WORDS(n_max, l) \
3499 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3500 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003501
Andy Ross73cb9582017-05-09 10:42:39 -07003502/* How many bytes for the bitfields of a single level? */
3503#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3504 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3505 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003506
Andy Ross73cb9582017-05-09 10:42:39 -07003507/* Size of the bitmap array that follows the buffer in allocated memory */
3508#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3509 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3510 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3511 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3512 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3513 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3514 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3515 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3516 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3517 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3518 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3519 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3520 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3521 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3522 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3523 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3524 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003525
3526/**
Allan Stephensc98da842016-11-11 15:45:03 -05003527 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003528 */
3529
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003530/**
Allan Stephensc98da842016-11-11 15:45:03 -05003531 * @addtogroup mem_pool_apis
3532 * @{
3533 */
3534
3535/**
3536 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003538 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3539 * long. The memory pool allows blocks to be repeatedly partitioned into
3540 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003541 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003542 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003543 * If the pool is to be accessed outside the module where it is defined, it
3544 * can be declared via
3545 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003546 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003548 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003549 * @param minsz Size of the smallest blocks in the pool (in bytes).
3550 * @param maxsz Size of the largest blocks in the pool (in bytes).
3551 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003552 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003553 */
Andy Ross73cb9582017-05-09 10:42:39 -07003554#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3555 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3556 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3557 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3558 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3559 .buf = _mpool_buf_##name, \
3560 .max_sz = maxsz, \
3561 .n_max = nmax, \
3562 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3563 .levels = _mpool_lvls_##name, \
3564 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003565
Peter Mitsis937042c2016-10-13 13:18:26 -04003566/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003567 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003569 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003570 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003571 * @param pool Address of the memory pool.
3572 * @param block Pointer to block descriptor for the allocated memory.
3573 * @param size Amount of memory to allocate (in bytes).
3574 * @param timeout Maximum time to wait for operation to complete
3575 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3576 * or K_FOREVER to wait as long as necessary.
3577 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003578 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003579 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003580 * @retval -ENOMEM Returned without waiting.
3581 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003582 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003583extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003584 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003585
3586/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003587 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003589 * This routine releases a previously allocated memory block back to its
3590 * memory pool.
3591 *
3592 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003593 *
3594 * @return N/A
3595 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003596extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003597
3598/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003599 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003600 *
Andy Ross73cb9582017-05-09 10:42:39 -07003601 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003602 *
Andy Ross73cb9582017-05-09 10:42:39 -07003603 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003604 *
3605 * @return N/A
3606 */
Andy Ross73cb9582017-05-09 10:42:39 -07003607static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003608
3609/**
Allan Stephensc98da842016-11-11 15:45:03 -05003610 * @} end addtogroup mem_pool_apis
3611 */
3612
3613/**
3614 * @defgroup heap_apis Heap Memory Pool APIs
3615 * @ingroup kernel_apis
3616 * @{
3617 */
3618
3619/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003620 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003622 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003623 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003625 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003626 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003627 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003628 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003629extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003630
3631/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003632 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003633 *
3634 * This routine provides traditional free() semantics. The memory being
3635 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003636 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003637 * If @a ptr is NULL, no operation is performed.
3638 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003639 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003640 *
3641 * @return N/A
3642 */
3643extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003644
Allan Stephensc98da842016-11-11 15:45:03 -05003645/**
3646 * @} end defgroup heap_apis
3647 */
3648
Benjamin Walshacc68c12017-01-29 18:57:45 -05003649/* polling API - PRIVATE */
3650
Benjamin Walshb0179862017-02-02 16:39:57 -05003651#ifdef CONFIG_POLL
3652#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3653#else
3654#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3655#endif
3656
Benjamin Walshacc68c12017-01-29 18:57:45 -05003657/* private - implementation data created as needed, per-type */
3658struct _poller {
3659 struct k_thread *thread;
3660};
3661
3662/* private - types bit positions */
3663enum _poll_types_bits {
3664 /* can be used to ignore an event */
3665 _POLL_TYPE_IGNORE,
3666
3667 /* to be signaled by k_poll_signal() */
3668 _POLL_TYPE_SIGNAL,
3669
3670 /* semaphore availability */
3671 _POLL_TYPE_SEM_AVAILABLE,
3672
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003673 /* queue/fifo/lifo data availability */
3674 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003675
3676 _POLL_NUM_TYPES
3677};
3678
3679#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3680
3681/* private - states bit positions */
3682enum _poll_states_bits {
3683 /* default state when creating event */
3684 _POLL_STATE_NOT_READY,
3685
Benjamin Walshacc68c12017-01-29 18:57:45 -05003686 /* signaled by k_poll_signal() */
3687 _POLL_STATE_SIGNALED,
3688
3689 /* semaphore is available */
3690 _POLL_STATE_SEM_AVAILABLE,
3691
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003692 /* data is available to read on queue/fifo/lifo */
3693 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003694
3695 _POLL_NUM_STATES
3696};
3697
3698#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3699
3700#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003701 (32 - (0 \
3702 + 8 /* tag */ \
3703 + _POLL_NUM_TYPES \
3704 + _POLL_NUM_STATES \
3705 + 1 /* modes */ \
3706 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003707
3708#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3709#error overflow of 32-bit word in struct k_poll_event
3710#endif
3711
3712/* end of polling API - PRIVATE */
3713
3714
3715/**
3716 * @defgroup poll_apis Async polling APIs
3717 * @ingroup kernel_apis
3718 * @{
3719 */
3720
3721/* Public polling API */
3722
3723/* public - values for k_poll_event.type bitfield */
3724#define K_POLL_TYPE_IGNORE 0
3725#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3726#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003727#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3728#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003729
3730/* public - polling modes */
3731enum k_poll_modes {
3732 /* polling thread does not take ownership of objects when available */
3733 K_POLL_MODE_NOTIFY_ONLY = 0,
3734
3735 K_POLL_NUM_MODES
3736};
3737
3738/* public - values for k_poll_event.state bitfield */
3739#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003740#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3741#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003742#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3743#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003744
3745/* public - poll signal object */
3746struct k_poll_signal {
3747 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003748 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003749
3750 /*
3751 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3752 * user resets it to 0.
3753 */
3754 unsigned int signaled;
3755
3756 /* custom result value passed to k_poll_signal() if needed */
3757 int result;
3758};
3759
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003760#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003761 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003762 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003763 .signaled = 0, \
3764 .result = 0, \
3765 }
3766
3767struct k_poll_event {
3768 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003769 sys_dnode_t _node;
3770
3771 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003772 struct _poller *poller;
3773
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003774 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003775 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003776
Benjamin Walshacc68c12017-01-29 18:57:45 -05003777 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003778 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003779
3780 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003781 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003782
3783 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003784 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003785
3786 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003787 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003788
3789 /* per-type data */
3790 union {
3791 void *obj;
3792 struct k_poll_signal *signal;
3793 struct k_sem *sem;
3794 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003795 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003796 };
3797};
3798
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003799#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003800 { \
3801 .poller = NULL, \
3802 .type = event_type, \
3803 .state = K_POLL_STATE_NOT_READY, \
3804 .mode = event_mode, \
3805 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003806 { .obj = event_obj }, \
3807 }
3808
3809#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3810 event_tag) \
3811 { \
3812 .type = event_type, \
3813 .tag = event_tag, \
3814 .state = K_POLL_STATE_NOT_READY, \
3815 .mode = event_mode, \
3816 .unused = 0, \
3817 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003818 }
3819
3820/**
3821 * @brief Initialize one struct k_poll_event instance
3822 *
3823 * After this routine is called on a poll event, the event it ready to be
3824 * placed in an event array to be passed to k_poll().
3825 *
3826 * @param event The event to initialize.
3827 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3828 * values. Only values that apply to the same object being polled
3829 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3830 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003831 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003832 * @param obj Kernel object or poll signal.
3833 *
3834 * @return N/A
3835 */
3836
Kumar Galacc334c72017-04-21 10:55:34 -05003837extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003838 int mode, void *obj);
3839
3840/**
3841 * @brief Wait for one or many of multiple poll events to occur
3842 *
3843 * This routine allows a thread to wait concurrently for one or many of
3844 * multiple poll events to have occurred. Such events can be a kernel object
3845 * being available, like a semaphore, or a poll signal event.
3846 *
3847 * When an event notifies that a kernel object is available, the kernel object
3848 * is not "given" to the thread calling k_poll(): it merely signals the fact
3849 * that the object was available when the k_poll() call was in effect. Also,
3850 * all threads trying to acquire an object the regular way, i.e. by pending on
3851 * the object, have precedence over the thread polling on the object. This
3852 * means that the polling thread will never get the poll event on an object
3853 * until the object becomes available and its pend queue is empty. For this
3854 * reason, the k_poll() call is more effective when the objects being polled
3855 * only have one thread, the polling thread, trying to acquire them.
3856 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003857 * When k_poll() returns 0, the caller should loop on all the events that were
3858 * passed to k_poll() and check the state field for the values that were
3859 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003860 *
3861 * Before being reused for another call to k_poll(), the user has to reset the
3862 * state field to K_POLL_STATE_NOT_READY.
3863 *
3864 * @param events An array of pointers to events to be polled for.
3865 * @param num_events The number of events in the array.
3866 * @param timeout Waiting period for an event to be ready (in milliseconds),
3867 * or one of the special values K_NO_WAIT and K_FOREVER.
3868 *
3869 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003870 * @retval -EAGAIN Waiting period timed out.
3871 */
3872
3873extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003874 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003875
3876/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003877 * @brief Initialize a poll signal object.
3878 *
3879 * Ready a poll signal object to be signaled via k_poll_signal().
3880 *
3881 * @param signal A poll signal.
3882 *
3883 * @return N/A
3884 */
3885
3886extern void k_poll_signal_init(struct k_poll_signal *signal);
3887
3888/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003889 * @brief Signal a poll signal object.
3890 *
3891 * This routine makes ready a poll signal, which is basically a poll event of
3892 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3893 * made ready to run. A @a result value can be specified.
3894 *
3895 * The poll signal contains a 'signaled' field that, when set by
3896 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3897 * be reset by the user before being passed again to k_poll() or k_poll() will
3898 * consider it being signaled, and will return immediately.
3899 *
3900 * @param signal A poll signal.
3901 * @param result The value to store in the result field of the signal.
3902 *
3903 * @retval 0 The signal was delivered successfully.
3904 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3905 */
3906
3907extern int k_poll_signal(struct k_poll_signal *signal, int result);
3908
3909/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003910extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003911
3912/**
3913 * @} end defgroup poll_apis
3914 */
3915
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003916/**
3917 * @brief Make the CPU idle.
3918 *
3919 * This function makes the CPU idle until an event wakes it up.
3920 *
3921 * In a regular system, the idle thread should be the only thread responsible
3922 * for making the CPU idle and triggering any type of power management.
3923 * However, in some more constrained systems, such as a single-threaded system,
3924 * the only thread would be responsible for this if needed.
3925 *
3926 * @return N/A
3927 */
3928extern void k_cpu_idle(void);
3929
3930/**
3931 * @brief Make the CPU idle in an atomic fashion.
3932 *
3933 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3934 * must be done atomically before making the CPU idle.
3935 *
3936 * @param key Interrupt locking key obtained from irq_lock().
3937 *
3938 * @return N/A
3939 */
3940extern void k_cpu_atomic_idle(unsigned int key);
3941
Kumar Galacc334c72017-04-21 10:55:34 -05003942extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003943
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003944#include <arch/cpu.h>
3945
Andrew Boiecdb94d62017-04-18 15:22:05 -07003946#ifdef _ARCH_EXCEPT
3947/* This archtecture has direct support for triggering a CPU exception */
3948#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3949#else
3950
3951#include <misc/printk.h>
3952
3953/* NOTE: This is the implementation for arches that do not implement
3954 * _ARCH_EXCEPT() to generate a real CPU exception.
3955 *
3956 * We won't have a real exception frame to determine the PC value when
3957 * the oops occurred, so print file and line number before we jump into
3958 * the fatal error handler.
3959 */
3960#define _k_except_reason(reason) do { \
3961 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3962 _NanoFatalErrorHandler(reason, &_default_esf); \
3963 CODE_UNREACHABLE; \
3964 } while (0)
3965
3966#endif /* _ARCH__EXCEPT */
3967
3968/**
3969 * @brief Fatally terminate a thread
3970 *
3971 * This should be called when a thread has encountered an unrecoverable
3972 * runtime condition and needs to terminate. What this ultimately
3973 * means is determined by the _fatal_error_handler() implementation, which
3974 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3975 *
3976 * If this is called from ISR context, the default system fatal error handler
3977 * will treat it as an unrecoverable system error, just like k_panic().
3978 */
3979#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3980
3981/**
3982 * @brief Fatally terminate the system
3983 *
3984 * This should be called when the Zephyr kernel has encountered an
3985 * unrecoverable runtime condition and needs to terminate. What this ultimately
3986 * means is determined by the _fatal_error_handler() implementation, which
3987 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3988 */
3989#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3990
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003991/*
3992 * private APIs that are utilized by one or more public APIs
3993 */
3994
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003995#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003996extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003997#else
3998#define _init_static_threads() do { } while ((0))
3999#endif
4000
4001extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004002extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004003
Andrew Boiedc5d9352017-06-02 12:56:47 -07004004/* arch/cpu.h may declare an architecture or platform-specific macro
4005 * for properly declaring stacks, compatible with MMU/MPU constraints if
4006 * enabled
4007 */
4008#ifdef _ARCH_THREAD_STACK_DEFINE
4009#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4010#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4011 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4012#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4013#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004014static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4015{
4016 return _ARCH_THREAD_STACK_BUFFER(sym);
4017}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004018#else
4019/**
4020 * @brief Declare a toplevel thread stack memory region
4021 *
4022 * This declares a region of memory suitable for use as a thread's stack.
4023 *
4024 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4025 * 'noinit' section so that it isn't zeroed at boot
4026 *
Andrew Boie507852a2017-07-25 18:47:07 -07004027 * The declared symbol will always be a k_thread_stack_t which can be passed to
4028 * k_thread_create, but should otherwise not be manipulated. If the buffer
4029 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004030 *
4031 * It is legal to precede this definition with the 'static' keyword.
4032 *
4033 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4034 * parameter of k_thread_create(), it may not be the same as the
4035 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4036 *
4037 * @param sym Thread stack symbol name
4038 * @param size Size of the stack memory region
4039 */
4040#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004041 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004042
4043/**
4044 * @brief Declare a toplevel array of thread stack memory regions
4045 *
4046 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4047 * definition for additional details and constraints.
4048 *
4049 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4050 * 'noinit' section so that it isn't zeroed at boot
4051 *
4052 * @param sym Thread stack symbol name
4053 * @param nmemb Number of stacks to declare
4054 * @param size Size of the stack memory region
4055 */
4056
4057#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004058 struct _k_thread_stack_element __noinit \
4059 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004060
4061/**
4062 * @brief Declare an embedded stack memory region
4063 *
4064 * Used for stacks embedded within other data structures. Use is highly
4065 * discouraged but in some cases necessary. For memory protection scenarios,
4066 * it is very important that any RAM preceding this member not be writable
4067 * by threads else a stack overflow will lead to silent corruption. In other
4068 * words, the containing data structure should live in RAM owned by the kernel.
4069 *
4070 * @param sym Thread stack symbol name
4071 * @param size Size of the stack memory region
4072 */
4073#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004074 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004075
4076/**
4077 * @brief Return the size in bytes of a stack memory region
4078 *
4079 * Convenience macro for passing the desired stack size to k_thread_create()
4080 * since the underlying implementation may actually create something larger
4081 * (for instance a guard area).
4082 *
4083 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004084 * passed to K_THREAD_STACK_DEFINE.
4085 *
4086 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4087 * it is not guaranteed to return the original value since each array
4088 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004089 *
4090 * @param sym Stack memory symbol
4091 * @return Size of the stack
4092 */
4093#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4094
4095/**
4096 * @brief Get a pointer to the physical stack buffer
4097 *
4098 * Convenience macro to get at the real underlying stack buffer used by
4099 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4100 * This is really only intended for diagnostic tools which want to examine
4101 * stack memory contents.
4102 *
4103 * @param sym Declared stack symbol name
4104 * @return The buffer itself, a char *
4105 */
Andrew Boie507852a2017-07-25 18:47:07 -07004106static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4107{
4108 return (char *)sym;
4109}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004110
4111#endif /* _ARCH_DECLARE_STACK */
4112
Chunlin Hane9c97022017-07-07 20:29:30 +08004113/**
4114 * @defgroup mem_domain_apis Memory domain APIs
4115 * @ingroup kernel_apis
4116 * @{
4117 */
4118
4119/** @def MEM_PARTITION_ENTRY
4120 * @brief Used to declare a memory partition entry
4121 */
4122#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4123 {\
4124 .start = _start, \
4125 .size = _size, \
4126 .attr = _attr, \
4127 }
4128
4129/** @def K_MEM_PARTITION_DEFINE
4130 * @brief Used to declare a memory partition
4131 */
4132#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4133#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4134 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4135 struct k_mem_partition name = \
4136 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4137#else
4138#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4139 struct k_mem_partition name = \
4140 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4141#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4142
4143
4144/* memory partition */
4145struct k_mem_partition {
4146 /* start address of memory partition */
4147 u32_t start;
4148 /* size of memory partition */
4149 u32_t size;
4150 /* attribute of memory partition */
4151 u32_t attr;
4152};
4153
4154#if defined(CONFIG_USERSPACE)
4155/* memory domian */
4156struct k_mem_domain {
4157 /* number of partitions in the domain */
4158 u32_t num_partitions;
4159 /* partitions in the domain */
4160 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
4161 /* domain q */
4162 sys_dlist_t mem_domain_q;
4163};
4164#endif /* CONFIG_USERSPACE */
4165
4166/**
4167 * @brief Initialize a memory domain.
4168 *
4169 * Initialize a memory domain with given name and memory partitions.
4170 *
4171 * @param domain The memory domain to be initialized.
4172 * @param num_parts The number of array items of "parts" parameter.
4173 * @param parts An array of pointers to the memory partitions. Can be NULL
4174 * if num_parts is zero.
4175 */
4176
4177extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4178 struct k_mem_partition *parts[]);
4179/**
4180 * @brief Destroy a memory domain.
4181 *
4182 * Destroy a memory domain.
4183 *
4184 * @param domain The memory domain to be destroyed.
4185 */
4186
4187extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4188
4189/**
4190 * @brief Add a memory partition into a memory domain.
4191 *
4192 * Add a memory partition into a memory domain.
4193 *
4194 * @param domain The memory domain to be added a memory partition.
4195 * @param part The memory partition to be added
4196 */
4197
4198extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4199 struct k_mem_partition *part);
4200
4201/**
4202 * @brief Remove a memory partition from a memory domain.
4203 *
4204 * Remove a memory partition from a memory domain.
4205 *
4206 * @param domain The memory domain to be removed a memory partition.
4207 * @param part The memory partition to be removed
4208 */
4209
4210extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4211 struct k_mem_partition *part);
4212
4213/**
4214 * @brief Add a thread into a memory domain.
4215 *
4216 * Add a thread into a memory domain.
4217 *
4218 * @param domain The memory domain that the thread is going to be added into.
4219 * @param thread ID of thread going to be added into the memory domain.
4220 */
4221
4222extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4223 k_tid_t thread);
4224
4225/**
4226 * @brief Remove a thread from its memory domain.
4227 *
4228 * Remove a thread from its memory domain.
4229 *
4230 * @param thread ID of thread going to be removed from its memory domain.
4231 */
4232
4233extern void k_mem_domain_remove_thread(k_tid_t thread);
4234
4235/**
4236 * @} end defgroup mem_domain_apis
4237 */
4238
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004239#ifdef __cplusplus
4240}
4241#endif
4242
Andrew Boiee004dec2016-11-07 09:01:19 -08004243#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4244/*
4245 * Define new and delete operators.
4246 * At this moment, the operators do nothing since objects are supposed
4247 * to be statically allocated.
4248 */
4249inline void operator delete(void *ptr)
4250{
4251 (void)ptr;
4252}
4253
4254inline void operator delete[](void *ptr)
4255{
4256 (void)ptr;
4257}
4258
4259inline void *operator new(size_t size)
4260{
4261 (void)size;
4262 return NULL;
4263}
4264
4265inline void *operator new[](size_t size)
4266{
4267 (void)size;
4268 return NULL;
4269}
4270
4271/* Placement versions of operator new and delete */
4272inline void operator delete(void *ptr1, void *ptr2)
4273{
4274 (void)ptr1;
4275 (void)ptr2;
4276}
4277
4278inline void operator delete[](void *ptr1, void *ptr2)
4279{
4280 (void)ptr1;
4281 (void)ptr2;
4282}
4283
4284inline void *operator new(size_t size, void *ptr)
4285{
4286 (void)size;
4287 return ptr;
4288}
4289
4290inline void *operator new[](size_t size, void *ptr)
4291{
4292 (void)size;
4293 return ptr;
4294}
4295
4296#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4297
Andrew Boiefa94ee72017-09-28 16:54:35 -07004298#include <syscalls/kernel.h>
4299
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004300#endif /* !_ASMLANGUAGE */
4301
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004302#endif /* _kernel__h_ */