blob: dd5c6c3a4e976fa6d7f1eea31e25fe0a665bebde [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070030#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070031#include <syscall.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040032
33#ifdef __cplusplus
34extern "C" {
35#endif
36
Anas Nashifbbb157d2017-01-15 08:46:31 -050037/**
38 * @brief Kernel APIs
39 * @defgroup kernel_apis Kernel APIs
40 * @{
41 * @}
42 */
43
Anas Nashif61f4b242016-11-18 10:53:59 -050044#ifdef CONFIG_KERNEL_DEBUG
45#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040046#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
47#else
48#define K_DEBUG(fmt, ...)
49#endif
50
Benjamin Walsh2f280412017-01-14 19:23:46 -050051#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
52#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
53#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
54#elif defined(CONFIG_COOP_ENABLED)
55#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
56#define _NUM_PREEMPT_PRIO (0)
57#elif defined(CONFIG_PREEMPT_ENABLED)
58#define _NUM_COOP_PRIO (0)
59#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
60#else
61#error "invalid configuration"
62#endif
63
64#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040065#define K_PRIO_PREEMPT(x) (x)
66
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_ANY NULL
68#define K_END NULL
69
Benjamin Walshedb35702017-01-14 18:47:22 -050070#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040071#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050072#elif defined(CONFIG_COOP_ENABLED)
73#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
74#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040075#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050076#else
77#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040078#endif
79
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050080#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040081#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
82#else
83#define K_LOWEST_THREAD_PRIO -1
84#endif
85
Benjamin Walshfab8d922016-11-08 15:36:36 -050086#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
87
Benjamin Walsh456c6da2016-09-02 18:55:39 -040088#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
89#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
90
91typedef sys_dlist_t _wait_q_t;
92
Anas Nashif2f203c22016-12-18 06:57:45 -050093#ifdef CONFIG_OBJECT_TRACING
94#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
95#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096#else
Anas Nashif2f203c22016-12-18 06:57:45 -050097#define _OBJECT_TRACING_INIT
98#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099#endif
100
Benjamin Walshacc68c12017-01-29 18:57:45 -0500101#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300102#define _POLL_EVENT_OBJ_INIT(obj) \
103 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
104#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500105#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300106#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500107#define _POLL_EVENT
108#endif
109
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500110struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400111struct k_mutex;
112struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400113struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400114struct k_msgq;
115struct k_mbox;
116struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200117struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400118struct k_fifo;
119struct k_lifo;
120struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400121struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122struct k_mem_pool;
123struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500124struct k_poll_event;
125struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800126struct k_mem_domain;
127struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400128
Andrew Boie5bd891d2017-09-27 12:59:28 -0700129/* This enumeration needs to be kept in sync with the lists of kernel objects
130 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
131 * function in kernel/userspace.c
132 */
Andrew Boie945af952017-08-22 13:15:23 -0700133enum k_objects {
Andrew Boie5bd891d2017-09-27 12:59:28 -0700134 /* Core kernel objects */
Andrew Boie945af952017-08-22 13:15:23 -0700135 K_OBJ_ALERT,
136 K_OBJ_DELAYED_WORK,
137 K_OBJ_MEM_SLAB,
138 K_OBJ_MSGQ,
139 K_OBJ_MUTEX,
140 K_OBJ_PIPE,
141 K_OBJ_SEM,
142 K_OBJ_STACK,
143 K_OBJ_THREAD,
144 K_OBJ_TIMER,
145 K_OBJ_WORK,
146 K_OBJ_WORK_Q,
147
Andrew Boie5bd891d2017-09-27 12:59:28 -0700148 /* Driver subsystems */
149 K_OBJ_DRIVER_ADC,
150 K_OBJ_DRIVER_AIO_CMP,
151 K_OBJ_DRIVER_CLOCK_CONTROL,
152 K_OBJ_DRIVER_COUNTER,
153 K_OBJ_DRIVER_CRYPTO,
154 K_OBJ_DRIVER_DMA,
155 K_OBJ_DRIVER_ETH,
156 K_OBJ_DRIVER_FLASH,
157 K_OBJ_DRIVER_GPIO,
158 K_OBJ_DRIVER_I2C,
159 K_OBJ_DRIVER_I2S,
160 K_OBJ_DRIVER_IPM,
161 K_OBJ_DRIVER_PINMUX,
162 K_OBJ_DRIVER_PWM,
163 K_OBJ_DRIVER_RANDOM,
164 K_OBJ_DRIVER_RTC,
165 K_OBJ_DRIVER_SENSOR,
166 K_OBJ_DRIVER_SHARED_IRQ,
167 K_OBJ_DRIVER_SPI,
168 K_OBJ_DRIVER_UART,
169 K_OBJ_DRIVER_WDT,
170
Andrew Boie945af952017-08-22 13:15:23 -0700171 K_OBJ_LAST
172};
173
174#ifdef CONFIG_USERSPACE
175/* Table generated by gperf, these objects are retrieved via
176 * _k_object_find() */
177struct _k_object {
178 char *name;
179 char perms[CONFIG_MAX_THREAD_BYTES];
180 char type;
181 char flags;
182} __packed;
183
184#define K_OBJ_FLAG_INITIALIZED BIT(0)
185/**
186 * Ensure a system object is a valid object of the expected type
187 *
188 * Searches for the object and ensures that it is indeed an object
189 * of the expected type, that the caller has the right permissions on it,
190 * and that the object has been initialized.
191 *
192 * This function is intended to be called on the kernel-side system
193 * call handlers to validate kernel object pointers passed in from
194 * userspace.
195 *
196 * @param obj Address of the kernel object
197 * @param otype Expected type of the kernel object
198 * @param init If true, this is for an init function and we will not error
199 * out if the object is not initialized
200 * @return 0 If the object is valid
201 * -EBADF if not a valid object of the specified type
202 * -EPERM If the caller does not have permissions
David B. Kinder8065dbc2017-09-21 15:25:40 -0700203 * -EINVAL Object is not initialized
Andrew Boie945af952017-08-22 13:15:23 -0700204 */
205int _k_object_validate(void *obj, enum k_objects otype, int init);
206
207
208/**
209 * Lookup a kernel object and init its metadata if it exists
210 *
211 * Calling this on an object will make it usable from userspace.
212 * Intended to be called as the last statement in kernel object init
213 * functions.
214 *
215 * @param object Address of the kernel object
216 */
217void _k_object_init(void *obj);
218
219
220/**
221 * grant a thread access to a kernel object
222 *
223 * The thread will be granted access to the object if the caller is from
224 * supervisor mode, or the caller is from user mode AND has permissions
225 * on the object already.
226 *
227 * @param object Address of kernel object
228 * @param thread Thread to grant access to the object
229 */
Andrew Boie217017c2017-10-04 11:49:10 -0700230void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700231
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
Andrew Boie217017c2017-10-04 11:49:10 -0700247static inline void k_object_access_grant(void *object, struct k_thread *thread)
Andrew Boie945af952017-08-22 13:15:23 -0700248{
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/**
560 * @brief Drop a thread's privileges permanently to user mode
561 *
562 * @param entry Function to start executing from
563 * @param p1 1st entry point parameter
564 * @param p2 2nd entry point parameter
565 * @param p3 3rd entry point parameter
566 */
567extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
568 void *p1, void *p2,
569 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700570
Andrew Boied26cf2d2017-03-30 13:07:02 -0700571/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500572 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400573 *
Allan Stephensc98da842016-11-11 15:45:03 -0500574 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500575 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500577 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400578 *
579 * @return N/A
580 */
Kumar Galacc334c72017-04-21 10:55:34 -0500581extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400582
583/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500584 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400585 *
586 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500587 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400588 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400589 * @return N/A
590 */
Kumar Galacc334c72017-04-21 10:55:34 -0500591extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400592
593/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500594 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500596 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400597 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500598 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599 *
600 * @return N/A
601 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400602extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400603
604/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500605 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500607 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500609 * If @a thread is not currently sleeping, the routine has no effect.
610 *
611 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400612 *
613 * @return N/A
614 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400615extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400616
617/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500618 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500620 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400621 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400622extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400623
624/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500625 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400626 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500627 * This routine prevents @a thread from executing if it has not yet started
628 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400629 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500630 * @param thread ID of thread to cancel.
631 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700632 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500633 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400634 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400635extern int k_thread_cancel(k_tid_t thread);
636
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400637/**
Allan Stephensc98da842016-11-11 15:45:03 -0500638 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500640 * This routine permanently stops execution of @a thread. The thread is taken
641 * off all kernel queues it is part of (i.e. the ready queue, the timeout
642 * queue, or a kernel object wait queue). However, any kernel resources the
643 * thread might currently own (such as mutexes or memory blocks) are not
644 * released. It is the responsibility of the caller of this routine to ensure
645 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500647 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400648 *
649 * @return N/A
650 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400651extern void k_thread_abort(k_tid_t thread);
652
Andrew Boie7d627c52017-08-30 11:01:56 -0700653
654/**
655 * @brief Start an inactive thread
656 *
657 * If a thread was created with K_FOREVER in the delay parameter, it will
658 * not be added to the scheduling queue until this function is called
659 * on it.
660 *
661 * @param thread thread to start
662 */
663extern void k_thread_start(k_tid_t thread);
664
Allan Stephensc98da842016-11-11 15:45:03 -0500665/**
666 * @cond INTERNAL_HIDDEN
667 */
668
Benjamin Walshd211a522016-12-06 11:44:01 -0500669/* timeout has timed out and is not on _timeout_q anymore */
670#define _EXPIRED (-2)
671
672/* timeout is not in use */
673#define _INACTIVE (-1)
674
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400675struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700676 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700677 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400678 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700679 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500680 void *init_p1;
681 void *init_p2;
682 void *init_p3;
683 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500684 u32_t init_options;
685 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500686 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500687 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400688};
689
Andrew Boied26cf2d2017-03-30 13:07:02 -0700690#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400691 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500692 prio, options, delay, abort, groups) \
693 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700694 .init_thread = (thread), \
695 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500696 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700697 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400698 .init_p1 = (void *)p1, \
699 .init_p2 = (void *)p2, \
700 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500701 .init_prio = (prio), \
702 .init_options = (options), \
703 .init_delay = (delay), \
704 .init_abort = (abort), \
705 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400706 }
707
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400708/**
Allan Stephensc98da842016-11-11 15:45:03 -0500709 * INTERNAL_HIDDEN @endcond
710 */
711
712/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500713 * @brief Statically define and initialize a thread.
714 *
715 * The thread may be scheduled for immediate execution or a delayed start.
716 *
717 * Thread options are architecture-specific, and can include K_ESSENTIAL,
718 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
719 * them using "|" (the logical OR operator).
720 *
721 * The ID of the thread can be accessed using:
722 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500723 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500724 *
725 * @param name Name of the thread.
726 * @param stack_size Stack size in bytes.
727 * @param entry Thread entry function.
728 * @param p1 1st entry point parameter.
729 * @param p2 2nd entry point parameter.
730 * @param p3 3rd entry point parameter.
731 * @param prio Thread priority.
732 * @param options Thread options.
733 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400734 *
735 * @internal It has been observed that the x86 compiler by default aligns
736 * these _static_thread_data structures to 32-byte boundaries, thereby
737 * wasting space. To work around this, force a 4-byte alignment.
738 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500739#define K_THREAD_DEFINE(name, stack_size, \
740 entry, p1, p2, p3, \
741 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700742 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700743 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500744 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500745 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700746 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
747 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500748 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700749 NULL, 0); \
750 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400751
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400752/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500753 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500755 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500757 * @param thread ID of thread whose priority is needed.
758 *
759 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400760 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500761extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400762
763/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500764 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500766 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400767 *
768 * Rescheduling can occur immediately depending on the priority @a thread is
769 * set to:
770 *
771 * - If its priority is raised above the priority of the caller of this
772 * function, and the caller is preemptible, @a thread will be scheduled in.
773 *
774 * - If the caller operates on itself, it lowers its priority below that of
775 * other threads in the system, and the caller is preemptible, the thread of
776 * highest priority will be scheduled in.
777 *
778 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
779 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
780 * highest priority.
781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500782 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400783 * @param prio New priority.
784 *
785 * @warning Changing the priority of a thread currently involved in mutex
786 * priority inheritance may result in undefined behavior.
787 *
788 * @return N/A
789 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400790extern void k_thread_priority_set(k_tid_t thread, int prio);
791
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400792/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500793 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500795 * This routine prevents the kernel scheduler from making @a thread the
796 * current thread. All other internal operations on @a thread are still
797 * performed; for example, any timeout it is waiting on keeps ticking,
798 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500800 * If @a thread is already suspended, the routine has no effect.
801 *
802 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803 *
804 * @return N/A
805 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400806extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400807
808/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * This routine allows the kernel scheduler to make @a thread the current
812 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500814 * If @a thread is not currently suspended, the routine has no effect.
815 *
816 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817 *
818 * @return N/A
819 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400820extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400821
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400822/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500823 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500825 * This routine specifies how the scheduler will perform time slicing of
826 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500828 * To enable time slicing, @a slice must be non-zero. The scheduler
829 * ensures that no thread runs for more than the specified time limit
830 * before other threads of that priority are given a chance to execute.
831 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700832 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500834 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835 * execute. Once the scheduler selects a thread for execution, there is no
836 * minimum guaranteed time the thread will execute before threads of greater or
837 * equal priority are scheduled.
838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500839 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 * for execution, this routine has no effect; the thread is immediately
841 * rescheduled after the slice period expires.
842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * To disable timeslicing, set both @a slice and @a prio to zero.
844 *
845 * @param slice Maximum time slice length (in milliseconds).
846 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 *
848 * @return N/A
849 */
Kumar Galacc334c72017-04-21 10:55:34 -0500850extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400851
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400852/**
Allan Stephensc98da842016-11-11 15:45:03 -0500853 * @} end defgroup thread_apis
854 */
855
856/**
857 * @addtogroup isr_apis
858 * @{
859 */
860
861/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500862 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863 *
Allan Stephensc98da842016-11-11 15:45:03 -0500864 * This routine allows the caller to customize its actions, depending on
865 * whether it is a thread or an ISR.
866 *
867 * @note Can be called by ISRs.
868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500869 * @return 0 if invoked by a thread.
870 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500872extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400873
Benjamin Walsh445830d2016-11-10 15:54:27 -0500874/**
875 * @brief Determine if code is running in a preemptible thread.
876 *
Allan Stephensc98da842016-11-11 15:45:03 -0500877 * This routine allows the caller to customize its actions, depending on
878 * whether it can be preempted by another thread. The routine returns a 'true'
879 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500880 *
Allan Stephensc98da842016-11-11 15:45:03 -0500881 * - The code is running in a thread, not at ISR.
882 * - The thread's priority is in the preemptible range.
883 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500884 *
Allan Stephensc98da842016-11-11 15:45:03 -0500885 * @note Can be called by ISRs.
886 *
887 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500888 * @return Non-zero if invoked by a preemptible thread.
889 */
890extern int k_is_preempt_thread(void);
891
Allan Stephensc98da842016-11-11 15:45:03 -0500892/**
893 * @} end addtogroup isr_apis
894 */
895
896/**
897 * @addtogroup thread_apis
898 * @{
899 */
900
901/**
902 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500903 *
Allan Stephensc98da842016-11-11 15:45:03 -0500904 * This routine prevents the current thread from being preempted by another
905 * thread by instructing the scheduler to treat it as a cooperative thread.
906 * If the thread subsequently performs an operation that makes it unready,
907 * it will be context switched out in the normal manner. When the thread
908 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500909 *
Allan Stephensc98da842016-11-11 15:45:03 -0500910 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500911 *
Allan Stephensc98da842016-11-11 15:45:03 -0500912 * @note k_sched_lock() and k_sched_unlock() should normally be used
913 * when the operation being performed can be safely interrupted by ISRs.
914 * However, if the amount of processing involved is very small, better
915 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500916 *
917 * @return N/A
918 */
919extern void k_sched_lock(void);
920
Allan Stephensc98da842016-11-11 15:45:03 -0500921/**
922 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500923 *
Allan Stephensc98da842016-11-11 15:45:03 -0500924 * This routine reverses the effect of a previous call to k_sched_lock().
925 * A thread must call the routine once for each time it called k_sched_lock()
926 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500927 *
928 * @return N/A
929 */
930extern void k_sched_unlock(void);
931
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400932/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500933 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500935 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500937 * Custom data is not used by the kernel itself, and is freely available
938 * for a thread to use as it sees fit. It can be used as a framework
939 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500941 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400942 *
943 * @return N/A
944 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400945extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400946
947/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500948 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500950 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500952 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400953 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400954extern void *k_thread_custom_data_get(void);
955
956/**
Allan Stephensc98da842016-11-11 15:45:03 -0500957 * @} end addtogroup thread_apis
958 */
959
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400960#include <sys_clock.h>
961
Allan Stephensc2f15a42016-11-17 12:24:22 -0500962/**
963 * @addtogroup clock_apis
964 * @{
965 */
966
967/**
968 * @brief Generate null timeout delay.
969 *
970 * This macro generates a timeout delay that that instructs a kernel API
971 * not to wait if the requested operation cannot be performed immediately.
972 *
973 * @return Timeout delay value.
974 */
975#define K_NO_WAIT 0
976
977/**
978 * @brief Generate timeout delay from milliseconds.
979 *
980 * This macro generates a timeout delay that that instructs a kernel API
981 * to wait up to @a ms milliseconds to perform the requested operation.
982 *
983 * @param ms Duration in milliseconds.
984 *
985 * @return Timeout delay value.
986 */
Johan Hedberg14471692016-11-13 10:52:15 +0200987#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500988
989/**
990 * @brief Generate timeout delay from seconds.
991 *
992 * This macro generates a timeout delay that that instructs a kernel API
993 * to wait up to @a s seconds to perform the requested operation.
994 *
995 * @param s Duration in seconds.
996 *
997 * @return Timeout delay value.
998 */
Johan Hedberg14471692016-11-13 10:52:15 +0200999#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001000
1001/**
1002 * @brief Generate timeout delay from minutes.
1003 *
1004 * This macro generates a timeout delay that that instructs a kernel API
1005 * to wait up to @a m minutes to perform the requested operation.
1006 *
1007 * @param m Duration in minutes.
1008 *
1009 * @return Timeout delay value.
1010 */
Johan Hedberg14471692016-11-13 10:52:15 +02001011#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001012
1013/**
1014 * @brief Generate timeout delay from hours.
1015 *
1016 * This macro generates a timeout delay that that instructs a kernel API
1017 * to wait up to @a h hours to perform the requested operation.
1018 *
1019 * @param h Duration in hours.
1020 *
1021 * @return Timeout delay value.
1022 */
Johan Hedberg14471692016-11-13 10:52:15 +02001023#define K_HOURS(h) K_MINUTES((h) * 60)
1024
Allan Stephensc98da842016-11-11 15:45:03 -05001025/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001026 * @brief Generate infinite timeout delay.
1027 *
1028 * This macro generates a timeout delay that that instructs a kernel API
1029 * to wait as long as necessary to perform the requested operation.
1030 *
1031 * @return Timeout delay value.
1032 */
1033#define K_FOREVER (-1)
1034
1035/**
1036 * @} end addtogroup clock_apis
1037 */
1038
1039/**
Allan Stephensc98da842016-11-11 15:45:03 -05001040 * @cond INTERNAL_HIDDEN
1041 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001042
Benjamin Walsh62092182016-12-20 14:39:08 -05001043/* kernel clocks */
1044
1045#if (sys_clock_ticks_per_sec == 1000) || \
1046 (sys_clock_ticks_per_sec == 500) || \
1047 (sys_clock_ticks_per_sec == 250) || \
1048 (sys_clock_ticks_per_sec == 125) || \
1049 (sys_clock_ticks_per_sec == 100) || \
1050 (sys_clock_ticks_per_sec == 50) || \
1051 (sys_clock_ticks_per_sec == 25) || \
1052 (sys_clock_ticks_per_sec == 20) || \
1053 (sys_clock_ticks_per_sec == 10) || \
1054 (sys_clock_ticks_per_sec == 1)
1055
1056 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1057#else
1058 /* yields horrible 64-bit math on many architectures: try to avoid */
1059 #define _NON_OPTIMIZED_TICKS_PER_SEC
1060#endif
1061
1062#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001063extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001064#else
Kumar Galacc334c72017-04-21 10:55:34 -05001065static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001066{
Kumar Galacc334c72017-04-21 10:55:34 -05001067 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001068}
1069#endif
1070
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001071/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001072#ifdef CONFIG_TICKLESS_KERNEL
1073#define _TICK_ALIGN 0
1074#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001075#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001076#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001077
Kumar Galacc334c72017-04-21 10:55:34 -05001078static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001079{
Benjamin Walsh62092182016-12-20 14:39:08 -05001080#ifdef CONFIG_SYS_CLOCK_EXISTS
1081
1082#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001083 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001084#else
Kumar Galacc334c72017-04-21 10:55:34 -05001085 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001086#endif
1087
1088#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001089 __ASSERT(ticks == 0, "");
1090 return 0;
1091#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001092}
1093
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001094struct k_timer {
1095 /*
1096 * _timeout structure must be first here if we want to use
1097 * dynamic timer allocation. timeout.node is used in the double-linked
1098 * list of free timers
1099 */
1100 struct _timeout timeout;
1101
Allan Stephens45bfa372016-10-12 12:39:42 -05001102 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001103 _wait_q_t wait_q;
1104
1105 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001106 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001107
1108 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001109 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001110
1111 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001112 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001113
Allan Stephens45bfa372016-10-12 12:39:42 -05001114 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001115 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001116
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001117 /* user-specific data, also used to support legacy features */
1118 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001119
Anas Nashif2f203c22016-12-18 06:57:45 -05001120 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001121};
1122
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001123#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001124 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001125 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001126 .timeout.wait_q = NULL, \
1127 .timeout.thread = NULL, \
1128 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001129 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001130 .expiry_fn = expiry, \
1131 .stop_fn = stop, \
1132 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001133 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001134 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001135 }
1136
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001137#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1138
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001139/**
Allan Stephensc98da842016-11-11 15:45:03 -05001140 * INTERNAL_HIDDEN @endcond
1141 */
1142
1143/**
1144 * @defgroup timer_apis Timer APIs
1145 * @ingroup kernel_apis
1146 * @{
1147 */
1148
1149/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001150 * @typedef k_timer_expiry_t
1151 * @brief Timer expiry function type.
1152 *
1153 * A timer's expiry function is executed by the system clock interrupt handler
1154 * each time the timer expires. The expiry function is optional, and is only
1155 * invoked if the timer has been initialized with one.
1156 *
1157 * @param timer Address of timer.
1158 *
1159 * @return N/A
1160 */
1161typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1162
1163/**
1164 * @typedef k_timer_stop_t
1165 * @brief Timer stop function type.
1166 *
1167 * A timer's stop function is executed if the timer is stopped prematurely.
1168 * The function runs in the context of the thread that stops the timer.
1169 * The stop function is optional, and is only invoked if the timer has been
1170 * initialized with one.
1171 *
1172 * @param timer Address of timer.
1173 *
1174 * @return N/A
1175 */
1176typedef void (*k_timer_stop_t)(struct k_timer *timer);
1177
1178/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001179 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001181 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001182 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001183 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001184 *
1185 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001186 * @param expiry_fn Function to invoke each time the timer expires.
1187 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001188 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001189#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001190 struct k_timer name \
1191 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001192 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001193
Allan Stephens45bfa372016-10-12 12:39:42 -05001194/**
1195 * @brief Initialize a timer.
1196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001197 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001198 *
1199 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001200 * @param expiry_fn Function to invoke each time the timer expires.
1201 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001202 *
1203 * @return N/A
1204 */
1205extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001206 k_timer_expiry_t expiry_fn,
1207 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001208
Allan Stephens45bfa372016-10-12 12:39:42 -05001209/**
1210 * @brief Start a timer.
1211 *
1212 * This routine starts a timer, and resets its status to zero. The timer
1213 * begins counting down using the specified duration and period values.
1214 *
1215 * Attempting to start a timer that is already running is permitted.
1216 * The timer's status is reset to zero and the timer begins counting down
1217 * using the new duration and period values.
1218 *
1219 * @param timer Address of timer.
1220 * @param duration Initial timer duration (in milliseconds).
1221 * @param period Timer period (in milliseconds).
1222 *
1223 * @return N/A
1224 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001225extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001226 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001227
1228/**
1229 * @brief Stop a timer.
1230 *
1231 * This routine stops a running timer prematurely. The timer's stop function,
1232 * if one exists, is invoked by the caller.
1233 *
1234 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001235 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001236 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001237 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1238 * if @a k_timer_stop is to be called from ISRs.
1239 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001240 * @param timer Address of timer.
1241 *
1242 * @return N/A
1243 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001244extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001245
1246/**
1247 * @brief Read timer status.
1248 *
1249 * This routine reads the timer's status, which indicates the number of times
1250 * it has expired since its status was last read.
1251 *
1252 * Calling this routine resets the timer's status to zero.
1253 *
1254 * @param timer Address of timer.
1255 *
1256 * @return Timer status.
1257 */
Kumar Galacc334c72017-04-21 10:55:34 -05001258extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001259
1260/**
1261 * @brief Synchronize thread to timer expiration.
1262 *
1263 * This routine blocks the calling thread until the timer's status is non-zero
1264 * (indicating that it has expired at least once since it was last examined)
1265 * or the timer is stopped. If the timer status is already non-zero,
1266 * or the timer is already stopped, the caller continues without waiting.
1267 *
1268 * Calling this routine resets the timer's status to zero.
1269 *
1270 * This routine must not be used by interrupt handlers, since they are not
1271 * allowed to block.
1272 *
1273 * @param timer Address of timer.
1274 *
1275 * @return Timer status.
1276 */
Kumar Galacc334c72017-04-21 10:55:34 -05001277extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001278
1279/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001280 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001281 *
1282 * This routine computes the (approximate) time remaining before a running
1283 * timer next expires. If the timer is not running, it returns zero.
1284 *
1285 * @param timer Address of timer.
1286 *
1287 * @return Remaining time (in milliseconds).
1288 */
Kumar Galacc334c72017-04-21 10:55:34 -05001289static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001290{
1291 return _timeout_remaining_get(&timer->timeout);
1292}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001293
Allan Stephensc98da842016-11-11 15:45:03 -05001294/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001295 * @brief Associate user-specific data with a timer.
1296 *
1297 * This routine records the @a user_data with the @a timer, to be retrieved
1298 * later.
1299 *
1300 * It can be used e.g. in a timer handler shared across multiple subsystems to
1301 * retrieve data specific to the subsystem this timer is associated with.
1302 *
1303 * @param timer Address of timer.
1304 * @param user_data User data to associate with the timer.
1305 *
1306 * @return N/A
1307 */
1308static inline void k_timer_user_data_set(struct k_timer *timer,
1309 void *user_data)
1310{
1311 timer->user_data = user_data;
1312}
1313
1314/**
1315 * @brief Retrieve the user-specific data from a timer.
1316 *
1317 * @param timer Address of timer.
1318 *
1319 * @return The user data.
1320 */
1321static inline void *k_timer_user_data_get(struct k_timer *timer)
1322{
1323 return timer->user_data;
1324}
1325
1326/**
Allan Stephensc98da842016-11-11 15:45:03 -05001327 * @} end defgroup timer_apis
1328 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001329
Allan Stephensc98da842016-11-11 15:45:03 -05001330/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001331 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001332 * @{
1333 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001334
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001335/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001336 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001338 * This routine returns the elapsed time since the system booted,
1339 * in milliseconds.
1340 *
1341 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001342 */
Kumar Galacc334c72017-04-21 10:55:34 -05001343extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001344
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001345#ifdef CONFIG_TICKLESS_KERNEL
1346/**
1347 * @brief Enable clock always on in tickless kernel
1348 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001349 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001350 * there are no timer events programmed in tickless kernel
1351 * scheduling. This is necessary if the clock is used to track
1352 * passage of time.
1353 *
1354 * @retval prev_status Previous status of always on flag
1355 */
1356static inline int k_enable_sys_clock_always_on(void)
1357{
1358 int prev_status = _sys_clock_always_on;
1359
1360 _sys_clock_always_on = 1;
1361 _enable_sys_clock();
1362
1363 return prev_status;
1364}
1365
1366/**
1367 * @brief Disable clock always on in tickless kernel
1368 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001369 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001370 * there are no timer events programmed in tickless kernel
1371 * scheduling. To save power, this routine should be called
1372 * immediately when clock is not used to track time.
1373 */
1374static inline void k_disable_sys_clock_always_on(void)
1375{
1376 _sys_clock_always_on = 0;
1377}
1378#else
1379#define k_enable_sys_clock_always_on() do { } while ((0))
1380#define k_disable_sys_clock_always_on() do { } while ((0))
1381#endif
1382
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001383/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001384 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001386 * This routine returns the lower 32-bits of the elapsed time since the system
1387 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001389 * This routine can be more efficient than k_uptime_get(), as it reduces the
1390 * need for interrupt locking and 64-bit math. However, the 32-bit result
1391 * cannot hold a system uptime time larger than approximately 50 days, so the
1392 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001394 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001395 */
Kumar Galacc334c72017-04-21 10:55:34 -05001396extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001397
1398/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001399 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001401 * This routine computes the elapsed time between the current system uptime
1402 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001404 * @param reftime Pointer to a reference time, which is updated to the current
1405 * uptime upon return.
1406 *
1407 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001408 */
Kumar Galacc334c72017-04-21 10:55:34 -05001409extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001410
1411/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001412 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001414 * This routine computes the elapsed time between the current system uptime
1415 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001417 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1418 * need for interrupt locking and 64-bit math. However, the 32-bit result
1419 * cannot hold an elapsed time larger than approximately 50 days, so the
1420 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001422 * @param reftime Pointer to a reference time, which is updated to the current
1423 * uptime upon return.
1424 *
1425 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001426 */
Kumar Galacc334c72017-04-21 10:55:34 -05001427extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001428
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001429/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001430 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001432 * This routine returns the current time, as measured by the system's hardware
1433 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001435 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001436 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001437#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001438
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001439/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001440 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001441 */
1442
Allan Stephensc98da842016-11-11 15:45:03 -05001443/**
1444 * @cond INTERNAL_HIDDEN
1445 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001446
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001447struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001448 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001449 union {
1450 _wait_q_t wait_q;
1451
1452 _POLL_EVENT;
1453 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001454
1455 _OBJECT_TRACING_NEXT_PTR(k_queue);
1456};
1457
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001458#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001459 { \
1460 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1461 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001462 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001463 _OBJECT_TRACING_INIT \
1464 }
1465
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001466#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1467
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001468/**
1469 * INTERNAL_HIDDEN @endcond
1470 */
1471
1472/**
1473 * @defgroup queue_apis Queue APIs
1474 * @ingroup kernel_apis
1475 * @{
1476 */
1477
1478/**
1479 * @brief Initialize a queue.
1480 *
1481 * This routine initializes a queue object, prior to its first use.
1482 *
1483 * @param queue Address of the queue.
1484 *
1485 * @return N/A
1486 */
1487extern void k_queue_init(struct k_queue *queue);
1488
1489/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001490 * @brief Cancel waiting on a queue.
1491 *
1492 * This routine causes first thread pending on @a queue, if any, to
1493 * return from k_queue_get() call with NULL value (as if timeout expired).
1494 *
1495 * @note Can be called by ISRs.
1496 *
1497 * @param queue Address of the queue.
1498 *
1499 * @return N/A
1500 */
1501extern void k_queue_cancel_wait(struct k_queue *queue);
1502
1503/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001504 * @brief Append an element to the end of a queue.
1505 *
1506 * This routine appends a data item to @a queue. A queue data item must be
1507 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1508 * reserved for the kernel's use.
1509 *
1510 * @note Can be called by ISRs.
1511 *
1512 * @param queue Address of the queue.
1513 * @param data Address of the data item.
1514 *
1515 * @return N/A
1516 */
1517extern void k_queue_append(struct k_queue *queue, void *data);
1518
1519/**
1520 * @brief Prepend an element to a queue.
1521 *
1522 * This routine prepends a data item to @a queue. A queue data item must be
1523 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1524 * reserved for the kernel's use.
1525 *
1526 * @note Can be called by ISRs.
1527 *
1528 * @param queue Address of the queue.
1529 * @param data Address of the data item.
1530 *
1531 * @return N/A
1532 */
1533extern void k_queue_prepend(struct k_queue *queue, void *data);
1534
1535/**
1536 * @brief Inserts an element to a queue.
1537 *
1538 * This routine inserts a data item to @a queue after previous item. A queue
1539 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1540 * item are reserved for the kernel's use.
1541 *
1542 * @note Can be called by ISRs.
1543 *
1544 * @param queue Address of the queue.
1545 * @param prev Address of the previous data item.
1546 * @param data Address of the data item.
1547 *
1548 * @return N/A
1549 */
1550extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1551
1552/**
1553 * @brief Atomically append a list of elements to a queue.
1554 *
1555 * This routine adds a list of data items to @a queue in one operation.
1556 * The data items must be in a singly-linked list, with the first 32 bits
1557 * in each data item pointing to the next data item; the list must be
1558 * NULL-terminated.
1559 *
1560 * @note Can be called by ISRs.
1561 *
1562 * @param queue Address of the queue.
1563 * @param head Pointer to first node in singly-linked list.
1564 * @param tail Pointer to last node in singly-linked list.
1565 *
1566 * @return N/A
1567 */
1568extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1569
1570/**
1571 * @brief Atomically add a list of elements to a queue.
1572 *
1573 * This routine adds a list of data items to @a queue in one operation.
1574 * The data items must be in a singly-linked list implemented using a
1575 * sys_slist_t object. Upon completion, the original list is empty.
1576 *
1577 * @note Can be called by ISRs.
1578 *
1579 * @param queue Address of the queue.
1580 * @param list Pointer to sys_slist_t object.
1581 *
1582 * @return N/A
1583 */
1584extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1585
1586/**
1587 * @brief Get an element from a queue.
1588 *
1589 * This routine removes first data item from @a queue. The first 32 bits of the
1590 * data item are reserved for the kernel's use.
1591 *
1592 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1593 *
1594 * @param queue Address of the queue.
1595 * @param timeout Waiting period to obtain a data item (in milliseconds),
1596 * or one of the special values K_NO_WAIT and K_FOREVER.
1597 *
1598 * @return Address of the data item if successful; NULL if returned
1599 * without waiting, or waiting period timed out.
1600 */
Kumar Galacc334c72017-04-21 10:55:34 -05001601extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001602
1603/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001604 * @brief Remove an element from a queue.
1605 *
1606 * This routine removes data item from @a queue. The first 32 bits of the
1607 * data item are reserved for the kernel's use. Removing elements from k_queue
1608 * rely on sys_slist_find_and_remove which is not a constant time operation.
1609 *
1610 * @note Can be called by ISRs
1611 *
1612 * @param queue Address of the queue.
1613 * @param data Address of the data item.
1614 *
1615 * @return true if data item was removed
1616 */
1617static inline bool k_queue_remove(struct k_queue *queue, void *data)
1618{
1619 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1620}
1621
1622/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001623 * @brief Query a queue to see if it has data available.
1624 *
1625 * Note that the data might be already gone by the time this function returns
1626 * if other threads are also trying to read from the queue.
1627 *
1628 * @note Can be called by ISRs.
1629 *
1630 * @param queue Address of the queue.
1631 *
1632 * @return Non-zero if the queue is empty.
1633 * @return 0 if data is available.
1634 */
1635static inline int k_queue_is_empty(struct k_queue *queue)
1636{
1637 return (int)sys_slist_is_empty(&queue->data_q);
1638}
1639
1640/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001641 * @brief Peek element at the head of queue.
1642 *
1643 * Return element from the head of queue without removing it.
1644 *
1645 * @param queue Address of the queue.
1646 *
1647 * @return Head element, or NULL if queue is empty.
1648 */
1649static inline void *k_queue_peek_head(struct k_queue *queue)
1650{
1651 return sys_slist_peek_head(&queue->data_q);
1652}
1653
1654/**
1655 * @brief Peek element at the tail of queue.
1656 *
1657 * Return element from the tail of queue without removing it.
1658 *
1659 * @param queue Address of the queue.
1660 *
1661 * @return Tail element, or NULL if queue is empty.
1662 */
1663static inline void *k_queue_peek_tail(struct k_queue *queue)
1664{
1665 return sys_slist_peek_tail(&queue->data_q);
1666}
1667
1668/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001669 * @brief Statically define and initialize a queue.
1670 *
1671 * The queue can be accessed outside the module where it is defined using:
1672 *
1673 * @code extern struct k_queue <name>; @endcode
1674 *
1675 * @param name Name of the queue.
1676 */
1677#define K_QUEUE_DEFINE(name) \
1678 struct k_queue name \
1679 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001680 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001681
1682/**
1683 * @} end defgroup queue_apis
1684 */
1685
1686/**
1687 * @cond INTERNAL_HIDDEN
1688 */
1689
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001690struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001691 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001692};
1693
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001694#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001695 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001696 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001697 }
1698
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001699#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1700
Allan Stephensc98da842016-11-11 15:45:03 -05001701/**
1702 * INTERNAL_HIDDEN @endcond
1703 */
1704
1705/**
1706 * @defgroup fifo_apis Fifo APIs
1707 * @ingroup kernel_apis
1708 * @{
1709 */
1710
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001711/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001712 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001714 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001716 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001717 *
1718 * @return N/A
1719 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001720#define k_fifo_init(fifo) \
1721 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001722
1723/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001724 * @brief Cancel waiting on a fifo.
1725 *
1726 * This routine causes first thread pending on @a fifo, if any, to
1727 * return from k_fifo_get() call with NULL value (as if timeout
1728 * expired).
1729 *
1730 * @note Can be called by ISRs.
1731 *
1732 * @param fifo Address of the fifo.
1733 *
1734 * @return N/A
1735 */
1736#define k_fifo_cancel_wait(fifo) \
1737 k_queue_cancel_wait((struct k_queue *) fifo)
1738
1739/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001740 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001742 * This routine adds a data item to @a fifo. A fifo data item must be
1743 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1744 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001745 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001746 * @note Can be called by ISRs.
1747 *
1748 * @param fifo Address of the fifo.
1749 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001750 *
1751 * @return N/A
1752 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001753#define k_fifo_put(fifo, data) \
1754 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001755
1756/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001757 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001759 * This routine adds a list of data items to @a fifo in one operation.
1760 * The data items must be in a singly-linked list, with the first 32 bits
1761 * each data item pointing to the next data item; the list must be
1762 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001764 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001766 * @param fifo Address of the fifo.
1767 * @param head Pointer to first node in singly-linked list.
1768 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001769 *
1770 * @return N/A
1771 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001772#define k_fifo_put_list(fifo, head, tail) \
1773 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001774
1775/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001776 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001778 * This routine adds a list of data items to @a fifo in one operation.
1779 * The data items must be in a singly-linked list implemented using a
1780 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001781 * and must be re-initialized via sys_slist_init().
1782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001783 * @note Can be called by ISRs.
1784 *
1785 * @param fifo Address of the fifo.
1786 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001787 *
1788 * @return N/A
1789 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001790#define k_fifo_put_slist(fifo, list) \
1791 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001792
1793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001794 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001796 * This routine removes a data item from @a fifo in a "first in, first out"
1797 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1800 *
1801 * @param fifo Address of the fifo.
1802 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001803 * or one of the special values K_NO_WAIT and K_FOREVER.
1804 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001805 * @return Address of the data item if successful; NULL if returned
1806 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001807 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001808#define k_fifo_get(fifo, timeout) \
1809 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001810
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001811/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001812 * @brief Query a fifo to see if it has data available.
1813 *
1814 * Note that the data might be already gone by the time this function returns
1815 * if other threads is also trying to read from the fifo.
1816 *
1817 * @note Can be called by ISRs.
1818 *
1819 * @param fifo Address of the fifo.
1820 *
1821 * @return Non-zero if the fifo is empty.
1822 * @return 0 if data is available.
1823 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001824#define k_fifo_is_empty(fifo) \
1825 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001826
1827/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001828 * @brief Peek element at the head of fifo.
1829 *
1830 * Return element from the head of fifo without removing it. A usecase
1831 * for this is if elements of the fifo are themselves containers. Then
1832 * on each iteration of processing, a head container will be peeked,
1833 * and some data processed out of it, and only if the container is empty,
1834 * it will be completely remove from the fifo.
1835 *
1836 * @param fifo Address of the fifo.
1837 *
1838 * @return Head element, or NULL if the fifo is empty.
1839 */
1840#define k_fifo_peek_head(fifo) \
1841 k_queue_peek_head((struct k_queue *) fifo)
1842
1843/**
1844 * @brief Peek element at the tail of fifo.
1845 *
1846 * Return element from the tail of fifo (without removing it). A usecase
1847 * for this is if elements of the fifo are themselves containers. Then
1848 * it may be useful to add more data to the last container in fifo.
1849 *
1850 * @param fifo Address of the fifo.
1851 *
1852 * @return Tail element, or NULL if fifo is empty.
1853 */
1854#define k_fifo_peek_tail(fifo) \
1855 k_queue_peek_tail((struct k_queue *) fifo)
1856
1857/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001858 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001860 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001861 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001862 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001864 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001865 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001866#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001867 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001868 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001869 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001870
Allan Stephensc98da842016-11-11 15:45:03 -05001871/**
1872 * @} end defgroup fifo_apis
1873 */
1874
1875/**
1876 * @cond INTERNAL_HIDDEN
1877 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001878
1879struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001880 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001881};
1882
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001883#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001884 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001885 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001886 }
1887
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001888#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1889
Allan Stephensc98da842016-11-11 15:45:03 -05001890/**
1891 * INTERNAL_HIDDEN @endcond
1892 */
1893
1894/**
1895 * @defgroup lifo_apis Lifo APIs
1896 * @ingroup kernel_apis
1897 * @{
1898 */
1899
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001900/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001901 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001903 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001905 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001906 *
1907 * @return N/A
1908 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001909#define k_lifo_init(lifo) \
1910 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001911
1912/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001913 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001915 * This routine adds a data item to @a lifo. A lifo data item must be
1916 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1917 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001919 * @note Can be called by ISRs.
1920 *
1921 * @param lifo Address of the lifo.
1922 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001923 *
1924 * @return N/A
1925 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001926#define k_lifo_put(lifo, data) \
1927 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001928
1929/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001930 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001932 * This routine removes a data item from @a lifo in a "last in, first out"
1933 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001935 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1936 *
1937 * @param lifo Address of the lifo.
1938 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001939 * or one of the special values K_NO_WAIT and K_FOREVER.
1940 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001941 * @return Address of the data item if successful; NULL if returned
1942 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001943 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001944#define k_lifo_get(lifo, timeout) \
1945 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001946
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001947/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001948 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001950 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001951 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001952 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001954 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001955 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001956#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001957 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001958 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001959 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001960
Allan Stephensc98da842016-11-11 15:45:03 -05001961/**
1962 * @} end defgroup lifo_apis
1963 */
1964
1965/**
1966 * @cond INTERNAL_HIDDEN
1967 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001968
1969struct k_stack {
1970 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001971 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001972
Anas Nashif2f203c22016-12-18 06:57:45 -05001973 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001974};
1975
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001976#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001977 { \
1978 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1979 .base = stack_buffer, \
1980 .next = stack_buffer, \
1981 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001982 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001983 }
1984
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001985#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1986
Allan Stephensc98da842016-11-11 15:45:03 -05001987/**
1988 * INTERNAL_HIDDEN @endcond
1989 */
1990
1991/**
1992 * @defgroup stack_apis Stack APIs
1993 * @ingroup kernel_apis
1994 * @{
1995 */
1996
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001997/**
1998 * @brief Initialize a stack.
1999 *
2000 * This routine initializes a stack object, prior to its first use.
2001 *
2002 * @param stack Address of the stack.
2003 * @param buffer Address of array used to hold stacked values.
2004 * @param num_entries Maximum number of values that can be stacked.
2005 *
2006 * @return N/A
2007 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05002008extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05002009 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002010
2011/**
2012 * @brief Push an element onto a stack.
2013 *
2014 * This routine adds a 32-bit value @a data to @a stack.
2015 *
2016 * @note Can be called by ISRs.
2017 *
2018 * @param stack Address of the stack.
2019 * @param data Value to push onto the stack.
2020 *
2021 * @return N/A
2022 */
Kumar Galacc334c72017-04-21 10:55:34 -05002023extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002024
2025/**
2026 * @brief Pop an element from a stack.
2027 *
2028 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2029 * manner and stores the value in @a data.
2030 *
2031 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2032 *
2033 * @param stack Address of the stack.
2034 * @param data Address of area to hold the value popped from the stack.
2035 * @param timeout Waiting period to obtain a value (in milliseconds),
2036 * or one of the special values K_NO_WAIT and K_FOREVER.
2037 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002038 * @retval 0 Element popped from stack.
2039 * @retval -EBUSY Returned without waiting.
2040 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041 */
Kumar Galacc334c72017-04-21 10:55:34 -05002042extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002043
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002044/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002045 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002047 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002048 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002049 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002051 * @param name Name of the stack.
2052 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002053 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002054#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002055 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002056 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002057 struct k_stack name \
2058 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002059 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002060 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002061
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002062/**
Allan Stephensc98da842016-11-11 15:45:03 -05002063 * @} end defgroup stack_apis
2064 */
2065
Allan Stephens6bba9b02016-11-16 14:56:54 -05002066struct k_work;
2067
Allan Stephensc98da842016-11-11 15:45:03 -05002068/**
2069 * @defgroup workqueue_apis Workqueue Thread APIs
2070 * @ingroup kernel_apis
2071 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002072 */
2073
Allan Stephens6bba9b02016-11-16 14:56:54 -05002074/**
2075 * @typedef k_work_handler_t
2076 * @brief Work item handler function type.
2077 *
2078 * A work item's handler function is executed by a workqueue's thread
2079 * when the work item is processed by the workqueue.
2080 *
2081 * @param work Address of the work item.
2082 *
2083 * @return N/A
2084 */
2085typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002086
2087/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002088 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002089 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002090
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002091struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002092 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002093 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002094};
2095
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002096enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002097 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002098};
2099
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002100struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002101 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002102 k_work_handler_t handler;
2103 atomic_t flags[1];
2104};
2105
Allan Stephens6bba9b02016-11-16 14:56:54 -05002106struct k_delayed_work {
2107 struct k_work work;
2108 struct _timeout timeout;
2109 struct k_work_q *work_q;
2110};
2111
2112extern struct k_work_q k_sys_work_q;
2113
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002114/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002115 * INTERNAL_HIDDEN @endcond
2116 */
2117
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002118#define _K_WORK_INITIALIZER(work_handler) \
2119 { \
2120 ._reserved = NULL, \
2121 .handler = work_handler, \
2122 .flags = { 0 } \
2123 }
2124
2125#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2126
Allan Stephens6bba9b02016-11-16 14:56:54 -05002127/**
2128 * @brief Initialize a statically-defined work item.
2129 *
2130 * This macro can be used to initialize a statically-defined workqueue work
2131 * item, prior to its first use. For example,
2132 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002133 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002134 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002135 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002136 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002137 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002138#define K_WORK_DEFINE(work, work_handler) \
2139 struct k_work work \
2140 __in_section(_k_work, static, work) = \
2141 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002142
2143/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002144 * @brief Initialize a work item.
2145 *
2146 * This routine initializes a workqueue work item, prior to its first use.
2147 *
2148 * @param work Address of work item.
2149 * @param handler Function to invoke each time work item is processed.
2150 *
2151 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002152 */
2153static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2154{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002155 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002156 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002157 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158}
2159
2160/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002161 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002162 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002163 * This routine submits work item @a work to be processed by workqueue
2164 * @a work_q. If the work item is already pending in the workqueue's queue
2165 * as a result of an earlier submission, this routine has no effect on the
2166 * work item. If the work item has already been processed, or is currently
2167 * being processed, its work is considered complete and the work item can be
2168 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002169 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002170 * @warning
2171 * A submitted work item must not be modified until it has been processed
2172 * by the workqueue.
2173 *
2174 * @note Can be called by ISRs.
2175 *
2176 * @param work_q Address of workqueue.
2177 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002178 *
2179 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002180 */
2181static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2182 struct k_work *work)
2183{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002184 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002185 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002186 }
2187}
2188
2189/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002190 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002191 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002192 * This routine indicates if work item @a work is pending in a workqueue's
2193 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002194 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002195 * @note Can be called by ISRs.
2196 *
2197 * @param work Address of work item.
2198 *
2199 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002200 */
2201static inline int k_work_pending(struct k_work *work)
2202{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002203 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002204}
2205
2206/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002207 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002208 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002209 * This routine starts workqueue @a work_q. The workqueue spawns its work
2210 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002211 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002212 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002213 * @param stack Pointer to work queue thread's stack space, as defined by
2214 * K_THREAD_STACK_DEFINE()
2215 * @param stack_size Size of the work queue thread's stack (in bytes), which
2216 * should either be the same constant passed to
2217 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002218 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002219 *
2220 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221 */
Andrew Boie507852a2017-07-25 18:47:07 -07002222extern void k_work_q_start(struct k_work_q *work_q,
2223 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002224 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002225
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002226/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002227 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002228 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002229 * This routine initializes a workqueue delayed work item, prior to
2230 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002231 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002232 * @param work Address of delayed work item.
2233 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002234 *
2235 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002236 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002237extern void k_delayed_work_init(struct k_delayed_work *work,
2238 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239
2240/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002241 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002242 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002243 * This routine schedules work item @a work to be processed by workqueue
2244 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002245 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002246 * Only when the countdown completes is the work item actually submitted to
2247 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002248 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002249 * Submitting a previously submitted delayed work item that is still
2250 * counting down cancels the existing submission and restarts the countdown
2251 * using the new delay. If the work item is currently pending on the
2252 * workqueue's queue because the countdown has completed it is too late to
2253 * resubmit the item, and resubmission fails without impacting the work item.
2254 * If the work item has already been processed, or is currently being processed,
2255 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002256 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002257 * @warning
2258 * A delayed work item must not be modified until it has been processed
2259 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002260 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002261 * @note Can be called by ISRs.
2262 *
2263 * @param work_q Address of workqueue.
2264 * @param work Address of delayed work item.
2265 * @param delay Delay before submitting the work item (in milliseconds).
2266 *
2267 * @retval 0 Work item countdown started.
2268 * @retval -EINPROGRESS Work item is already pending.
2269 * @retval -EINVAL Work item is being processed or has completed its work.
2270 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002271 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002272extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2273 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002274 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002275
2276/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002277 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002278 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002279 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002280 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002281 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002282 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002283 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002285 * @param work Address of delayed work item.
2286 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002287 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002288 * @retval -EINPROGRESS Work item is already pending.
2289 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002290 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002291extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002292
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002293/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002294 * @brief Submit a work item to the system workqueue.
2295 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002296 * This routine submits work item @a work to be processed by the system
2297 * workqueue. If the work item is already pending in the workqueue's queue
2298 * as a result of an earlier submission, this routine has no effect on the
2299 * work item. If the work item has already been processed, or is currently
2300 * being processed, its work is considered complete and the work item can be
2301 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002302 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002303 * @warning
2304 * Work items submitted to the system workqueue should avoid using handlers
2305 * that block or yield since this may prevent the system workqueue from
2306 * processing other work items in a timely manner.
2307 *
2308 * @note Can be called by ISRs.
2309 *
2310 * @param work Address of work item.
2311 *
2312 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002313 */
2314static inline void k_work_submit(struct k_work *work)
2315{
2316 k_work_submit_to_queue(&k_sys_work_q, work);
2317}
2318
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002319/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002320 * @brief Submit a delayed work item to the system workqueue.
2321 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002322 * This routine schedules work item @a work to be processed by the system
2323 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002324 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002325 * Only when the countdown completes is the work item actually submitted to
2326 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002327 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002328 * Submitting a previously submitted delayed work item that is still
2329 * counting down cancels the existing submission and restarts the countdown
2330 * using the new delay. If the work item is currently pending on the
2331 * workqueue's queue because the countdown has completed it is too late to
2332 * resubmit the item, and resubmission fails without impacting the work item.
2333 * If the work item has already been processed, or is currently being processed,
2334 * its work is considered complete and the work item can be resubmitted.
2335 *
2336 * @warning
2337 * Work items submitted to the system workqueue should avoid using handlers
2338 * that block or yield since this may prevent the system workqueue from
2339 * processing other work items in a timely manner.
2340 *
2341 * @note Can be called by ISRs.
2342 *
2343 * @param work Address of delayed work item.
2344 * @param delay Delay before submitting the work item (in milliseconds).
2345 *
2346 * @retval 0 Work item countdown started.
2347 * @retval -EINPROGRESS Work item is already pending.
2348 * @retval -EINVAL Work item is being processed or has completed its work.
2349 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002350 */
2351static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002352 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002353{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002354 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002355}
2356
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002358 * @brief Get time remaining before a delayed work gets scheduled.
2359 *
2360 * This routine computes the (approximate) time remaining before a
2361 * delayed work gets executed. If the delayed work is not waiting to be
2362 * schedules, it returns zero.
2363 *
2364 * @param work Delayed work item.
2365 *
2366 * @return Remaining time (in milliseconds).
2367 */
Kumar Galacc334c72017-04-21 10:55:34 -05002368static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002369{
2370 return _timeout_remaining_get(&work->timeout);
2371}
2372
2373/**
Allan Stephensc98da842016-11-11 15:45:03 -05002374 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002375 */
2376
Allan Stephensc98da842016-11-11 15:45:03 -05002377/**
2378 * @cond INTERNAL_HIDDEN
2379 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002380
2381struct k_mutex {
2382 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002383 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002384 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002385 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002386
Anas Nashif2f203c22016-12-18 06:57:45 -05002387 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002388};
2389
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002390#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002391 { \
2392 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2393 .owner = NULL, \
2394 .lock_count = 0, \
2395 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002396 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002397 }
2398
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002399#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2400
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002401/**
Allan Stephensc98da842016-11-11 15:45:03 -05002402 * INTERNAL_HIDDEN @endcond
2403 */
2404
2405/**
2406 * @defgroup mutex_apis Mutex APIs
2407 * @ingroup kernel_apis
2408 * @{
2409 */
2410
2411/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002412 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002414 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002415 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002416 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002418 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002419 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002420#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002421 struct k_mutex name \
2422 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002423 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002424
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002425/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002426 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002428 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002430 * Upon completion, the mutex is available and does not have an owner.
2431 *
2432 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002433 *
2434 * @return N/A
2435 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002436extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002437
2438/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002439 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002440 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002441 * This routine locks @a mutex. If the mutex is locked by another thread,
2442 * the calling thread waits until the mutex becomes available or until
2443 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002444 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002445 * A thread is permitted to lock a mutex it has already locked. The operation
2446 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002448 * @param mutex Address of the mutex.
2449 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002450 * or one of the special values K_NO_WAIT and K_FOREVER.
2451 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002452 * @retval 0 Mutex locked.
2453 * @retval -EBUSY Returned without waiting.
2454 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002455 */
Kumar Galacc334c72017-04-21 10:55:34 -05002456extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002457
2458/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002459 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002461 * This routine unlocks @a mutex. The mutex must already be locked by the
2462 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002463 *
2464 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002465 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002466 * thread.
2467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002468 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002469 *
2470 * @return N/A
2471 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002472extern void k_mutex_unlock(struct k_mutex *mutex);
2473
Allan Stephensc98da842016-11-11 15:45:03 -05002474/**
2475 * @} end defgroup mutex_apis
2476 */
2477
2478/**
2479 * @cond INTERNAL_HIDDEN
2480 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481
2482struct k_sem {
2483 _wait_q_t wait_q;
2484 unsigned int count;
2485 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002486 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002487
Anas Nashif2f203c22016-12-18 06:57:45 -05002488 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002489};
2490
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002491#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002492 { \
2493 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2494 .count = initial_count, \
2495 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002496 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002497 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002498 }
2499
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002500#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2501
Allan Stephensc98da842016-11-11 15:45:03 -05002502/**
2503 * INTERNAL_HIDDEN @endcond
2504 */
2505
2506/**
2507 * @defgroup semaphore_apis Semaphore APIs
2508 * @ingroup kernel_apis
2509 * @{
2510 */
2511
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002512/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002513 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002515 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002517 * @param sem Address of the semaphore.
2518 * @param initial_count Initial semaphore count.
2519 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002520 *
2521 * @return N/A
2522 */
Andrew Boie99280232017-09-29 14:17:47 -07002523__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2524 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002525
2526/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002527 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002531 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2532 *
2533 * @param sem Address of the semaphore.
2534 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002535 * or one of the special values K_NO_WAIT and K_FOREVER.
2536 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002537 * @note When porting code from the nanokernel legacy API to the new API, be
2538 * careful with the return value of this function. The return value is the
2539 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2540 * non-zero means failure, while the nano_sem_take family returns 1 for success
2541 * and 0 for failure.
2542 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002543 * @retval 0 Semaphore taken.
2544 * @retval -EBUSY Returned without waiting.
2545 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002546 */
Andrew Boie99280232017-09-29 14:17:47 -07002547__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002548
2549/**
2550 * @brief Give a semaphore.
2551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002552 * This routine gives @a sem, unless the semaphore is already at its maximum
2553 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002555 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002557 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002558 *
2559 * @return N/A
2560 */
Andrew Boie99280232017-09-29 14:17:47 -07002561__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002563/**
2564 * @brief Reset a semaphore's count to zero.
2565 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002566 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002568 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002569 *
2570 * @return N/A
2571 */
Andrew Boie990bf162017-10-03 12:36:49 -07002572__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002573
2574static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002575{
2576 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002577}
2578
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002579/**
2580 * @brief Get a semaphore's count.
2581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002582 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002584 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002585 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002586 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002587 */
Andrew Boie990bf162017-10-03 12:36:49 -07002588__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002589
2590static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002591{
2592 return sem->count;
2593}
2594
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002595/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002596 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002598 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002599 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002600 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002602 * @param name Name of the semaphore.
2603 * @param initial_count Initial semaphore count.
2604 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002605 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002606#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002607 struct k_sem name \
2608 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002609 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002610
Allan Stephensc98da842016-11-11 15:45:03 -05002611/**
2612 * @} end defgroup semaphore_apis
2613 */
2614
2615/**
2616 * @defgroup alert_apis Alert APIs
2617 * @ingroup kernel_apis
2618 * @{
2619 */
2620
Allan Stephens5eceb852016-11-16 10:16:30 -05002621/**
2622 * @typedef k_alert_handler_t
2623 * @brief Alert handler function type.
2624 *
2625 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002626 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002627 * and is only invoked if the alert has been initialized with one.
2628 *
2629 * @param alert Address of the alert.
2630 *
2631 * @return 0 if alert has been consumed; non-zero if alert should pend.
2632 */
2633typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002634
2635/**
2636 * @} end defgroup alert_apis
2637 */
2638
2639/**
2640 * @cond INTERNAL_HIDDEN
2641 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002642
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002643#define K_ALERT_DEFAULT NULL
2644#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002645
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002646struct k_alert {
2647 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002648 atomic_t send_count;
2649 struct k_work work_item;
2650 struct k_sem sem;
2651
Anas Nashif2f203c22016-12-18 06:57:45 -05002652 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002653};
2654
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002655extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002656
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002657#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002658 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002659 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002660 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002661 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2662 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002663 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002664 }
2665
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002666#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2667
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002668/**
Allan Stephensc98da842016-11-11 15:45:03 -05002669 * INTERNAL_HIDDEN @endcond
2670 */
2671
2672/**
2673 * @addtogroup alert_apis
2674 * @{
2675 */
2676
2677/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002678 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002679 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002680 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002681 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002682 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002684 * @param name Name of the alert.
2685 * @param alert_handler Action to take when alert is sent. Specify either
2686 * the address of a function to be invoked by the system workqueue
2687 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2688 * K_ALERT_DEFAULT (which causes the alert to pend).
2689 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002690 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002691#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002692 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002693 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002694 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002695 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002696
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002697/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002698 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002700 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002702 * @param alert Address of the alert.
2703 * @param handler Action to take when alert is sent. Specify either the address
2704 * of a function to be invoked by the system workqueue thread,
2705 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2706 * K_ALERT_DEFAULT (which causes the alert to pend).
2707 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002708 *
2709 * @return N/A
2710 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002711extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2712 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002713
2714/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002715 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002717 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002719 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2720 *
2721 * @param alert Address of the alert.
2722 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002723 * or one of the special values K_NO_WAIT and K_FOREVER.
2724 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002725 * @retval 0 Alert received.
2726 * @retval -EBUSY Returned without waiting.
2727 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002728 */
Kumar Galacc334c72017-04-21 10:55:34 -05002729extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002730
2731/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002732 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002734 * This routine signals @a alert. The action specified for @a alert will
2735 * be taken, which may trigger the execution of an alert handler function
2736 * and/or cause the alert to pend (assuming the alert has not reached its
2737 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002739 * @note Can be called by ISRs.
2740 *
2741 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002742 *
2743 * @return N/A
2744 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002745extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002746
2747/**
Allan Stephensc98da842016-11-11 15:45:03 -05002748 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002749 */
2750
Allan Stephensc98da842016-11-11 15:45:03 -05002751/**
2752 * @cond INTERNAL_HIDDEN
2753 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002754
2755struct k_msgq {
2756 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002757 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002758 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002759 char *buffer_start;
2760 char *buffer_end;
2761 char *read_ptr;
2762 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002763 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002764
Anas Nashif2f203c22016-12-18 06:57:45 -05002765 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002766};
2767
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002768#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769 { \
2770 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002771 .max_msgs = q_max_msgs, \
2772 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002773 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002774 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002775 .read_ptr = q_buffer, \
2776 .write_ptr = q_buffer, \
2777 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002778 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002779 }
2780
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002781#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2782
Peter Mitsis1da807e2016-10-06 11:36:59 -04002783/**
Allan Stephensc98da842016-11-11 15:45:03 -05002784 * INTERNAL_HIDDEN @endcond
2785 */
2786
2787/**
2788 * @defgroup msgq_apis Message Queue APIs
2789 * @ingroup kernel_apis
2790 * @{
2791 */
2792
2793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002794 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002796 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2797 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002798 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2799 * message is similarly aligned to this boundary, @a q_msg_size must also be
2800 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002802 * The message queue can be accessed outside the module where it is defined
2803 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002804 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002805 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002807 * @param q_name Name of the message queue.
2808 * @param q_msg_size Message size (in bytes).
2809 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002810 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002811 */
2812#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2813 static char __noinit __aligned(q_align) \
2814 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002815 struct k_msgq q_name \
2816 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002817 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002818 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002819
Peter Mitsisd7a37502016-10-13 11:37:40 -04002820/**
2821 * @brief Initialize a message queue.
2822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002823 * This routine initializes a message queue object, prior to its first use.
2824 *
Allan Stephensda827222016-11-09 14:23:58 -06002825 * The message queue's ring buffer must contain space for @a max_msgs messages,
2826 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2827 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2828 * that each message is similarly aligned to this boundary, @a q_msg_size
2829 * must also be a multiple of N.
2830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002831 * @param q Address of the message queue.
2832 * @param buffer Pointer to ring buffer that holds queued messages.
2833 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002834 * @param max_msgs Maximum number of messages that can be queued.
2835 *
2836 * @return N/A
2837 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002838extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002839 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002840
2841/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002842 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002843 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002844 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002845 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002846 * @note Can be called by ISRs.
2847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002848 * @param q Address of the message queue.
2849 * @param data Pointer to the message.
2850 * @param timeout Waiting period to add the message (in milliseconds),
2851 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002852 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002853 * @retval 0 Message sent.
2854 * @retval -ENOMSG Returned without waiting or queue purged.
2855 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002856 */
Kumar Galacc334c72017-04-21 10:55:34 -05002857extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002858
2859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002860 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002862 * This routine receives a message from message queue @a q in a "first in,
2863 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002864 *
Allan Stephensc98da842016-11-11 15:45:03 -05002865 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * @param q Address of the message queue.
2868 * @param data Address of area to hold the received message.
2869 * @param timeout Waiting period to receive the message (in milliseconds),
2870 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002871 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002872 * @retval 0 Message received.
2873 * @retval -ENOMSG Returned without waiting.
2874 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002875 */
Kumar Galacc334c72017-04-21 10:55:34 -05002876extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002877
2878/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002879 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002881 * This routine discards all unreceived messages in a message queue's ring
2882 * buffer. Any threads that are blocked waiting to send a message to the
2883 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002886 *
2887 * @return N/A
2888 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002889extern void k_msgq_purge(struct k_msgq *q);
2890
Peter Mitsis67be2492016-10-07 11:44:34 -04002891/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002892 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002894 * This routine returns the number of unused entries in a message queue's
2895 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002897 * @param q Address of the message queue.
2898 *
2899 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002900 */
Kumar Galacc334c72017-04-21 10:55:34 -05002901static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002902{
2903 return q->max_msgs - q->used_msgs;
2904}
2905
Peter Mitsisd7a37502016-10-13 11:37:40 -04002906/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002907 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002909 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002911 * @param q Address of the message queue.
2912 *
2913 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002914 */
Kumar Galacc334c72017-04-21 10:55:34 -05002915static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002916{
2917 return q->used_msgs;
2918}
2919
Allan Stephensc98da842016-11-11 15:45:03 -05002920/**
2921 * @} end defgroup msgq_apis
2922 */
2923
2924/**
2925 * @defgroup mem_pool_apis Memory Pool APIs
2926 * @ingroup kernel_apis
2927 * @{
2928 */
2929
Andy Ross73cb9582017-05-09 10:42:39 -07002930/* Note on sizing: the use of a 20 bit field for block means that,
2931 * assuming a reasonable minimum block size of 16 bytes, we're limited
2932 * to 16M of memory managed by a single pool. Long term it would be
2933 * good to move to a variable bit size based on configuration.
2934 */
2935struct k_mem_block_id {
2936 u32_t pool : 8;
2937 u32_t level : 4;
2938 u32_t block : 20;
2939};
2940
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002941struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002942 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002943 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944};
2945
Allan Stephensc98da842016-11-11 15:45:03 -05002946/**
2947 * @} end defgroup mem_pool_apis
2948 */
2949
2950/**
2951 * @defgroup mailbox_apis Mailbox APIs
2952 * @ingroup kernel_apis
2953 * @{
2954 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002955
2956struct k_mbox_msg {
2957 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002958 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002959 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002960 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002961 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002962 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002963 /** sender's message data buffer */
2964 void *tx_data;
2965 /** internal use only - needed for legacy API support */
2966 void *_rx_data;
2967 /** message data block descriptor */
2968 struct k_mem_block tx_block;
2969 /** source thread id */
2970 k_tid_t rx_source_thread;
2971 /** target thread id */
2972 k_tid_t tx_target_thread;
2973 /** internal use only - thread waiting on send (may be a dummy) */
2974 k_tid_t _syncing_thread;
2975#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2976 /** internal use only - semaphore used during asynchronous send */
2977 struct k_sem *_async_sem;
2978#endif
2979};
2980
Allan Stephensc98da842016-11-11 15:45:03 -05002981/**
2982 * @cond INTERNAL_HIDDEN
2983 */
2984
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002985struct k_mbox {
2986 _wait_q_t tx_msg_queue;
2987 _wait_q_t rx_msg_queue;
2988
Anas Nashif2f203c22016-12-18 06:57:45 -05002989 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002990};
2991
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002992#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002993 { \
2994 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2995 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002996 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002997 }
2998
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002999#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3000
Peter Mitsis12092702016-10-14 12:57:23 -04003001/**
Allan Stephensc98da842016-11-11 15:45:03 -05003002 * INTERNAL_HIDDEN @endcond
3003 */
3004
3005/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003006 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003008 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003009 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003010 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003012 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003013 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003014#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003015 struct k_mbox name \
3016 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003017 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003018
Peter Mitsis12092702016-10-14 12:57:23 -04003019/**
3020 * @brief Initialize a mailbox.
3021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003022 * This routine initializes a mailbox object, prior to its first use.
3023 *
3024 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003025 *
3026 * @return N/A
3027 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003028extern void k_mbox_init(struct k_mbox *mbox);
3029
Peter Mitsis12092702016-10-14 12:57:23 -04003030/**
3031 * @brief Send a mailbox message in a synchronous manner.
3032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * This routine sends a message to @a mbox and waits for a receiver to both
3034 * receive and process it. The message data may be in a buffer, in a memory
3035 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003037 * @param mbox Address of the mailbox.
3038 * @param tx_msg Address of the transmit message descriptor.
3039 * @param timeout Waiting period for the message to be received (in
3040 * milliseconds), or one of the special values K_NO_WAIT
3041 * and K_FOREVER. Once the message has been received,
3042 * this routine waits as long as necessary for the message
3043 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003044 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003045 * @retval 0 Message sent.
3046 * @retval -ENOMSG Returned without waiting.
3047 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003048 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003049extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003050 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003051
Peter Mitsis12092702016-10-14 12:57:23 -04003052/**
3053 * @brief Send a mailbox message in an asynchronous manner.
3054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * This routine sends a message to @a mbox without waiting for a receiver
3056 * to process it. The message data may be in a buffer, in a memory pool block,
3057 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3058 * will be given when the message has been both received and completely
3059 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003061 * @param mbox Address of the mailbox.
3062 * @param tx_msg Address of the transmit message descriptor.
3063 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003064 *
3065 * @return N/A
3066 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003067extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003068 struct k_sem *sem);
3069
Peter Mitsis12092702016-10-14 12:57:23 -04003070/**
3071 * @brief Receive a mailbox message.
3072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003073 * This routine receives a message from @a mbox, then optionally retrieves
3074 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003076 * @param mbox Address of the mailbox.
3077 * @param rx_msg Address of the receive message descriptor.
3078 * @param buffer Address of the buffer to receive data, or NULL to defer data
3079 * retrieval and message disposal until later.
3080 * @param timeout Waiting period for a message to be received (in
3081 * milliseconds), or one of the special values K_NO_WAIT
3082 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003083 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003084 * @retval 0 Message received.
3085 * @retval -ENOMSG Returned without waiting.
3086 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003087 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003088extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003089 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003090
3091/**
3092 * @brief Retrieve mailbox message data into a buffer.
3093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003094 * This routine completes the processing of a received message by retrieving
3095 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003096 *
3097 * Alternatively, this routine can be used to dispose of a received message
3098 * without retrieving its data.
3099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003100 * @param rx_msg Address of the receive message descriptor.
3101 * @param buffer Address of the buffer to receive data, or NULL to discard
3102 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003103 *
3104 * @return N/A
3105 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003106extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003107
3108/**
3109 * @brief Retrieve mailbox message data into a memory pool block.
3110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003111 * This routine completes the processing of a received message by retrieving
3112 * its data into a memory pool block, then disposing of the message.
3113 * The memory pool block that results from successful retrieval must be
3114 * returned to the pool once the data has been processed, even in cases
3115 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003116 *
3117 * Alternatively, this routine can be used to dispose of a received message
3118 * without retrieving its data. In this case there is no need to return a
3119 * memory pool block to the pool.
3120 *
3121 * This routine allocates a new memory pool block for the data only if the
3122 * data is not already in one. If a new block cannot be allocated, the routine
3123 * returns a failure code and the received message is left unchanged. This
3124 * permits the caller to reattempt data retrieval at a later time or to dispose
3125 * of the received message without retrieving its data.
3126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * @param rx_msg Address of a receive message descriptor.
3128 * @param pool Address of memory pool, or NULL to discard data.
3129 * @param block Address of the area to hold memory pool block info.
3130 * @param timeout Waiting period to wait for a memory pool block (in
3131 * milliseconds), or one of the special values K_NO_WAIT
3132 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003133 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003134 * @retval 0 Data retrieved.
3135 * @retval -ENOMEM Returned without waiting.
3136 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003137 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003138extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003139 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003140 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003141
Allan Stephensc98da842016-11-11 15:45:03 -05003142/**
3143 * @} end defgroup mailbox_apis
3144 */
3145
3146/**
3147 * @cond INTERNAL_HIDDEN
3148 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003149
3150struct k_pipe {
3151 unsigned char *buffer; /* Pipe buffer: may be NULL */
3152 size_t size; /* Buffer size */
3153 size_t bytes_used; /* # bytes used in buffer */
3154 size_t read_index; /* Where in buffer to read from */
3155 size_t write_index; /* Where in buffer to write */
3156
3157 struct {
3158 _wait_q_t readers; /* Reader wait queue */
3159 _wait_q_t writers; /* Writer wait queue */
3160 } wait_q;
3161
Anas Nashif2f203c22016-12-18 06:57:45 -05003162 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003163};
3164
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003165#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003166 { \
3167 .buffer = pipe_buffer, \
3168 .size = pipe_buffer_size, \
3169 .bytes_used = 0, \
3170 .read_index = 0, \
3171 .write_index = 0, \
3172 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3173 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003174 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003175 }
3176
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003177#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3178
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003179/**
Allan Stephensc98da842016-11-11 15:45:03 -05003180 * INTERNAL_HIDDEN @endcond
3181 */
3182
3183/**
3184 * @defgroup pipe_apis Pipe APIs
3185 * @ingroup kernel_apis
3186 * @{
3187 */
3188
3189/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003190 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003192 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003193 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003194 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003196 * @param name Name of the pipe.
3197 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3198 * or zero if no ring buffer is used.
3199 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003200 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003201#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3202 static unsigned char __noinit __aligned(pipe_align) \
3203 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003204 struct k_pipe name \
3205 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003206 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003207
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003208/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003209 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003211 * This routine initializes a pipe object, prior to its first use.
3212 *
3213 * @param pipe Address of the pipe.
3214 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3215 * is used.
3216 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3217 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003218 *
3219 * @return N/A
3220 */
3221extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3222 size_t size);
3223
3224/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003225 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003227 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003229 * @param pipe Address of the pipe.
3230 * @param data Address of data to write.
3231 * @param bytes_to_write Size of data (in bytes).
3232 * @param bytes_written Address of area to hold the number of bytes written.
3233 * @param min_xfer Minimum number of bytes to write.
3234 * @param timeout Waiting period to wait for the data to be written (in
3235 * milliseconds), or one of the special values K_NO_WAIT
3236 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003237 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003238 * @retval 0 At least @a min_xfer bytes of data were written.
3239 * @retval -EIO Returned without waiting; zero data bytes were written.
3240 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003241 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003242 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003243extern int k_pipe_put(struct k_pipe *pipe, void *data,
3244 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003245 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246
3247/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003248 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003250 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003252 * @param pipe Address of the pipe.
3253 * @param data Address to place the data read from pipe.
3254 * @param bytes_to_read Maximum number of data bytes to read.
3255 * @param bytes_read Address of area to hold the number of bytes read.
3256 * @param min_xfer Minimum number of data bytes to read.
3257 * @param timeout Waiting period to wait for the data to be read (in
3258 * milliseconds), or one of the special values K_NO_WAIT
3259 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003260 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003261 * @retval 0 At least @a min_xfer bytes of data were read.
3262 * @retval -EIO Returned without waiting; zero data bytes were read.
3263 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003264 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003265 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003266extern int k_pipe_get(struct k_pipe *pipe, void *data,
3267 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003268 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003269
3270/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003271 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003273 * This routine writes the data contained in a memory block to @a pipe.
3274 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003275 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003277 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003278 * @param block Memory block containing data to send
3279 * @param size Number of data bytes in memory block to send
3280 * @param sem Semaphore to signal upon completion (else NULL)
3281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003282 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003283 */
3284extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3285 size_t size, struct k_sem *sem);
3286
3287/**
Allan Stephensc98da842016-11-11 15:45:03 -05003288 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003289 */
3290
Allan Stephensc98da842016-11-11 15:45:03 -05003291/**
3292 * @cond INTERNAL_HIDDEN
3293 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003294
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003295struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003296 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003297 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003298 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003299 char *buffer;
3300 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003301 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003302
Anas Nashif2f203c22016-12-18 06:57:45 -05003303 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003304};
3305
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003306#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003307 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003308 { \
3309 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003310 .num_blocks = slab_num_blocks, \
3311 .block_size = slab_block_size, \
3312 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003313 .free_list = NULL, \
3314 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003315 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003316 }
3317
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003318#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3319
3320
Peter Mitsis578f9112016-10-07 13:50:31 -04003321/**
Allan Stephensc98da842016-11-11 15:45:03 -05003322 * INTERNAL_HIDDEN @endcond
3323 */
3324
3325/**
3326 * @defgroup mem_slab_apis Memory Slab APIs
3327 * @ingroup kernel_apis
3328 * @{
3329 */
3330
3331/**
Allan Stephensda827222016-11-09 14:23:58 -06003332 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003333 *
Allan Stephensda827222016-11-09 14:23:58 -06003334 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003335 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003336 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3337 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003338 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003339 *
Allan Stephensda827222016-11-09 14:23:58 -06003340 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003341 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003342 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003343 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * @param name Name of the memory slab.
3346 * @param slab_block_size Size of each memory block (in bytes).
3347 * @param slab_num_blocks Number memory blocks.
3348 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003349 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003350#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3351 char __noinit __aligned(slab_align) \
3352 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3353 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003354 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003355 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003356 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003357
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003358/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003359 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003361 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003362 *
Allan Stephensda827222016-11-09 14:23:58 -06003363 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3364 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3365 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3366 * To ensure that each memory block is similarly aligned to this boundary,
3367 * @a slab_block_size must also be a multiple of N.
3368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * @param slab Address of the memory slab.
3370 * @param buffer Pointer to buffer used for the memory blocks.
3371 * @param block_size Size of each memory block (in bytes).
3372 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003373 *
3374 * @return N/A
3375 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003376extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003377 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003378
3379/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003380 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003382 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003384 * @param slab Address of the memory slab.
3385 * @param mem Pointer to block address area.
3386 * @param timeout Maximum time to wait for operation to complete
3387 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3388 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003389 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003390 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003391 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003392 * @retval -ENOMEM Returned without waiting.
3393 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003394 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003395extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003396 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003397
3398/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003399 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003401 * This routine releases a previously allocated memory block back to its
3402 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * @param slab Address of the memory slab.
3405 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003406 *
3407 * @return N/A
3408 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003409extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003410
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003411/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003412 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003414 * This routine gets the number of memory blocks that are currently
3415 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003417 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003419 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003420 */
Kumar Galacc334c72017-04-21 10:55:34 -05003421static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003422{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003423 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003424}
3425
Peter Mitsisc001aa82016-10-13 13:53:37 -04003426/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003427 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003429 * This routine gets the number of memory blocks that are currently
3430 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003432 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003434 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003435 */
Kumar Galacc334c72017-04-21 10:55:34 -05003436static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003437{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003438 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003439}
3440
Allan Stephensc98da842016-11-11 15:45:03 -05003441/**
3442 * @} end defgroup mem_slab_apis
3443 */
3444
3445/**
3446 * @cond INTERNAL_HIDDEN
3447 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003448
Andy Ross73cb9582017-05-09 10:42:39 -07003449struct k_mem_pool_lvl {
3450 union {
3451 u32_t *bits_p;
3452 u32_t bits;
3453 };
3454 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003455};
3456
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003457struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003458 void *buf;
3459 size_t max_sz;
3460 u16_t n_max;
3461 u8_t n_levels;
3462 u8_t max_inline_level;
3463 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003464 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003465};
3466
Andy Ross73cb9582017-05-09 10:42:39 -07003467#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003468
Andy Ross73cb9582017-05-09 10:42:39 -07003469#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3470
3471#define _MPOOL_LVLS(maxsz, minsz) \
3472 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3473 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3474 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3475 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3476 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3477 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3478 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3479 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3480 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3481 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3482 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3483 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3484 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3485 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3486 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3487 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3488
3489/* Rounds the needed bits up to integer multiples of u32_t */
3490#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3491 ((((n_max) << (2*(l))) + 31) / 32)
3492
3493/* One word gets stored free unioned with the pointer, otherwise the
3494 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003495 */
Andy Ross73cb9582017-05-09 10:42:39 -07003496#define _MPOOL_LBIT_WORDS(n_max, l) \
3497 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3498 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003499
Andy Ross73cb9582017-05-09 10:42:39 -07003500/* How many bytes for the bitfields of a single level? */
3501#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3502 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3503 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003504
Andy Ross73cb9582017-05-09 10:42:39 -07003505/* Size of the bitmap array that follows the buffer in allocated memory */
3506#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3507 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3508 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3509 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3510 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3511 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3512 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3513 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3514 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3515 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3516 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3517 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3518 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3519 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3520 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3521 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3522 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003523
3524/**
Allan Stephensc98da842016-11-11 15:45:03 -05003525 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003526 */
3527
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003528/**
Allan Stephensc98da842016-11-11 15:45:03 -05003529 * @addtogroup mem_pool_apis
3530 * @{
3531 */
3532
3533/**
3534 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003536 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3537 * long. The memory pool allows blocks to be repeatedly partitioned into
3538 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003539 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003540 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003541 * If the pool is to be accessed outside the module where it is defined, it
3542 * can be declared via
3543 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003544 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003546 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003547 * @param minsz Size of the smallest blocks in the pool (in bytes).
3548 * @param maxsz Size of the largest blocks in the pool (in bytes).
3549 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003550 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003551 */
Andy Ross73cb9582017-05-09 10:42:39 -07003552#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3553 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3554 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3555 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3556 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3557 .buf = _mpool_buf_##name, \
3558 .max_sz = maxsz, \
3559 .n_max = nmax, \
3560 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3561 .levels = _mpool_lvls_##name, \
3562 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003563
Peter Mitsis937042c2016-10-13 13:18:26 -04003564/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003565 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003566 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003567 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003569 * @param pool Address of the memory pool.
3570 * @param block Pointer to block descriptor for the allocated memory.
3571 * @param size Amount of memory to allocate (in bytes).
3572 * @param timeout Maximum time to wait for operation to complete
3573 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3574 * or K_FOREVER to wait as long as necessary.
3575 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003576 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003577 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003578 * @retval -ENOMEM Returned without waiting.
3579 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003580 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003581extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003582 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003583
3584/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003585 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003586 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003587 * This routine releases a previously allocated memory block back to its
3588 * memory pool.
3589 *
3590 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003591 *
3592 * @return N/A
3593 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003594extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003595
3596/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003597 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003598 *
Andy Ross73cb9582017-05-09 10:42:39 -07003599 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003600 *
Andy Ross73cb9582017-05-09 10:42:39 -07003601 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003602 *
3603 * @return N/A
3604 */
Andy Ross73cb9582017-05-09 10:42:39 -07003605static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003606
3607/**
Allan Stephensc98da842016-11-11 15:45:03 -05003608 * @} end addtogroup mem_pool_apis
3609 */
3610
3611/**
3612 * @defgroup heap_apis Heap Memory Pool APIs
3613 * @ingroup kernel_apis
3614 * @{
3615 */
3616
3617/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003618 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003620 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003621 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003623 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003625 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003626 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003627extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003628
3629/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003630 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003631 *
3632 * This routine provides traditional free() semantics. The memory being
3633 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003634 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003635 * If @a ptr is NULL, no operation is performed.
3636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003637 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003638 *
3639 * @return N/A
3640 */
3641extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003642
Allan Stephensc98da842016-11-11 15:45:03 -05003643/**
3644 * @} end defgroup heap_apis
3645 */
3646
Benjamin Walshacc68c12017-01-29 18:57:45 -05003647/* polling API - PRIVATE */
3648
Benjamin Walshb0179862017-02-02 16:39:57 -05003649#ifdef CONFIG_POLL
3650#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3651#else
3652#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3653#endif
3654
Benjamin Walshacc68c12017-01-29 18:57:45 -05003655/* private - implementation data created as needed, per-type */
3656struct _poller {
3657 struct k_thread *thread;
3658};
3659
3660/* private - types bit positions */
3661enum _poll_types_bits {
3662 /* can be used to ignore an event */
3663 _POLL_TYPE_IGNORE,
3664
3665 /* to be signaled by k_poll_signal() */
3666 _POLL_TYPE_SIGNAL,
3667
3668 /* semaphore availability */
3669 _POLL_TYPE_SEM_AVAILABLE,
3670
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003671 /* queue/fifo/lifo data availability */
3672 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003673
3674 _POLL_NUM_TYPES
3675};
3676
3677#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3678
3679/* private - states bit positions */
3680enum _poll_states_bits {
3681 /* default state when creating event */
3682 _POLL_STATE_NOT_READY,
3683
Benjamin Walshacc68c12017-01-29 18:57:45 -05003684 /* signaled by k_poll_signal() */
3685 _POLL_STATE_SIGNALED,
3686
3687 /* semaphore is available */
3688 _POLL_STATE_SEM_AVAILABLE,
3689
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003690 /* data is available to read on queue/fifo/lifo */
3691 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003692
3693 _POLL_NUM_STATES
3694};
3695
3696#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3697
3698#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003699 (32 - (0 \
3700 + 8 /* tag */ \
3701 + _POLL_NUM_TYPES \
3702 + _POLL_NUM_STATES \
3703 + 1 /* modes */ \
3704 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003705
3706#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3707#error overflow of 32-bit word in struct k_poll_event
3708#endif
3709
3710/* end of polling API - PRIVATE */
3711
3712
3713/**
3714 * @defgroup poll_apis Async polling APIs
3715 * @ingroup kernel_apis
3716 * @{
3717 */
3718
3719/* Public polling API */
3720
3721/* public - values for k_poll_event.type bitfield */
3722#define K_POLL_TYPE_IGNORE 0
3723#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3724#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003725#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3726#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003727
3728/* public - polling modes */
3729enum k_poll_modes {
3730 /* polling thread does not take ownership of objects when available */
3731 K_POLL_MODE_NOTIFY_ONLY = 0,
3732
3733 K_POLL_NUM_MODES
3734};
3735
3736/* public - values for k_poll_event.state bitfield */
3737#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003738#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3739#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003740#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3741#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003742
3743/* public - poll signal object */
3744struct k_poll_signal {
3745 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003746 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003747
3748 /*
3749 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3750 * user resets it to 0.
3751 */
3752 unsigned int signaled;
3753
3754 /* custom result value passed to k_poll_signal() if needed */
3755 int result;
3756};
3757
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003758#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003759 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003760 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003761 .signaled = 0, \
3762 .result = 0, \
3763 }
3764
3765struct k_poll_event {
3766 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003767 sys_dnode_t _node;
3768
3769 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003770 struct _poller *poller;
3771
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003772 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003773 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003774
Benjamin Walshacc68c12017-01-29 18:57:45 -05003775 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003776 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003777
3778 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003779 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003780
3781 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003782 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003783
3784 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003785 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003786
3787 /* per-type data */
3788 union {
3789 void *obj;
3790 struct k_poll_signal *signal;
3791 struct k_sem *sem;
3792 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003793 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003794 };
3795};
3796
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003797#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003798 { \
3799 .poller = NULL, \
3800 .type = event_type, \
3801 .state = K_POLL_STATE_NOT_READY, \
3802 .mode = event_mode, \
3803 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003804 { .obj = event_obj }, \
3805 }
3806
3807#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3808 event_tag) \
3809 { \
3810 .type = event_type, \
3811 .tag = event_tag, \
3812 .state = K_POLL_STATE_NOT_READY, \
3813 .mode = event_mode, \
3814 .unused = 0, \
3815 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003816 }
3817
3818/**
3819 * @brief Initialize one struct k_poll_event instance
3820 *
3821 * After this routine is called on a poll event, the event it ready to be
3822 * placed in an event array to be passed to k_poll().
3823 *
3824 * @param event The event to initialize.
3825 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3826 * values. Only values that apply to the same object being polled
3827 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3828 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003829 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003830 * @param obj Kernel object or poll signal.
3831 *
3832 * @return N/A
3833 */
3834
Kumar Galacc334c72017-04-21 10:55:34 -05003835extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003836 int mode, void *obj);
3837
3838/**
3839 * @brief Wait for one or many of multiple poll events to occur
3840 *
3841 * This routine allows a thread to wait concurrently for one or many of
3842 * multiple poll events to have occurred. Such events can be a kernel object
3843 * being available, like a semaphore, or a poll signal event.
3844 *
3845 * When an event notifies that a kernel object is available, the kernel object
3846 * is not "given" to the thread calling k_poll(): it merely signals the fact
3847 * that the object was available when the k_poll() call was in effect. Also,
3848 * all threads trying to acquire an object the regular way, i.e. by pending on
3849 * the object, have precedence over the thread polling on the object. This
3850 * means that the polling thread will never get the poll event on an object
3851 * until the object becomes available and its pend queue is empty. For this
3852 * reason, the k_poll() call is more effective when the objects being polled
3853 * only have one thread, the polling thread, trying to acquire them.
3854 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003855 * When k_poll() returns 0, the caller should loop on all the events that were
3856 * passed to k_poll() and check the state field for the values that were
3857 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003858 *
3859 * Before being reused for another call to k_poll(), the user has to reset the
3860 * state field to K_POLL_STATE_NOT_READY.
3861 *
3862 * @param events An array of pointers to events to be polled for.
3863 * @param num_events The number of events in the array.
3864 * @param timeout Waiting period for an event to be ready (in milliseconds),
3865 * or one of the special values K_NO_WAIT and K_FOREVER.
3866 *
3867 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003868 * @retval -EAGAIN Waiting period timed out.
3869 */
3870
3871extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003872 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003873
3874/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003875 * @brief Initialize a poll signal object.
3876 *
3877 * Ready a poll signal object to be signaled via k_poll_signal().
3878 *
3879 * @param signal A poll signal.
3880 *
3881 * @return N/A
3882 */
3883
3884extern void k_poll_signal_init(struct k_poll_signal *signal);
3885
3886/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003887 * @brief Signal a poll signal object.
3888 *
3889 * This routine makes ready a poll signal, which is basically a poll event of
3890 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3891 * made ready to run. A @a result value can be specified.
3892 *
3893 * The poll signal contains a 'signaled' field that, when set by
3894 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3895 * be reset by the user before being passed again to k_poll() or k_poll() will
3896 * consider it being signaled, and will return immediately.
3897 *
3898 * @param signal A poll signal.
3899 * @param result The value to store in the result field of the signal.
3900 *
3901 * @retval 0 The signal was delivered successfully.
3902 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3903 */
3904
3905extern int k_poll_signal(struct k_poll_signal *signal, int result);
3906
3907/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003908extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003909
3910/**
3911 * @} end defgroup poll_apis
3912 */
3913
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003914/**
3915 * @brief Make the CPU idle.
3916 *
3917 * This function makes the CPU idle until an event wakes it up.
3918 *
3919 * In a regular system, the idle thread should be the only thread responsible
3920 * for making the CPU idle and triggering any type of power management.
3921 * However, in some more constrained systems, such as a single-threaded system,
3922 * the only thread would be responsible for this if needed.
3923 *
3924 * @return N/A
3925 */
3926extern void k_cpu_idle(void);
3927
3928/**
3929 * @brief Make the CPU idle in an atomic fashion.
3930 *
3931 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3932 * must be done atomically before making the CPU idle.
3933 *
3934 * @param key Interrupt locking key obtained from irq_lock().
3935 *
3936 * @return N/A
3937 */
3938extern void k_cpu_atomic_idle(unsigned int key);
3939
Kumar Galacc334c72017-04-21 10:55:34 -05003940extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003941
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003942#include <arch/cpu.h>
3943
Andrew Boiecdb94d62017-04-18 15:22:05 -07003944#ifdef _ARCH_EXCEPT
3945/* This archtecture has direct support for triggering a CPU exception */
3946#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3947#else
3948
3949#include <misc/printk.h>
3950
3951/* NOTE: This is the implementation for arches that do not implement
3952 * _ARCH_EXCEPT() to generate a real CPU exception.
3953 *
3954 * We won't have a real exception frame to determine the PC value when
3955 * the oops occurred, so print file and line number before we jump into
3956 * the fatal error handler.
3957 */
3958#define _k_except_reason(reason) do { \
3959 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3960 _NanoFatalErrorHandler(reason, &_default_esf); \
3961 CODE_UNREACHABLE; \
3962 } while (0)
3963
3964#endif /* _ARCH__EXCEPT */
3965
3966/**
3967 * @brief Fatally terminate a thread
3968 *
3969 * This should be called when a thread has encountered an unrecoverable
3970 * runtime condition and needs to terminate. What this ultimately
3971 * means is determined by the _fatal_error_handler() implementation, which
3972 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3973 *
3974 * If this is called from ISR context, the default system fatal error handler
3975 * will treat it as an unrecoverable system error, just like k_panic().
3976 */
3977#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3978
3979/**
3980 * @brief Fatally terminate the system
3981 *
3982 * This should be called when the Zephyr kernel has encountered an
3983 * unrecoverable runtime condition and needs to terminate. What this ultimately
3984 * means is determined by the _fatal_error_handler() implementation, which
3985 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3986 */
3987#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3988
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003989/*
3990 * private APIs that are utilized by one or more public APIs
3991 */
3992
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003993#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003994extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003995#else
3996#define _init_static_threads() do { } while ((0))
3997#endif
3998
3999extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004000extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004002/* arch/cpu.h may declare an architecture or platform-specific macro
4003 * for properly declaring stacks, compatible with MMU/MPU constraints if
4004 * enabled
4005 */
4006#ifdef _ARCH_THREAD_STACK_DEFINE
4007#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4008#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4009 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4010#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4011#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004012static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4013{
4014 return _ARCH_THREAD_STACK_BUFFER(sym);
4015}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004016#else
4017/**
4018 * @brief Declare a toplevel thread stack memory region
4019 *
4020 * This declares a region of memory suitable for use as a thread's stack.
4021 *
4022 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4023 * 'noinit' section so that it isn't zeroed at boot
4024 *
Andrew Boie507852a2017-07-25 18:47:07 -07004025 * The declared symbol will always be a k_thread_stack_t which can be passed to
4026 * k_thread_create, but should otherwise not be manipulated. If the buffer
4027 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004028 *
4029 * It is legal to precede this definition with the 'static' keyword.
4030 *
4031 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4032 * parameter of k_thread_create(), it may not be the same as the
4033 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4034 *
4035 * @param sym Thread stack symbol name
4036 * @param size Size of the stack memory region
4037 */
4038#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004039 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004040
4041/**
4042 * @brief Declare a toplevel array of thread stack memory regions
4043 *
4044 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4045 * definition for additional details and constraints.
4046 *
4047 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4048 * 'noinit' section so that it isn't zeroed at boot
4049 *
4050 * @param sym Thread stack symbol name
4051 * @param nmemb Number of stacks to declare
4052 * @param size Size of the stack memory region
4053 */
4054
4055#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004056 struct _k_thread_stack_element __noinit \
4057 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004058
4059/**
4060 * @brief Declare an embedded stack memory region
4061 *
4062 * Used for stacks embedded within other data structures. Use is highly
4063 * discouraged but in some cases necessary. For memory protection scenarios,
4064 * it is very important that any RAM preceding this member not be writable
4065 * by threads else a stack overflow will lead to silent corruption. In other
4066 * words, the containing data structure should live in RAM owned by the kernel.
4067 *
4068 * @param sym Thread stack symbol name
4069 * @param size Size of the stack memory region
4070 */
4071#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004072 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004073
4074/**
4075 * @brief Return the size in bytes of a stack memory region
4076 *
4077 * Convenience macro for passing the desired stack size to k_thread_create()
4078 * since the underlying implementation may actually create something larger
4079 * (for instance a guard area).
4080 *
4081 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004082 * passed to K_THREAD_STACK_DEFINE.
4083 *
4084 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4085 * it is not guaranteed to return the original value since each array
4086 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004087 *
4088 * @param sym Stack memory symbol
4089 * @return Size of the stack
4090 */
4091#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4092
4093/**
4094 * @brief Get a pointer to the physical stack buffer
4095 *
4096 * Convenience macro to get at the real underlying stack buffer used by
4097 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4098 * This is really only intended for diagnostic tools which want to examine
4099 * stack memory contents.
4100 *
4101 * @param sym Declared stack symbol name
4102 * @return The buffer itself, a char *
4103 */
Andrew Boie507852a2017-07-25 18:47:07 -07004104static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4105{
4106 return (char *)sym;
4107}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004108
4109#endif /* _ARCH_DECLARE_STACK */
4110
Chunlin Hane9c97022017-07-07 20:29:30 +08004111/**
4112 * @defgroup mem_domain_apis Memory domain APIs
4113 * @ingroup kernel_apis
4114 * @{
4115 */
4116
4117/** @def MEM_PARTITION_ENTRY
4118 * @brief Used to declare a memory partition entry
4119 */
4120#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4121 {\
4122 .start = _start, \
4123 .size = _size, \
4124 .attr = _attr, \
4125 }
4126
4127/** @def K_MEM_PARTITION_DEFINE
4128 * @brief Used to declare a memory partition
4129 */
4130#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4131#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4132 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4133 struct k_mem_partition name = \
4134 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4135#else
4136#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4137 struct k_mem_partition name = \
4138 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4139#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4140
4141
4142/* memory partition */
4143struct k_mem_partition {
4144 /* start address of memory partition */
4145 u32_t start;
4146 /* size of memory partition */
4147 u32_t size;
4148 /* attribute of memory partition */
4149 u32_t attr;
4150};
4151
4152#if defined(CONFIG_USERSPACE)
4153/* memory domian */
4154struct k_mem_domain {
4155 /* number of partitions in the domain */
4156 u32_t num_partitions;
4157 /* partitions in the domain */
4158 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
4159 /* domain q */
4160 sys_dlist_t mem_domain_q;
4161};
4162#endif /* CONFIG_USERSPACE */
4163
4164/**
4165 * @brief Initialize a memory domain.
4166 *
4167 * Initialize a memory domain with given name and memory partitions.
4168 *
4169 * @param domain The memory domain to be initialized.
4170 * @param num_parts The number of array items of "parts" parameter.
4171 * @param parts An array of pointers to the memory partitions. Can be NULL
4172 * if num_parts is zero.
4173 */
4174
4175extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4176 struct k_mem_partition *parts[]);
4177/**
4178 * @brief Destroy a memory domain.
4179 *
4180 * Destroy a memory domain.
4181 *
4182 * @param domain The memory domain to be destroyed.
4183 */
4184
4185extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4186
4187/**
4188 * @brief Add a memory partition into a memory domain.
4189 *
4190 * Add a memory partition into a memory domain.
4191 *
4192 * @param domain The memory domain to be added a memory partition.
4193 * @param part The memory partition to be added
4194 */
4195
4196extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4197 struct k_mem_partition *part);
4198
4199/**
4200 * @brief Remove a memory partition from a memory domain.
4201 *
4202 * Remove a memory partition from a memory domain.
4203 *
4204 * @param domain The memory domain to be removed a memory partition.
4205 * @param part The memory partition to be removed
4206 */
4207
4208extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4209 struct k_mem_partition *part);
4210
4211/**
4212 * @brief Add a thread into a memory domain.
4213 *
4214 * Add a thread into a memory domain.
4215 *
4216 * @param domain The memory domain that the thread is going to be added into.
4217 * @param thread ID of thread going to be added into the memory domain.
4218 */
4219
4220extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4221 k_tid_t thread);
4222
4223/**
4224 * @brief Remove a thread from its memory domain.
4225 *
4226 * Remove a thread from its memory domain.
4227 *
4228 * @param thread ID of thread going to be removed from its memory domain.
4229 */
4230
4231extern void k_mem_domain_remove_thread(k_tid_t thread);
4232
4233/**
4234 * @} end defgroup mem_domain_apis
4235 */
4236
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004237#ifdef __cplusplus
4238}
4239#endif
4240
Andrew Boiee004dec2016-11-07 09:01:19 -08004241#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4242/*
4243 * Define new and delete operators.
4244 * At this moment, the operators do nothing since objects are supposed
4245 * to be statically allocated.
4246 */
4247inline void operator delete(void *ptr)
4248{
4249 (void)ptr;
4250}
4251
4252inline void operator delete[](void *ptr)
4253{
4254 (void)ptr;
4255}
4256
4257inline void *operator new(size_t size)
4258{
4259 (void)size;
4260 return NULL;
4261}
4262
4263inline void *operator new[](size_t size)
4264{
4265 (void)size;
4266 return NULL;
4267}
4268
4269/* Placement versions of operator new and delete */
4270inline void operator delete(void *ptr1, void *ptr2)
4271{
4272 (void)ptr1;
4273 (void)ptr2;
4274}
4275
4276inline void operator delete[](void *ptr1, void *ptr2)
4277{
4278 (void)ptr1;
4279 (void)ptr2;
4280}
4281
4282inline void *operator new(size_t size, void *ptr)
4283{
4284 (void)size;
4285 return ptr;
4286}
4287
4288inline void *operator new[](size_t size, void *ptr)
4289{
4290 (void)size;
4291 return ptr;
4292}
4293
4294#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4295
Andrew Boiefa94ee72017-09-28 16:54:35 -07004296#include <syscalls/kernel.h>
4297
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004298#endif /* !_ASMLANGUAGE */
4299
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004300#endif /* _kernel__h_ */