blob: 563fd01e3239f3eaaa29cbbc41360e55870bda78 [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;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400126
Andrew Boie5bd891d2017-09-27 12:59:28 -0700127/* This enumeration needs to be kept in sync with the lists of kernel objects
128 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
129 * function in kernel/userspace.c
130 */
Andrew Boie945af952017-08-22 13:15:23 -0700131enum k_objects {
Andrew Boie5bd891d2017-09-27 12:59:28 -0700132 /* Core kernel objects */
Andrew Boie945af952017-08-22 13:15:23 -0700133 K_OBJ_ALERT,
134 K_OBJ_DELAYED_WORK,
135 K_OBJ_MEM_SLAB,
136 K_OBJ_MSGQ,
137 K_OBJ_MUTEX,
138 K_OBJ_PIPE,
139 K_OBJ_SEM,
140 K_OBJ_STACK,
141 K_OBJ_THREAD,
142 K_OBJ_TIMER,
143 K_OBJ_WORK,
144 K_OBJ_WORK_Q,
145
Andrew Boie5bd891d2017-09-27 12:59:28 -0700146 /* Driver subsystems */
147 K_OBJ_DRIVER_ADC,
148 K_OBJ_DRIVER_AIO_CMP,
149 K_OBJ_DRIVER_CLOCK_CONTROL,
150 K_OBJ_DRIVER_COUNTER,
151 K_OBJ_DRIVER_CRYPTO,
152 K_OBJ_DRIVER_DMA,
153 K_OBJ_DRIVER_ETH,
154 K_OBJ_DRIVER_FLASH,
155 K_OBJ_DRIVER_GPIO,
156 K_OBJ_DRIVER_I2C,
157 K_OBJ_DRIVER_I2S,
158 K_OBJ_DRIVER_IPM,
159 K_OBJ_DRIVER_PINMUX,
160 K_OBJ_DRIVER_PWM,
161 K_OBJ_DRIVER_RANDOM,
162 K_OBJ_DRIVER_RTC,
163 K_OBJ_DRIVER_SENSOR,
164 K_OBJ_DRIVER_SHARED_IRQ,
165 K_OBJ_DRIVER_SPI,
166 K_OBJ_DRIVER_UART,
167 K_OBJ_DRIVER_WDT,
168
Andrew Boie945af952017-08-22 13:15:23 -0700169 K_OBJ_LAST
170};
171
172#ifdef CONFIG_USERSPACE
173/* Table generated by gperf, these objects are retrieved via
174 * _k_object_find() */
175struct _k_object {
176 char *name;
177 char perms[CONFIG_MAX_THREAD_BYTES];
178 char type;
179 char flags;
180} __packed;
181
182#define K_OBJ_FLAG_INITIALIZED BIT(0)
183/**
184 * Ensure a system object is a valid object of the expected type
185 *
186 * Searches for the object and ensures that it is indeed an object
187 * of the expected type, that the caller has the right permissions on it,
188 * and that the object has been initialized.
189 *
190 * This function is intended to be called on the kernel-side system
191 * call handlers to validate kernel object pointers passed in from
192 * userspace.
193 *
194 * @param obj Address of the kernel object
195 * @param otype Expected type of the kernel object
196 * @param init If true, this is for an init function and we will not error
197 * out if the object is not initialized
198 * @return 0 If the object is valid
199 * -EBADF if not a valid object of the specified type
200 * -EPERM If the caller does not have permissions
David B. Kinder8065dbc2017-09-21 15:25:40 -0700201 * -EINVAL Object is not initialized
Andrew Boie945af952017-08-22 13:15:23 -0700202 */
203int _k_object_validate(void *obj, enum k_objects otype, int init);
204
205
206/**
207 * Lookup a kernel object and init its metadata if it exists
208 *
209 * Calling this on an object will make it usable from userspace.
210 * Intended to be called as the last statement in kernel object init
211 * functions.
212 *
213 * @param object Address of the kernel object
214 */
215void _k_object_init(void *obj);
216
217
218/**
219 * grant a thread access to a kernel object
220 *
221 * The thread will be granted access to the object if the caller is from
222 * supervisor mode, or the caller is from user mode AND has permissions
223 * on the object already.
224 *
225 * @param object Address of kernel object
226 * @param thread Thread to grant access to the object
227 */
228void k_object_grant_access(void *object, struct k_thread *thread);
229
230#else
231static inline int _k_object_validate(void *obj, enum k_objects otype, int init)
232{
233 ARG_UNUSED(obj);
234 ARG_UNUSED(otype);
235 ARG_UNUSED(init);
236
237 return 0;
238}
239
240static inline void _k_object_init(void *obj)
241{
242 ARG_UNUSED(obj);
243}
244
245static inline void k_object_grant_access(void *object, struct k_thread *thread)
246{
247 ARG_UNUSED(object);
248 ARG_UNUSED(thread);
249}
250#endif /* CONFIG_USERSPACE */
251
Andrew Boie73abd322017-04-04 13:19:13 -0700252/* timeouts */
253
254struct _timeout;
255typedef void (*_timeout_func_t)(struct _timeout *t);
256
257struct _timeout {
258 sys_dnode_t node;
259 struct k_thread *thread;
260 sys_dlist_t *wait_q;
261 s32_t delta_ticks_from_prev;
262 _timeout_func_t func;
263};
264
265extern s32_t _timeout_remaining_get(struct _timeout *timeout);
266
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700267/**
268 * @typedef k_thread_entry_t
269 * @brief Thread entry point function type.
270 *
271 * A thread's entry point function is invoked when the thread starts executing.
272 * Up to 3 argument values can be passed to the function.
273 *
274 * The thread terminates execution permanently if the entry point function
275 * returns. The thread is responsible for releasing any shared resources
276 * it may own (such as mutexes and dynamically allocated memory), prior to
277 * returning.
278 *
279 * @param p1 First argument.
280 * @param p2 Second argument.
281 * @param p3 Third argument.
282 *
283 * @return N/A
284 */
285typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700286
287#ifdef CONFIG_THREAD_MONITOR
288struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700289 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700290 void *parameter1;
291 void *parameter2;
292 void *parameter3;
293};
294#endif
295
296/* can be used for creating 'dummy' threads, e.g. for pending on objects */
297struct _thread_base {
298
299 /* this thread's entry in a ready/wait queue */
300 sys_dnode_t k_q_node;
301
302 /* user facing 'thread options'; values defined in include/kernel.h */
303 u8_t user_options;
304
305 /* thread state */
306 u8_t thread_state;
307
308 /*
309 * scheduler lock count and thread priority
310 *
311 * These two fields control the preemptibility of a thread.
312 *
313 * When the scheduler is locked, sched_locked is decremented, which
314 * means that the scheduler is locked for values from 0xff to 0x01. A
315 * thread is coop if its prio is negative, thus 0x80 to 0xff when
316 * looked at the value as unsigned.
317 *
318 * By putting them end-to-end, this means that a thread is
319 * non-preemptible if the bundled value is greater than or equal to
320 * 0x0080.
321 */
322 union {
323 struct {
324#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
325 u8_t sched_locked;
326 s8_t prio;
327#else /* LITTLE and PDP */
328 s8_t prio;
329 u8_t sched_locked;
330#endif
331 };
332 u16_t preempt;
333 };
334
335 /* data returned by APIs */
336 void *swap_data;
337
338#ifdef CONFIG_SYS_CLOCK_EXISTS
339 /* this thread's entry in a timeout queue */
340 struct _timeout timeout;
341#endif
Andrew Boie2acfcd62017-08-30 14:31:03 -0700342
343#ifdef CONFIG_USERSPACE
344 /* Bit position in kernel object permissions bitfield for this thread */
345 unsigned int perm_index;
346#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700347};
348
349typedef struct _thread_base _thread_base_t;
350
351#if defined(CONFIG_THREAD_STACK_INFO)
352/* Contains the stack information of a thread */
353struct _thread_stack_info {
354 /* Stack Start */
355 u32_t start;
356 /* Stack Size */
357 u32_t size;
358};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700359
360typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700361#endif /* CONFIG_THREAD_STACK_INFO */
362
363struct k_thread {
364
365 struct _thread_base base;
366
367 /* defined by the architecture, but all archs need these */
368 struct _caller_saved caller_saved;
369 struct _callee_saved callee_saved;
370
371 /* static thread init data */
372 void *init_data;
373
374 /* abort function */
375 void (*fn_abort)(void);
376
377#if defined(CONFIG_THREAD_MONITOR)
378 /* thread entry and parameters description */
379 struct __thread_entry *entry;
380
381 /* next item in list of all threads */
382 struct k_thread *next_thread;
383#endif
384
385#ifdef CONFIG_THREAD_CUSTOM_DATA
386 /* crude thread-local storage */
387 void *custom_data;
388#endif
389
390#ifdef CONFIG_ERRNO
391 /* per-thread errno variable */
392 int errno_var;
393#endif
394
395#if defined(CONFIG_THREAD_STACK_INFO)
396 /* Stack Info */
397 struct _thread_stack_info stack_info;
398#endif /* CONFIG_THREAD_STACK_INFO */
399
400 /* arch-specifics: must always be at the end */
401 struct _thread_arch arch;
402};
403
404typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400405typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700406#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400407
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400408enum execution_context_types {
409 K_ISR = 0,
410 K_COOP_THREAD,
411 K_PREEMPT_THREAD,
412};
413
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400414/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100415 * @defgroup profiling_apis Profiling APIs
416 * @ingroup kernel_apis
417 * @{
418 */
419
420/**
421 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
422 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700423 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100424 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
425 *
426 * CONFIG_MAIN_STACK_SIZE
427 * CONFIG_IDLE_STACK_SIZE
428 * CONFIG_ISR_STACK_SIZE
429 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
430 *
431 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
432 * produce output.
433 *
434 * @return N/A
435 */
436extern void k_call_stacks_analyze(void);
437
438/**
439 * @} end defgroup profiling_apis
440 */
441
442/**
Allan Stephensc98da842016-11-11 15:45:03 -0500443 * @defgroup thread_apis Thread APIs
444 * @ingroup kernel_apis
445 * @{
446 */
447
Benjamin Walshed240f22017-01-22 13:05:08 -0500448#endif /* !_ASMLANGUAGE */
449
450
451/*
452 * Thread user options. May be needed by assembly code. Common part uses low
453 * bits, arch-specific use high bits.
454 */
455
456/* system thread that must not abort */
457#define K_ESSENTIAL (1 << 0)
458
459#if defined(CONFIG_FP_SHARING)
460/* thread uses floating point registers */
461#define K_FP_REGS (1 << 1)
462#endif
463
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700464/* This thread has dropped from supervisor mode to user mode and consequently
465 * has additional restrictions
466 */
467#define K_USER (1 << 2)
468
Benjamin Walshed240f22017-01-22 13:05:08 -0500469#ifdef CONFIG_X86
470/* x86 Bitmask definitions for threads user options */
471
472#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
473/* thread uses SSEx (and also FP) registers */
474#define K_SSE_REGS (1 << 7)
475#endif
476#endif
477
478/* end - thread options */
479
480#if !defined(_ASMLANGUAGE)
481
Andrew Boie507852a2017-07-25 18:47:07 -0700482/* Using typedef deliberately here, this is quite intended to be an opaque
483 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
484 *
485 * The purpose of this data type is to clearly distinguish between the
486 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
487 * buffer which composes the stack data actually used by the underlying
488 * thread; they cannot be used interchangably as some arches precede the
489 * stack buffer region with guard areas that trigger a MPU or MMU fault
490 * if written to.
491 *
492 * APIs that want to work with the buffer inside should continue to use
493 * char *.
494 *
495 * Stacks should always be created with K_THREAD_STACK_DEFINE().
496 */
497struct __packed _k_thread_stack_element {
498 char data;
499};
500typedef struct _k_thread_stack_element *k_thread_stack_t;
501
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400502
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400503/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700504 * @brief Create a thread.
505 *
506 * This routine initializes a thread, then schedules it for execution.
507 *
508 * The new thread may be scheduled for immediate execution or a delayed start.
509 * If the newly spawned thread does not have a delayed start the kernel
510 * scheduler may preempt the current thread to allow the new thread to
511 * execute.
512 *
513 * Thread options are architecture-specific, and can include K_ESSENTIAL,
514 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
515 * them using "|" (the logical OR operator).
516 *
517 * Historically, users often would use the beginning of the stack memory region
518 * to store the struct k_thread data, although corruption will occur if the
519 * stack overflows this region and stack protection features may not detect this
520 * situation.
521 *
522 * @param new_thread Pointer to uninitialized struct k_thread
523 * @param stack Pointer to the stack space.
524 * @param stack_size Stack size in bytes.
525 * @param entry Thread entry function.
526 * @param p1 1st entry point parameter.
527 * @param p2 2nd entry point parameter.
528 * @param p3 3rd entry point parameter.
529 * @param prio Thread priority.
530 * @param options Thread options.
531 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
532 *
533 * @return ID of new thread.
534 */
Andrew Boie507852a2017-07-25 18:47:07 -0700535extern k_tid_t k_thread_create(struct k_thread *new_thread,
536 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700537 size_t stack_size,
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700538 k_thread_entry_t entry,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700539 void *p1, void *p2, void *p3,
540 int prio, u32_t options, s32_t delay);
541
Andrew Boie3f091b52017-08-30 14:34:14 -0700542#ifdef CONFIG_USERSPACE
543/**
544 * @brief Drop a thread's privileges permanently to user mode
545 *
546 * @param entry Function to start executing from
547 * @param p1 1st entry point parameter
548 * @param p2 2nd entry point parameter
549 * @param p3 3rd entry point parameter
550 */
551extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
552 void *p1, void *p2,
553 void *p3);
554#endif
555
Andrew Boied26cf2d2017-03-30 13:07:02 -0700556/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500557 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400558 *
Allan Stephensc98da842016-11-11 15:45:03 -0500559 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500560 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500562 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400563 *
564 * @return N/A
565 */
Kumar Galacc334c72017-04-21 10:55:34 -0500566extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400567
568/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500569 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400570 *
571 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500572 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400573 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400574 * @return N/A
575 */
Kumar Galacc334c72017-04-21 10:55:34 -0500576extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400577
578/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500579 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500581 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400582 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500583 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400584 *
585 * @return N/A
586 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400587extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400588
589/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500590 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400591 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500592 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500594 * If @a thread is not currently sleeping, the routine has no effect.
595 *
596 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400597 *
598 * @return N/A
599 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400600extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400601
602/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500603 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500605 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400606 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400607extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400608
609/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500610 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500612 * This routine prevents @a thread from executing if it has not yet started
613 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500615 * @param thread ID of thread to cancel.
616 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700617 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500618 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400619 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400620extern int k_thread_cancel(k_tid_t thread);
621
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400622/**
Allan Stephensc98da842016-11-11 15:45:03 -0500623 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500625 * This routine permanently stops execution of @a thread. The thread is taken
626 * off all kernel queues it is part of (i.e. the ready queue, the timeout
627 * queue, or a kernel object wait queue). However, any kernel resources the
628 * thread might currently own (such as mutexes or memory blocks) are not
629 * released. It is the responsibility of the caller of this routine to ensure
630 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500632 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400633 *
634 * @return N/A
635 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400636extern void k_thread_abort(k_tid_t thread);
637
Andrew Boie7d627c52017-08-30 11:01:56 -0700638
639/**
640 * @brief Start an inactive thread
641 *
642 * If a thread was created with K_FOREVER in the delay parameter, it will
643 * not be added to the scheduling queue until this function is called
644 * on it.
645 *
646 * @param thread thread to start
647 */
648extern void k_thread_start(k_tid_t thread);
649
Allan Stephensc98da842016-11-11 15:45:03 -0500650/**
651 * @cond INTERNAL_HIDDEN
652 */
653
Benjamin Walshd211a522016-12-06 11:44:01 -0500654/* timeout has timed out and is not on _timeout_q anymore */
655#define _EXPIRED (-2)
656
657/* timeout is not in use */
658#define _INACTIVE (-1)
659
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400660struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700661 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700662 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400663 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700664 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500665 void *init_p1;
666 void *init_p2;
667 void *init_p3;
668 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500669 u32_t init_options;
670 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500671 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500672 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400673};
674
Andrew Boied26cf2d2017-03-30 13:07:02 -0700675#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400676 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500677 prio, options, delay, abort, groups) \
678 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700679 .init_thread = (thread), \
680 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500681 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700682 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400683 .init_p1 = (void *)p1, \
684 .init_p2 = (void *)p2, \
685 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500686 .init_prio = (prio), \
687 .init_options = (options), \
688 .init_delay = (delay), \
689 .init_abort = (abort), \
690 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400691 }
692
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400693/**
Allan Stephensc98da842016-11-11 15:45:03 -0500694 * INTERNAL_HIDDEN @endcond
695 */
696
697/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500698 * @brief Statically define and initialize a thread.
699 *
700 * The thread may be scheduled for immediate execution or a delayed start.
701 *
702 * Thread options are architecture-specific, and can include K_ESSENTIAL,
703 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
704 * them using "|" (the logical OR operator).
705 *
706 * The ID of the thread can be accessed using:
707 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500708 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500709 *
710 * @param name Name of the thread.
711 * @param stack_size Stack size in bytes.
712 * @param entry Thread entry function.
713 * @param p1 1st entry point parameter.
714 * @param p2 2nd entry point parameter.
715 * @param p3 3rd entry point parameter.
716 * @param prio Thread priority.
717 * @param options Thread options.
718 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400719 *
720 * @internal It has been observed that the x86 compiler by default aligns
721 * these _static_thread_data structures to 32-byte boundaries, thereby
722 * wasting space. To work around this, force a 4-byte alignment.
723 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500724#define K_THREAD_DEFINE(name, stack_size, \
725 entry, p1, p2, p3, \
726 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700727 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700728 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500729 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500730 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700731 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
732 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500733 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700734 NULL, 0); \
735 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400736
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400737/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500738 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500740 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500742 * @param thread ID of thread whose priority is needed.
743 *
744 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400745 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500746extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400747
748/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500749 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500751 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400752 *
753 * Rescheduling can occur immediately depending on the priority @a thread is
754 * set to:
755 *
756 * - If its priority is raised above the priority of the caller of this
757 * function, and the caller is preemptible, @a thread will be scheduled in.
758 *
759 * - If the caller operates on itself, it lowers its priority below that of
760 * other threads in the system, and the caller is preemptible, the thread of
761 * highest priority will be scheduled in.
762 *
763 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
764 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
765 * highest priority.
766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500767 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400768 * @param prio New priority.
769 *
770 * @warning Changing the priority of a thread currently involved in mutex
771 * priority inheritance may result in undefined behavior.
772 *
773 * @return N/A
774 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400775extern void k_thread_priority_set(k_tid_t thread, int prio);
776
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400777/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500778 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500780 * This routine prevents the kernel scheduler from making @a thread the
781 * current thread. All other internal operations on @a thread are still
782 * performed; for example, any timeout it is waiting on keeps ticking,
783 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500785 * If @a thread is already suspended, the routine has no effect.
786 *
787 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400788 *
789 * @return N/A
790 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400791extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400792
793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500794 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500796 * This routine allows the kernel scheduler to make @a thread the current
797 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * If @a thread is not currently suspended, the routine has no effect.
800 *
801 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400802 *
803 * @return N/A
804 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400805extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400806
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400807/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500808 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400809 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500810 * This routine specifies how the scheduler will perform time slicing of
811 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * To enable time slicing, @a slice must be non-zero. The scheduler
814 * ensures that no thread runs for more than the specified time limit
815 * before other threads of that priority are given a chance to execute.
816 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700817 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500819 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820 * execute. Once the scheduler selects a thread for execution, there is no
821 * minimum guaranteed time the thread will execute before threads of greater or
822 * equal priority are scheduled.
823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500824 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825 * for execution, this routine has no effect; the thread is immediately
826 * rescheduled after the slice period expires.
827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500828 * To disable timeslicing, set both @a slice and @a prio to zero.
829 *
830 * @param slice Maximum time slice length (in milliseconds).
831 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 *
833 * @return N/A
834 */
Kumar Galacc334c72017-04-21 10:55:34 -0500835extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400836
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837/**
Allan Stephensc98da842016-11-11 15:45:03 -0500838 * @} end defgroup thread_apis
839 */
840
841/**
842 * @addtogroup isr_apis
843 * @{
844 */
845
846/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500847 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 *
Allan Stephensc98da842016-11-11 15:45:03 -0500849 * This routine allows the caller to customize its actions, depending on
850 * whether it is a thread or an ISR.
851 *
852 * @note Can be called by ISRs.
853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500854 * @return 0 if invoked by a thread.
855 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500857extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400858
Benjamin Walsh445830d2016-11-10 15:54:27 -0500859/**
860 * @brief Determine if code is running in a preemptible thread.
861 *
Allan Stephensc98da842016-11-11 15:45:03 -0500862 * This routine allows the caller to customize its actions, depending on
863 * whether it can be preempted by another thread. The routine returns a 'true'
864 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500865 *
Allan Stephensc98da842016-11-11 15:45:03 -0500866 * - The code is running in a thread, not at ISR.
867 * - The thread's priority is in the preemptible range.
868 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500869 *
Allan Stephensc98da842016-11-11 15:45:03 -0500870 * @note Can be called by ISRs.
871 *
872 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500873 * @return Non-zero if invoked by a preemptible thread.
874 */
875extern int k_is_preempt_thread(void);
876
Allan Stephensc98da842016-11-11 15:45:03 -0500877/**
878 * @} end addtogroup isr_apis
879 */
880
881/**
882 * @addtogroup thread_apis
883 * @{
884 */
885
886/**
887 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500888 *
Allan Stephensc98da842016-11-11 15:45:03 -0500889 * This routine prevents the current thread from being preempted by another
890 * thread by instructing the scheduler to treat it as a cooperative thread.
891 * If the thread subsequently performs an operation that makes it unready,
892 * it will be context switched out in the normal manner. When the thread
893 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500894 *
Allan Stephensc98da842016-11-11 15:45:03 -0500895 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500896 *
Allan Stephensc98da842016-11-11 15:45:03 -0500897 * @note k_sched_lock() and k_sched_unlock() should normally be used
898 * when the operation being performed can be safely interrupted by ISRs.
899 * However, if the amount of processing involved is very small, better
900 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500901 *
902 * @return N/A
903 */
904extern void k_sched_lock(void);
905
Allan Stephensc98da842016-11-11 15:45:03 -0500906/**
907 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500908 *
Allan Stephensc98da842016-11-11 15:45:03 -0500909 * This routine reverses the effect of a previous call to k_sched_lock().
910 * A thread must call the routine once for each time it called k_sched_lock()
911 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500912 *
913 * @return N/A
914 */
915extern void k_sched_unlock(void);
916
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400917/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500918 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500920 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500922 * Custom data is not used by the kernel itself, and is freely available
923 * for a thread to use as it sees fit. It can be used as a framework
924 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500926 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400927 *
928 * @return N/A
929 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400930extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400931
932/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500933 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500935 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500937 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400938 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400939extern void *k_thread_custom_data_get(void);
940
941/**
Allan Stephensc98da842016-11-11 15:45:03 -0500942 * @} end addtogroup thread_apis
943 */
944
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400945#include <sys_clock.h>
946
Allan Stephensc2f15a42016-11-17 12:24:22 -0500947/**
948 * @addtogroup clock_apis
949 * @{
950 */
951
952/**
953 * @brief Generate null timeout delay.
954 *
955 * This macro generates a timeout delay that that instructs a kernel API
956 * not to wait if the requested operation cannot be performed immediately.
957 *
958 * @return Timeout delay value.
959 */
960#define K_NO_WAIT 0
961
962/**
963 * @brief Generate timeout delay from milliseconds.
964 *
965 * This macro generates a timeout delay that that instructs a kernel API
966 * to wait up to @a ms milliseconds to perform the requested operation.
967 *
968 * @param ms Duration in milliseconds.
969 *
970 * @return Timeout delay value.
971 */
Johan Hedberg14471692016-11-13 10:52:15 +0200972#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500973
974/**
975 * @brief Generate timeout delay from seconds.
976 *
977 * This macro generates a timeout delay that that instructs a kernel API
978 * to wait up to @a s seconds to perform the requested operation.
979 *
980 * @param s Duration in seconds.
981 *
982 * @return Timeout delay value.
983 */
Johan Hedberg14471692016-11-13 10:52:15 +0200984#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500985
986/**
987 * @brief Generate timeout delay from minutes.
988 *
989 * This macro generates a timeout delay that that instructs a kernel API
990 * to wait up to @a m minutes to perform the requested operation.
991 *
992 * @param m Duration in minutes.
993 *
994 * @return Timeout delay value.
995 */
Johan Hedberg14471692016-11-13 10:52:15 +0200996#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500997
998/**
999 * @brief Generate timeout delay from hours.
1000 *
1001 * This macro generates a timeout delay that that instructs a kernel API
1002 * to wait up to @a h hours to perform the requested operation.
1003 *
1004 * @param h Duration in hours.
1005 *
1006 * @return Timeout delay value.
1007 */
Johan Hedberg14471692016-11-13 10:52:15 +02001008#define K_HOURS(h) K_MINUTES((h) * 60)
1009
Allan Stephensc98da842016-11-11 15:45:03 -05001010/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001011 * @brief Generate infinite timeout delay.
1012 *
1013 * This macro generates a timeout delay that that instructs a kernel API
1014 * to wait as long as necessary to perform the requested operation.
1015 *
1016 * @return Timeout delay value.
1017 */
1018#define K_FOREVER (-1)
1019
1020/**
1021 * @} end addtogroup clock_apis
1022 */
1023
1024/**
Allan Stephensc98da842016-11-11 15:45:03 -05001025 * @cond INTERNAL_HIDDEN
1026 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001027
Benjamin Walsh62092182016-12-20 14:39:08 -05001028/* kernel clocks */
1029
1030#if (sys_clock_ticks_per_sec == 1000) || \
1031 (sys_clock_ticks_per_sec == 500) || \
1032 (sys_clock_ticks_per_sec == 250) || \
1033 (sys_clock_ticks_per_sec == 125) || \
1034 (sys_clock_ticks_per_sec == 100) || \
1035 (sys_clock_ticks_per_sec == 50) || \
1036 (sys_clock_ticks_per_sec == 25) || \
1037 (sys_clock_ticks_per_sec == 20) || \
1038 (sys_clock_ticks_per_sec == 10) || \
1039 (sys_clock_ticks_per_sec == 1)
1040
1041 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1042#else
1043 /* yields horrible 64-bit math on many architectures: try to avoid */
1044 #define _NON_OPTIMIZED_TICKS_PER_SEC
1045#endif
1046
1047#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001048extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001049#else
Kumar Galacc334c72017-04-21 10:55:34 -05001050static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001051{
Kumar Galacc334c72017-04-21 10:55:34 -05001052 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001053}
1054#endif
1055
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001056/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001057#ifdef CONFIG_TICKLESS_KERNEL
1058#define _TICK_ALIGN 0
1059#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001060#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001061#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001062
Kumar Galacc334c72017-04-21 10:55:34 -05001063static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001064{
Benjamin Walsh62092182016-12-20 14:39:08 -05001065#ifdef CONFIG_SYS_CLOCK_EXISTS
1066
1067#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001068 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001069#else
Kumar Galacc334c72017-04-21 10:55:34 -05001070 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001071#endif
1072
1073#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001074 __ASSERT(ticks == 0, "");
1075 return 0;
1076#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001077}
1078
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001079struct k_timer {
1080 /*
1081 * _timeout structure must be first here if we want to use
1082 * dynamic timer allocation. timeout.node is used in the double-linked
1083 * list of free timers
1084 */
1085 struct _timeout timeout;
1086
Allan Stephens45bfa372016-10-12 12:39:42 -05001087 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001088 _wait_q_t wait_q;
1089
1090 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001091 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001092
1093 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001094 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001095
1096 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001097 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001098
Allan Stephens45bfa372016-10-12 12:39:42 -05001099 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001100 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001101
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001102 /* user-specific data, also used to support legacy features */
1103 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001104
Anas Nashif2f203c22016-12-18 06:57:45 -05001105 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001106};
1107
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001108#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001109 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001110 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001111 .timeout.wait_q = NULL, \
1112 .timeout.thread = NULL, \
1113 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001114 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001115 .expiry_fn = expiry, \
1116 .stop_fn = stop, \
1117 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001118 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001119 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001120 }
1121
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001122#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1123
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001124/**
Allan Stephensc98da842016-11-11 15:45:03 -05001125 * INTERNAL_HIDDEN @endcond
1126 */
1127
1128/**
1129 * @defgroup timer_apis Timer APIs
1130 * @ingroup kernel_apis
1131 * @{
1132 */
1133
1134/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001135 * @typedef k_timer_expiry_t
1136 * @brief Timer expiry function type.
1137 *
1138 * A timer's expiry function is executed by the system clock interrupt handler
1139 * each time the timer expires. The expiry function is optional, and is only
1140 * invoked if the timer has been initialized with one.
1141 *
1142 * @param timer Address of timer.
1143 *
1144 * @return N/A
1145 */
1146typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1147
1148/**
1149 * @typedef k_timer_stop_t
1150 * @brief Timer stop function type.
1151 *
1152 * A timer's stop function is executed if the timer is stopped prematurely.
1153 * The function runs in the context of the thread that stops the timer.
1154 * The stop function is optional, and is only invoked if the timer has been
1155 * initialized with one.
1156 *
1157 * @param timer Address of timer.
1158 *
1159 * @return N/A
1160 */
1161typedef void (*k_timer_stop_t)(struct k_timer *timer);
1162
1163/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001164 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001166 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001167 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001168 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001169 *
1170 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001171 * @param expiry_fn Function to invoke each time the timer expires.
1172 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001173 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001174#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001175 struct k_timer name \
1176 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001177 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001178
Allan Stephens45bfa372016-10-12 12:39:42 -05001179/**
1180 * @brief Initialize a timer.
1181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001182 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001183 *
1184 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001185 * @param expiry_fn Function to invoke each time the timer expires.
1186 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001187 *
1188 * @return N/A
1189 */
1190extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001191 k_timer_expiry_t expiry_fn,
1192 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001193
Allan Stephens45bfa372016-10-12 12:39:42 -05001194/**
1195 * @brief Start a timer.
1196 *
1197 * This routine starts a timer, and resets its status to zero. The timer
1198 * begins counting down using the specified duration and period values.
1199 *
1200 * Attempting to start a timer that is already running is permitted.
1201 * The timer's status is reset to zero and the timer begins counting down
1202 * using the new duration and period values.
1203 *
1204 * @param timer Address of timer.
1205 * @param duration Initial timer duration (in milliseconds).
1206 * @param period Timer period (in milliseconds).
1207 *
1208 * @return N/A
1209 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001210extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001211 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001212
1213/**
1214 * @brief Stop a timer.
1215 *
1216 * This routine stops a running timer prematurely. The timer's stop function,
1217 * if one exists, is invoked by the caller.
1218 *
1219 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001220 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001221 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001222 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1223 * if @a k_timer_stop is to be called from ISRs.
1224 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001225 * @param timer Address of timer.
1226 *
1227 * @return N/A
1228 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001229extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001230
1231/**
1232 * @brief Read timer status.
1233 *
1234 * This routine reads the timer's status, which indicates the number of times
1235 * it has expired since its status was last read.
1236 *
1237 * Calling this routine resets the timer's status to zero.
1238 *
1239 * @param timer Address of timer.
1240 *
1241 * @return Timer status.
1242 */
Kumar Galacc334c72017-04-21 10:55:34 -05001243extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001244
1245/**
1246 * @brief Synchronize thread to timer expiration.
1247 *
1248 * This routine blocks the calling thread until the timer's status is non-zero
1249 * (indicating that it has expired at least once since it was last examined)
1250 * or the timer is stopped. If the timer status is already non-zero,
1251 * or the timer is already stopped, the caller continues without waiting.
1252 *
1253 * Calling this routine resets the timer's status to zero.
1254 *
1255 * This routine must not be used by interrupt handlers, since they are not
1256 * allowed to block.
1257 *
1258 * @param timer Address of timer.
1259 *
1260 * @return Timer status.
1261 */
Kumar Galacc334c72017-04-21 10:55:34 -05001262extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001263
1264/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001265 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001266 *
1267 * This routine computes the (approximate) time remaining before a running
1268 * timer next expires. If the timer is not running, it returns zero.
1269 *
1270 * @param timer Address of timer.
1271 *
1272 * @return Remaining time (in milliseconds).
1273 */
Kumar Galacc334c72017-04-21 10:55:34 -05001274static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001275{
1276 return _timeout_remaining_get(&timer->timeout);
1277}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001278
Allan Stephensc98da842016-11-11 15:45:03 -05001279/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001280 * @brief Associate user-specific data with a timer.
1281 *
1282 * This routine records the @a user_data with the @a timer, to be retrieved
1283 * later.
1284 *
1285 * It can be used e.g. in a timer handler shared across multiple subsystems to
1286 * retrieve data specific to the subsystem this timer is associated with.
1287 *
1288 * @param timer Address of timer.
1289 * @param user_data User data to associate with the timer.
1290 *
1291 * @return N/A
1292 */
1293static inline void k_timer_user_data_set(struct k_timer *timer,
1294 void *user_data)
1295{
1296 timer->user_data = user_data;
1297}
1298
1299/**
1300 * @brief Retrieve the user-specific data from a timer.
1301 *
1302 * @param timer Address of timer.
1303 *
1304 * @return The user data.
1305 */
1306static inline void *k_timer_user_data_get(struct k_timer *timer)
1307{
1308 return timer->user_data;
1309}
1310
1311/**
Allan Stephensc98da842016-11-11 15:45:03 -05001312 * @} end defgroup timer_apis
1313 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001314
Allan Stephensc98da842016-11-11 15:45:03 -05001315/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001316 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001317 * @{
1318 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001319
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001320/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001321 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001323 * This routine returns the elapsed time since the system booted,
1324 * in milliseconds.
1325 *
1326 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001327 */
Kumar Galacc334c72017-04-21 10:55:34 -05001328extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001329
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001330#ifdef CONFIG_TICKLESS_KERNEL
1331/**
1332 * @brief Enable clock always on in tickless kernel
1333 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001334 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001335 * there are no timer events programmed in tickless kernel
1336 * scheduling. This is necessary if the clock is used to track
1337 * passage of time.
1338 *
1339 * @retval prev_status Previous status of always on flag
1340 */
1341static inline int k_enable_sys_clock_always_on(void)
1342{
1343 int prev_status = _sys_clock_always_on;
1344
1345 _sys_clock_always_on = 1;
1346 _enable_sys_clock();
1347
1348 return prev_status;
1349}
1350
1351/**
1352 * @brief Disable clock always on in tickless kernel
1353 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001354 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001355 * there are no timer events programmed in tickless kernel
1356 * scheduling. To save power, this routine should be called
1357 * immediately when clock is not used to track time.
1358 */
1359static inline void k_disable_sys_clock_always_on(void)
1360{
1361 _sys_clock_always_on = 0;
1362}
1363#else
1364#define k_enable_sys_clock_always_on() do { } while ((0))
1365#define k_disable_sys_clock_always_on() do { } while ((0))
1366#endif
1367
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001368/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001369 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001371 * This routine returns the lower 32-bits of the elapsed time since the system
1372 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001374 * This routine can be more efficient than k_uptime_get(), as it reduces the
1375 * need for interrupt locking and 64-bit math. However, the 32-bit result
1376 * cannot hold a system uptime time larger than approximately 50 days, so the
1377 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001379 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001380 */
Kumar Galacc334c72017-04-21 10:55:34 -05001381extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001382
1383/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001384 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001386 * This routine computes the elapsed time between the current system uptime
1387 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001389 * @param reftime Pointer to a reference time, which is updated to the current
1390 * uptime upon return.
1391 *
1392 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001393 */
Kumar Galacc334c72017-04-21 10:55:34 -05001394extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001395
1396/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001397 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001399 * This routine computes the elapsed time between the current system uptime
1400 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001402 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1403 * need for interrupt locking and 64-bit math. However, the 32-bit result
1404 * cannot hold an elapsed time larger than approximately 50 days, so the
1405 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001407 * @param reftime Pointer to a reference time, which is updated to the current
1408 * uptime upon return.
1409 *
1410 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001411 */
Kumar Galacc334c72017-04-21 10:55:34 -05001412extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001413
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001414/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001415 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001417 * This routine returns the current time, as measured by the system's hardware
1418 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001420 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001421 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001422#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001423
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001424/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001425 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001426 */
1427
Allan Stephensc98da842016-11-11 15:45:03 -05001428/**
1429 * @cond INTERNAL_HIDDEN
1430 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001431
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001432struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001433 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001434 union {
1435 _wait_q_t wait_q;
1436
1437 _POLL_EVENT;
1438 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001439
1440 _OBJECT_TRACING_NEXT_PTR(k_queue);
1441};
1442
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001443#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001444 { \
1445 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1446 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001447 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001448 _OBJECT_TRACING_INIT \
1449 }
1450
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001451#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1452
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001453/**
1454 * INTERNAL_HIDDEN @endcond
1455 */
1456
1457/**
1458 * @defgroup queue_apis Queue APIs
1459 * @ingroup kernel_apis
1460 * @{
1461 */
1462
1463/**
1464 * @brief Initialize a queue.
1465 *
1466 * This routine initializes a queue object, prior to its first use.
1467 *
1468 * @param queue Address of the queue.
1469 *
1470 * @return N/A
1471 */
1472extern void k_queue_init(struct k_queue *queue);
1473
1474/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001475 * @brief Cancel waiting on a queue.
1476 *
1477 * This routine causes first thread pending on @a queue, if any, to
1478 * return from k_queue_get() call with NULL value (as if timeout expired).
1479 *
1480 * @note Can be called by ISRs.
1481 *
1482 * @param queue Address of the queue.
1483 *
1484 * @return N/A
1485 */
1486extern void k_queue_cancel_wait(struct k_queue *queue);
1487
1488/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001489 * @brief Append an element to the end of a queue.
1490 *
1491 * This routine appends a data item to @a queue. A queue data item must be
1492 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1493 * reserved for the kernel's use.
1494 *
1495 * @note Can be called by ISRs.
1496 *
1497 * @param queue Address of the queue.
1498 * @param data Address of the data item.
1499 *
1500 * @return N/A
1501 */
1502extern void k_queue_append(struct k_queue *queue, void *data);
1503
1504/**
1505 * @brief Prepend an element to a queue.
1506 *
1507 * This routine prepends a data item to @a queue. A queue data item must be
1508 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1509 * reserved for the kernel's use.
1510 *
1511 * @note Can be called by ISRs.
1512 *
1513 * @param queue Address of the queue.
1514 * @param data Address of the data item.
1515 *
1516 * @return N/A
1517 */
1518extern void k_queue_prepend(struct k_queue *queue, void *data);
1519
1520/**
1521 * @brief Inserts an element to a queue.
1522 *
1523 * This routine inserts a data item to @a queue after previous item. A queue
1524 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1525 * item are reserved for the kernel's use.
1526 *
1527 * @note Can be called by ISRs.
1528 *
1529 * @param queue Address of the queue.
1530 * @param prev Address of the previous data item.
1531 * @param data Address of the data item.
1532 *
1533 * @return N/A
1534 */
1535extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1536
1537/**
1538 * @brief Atomically append a list of elements to a queue.
1539 *
1540 * This routine adds a list of data items to @a queue in one operation.
1541 * The data items must be in a singly-linked list, with the first 32 bits
1542 * in each data item pointing to the next data item; the list must be
1543 * NULL-terminated.
1544 *
1545 * @note Can be called by ISRs.
1546 *
1547 * @param queue Address of the queue.
1548 * @param head Pointer to first node in singly-linked list.
1549 * @param tail Pointer to last node in singly-linked list.
1550 *
1551 * @return N/A
1552 */
1553extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1554
1555/**
1556 * @brief Atomically add a list of elements to a queue.
1557 *
1558 * This routine adds a list of data items to @a queue in one operation.
1559 * The data items must be in a singly-linked list implemented using a
1560 * sys_slist_t object. Upon completion, the original list is empty.
1561 *
1562 * @note Can be called by ISRs.
1563 *
1564 * @param queue Address of the queue.
1565 * @param list Pointer to sys_slist_t object.
1566 *
1567 * @return N/A
1568 */
1569extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1570
1571/**
1572 * @brief Get an element from a queue.
1573 *
1574 * This routine removes first data item from @a queue. The first 32 bits of the
1575 * data item are reserved for the kernel's use.
1576 *
1577 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1578 *
1579 * @param queue Address of the queue.
1580 * @param timeout Waiting period to obtain a data item (in milliseconds),
1581 * or one of the special values K_NO_WAIT and K_FOREVER.
1582 *
1583 * @return Address of the data item if successful; NULL if returned
1584 * without waiting, or waiting period timed out.
1585 */
Kumar Galacc334c72017-04-21 10:55:34 -05001586extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001587
1588/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001589 * @brief Remove an element from a queue.
1590 *
1591 * This routine removes data item from @a queue. The first 32 bits of the
1592 * data item are reserved for the kernel's use. Removing elements from k_queue
1593 * rely on sys_slist_find_and_remove which is not a constant time operation.
1594 *
1595 * @note Can be called by ISRs
1596 *
1597 * @param queue Address of the queue.
1598 * @param data Address of the data item.
1599 *
1600 * @return true if data item was removed
1601 */
1602static inline bool k_queue_remove(struct k_queue *queue, void *data)
1603{
1604 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1605}
1606
1607/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001608 * @brief Query a queue to see if it has data available.
1609 *
1610 * Note that the data might be already gone by the time this function returns
1611 * if other threads are also trying to read from the queue.
1612 *
1613 * @note Can be called by ISRs.
1614 *
1615 * @param queue Address of the queue.
1616 *
1617 * @return Non-zero if the queue is empty.
1618 * @return 0 if data is available.
1619 */
1620static inline int k_queue_is_empty(struct k_queue *queue)
1621{
1622 return (int)sys_slist_is_empty(&queue->data_q);
1623}
1624
1625/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001626 * @brief Peek element at the head of queue.
1627 *
1628 * Return element from the head of queue without removing it.
1629 *
1630 * @param queue Address of the queue.
1631 *
1632 * @return Head element, or NULL if queue is empty.
1633 */
1634static inline void *k_queue_peek_head(struct k_queue *queue)
1635{
1636 return sys_slist_peek_head(&queue->data_q);
1637}
1638
1639/**
1640 * @brief Peek element at the tail of queue.
1641 *
1642 * Return element from the tail of queue without removing it.
1643 *
1644 * @param queue Address of the queue.
1645 *
1646 * @return Tail element, or NULL if queue is empty.
1647 */
1648static inline void *k_queue_peek_tail(struct k_queue *queue)
1649{
1650 return sys_slist_peek_tail(&queue->data_q);
1651}
1652
1653/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001654 * @brief Statically define and initialize a queue.
1655 *
1656 * The queue can be accessed outside the module where it is defined using:
1657 *
1658 * @code extern struct k_queue <name>; @endcode
1659 *
1660 * @param name Name of the queue.
1661 */
1662#define K_QUEUE_DEFINE(name) \
1663 struct k_queue name \
1664 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001665 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001666
1667/**
1668 * @} end defgroup queue_apis
1669 */
1670
1671/**
1672 * @cond INTERNAL_HIDDEN
1673 */
1674
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001675struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001676 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001677};
1678
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001679#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001680 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001681 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001682 }
1683
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001684#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1685
Allan Stephensc98da842016-11-11 15:45:03 -05001686/**
1687 * INTERNAL_HIDDEN @endcond
1688 */
1689
1690/**
1691 * @defgroup fifo_apis Fifo APIs
1692 * @ingroup kernel_apis
1693 * @{
1694 */
1695
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001696/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001697 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001698 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001699 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001701 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001702 *
1703 * @return N/A
1704 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001705#define k_fifo_init(fifo) \
1706 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001707
1708/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001709 * @brief Cancel waiting on a fifo.
1710 *
1711 * This routine causes first thread pending on @a fifo, if any, to
1712 * return from k_fifo_get() call with NULL value (as if timeout
1713 * expired).
1714 *
1715 * @note Can be called by ISRs.
1716 *
1717 * @param fifo Address of the fifo.
1718 *
1719 * @return N/A
1720 */
1721#define k_fifo_cancel_wait(fifo) \
1722 k_queue_cancel_wait((struct k_queue *) fifo)
1723
1724/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001725 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001726 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001727 * This routine adds a data item to @a fifo. A fifo data item must be
1728 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1729 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001731 * @note Can be called by ISRs.
1732 *
1733 * @param fifo Address of the fifo.
1734 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001735 *
1736 * @return N/A
1737 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001738#define k_fifo_put(fifo, data) \
1739 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001740
1741/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001742 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001744 * This routine adds a list of data items to @a fifo in one operation.
1745 * The data items must be in a singly-linked list, with the first 32 bits
1746 * each data item pointing to the next data item; the list must be
1747 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001749 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * @param fifo Address of the fifo.
1752 * @param head Pointer to first node in singly-linked list.
1753 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001754 *
1755 * @return N/A
1756 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001757#define k_fifo_put_list(fifo, head, tail) \
1758 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001759
1760/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001761 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001763 * This routine adds a list of data items to @a fifo in one operation.
1764 * The data items must be in a singly-linked list implemented using a
1765 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001766 * and must be re-initialized via sys_slist_init().
1767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001768 * @note Can be called by ISRs.
1769 *
1770 * @param fifo Address of the fifo.
1771 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001772 *
1773 * @return N/A
1774 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001775#define k_fifo_put_slist(fifo, list) \
1776 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001777
1778/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001779 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001781 * This routine removes a data item from @a fifo in a "first in, first out"
1782 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001784 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1785 *
1786 * @param fifo Address of the fifo.
1787 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001788 * or one of the special values K_NO_WAIT and K_FOREVER.
1789 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001790 * @return Address of the data item if successful; NULL if returned
1791 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001792 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001793#define k_fifo_get(fifo, timeout) \
1794 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001795
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001796/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001797 * @brief Query a fifo to see if it has data available.
1798 *
1799 * Note that the data might be already gone by the time this function returns
1800 * if other threads is also trying to read from the fifo.
1801 *
1802 * @note Can be called by ISRs.
1803 *
1804 * @param fifo Address of the fifo.
1805 *
1806 * @return Non-zero if the fifo is empty.
1807 * @return 0 if data is available.
1808 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001809#define k_fifo_is_empty(fifo) \
1810 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001811
1812/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001813 * @brief Peek element at the head of fifo.
1814 *
1815 * Return element from the head of fifo without removing it. A usecase
1816 * for this is if elements of the fifo are themselves containers. Then
1817 * on each iteration of processing, a head container will be peeked,
1818 * and some data processed out of it, and only if the container is empty,
1819 * it will be completely remove from the fifo.
1820 *
1821 * @param fifo Address of the fifo.
1822 *
1823 * @return Head element, or NULL if the fifo is empty.
1824 */
1825#define k_fifo_peek_head(fifo) \
1826 k_queue_peek_head((struct k_queue *) fifo)
1827
1828/**
1829 * @brief Peek element at the tail of fifo.
1830 *
1831 * Return element from the tail of fifo (without removing it). A usecase
1832 * for this is if elements of the fifo are themselves containers. Then
1833 * it may be useful to add more data to the last container in fifo.
1834 *
1835 * @param fifo Address of the fifo.
1836 *
1837 * @return Tail element, or NULL if fifo is empty.
1838 */
1839#define k_fifo_peek_tail(fifo) \
1840 k_queue_peek_tail((struct k_queue *) fifo)
1841
1842/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001843 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001845 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001846 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001847 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001849 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001850 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001851#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001852 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001853 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001854 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001855
Allan Stephensc98da842016-11-11 15:45:03 -05001856/**
1857 * @} end defgroup fifo_apis
1858 */
1859
1860/**
1861 * @cond INTERNAL_HIDDEN
1862 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001863
1864struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001865 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001866};
1867
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001868#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001869 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001870 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001871 }
1872
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001873#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1874
Allan Stephensc98da842016-11-11 15:45:03 -05001875/**
1876 * INTERNAL_HIDDEN @endcond
1877 */
1878
1879/**
1880 * @defgroup lifo_apis Lifo APIs
1881 * @ingroup kernel_apis
1882 * @{
1883 */
1884
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001885/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001886 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001888 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001890 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001891 *
1892 * @return N/A
1893 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001894#define k_lifo_init(lifo) \
1895 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001896
1897/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001898 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001900 * This routine adds a data item to @a lifo. A lifo data item must be
1901 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1902 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001903 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001904 * @note Can be called by ISRs.
1905 *
1906 * @param lifo Address of the lifo.
1907 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001908 *
1909 * @return N/A
1910 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001911#define k_lifo_put(lifo, data) \
1912 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001913
1914/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001915 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001917 * This routine removes a data item from @a lifo in a "last in, first out"
1918 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001920 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1921 *
1922 * @param lifo Address of the lifo.
1923 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001924 * or one of the special values K_NO_WAIT and K_FOREVER.
1925 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001926 * @return Address of the data item if successful; NULL if returned
1927 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001928 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001929#define k_lifo_get(lifo, timeout) \
1930 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001931
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001932/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001933 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001935 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001936 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001937 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001939 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001940 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001941#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001942 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001943 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001944 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001945
Allan Stephensc98da842016-11-11 15:45:03 -05001946/**
1947 * @} end defgroup lifo_apis
1948 */
1949
1950/**
1951 * @cond INTERNAL_HIDDEN
1952 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001953
1954struct k_stack {
1955 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001956 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001957
Anas Nashif2f203c22016-12-18 06:57:45 -05001958 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001959};
1960
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001961#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001962 { \
1963 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1964 .base = stack_buffer, \
1965 .next = stack_buffer, \
1966 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001967 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001968 }
1969
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001970#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1971
Allan Stephensc98da842016-11-11 15:45:03 -05001972/**
1973 * INTERNAL_HIDDEN @endcond
1974 */
1975
1976/**
1977 * @defgroup stack_apis Stack APIs
1978 * @ingroup kernel_apis
1979 * @{
1980 */
1981
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001982/**
1983 * @brief Initialize a stack.
1984 *
1985 * This routine initializes a stack object, prior to its first use.
1986 *
1987 * @param stack Address of the stack.
1988 * @param buffer Address of array used to hold stacked values.
1989 * @param num_entries Maximum number of values that can be stacked.
1990 *
1991 * @return N/A
1992 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001993extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001994 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001995
1996/**
1997 * @brief Push an element onto a stack.
1998 *
1999 * This routine adds a 32-bit value @a data to @a stack.
2000 *
2001 * @note Can be called by ISRs.
2002 *
2003 * @param stack Address of the stack.
2004 * @param data Value to push onto the stack.
2005 *
2006 * @return N/A
2007 */
Kumar Galacc334c72017-04-21 10:55:34 -05002008extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002009
2010/**
2011 * @brief Pop an element from a stack.
2012 *
2013 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2014 * manner and stores the value in @a data.
2015 *
2016 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2017 *
2018 * @param stack Address of the stack.
2019 * @param data Address of area to hold the value popped from the stack.
2020 * @param timeout Waiting period to obtain a value (in milliseconds),
2021 * or one of the special values K_NO_WAIT and K_FOREVER.
2022 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002023 * @retval 0 Element popped from stack.
2024 * @retval -EBUSY Returned without waiting.
2025 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002026 */
Kumar Galacc334c72017-04-21 10:55:34 -05002027extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002028
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002029/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002030 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002032 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002033 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002034 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002036 * @param name Name of the stack.
2037 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002038 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002039#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002040 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002041 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002042 struct k_stack name \
2043 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002044 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002045 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002046
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002047/**
Allan Stephensc98da842016-11-11 15:45:03 -05002048 * @} end defgroup stack_apis
2049 */
2050
Allan Stephens6bba9b02016-11-16 14:56:54 -05002051struct k_work;
2052
Allan Stephensc98da842016-11-11 15:45:03 -05002053/**
2054 * @defgroup workqueue_apis Workqueue Thread APIs
2055 * @ingroup kernel_apis
2056 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002057 */
2058
Allan Stephens6bba9b02016-11-16 14:56:54 -05002059/**
2060 * @typedef k_work_handler_t
2061 * @brief Work item handler function type.
2062 *
2063 * A work item's handler function is executed by a workqueue's thread
2064 * when the work item is processed by the workqueue.
2065 *
2066 * @param work Address of the work item.
2067 *
2068 * @return N/A
2069 */
2070typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002071
2072/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002073 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002074 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002075
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002076struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002077 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002078 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002079};
2080
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002081enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002082 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002083};
2084
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002085struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002086 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002087 k_work_handler_t handler;
2088 atomic_t flags[1];
2089};
2090
Allan Stephens6bba9b02016-11-16 14:56:54 -05002091struct k_delayed_work {
2092 struct k_work work;
2093 struct _timeout timeout;
2094 struct k_work_q *work_q;
2095};
2096
2097extern struct k_work_q k_sys_work_q;
2098
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002099/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002100 * INTERNAL_HIDDEN @endcond
2101 */
2102
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002103#define _K_WORK_INITIALIZER(work_handler) \
2104 { \
2105 ._reserved = NULL, \
2106 .handler = work_handler, \
2107 .flags = { 0 } \
2108 }
2109
2110#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2111
Allan Stephens6bba9b02016-11-16 14:56:54 -05002112/**
2113 * @brief Initialize a statically-defined work item.
2114 *
2115 * This macro can be used to initialize a statically-defined workqueue work
2116 * item, prior to its first use. For example,
2117 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002118 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002119 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002120 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002121 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002123#define K_WORK_DEFINE(work, work_handler) \
2124 struct k_work work \
2125 __in_section(_k_work, static, work) = \
2126 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002127
2128/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002129 * @brief Initialize a work item.
2130 *
2131 * This routine initializes a workqueue work item, prior to its first use.
2132 *
2133 * @param work Address of work item.
2134 * @param handler Function to invoke each time work item is processed.
2135 *
2136 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002137 */
2138static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2139{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002140 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002141 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002142 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002143}
2144
2145/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002146 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002147 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002148 * This routine submits work item @a work to be processed by workqueue
2149 * @a work_q. If the work item is already pending in the workqueue's queue
2150 * as a result of an earlier submission, this routine has no effect on the
2151 * work item. If the work item has already been processed, or is currently
2152 * being processed, its work is considered complete and the work item can be
2153 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002154 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002155 * @warning
2156 * A submitted work item must not be modified until it has been processed
2157 * by the workqueue.
2158 *
2159 * @note Can be called by ISRs.
2160 *
2161 * @param work_q Address of workqueue.
2162 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002163 *
2164 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002165 */
2166static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2167 struct k_work *work)
2168{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002169 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002170 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002171 }
2172}
2173
2174/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002175 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002176 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002177 * This routine indicates if work item @a work is pending in a workqueue's
2178 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002179 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002180 * @note Can be called by ISRs.
2181 *
2182 * @param work Address of work item.
2183 *
2184 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002185 */
2186static inline int k_work_pending(struct k_work *work)
2187{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002188 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002189}
2190
2191/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002192 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002193 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002194 * This routine starts workqueue @a work_q. The workqueue spawns its work
2195 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002196 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002197 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002198 * @param stack Pointer to work queue thread's stack space, as defined by
2199 * K_THREAD_STACK_DEFINE()
2200 * @param stack_size Size of the work queue thread's stack (in bytes), which
2201 * should either be the same constant passed to
2202 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002203 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002204 *
2205 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002206 */
Andrew Boie507852a2017-07-25 18:47:07 -07002207extern void k_work_q_start(struct k_work_q *work_q,
2208 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002209 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002210
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002211/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002212 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002213 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002214 * This routine initializes a workqueue delayed work item, prior to
2215 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002216 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002217 * @param work Address of delayed work item.
2218 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002219 *
2220 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002222extern void k_delayed_work_init(struct k_delayed_work *work,
2223 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224
2225/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002226 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002227 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002228 * This routine schedules work item @a work to be processed by workqueue
2229 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002230 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002231 * Only when the countdown completes is the work item actually submitted to
2232 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002233 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002234 * Submitting a previously submitted delayed work item that is still
2235 * counting down cancels the existing submission and restarts the countdown
2236 * using the new delay. If the work item is currently pending on the
2237 * workqueue's queue because the countdown has completed it is too late to
2238 * resubmit the item, and resubmission fails without impacting the work item.
2239 * If the work item has already been processed, or is currently being processed,
2240 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002241 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002242 * @warning
2243 * A delayed work item must not be modified until it has been processed
2244 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002245 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002246 * @note Can be called by ISRs.
2247 *
2248 * @param work_q Address of workqueue.
2249 * @param work Address of delayed work item.
2250 * @param delay Delay before submitting the work item (in milliseconds).
2251 *
2252 * @retval 0 Work item countdown started.
2253 * @retval -EINPROGRESS Work item is already pending.
2254 * @retval -EINVAL Work item is being processed or has completed its work.
2255 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002256 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002257extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2258 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002259 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002260
2261/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002262 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002263 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002264 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002265 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002266 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002267 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002268 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002269 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002270 * @param work Address of delayed work item.
2271 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002272 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002273 * @retval -EINPROGRESS Work item is already pending.
2274 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002275 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002276extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002277
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002278/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002279 * @brief Submit a work item to the system workqueue.
2280 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002281 * This routine submits work item @a work to be processed by the system
2282 * workqueue. If the work item is already pending in the workqueue's queue
2283 * as a result of an earlier submission, this routine has no effect on the
2284 * work item. If the work item has already been processed, or is currently
2285 * being processed, its work is considered complete and the work item can be
2286 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002287 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002288 * @warning
2289 * Work items submitted to the system workqueue should avoid using handlers
2290 * that block or yield since this may prevent the system workqueue from
2291 * processing other work items in a timely manner.
2292 *
2293 * @note Can be called by ISRs.
2294 *
2295 * @param work Address of work item.
2296 *
2297 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002298 */
2299static inline void k_work_submit(struct k_work *work)
2300{
2301 k_work_submit_to_queue(&k_sys_work_q, work);
2302}
2303
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002304/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002305 * @brief Submit a delayed work item to the system workqueue.
2306 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002307 * This routine schedules work item @a work to be processed by the system
2308 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002309 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002310 * Only when the countdown completes is the work item actually submitted to
2311 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002313 * Submitting a previously submitted delayed work item that is still
2314 * counting down cancels the existing submission and restarts the countdown
2315 * using the new delay. If the work item is currently pending on the
2316 * workqueue's queue because the countdown has completed it is too late to
2317 * resubmit the item, and resubmission fails without impacting the work item.
2318 * If the work item has already been processed, or is currently being processed,
2319 * its work is considered complete and the work item can be resubmitted.
2320 *
2321 * @warning
2322 * Work items submitted to the system workqueue should avoid using handlers
2323 * that block or yield since this may prevent the system workqueue from
2324 * processing other work items in a timely manner.
2325 *
2326 * @note Can be called by ISRs.
2327 *
2328 * @param work Address of delayed work item.
2329 * @param delay Delay before submitting the work item (in milliseconds).
2330 *
2331 * @retval 0 Work item countdown started.
2332 * @retval -EINPROGRESS Work item is already pending.
2333 * @retval -EINVAL Work item is being processed or has completed its work.
2334 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002335 */
2336static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002337 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002338{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002339 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002340}
2341
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002343 * @brief Get time remaining before a delayed work gets scheduled.
2344 *
2345 * This routine computes the (approximate) time remaining before a
2346 * delayed work gets executed. If the delayed work is not waiting to be
2347 * schedules, it returns zero.
2348 *
2349 * @param work Delayed work item.
2350 *
2351 * @return Remaining time (in milliseconds).
2352 */
Kumar Galacc334c72017-04-21 10:55:34 -05002353static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002354{
2355 return _timeout_remaining_get(&work->timeout);
2356}
2357
2358/**
Allan Stephensc98da842016-11-11 15:45:03 -05002359 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002360 */
2361
Allan Stephensc98da842016-11-11 15:45:03 -05002362/**
2363 * @cond INTERNAL_HIDDEN
2364 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002365
2366struct k_mutex {
2367 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002368 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002369 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002370 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002371
Anas Nashif2f203c22016-12-18 06:57:45 -05002372 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002373};
2374
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002375#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002376 { \
2377 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2378 .owner = NULL, \
2379 .lock_count = 0, \
2380 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002381 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002382 }
2383
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002384#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2385
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002386/**
Allan Stephensc98da842016-11-11 15:45:03 -05002387 * INTERNAL_HIDDEN @endcond
2388 */
2389
2390/**
2391 * @defgroup mutex_apis Mutex APIs
2392 * @ingroup kernel_apis
2393 * @{
2394 */
2395
2396/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002399 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002400 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002401 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002403 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002404 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002405#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002406 struct k_mutex name \
2407 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002408 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002410/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002411 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002413 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002415 * Upon completion, the mutex is available and does not have an owner.
2416 *
2417 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002418 *
2419 * @return N/A
2420 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002421extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002422
2423/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002424 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002426 * This routine locks @a mutex. If the mutex is locked by another thread,
2427 * the calling thread waits until the mutex becomes available or until
2428 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002430 * A thread is permitted to lock a mutex it has already locked. The operation
2431 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002432 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002433 * @param mutex Address of the mutex.
2434 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002435 * or one of the special values K_NO_WAIT and K_FOREVER.
2436 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002437 * @retval 0 Mutex locked.
2438 * @retval -EBUSY Returned without waiting.
2439 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002440 */
Kumar Galacc334c72017-04-21 10:55:34 -05002441extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002442
2443/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002444 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002446 * This routine unlocks @a mutex. The mutex must already be locked by the
2447 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002448 *
2449 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002450 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002451 * thread.
2452 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002453 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002454 *
2455 * @return N/A
2456 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002457extern void k_mutex_unlock(struct k_mutex *mutex);
2458
Allan Stephensc98da842016-11-11 15:45:03 -05002459/**
2460 * @} end defgroup mutex_apis
2461 */
2462
2463/**
2464 * @cond INTERNAL_HIDDEN
2465 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002466
2467struct k_sem {
2468 _wait_q_t wait_q;
2469 unsigned int count;
2470 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002471 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002472
Anas Nashif2f203c22016-12-18 06:57:45 -05002473 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002474};
2475
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002476#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002477 { \
2478 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2479 .count = initial_count, \
2480 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002481 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002482 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002483 }
2484
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002485#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2486
Allan Stephensc98da842016-11-11 15:45:03 -05002487/**
2488 * INTERNAL_HIDDEN @endcond
2489 */
2490
2491/**
2492 * @defgroup semaphore_apis Semaphore APIs
2493 * @ingroup kernel_apis
2494 * @{
2495 */
2496
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002497/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002500 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002502 * @param sem Address of the semaphore.
2503 * @param initial_count Initial semaphore count.
2504 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002505 *
2506 * @return N/A
2507 */
Andrew Boiefa94ee72017-09-28 16:54:35 -07002508__syscall static inline void k_sem_init(struct k_sem *sem,
2509 unsigned int initial_count,
2510 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002511
2512/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002513 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002515 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002517 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2518 *
2519 * @param sem Address of the semaphore.
2520 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002521 * or one of the special values K_NO_WAIT and K_FOREVER.
2522 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002523 * @note When porting code from the nanokernel legacy API to the new API, be
2524 * careful with the return value of this function. The return value is the
2525 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2526 * non-zero means failure, while the nano_sem_take family returns 1 for success
2527 * and 0 for failure.
2528 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002529 * @retval 0 Semaphore taken.
2530 * @retval -EBUSY Returned without waiting.
2531 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002532 */
Andrew Boiefa94ee72017-09-28 16:54:35 -07002533__syscall static inline int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002534
2535/**
2536 * @brief Give a semaphore.
2537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002538 * This routine gives @a sem, unless the semaphore is already at its maximum
2539 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002541 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002543 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002544 *
2545 * @return N/A
2546 */
Andrew Boiefa94ee72017-09-28 16:54:35 -07002547__syscall static inline void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002549/**
2550 * @brief Reset a semaphore's count to zero.
2551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002552 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002554 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002555 *
2556 * @return N/A
2557 */
Andrew Boiefa94ee72017-09-28 16:54:35 -07002558__syscall_inline static inline void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002559
2560static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002561{
2562 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002563}
2564
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002565/**
2566 * @brief Get a semaphore's count.
2567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002568 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002570 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002572 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002573 */
Andrew Boiefa94ee72017-09-28 16:54:35 -07002574__syscall_inline static inline unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002575
2576static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002577{
2578 return sem->count;
2579}
2580
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002581/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002582 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002584 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002585 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002586 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002588 * @param name Name of the semaphore.
2589 * @param initial_count Initial semaphore count.
2590 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002591 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002592#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002593 struct k_sem name \
2594 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002595 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002596
Allan Stephensc98da842016-11-11 15:45:03 -05002597/**
2598 * @} end defgroup semaphore_apis
2599 */
2600
2601/**
2602 * @defgroup alert_apis Alert APIs
2603 * @ingroup kernel_apis
2604 * @{
2605 */
2606
Allan Stephens5eceb852016-11-16 10:16:30 -05002607/**
2608 * @typedef k_alert_handler_t
2609 * @brief Alert handler function type.
2610 *
2611 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002612 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002613 * and is only invoked if the alert has been initialized with one.
2614 *
2615 * @param alert Address of the alert.
2616 *
2617 * @return 0 if alert has been consumed; non-zero if alert should pend.
2618 */
2619typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002620
2621/**
2622 * @} end defgroup alert_apis
2623 */
2624
2625/**
2626 * @cond INTERNAL_HIDDEN
2627 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002628
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002629#define K_ALERT_DEFAULT NULL
2630#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002631
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002632struct k_alert {
2633 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002634 atomic_t send_count;
2635 struct k_work work_item;
2636 struct k_sem sem;
2637
Anas Nashif2f203c22016-12-18 06:57:45 -05002638 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002639};
2640
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002641extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002642
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002643#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002644 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002645 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002646 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002647 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2648 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002649 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002650 }
2651
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002652#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2653
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002654/**
Allan Stephensc98da842016-11-11 15:45:03 -05002655 * INTERNAL_HIDDEN @endcond
2656 */
2657
2658/**
2659 * @addtogroup alert_apis
2660 * @{
2661 */
2662
2663/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002664 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002665 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002666 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002667 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002668 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002670 * @param name Name of the alert.
2671 * @param alert_handler Action to take when alert is sent. Specify either
2672 * the address of a function to be invoked by the system workqueue
2673 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2674 * K_ALERT_DEFAULT (which causes the alert to pend).
2675 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002676 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002677#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002678 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002679 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002680 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002681 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002683/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002684 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002686 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002688 * @param alert Address of the alert.
2689 * @param handler Action to take when alert is sent. Specify either the address
2690 * of a function to be invoked by the system workqueue thread,
2691 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2692 * K_ALERT_DEFAULT (which causes the alert to pend).
2693 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002694 *
2695 * @return N/A
2696 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002697extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2698 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002699
2700/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002701 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002703 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002705 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2706 *
2707 * @param alert Address of the alert.
2708 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002709 * or one of the special values K_NO_WAIT and K_FOREVER.
2710 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002711 * @retval 0 Alert received.
2712 * @retval -EBUSY Returned without waiting.
2713 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002714 */
Kumar Galacc334c72017-04-21 10:55:34 -05002715extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002716
2717/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002718 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002720 * This routine signals @a alert. The action specified for @a alert will
2721 * be taken, which may trigger the execution of an alert handler function
2722 * and/or cause the alert to pend (assuming the alert has not reached its
2723 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002725 * @note Can be called by ISRs.
2726 *
2727 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002728 *
2729 * @return N/A
2730 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002731extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002732
2733/**
Allan Stephensc98da842016-11-11 15:45:03 -05002734 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002735 */
2736
Allan Stephensc98da842016-11-11 15:45:03 -05002737/**
2738 * @cond INTERNAL_HIDDEN
2739 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002740
2741struct k_msgq {
2742 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002743 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002744 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002745 char *buffer_start;
2746 char *buffer_end;
2747 char *read_ptr;
2748 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002749 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750
Anas Nashif2f203c22016-12-18 06:57:45 -05002751 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002752};
2753
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002754#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002755 { \
2756 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002757 .max_msgs = q_max_msgs, \
2758 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002759 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002760 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002761 .read_ptr = q_buffer, \
2762 .write_ptr = q_buffer, \
2763 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002764 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765 }
2766
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002767#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2768
Peter Mitsis1da807e2016-10-06 11:36:59 -04002769/**
Allan Stephensc98da842016-11-11 15:45:03 -05002770 * INTERNAL_HIDDEN @endcond
2771 */
2772
2773/**
2774 * @defgroup msgq_apis Message Queue APIs
2775 * @ingroup kernel_apis
2776 * @{
2777 */
2778
2779/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002780 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002782 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2783 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002784 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2785 * message is similarly aligned to this boundary, @a q_msg_size must also be
2786 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002788 * The message queue can be accessed outside the module where it is defined
2789 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002790 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002791 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002792 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002793 * @param q_name Name of the message queue.
2794 * @param q_msg_size Message size (in bytes).
2795 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002796 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002797 */
2798#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2799 static char __noinit __aligned(q_align) \
2800 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002801 struct k_msgq q_name \
2802 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002803 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002804 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002805
Peter Mitsisd7a37502016-10-13 11:37:40 -04002806/**
2807 * @brief Initialize a message queue.
2808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002809 * This routine initializes a message queue object, prior to its first use.
2810 *
Allan Stephensda827222016-11-09 14:23:58 -06002811 * The message queue's ring buffer must contain space for @a max_msgs messages,
2812 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2813 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2814 * that each message is similarly aligned to this boundary, @a q_msg_size
2815 * must also be a multiple of N.
2816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002817 * @param q Address of the message queue.
2818 * @param buffer Pointer to ring buffer that holds queued messages.
2819 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002820 * @param max_msgs Maximum number of messages that can be queued.
2821 *
2822 * @return N/A
2823 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002824extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002825 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002826
2827/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002828 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002830 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002831 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002832 * @note Can be called by ISRs.
2833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002834 * @param q Address of the message queue.
2835 * @param data Pointer to the message.
2836 * @param timeout Waiting period to add the message (in milliseconds),
2837 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002838 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002839 * @retval 0 Message sent.
2840 * @retval -ENOMSG Returned without waiting or queue purged.
2841 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002842 */
Kumar Galacc334c72017-04-21 10:55:34 -05002843extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002844
2845/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002846 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002848 * This routine receives a message from message queue @a q in a "first in,
2849 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002850 *
Allan Stephensc98da842016-11-11 15:45:03 -05002851 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002853 * @param q Address of the message queue.
2854 * @param data Address of area to hold the received message.
2855 * @param timeout Waiting period to receive the message (in milliseconds),
2856 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002857 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002858 * @retval 0 Message received.
2859 * @retval -ENOMSG Returned without waiting.
2860 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002861 */
Kumar Galacc334c72017-04-21 10:55:34 -05002862extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002863
2864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * This routine discards all unreceived messages in a message queue's ring
2868 * buffer. Any threads that are blocked waiting to send a message to the
2869 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002871 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002872 *
2873 * @return N/A
2874 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002875extern void k_msgq_purge(struct k_msgq *q);
2876
Peter Mitsis67be2492016-10-07 11:44:34 -04002877/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002878 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002880 * This routine returns the number of unused entries in a message queue's
2881 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * @param q Address of the message queue.
2884 *
2885 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002886 */
Kumar Galacc334c72017-04-21 10:55:34 -05002887static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002888{
2889 return q->max_msgs - q->used_msgs;
2890}
2891
Peter Mitsisd7a37502016-10-13 11:37:40 -04002892/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002893 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002895 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002897 * @param q Address of the message queue.
2898 *
2899 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002900 */
Kumar Galacc334c72017-04-21 10:55:34 -05002901static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002902{
2903 return q->used_msgs;
2904}
2905
Allan Stephensc98da842016-11-11 15:45:03 -05002906/**
2907 * @} end defgroup msgq_apis
2908 */
2909
2910/**
2911 * @defgroup mem_pool_apis Memory Pool APIs
2912 * @ingroup kernel_apis
2913 * @{
2914 */
2915
Andy Ross73cb9582017-05-09 10:42:39 -07002916/* Note on sizing: the use of a 20 bit field for block means that,
2917 * assuming a reasonable minimum block size of 16 bytes, we're limited
2918 * to 16M of memory managed by a single pool. Long term it would be
2919 * good to move to a variable bit size based on configuration.
2920 */
2921struct k_mem_block_id {
2922 u32_t pool : 8;
2923 u32_t level : 4;
2924 u32_t block : 20;
2925};
2926
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002927struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002928 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002929 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002930};
2931
Allan Stephensc98da842016-11-11 15:45:03 -05002932/**
2933 * @} end defgroup mem_pool_apis
2934 */
2935
2936/**
2937 * @defgroup mailbox_apis Mailbox APIs
2938 * @ingroup kernel_apis
2939 * @{
2940 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002941
2942struct k_mbox_msg {
2943 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002944 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002945 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002946 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002947 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002948 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949 /** sender's message data buffer */
2950 void *tx_data;
2951 /** internal use only - needed for legacy API support */
2952 void *_rx_data;
2953 /** message data block descriptor */
2954 struct k_mem_block tx_block;
2955 /** source thread id */
2956 k_tid_t rx_source_thread;
2957 /** target thread id */
2958 k_tid_t tx_target_thread;
2959 /** internal use only - thread waiting on send (may be a dummy) */
2960 k_tid_t _syncing_thread;
2961#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2962 /** internal use only - semaphore used during asynchronous send */
2963 struct k_sem *_async_sem;
2964#endif
2965};
2966
Allan Stephensc98da842016-11-11 15:45:03 -05002967/**
2968 * @cond INTERNAL_HIDDEN
2969 */
2970
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002971struct k_mbox {
2972 _wait_q_t tx_msg_queue;
2973 _wait_q_t rx_msg_queue;
2974
Anas Nashif2f203c22016-12-18 06:57:45 -05002975 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002976};
2977
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002978#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002979 { \
2980 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2981 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002982 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002983 }
2984
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002985#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2986
Peter Mitsis12092702016-10-14 12:57:23 -04002987/**
Allan Stephensc98da842016-11-11 15:45:03 -05002988 * INTERNAL_HIDDEN @endcond
2989 */
2990
2991/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002992 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002994 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002995 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002996 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002998 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002999 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003000#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003001 struct k_mbox name \
3002 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003003 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003004
Peter Mitsis12092702016-10-14 12:57:23 -04003005/**
3006 * @brief Initialize a mailbox.
3007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003008 * This routine initializes a mailbox object, prior to its first use.
3009 *
3010 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003011 *
3012 * @return N/A
3013 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003014extern void k_mbox_init(struct k_mbox *mbox);
3015
Peter Mitsis12092702016-10-14 12:57:23 -04003016/**
3017 * @brief Send a mailbox message in a synchronous manner.
3018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003019 * This routine sends a message to @a mbox and waits for a receiver to both
3020 * receive and process it. The message data may be in a buffer, in a memory
3021 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003023 * @param mbox Address of the mailbox.
3024 * @param tx_msg Address of the transmit message descriptor.
3025 * @param timeout Waiting period for the message to be received (in
3026 * milliseconds), or one of the special values K_NO_WAIT
3027 * and K_FOREVER. Once the message has been received,
3028 * this routine waits as long as necessary for the message
3029 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003030 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003031 * @retval 0 Message sent.
3032 * @retval -ENOMSG Returned without waiting.
3033 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003034 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003035extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003036 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003037
Peter Mitsis12092702016-10-14 12:57:23 -04003038/**
3039 * @brief Send a mailbox message in an asynchronous manner.
3040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003041 * This routine sends a message to @a mbox without waiting for a receiver
3042 * to process it. The message data may be in a buffer, in a memory pool block,
3043 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3044 * will be given when the message has been both received and completely
3045 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003047 * @param mbox Address of the mailbox.
3048 * @param tx_msg Address of the transmit message descriptor.
3049 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003050 *
3051 * @return N/A
3052 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003053extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003054 struct k_sem *sem);
3055
Peter Mitsis12092702016-10-14 12:57:23 -04003056/**
3057 * @brief Receive a mailbox message.
3058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003059 * This routine receives a message from @a mbox, then optionally retrieves
3060 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003061 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003062 * @param mbox Address of the mailbox.
3063 * @param rx_msg Address of the receive message descriptor.
3064 * @param buffer Address of the buffer to receive data, or NULL to defer data
3065 * retrieval and message disposal until later.
3066 * @param timeout Waiting period for a message to be received (in
3067 * milliseconds), or one of the special values K_NO_WAIT
3068 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003069 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003070 * @retval 0 Message received.
3071 * @retval -ENOMSG Returned without waiting.
3072 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003073 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003074extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003075 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003076
3077/**
3078 * @brief Retrieve mailbox message data into a buffer.
3079 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003080 * This routine completes the processing of a received message by retrieving
3081 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003082 *
3083 * Alternatively, this routine can be used to dispose of a received message
3084 * without retrieving its data.
3085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003086 * @param rx_msg Address of the receive message descriptor.
3087 * @param buffer Address of the buffer to receive data, or NULL to discard
3088 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003089 *
3090 * @return N/A
3091 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003092extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003093
3094/**
3095 * @brief Retrieve mailbox message data into a memory pool block.
3096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003097 * This routine completes the processing of a received message by retrieving
3098 * its data into a memory pool block, then disposing of the message.
3099 * The memory pool block that results from successful retrieval must be
3100 * returned to the pool once the data has been processed, even in cases
3101 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003102 *
3103 * Alternatively, this routine can be used to dispose of a received message
3104 * without retrieving its data. In this case there is no need to return a
3105 * memory pool block to the pool.
3106 *
3107 * This routine allocates a new memory pool block for the data only if the
3108 * data is not already in one. If a new block cannot be allocated, the routine
3109 * returns a failure code and the received message is left unchanged. This
3110 * permits the caller to reattempt data retrieval at a later time or to dispose
3111 * of the received message without retrieving its data.
3112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003113 * @param rx_msg Address of a receive message descriptor.
3114 * @param pool Address of memory pool, or NULL to discard data.
3115 * @param block Address of the area to hold memory pool block info.
3116 * @param timeout Waiting period to wait for a memory pool block (in
3117 * milliseconds), or one of the special values K_NO_WAIT
3118 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003119 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003120 * @retval 0 Data retrieved.
3121 * @retval -ENOMEM Returned without waiting.
3122 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003123 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003124extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003125 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003126 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003127
Allan Stephensc98da842016-11-11 15:45:03 -05003128/**
3129 * @} end defgroup mailbox_apis
3130 */
3131
3132/**
3133 * @cond INTERNAL_HIDDEN
3134 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003135
3136struct k_pipe {
3137 unsigned char *buffer; /* Pipe buffer: may be NULL */
3138 size_t size; /* Buffer size */
3139 size_t bytes_used; /* # bytes used in buffer */
3140 size_t read_index; /* Where in buffer to read from */
3141 size_t write_index; /* Where in buffer to write */
3142
3143 struct {
3144 _wait_q_t readers; /* Reader wait queue */
3145 _wait_q_t writers; /* Writer wait queue */
3146 } wait_q;
3147
Anas Nashif2f203c22016-12-18 06:57:45 -05003148 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003149};
3150
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003151#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003152 { \
3153 .buffer = pipe_buffer, \
3154 .size = pipe_buffer_size, \
3155 .bytes_used = 0, \
3156 .read_index = 0, \
3157 .write_index = 0, \
3158 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3159 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003160 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003161 }
3162
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003163#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3164
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003165/**
Allan Stephensc98da842016-11-11 15:45:03 -05003166 * INTERNAL_HIDDEN @endcond
3167 */
3168
3169/**
3170 * @defgroup pipe_apis Pipe APIs
3171 * @ingroup kernel_apis
3172 * @{
3173 */
3174
3175/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003176 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003178 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003179 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003180 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003182 * @param name Name of the pipe.
3183 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3184 * or zero if no ring buffer is used.
3185 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003186 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003187#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3188 static unsigned char __noinit __aligned(pipe_align) \
3189 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003190 struct k_pipe name \
3191 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003192 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003193
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003194/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003195 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003197 * This routine initializes a pipe object, prior to its first use.
3198 *
3199 * @param pipe Address of the pipe.
3200 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3201 * is used.
3202 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3203 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003204 *
3205 * @return N/A
3206 */
3207extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3208 size_t size);
3209
3210/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003211 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003212 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003213 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003215 * @param pipe Address of the pipe.
3216 * @param data Address of data to write.
3217 * @param bytes_to_write Size of data (in bytes).
3218 * @param bytes_written Address of area to hold the number of bytes written.
3219 * @param min_xfer Minimum number of bytes to write.
3220 * @param timeout Waiting period to wait for the data to be written (in
3221 * milliseconds), or one of the special values K_NO_WAIT
3222 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003223 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003224 * @retval 0 At least @a min_xfer bytes of data were written.
3225 * @retval -EIO Returned without waiting; zero data bytes were written.
3226 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003227 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003228 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003229extern int k_pipe_put(struct k_pipe *pipe, void *data,
3230 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003231 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003232
3233/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003234 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003236 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003237 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003238 * @param pipe Address of the pipe.
3239 * @param data Address to place the data read from pipe.
3240 * @param bytes_to_read Maximum number of data bytes to read.
3241 * @param bytes_read Address of area to hold the number of bytes read.
3242 * @param min_xfer Minimum number of data bytes to read.
3243 * @param timeout Waiting period to wait for the data to be read (in
3244 * milliseconds), or one of the special values K_NO_WAIT
3245 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003247 * @retval 0 At least @a min_xfer bytes of data were read.
3248 * @retval -EIO Returned without waiting; zero data bytes were read.
3249 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003250 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003252extern int k_pipe_get(struct k_pipe *pipe, void *data,
3253 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003254 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003255
3256/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003257 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003258 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003259 * This routine writes the data contained in a memory block to @a pipe.
3260 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003261 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003263 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003264 * @param block Memory block containing data to send
3265 * @param size Number of data bytes in memory block to send
3266 * @param sem Semaphore to signal upon completion (else NULL)
3267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003268 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003269 */
3270extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3271 size_t size, struct k_sem *sem);
3272
3273/**
Allan Stephensc98da842016-11-11 15:45:03 -05003274 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003275 */
3276
Allan Stephensc98da842016-11-11 15:45:03 -05003277/**
3278 * @cond INTERNAL_HIDDEN
3279 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003280
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003281struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003282 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003283 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003284 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285 char *buffer;
3286 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003287 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003288
Anas Nashif2f203c22016-12-18 06:57:45 -05003289 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003290};
3291
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003292#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003293 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003294 { \
3295 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003296 .num_blocks = slab_num_blocks, \
3297 .block_size = slab_block_size, \
3298 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003299 .free_list = NULL, \
3300 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003301 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003302 }
3303
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003304#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3305
3306
Peter Mitsis578f9112016-10-07 13:50:31 -04003307/**
Allan Stephensc98da842016-11-11 15:45:03 -05003308 * INTERNAL_HIDDEN @endcond
3309 */
3310
3311/**
3312 * @defgroup mem_slab_apis Memory Slab APIs
3313 * @ingroup kernel_apis
3314 * @{
3315 */
3316
3317/**
Allan Stephensda827222016-11-09 14:23:58 -06003318 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003319 *
Allan Stephensda827222016-11-09 14:23:58 -06003320 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003321 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003322 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3323 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003324 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003325 *
Allan Stephensda827222016-11-09 14:23:58 -06003326 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003327 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003328 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003329 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003331 * @param name Name of the memory slab.
3332 * @param slab_block_size Size of each memory block (in bytes).
3333 * @param slab_num_blocks Number memory blocks.
3334 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003335 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003336#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3337 char __noinit __aligned(slab_align) \
3338 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3339 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003340 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003341 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003342 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003343
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003344/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003345 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003347 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003348 *
Allan Stephensda827222016-11-09 14:23:58 -06003349 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3350 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3351 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3352 * To ensure that each memory block is similarly aligned to this boundary,
3353 * @a slab_block_size must also be a multiple of N.
3354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * @param slab Address of the memory slab.
3356 * @param buffer Pointer to buffer used for the memory blocks.
3357 * @param block_size Size of each memory block (in bytes).
3358 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003359 *
3360 * @return N/A
3361 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003362extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003363 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003364
3365/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003366 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003368 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003369 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003370 * @param slab Address of the memory slab.
3371 * @param mem Pointer to block address area.
3372 * @param timeout Maximum time to wait for operation to complete
3373 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3374 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003375 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003376 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003377 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003378 * @retval -ENOMEM Returned without waiting.
3379 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003380 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003381extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003382 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003383
3384/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003385 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * This routine releases a previously allocated memory block back to its
3388 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * @param slab Address of the memory slab.
3391 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003392 *
3393 * @return N/A
3394 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003395extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003396
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003397/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003398 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003400 * This routine gets the number of memory blocks that are currently
3401 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003405 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003406 */
Kumar Galacc334c72017-04-21 10:55:34 -05003407static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003408{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003409 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003410}
3411
Peter Mitsisc001aa82016-10-13 13:53:37 -04003412/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003415 * This routine gets the number of memory blocks that are currently
3416 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003418 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003420 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003421 */
Kumar Galacc334c72017-04-21 10:55:34 -05003422static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003423{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003424 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003425}
3426
Allan Stephensc98da842016-11-11 15:45:03 -05003427/**
3428 * @} end defgroup mem_slab_apis
3429 */
3430
3431/**
3432 * @cond INTERNAL_HIDDEN
3433 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003434
Andy Ross73cb9582017-05-09 10:42:39 -07003435struct k_mem_pool_lvl {
3436 union {
3437 u32_t *bits_p;
3438 u32_t bits;
3439 };
3440 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003441};
3442
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003443struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003444 void *buf;
3445 size_t max_sz;
3446 u16_t n_max;
3447 u8_t n_levels;
3448 u8_t max_inline_level;
3449 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003450 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003451};
3452
Andy Ross73cb9582017-05-09 10:42:39 -07003453#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003454
Andy Ross73cb9582017-05-09 10:42:39 -07003455#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3456
3457#define _MPOOL_LVLS(maxsz, minsz) \
3458 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3459 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3460 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3461 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3462 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3463 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3464 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3465 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3466 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3467 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3468 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3469 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3470 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3471 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3472 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3473 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3474
3475/* Rounds the needed bits up to integer multiples of u32_t */
3476#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3477 ((((n_max) << (2*(l))) + 31) / 32)
3478
3479/* One word gets stored free unioned with the pointer, otherwise the
3480 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003481 */
Andy Ross73cb9582017-05-09 10:42:39 -07003482#define _MPOOL_LBIT_WORDS(n_max, l) \
3483 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3484 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003485
Andy Ross73cb9582017-05-09 10:42:39 -07003486/* How many bytes for the bitfields of a single level? */
3487#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3488 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3489 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003490
Andy Ross73cb9582017-05-09 10:42:39 -07003491/* Size of the bitmap array that follows the buffer in allocated memory */
3492#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3493 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3494 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3495 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3496 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3497 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3498 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3499 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3500 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3501 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3502 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3503 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3504 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3505 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3506 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3507 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3508 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003509
3510/**
Allan Stephensc98da842016-11-11 15:45:03 -05003511 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003512 */
3513
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003514/**
Allan Stephensc98da842016-11-11 15:45:03 -05003515 * @addtogroup mem_pool_apis
3516 * @{
3517 */
3518
3519/**
3520 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003522 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3523 * long. The memory pool allows blocks to be repeatedly partitioned into
3524 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003525 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003526 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003527 * If the pool is to be accessed outside the module where it is defined, it
3528 * can be declared via
3529 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003530 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003532 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003533 * @param minsz Size of the smallest blocks in the pool (in bytes).
3534 * @param maxsz Size of the largest blocks in the pool (in bytes).
3535 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003536 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003537 */
Andy Ross73cb9582017-05-09 10:42:39 -07003538#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3539 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3540 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3541 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3542 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3543 .buf = _mpool_buf_##name, \
3544 .max_sz = maxsz, \
3545 .n_max = nmax, \
3546 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3547 .levels = _mpool_lvls_##name, \
3548 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003549
Peter Mitsis937042c2016-10-13 13:18:26 -04003550/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003551 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003553 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003555 * @param pool Address of the memory pool.
3556 * @param block Pointer to block descriptor for the allocated memory.
3557 * @param size Amount of memory to allocate (in bytes).
3558 * @param timeout Maximum time to wait for operation to complete
3559 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3560 * or K_FOREVER to wait as long as necessary.
3561 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003562 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003563 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003564 * @retval -ENOMEM Returned without waiting.
3565 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003566 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003567extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003568 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003569
3570/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003571 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003573 * This routine releases a previously allocated memory block back to its
3574 * memory pool.
3575 *
3576 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003577 *
3578 * @return N/A
3579 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003580extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003581
3582/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003583 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003584 *
Andy Ross73cb9582017-05-09 10:42:39 -07003585 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003586 *
Andy Ross73cb9582017-05-09 10:42:39 -07003587 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003588 *
3589 * @return N/A
3590 */
Andy Ross73cb9582017-05-09 10:42:39 -07003591static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003592
3593/**
Allan Stephensc98da842016-11-11 15:45:03 -05003594 * @} end addtogroup mem_pool_apis
3595 */
3596
3597/**
3598 * @defgroup heap_apis Heap Memory Pool APIs
3599 * @ingroup kernel_apis
3600 * @{
3601 */
3602
3603/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003604 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003606 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003607 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003609 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003611 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003612 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003613extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003614
3615/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003616 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003617 *
3618 * This routine provides traditional free() semantics. The memory being
3619 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003620 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003621 * If @a ptr is NULL, no operation is performed.
3622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003623 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003624 *
3625 * @return N/A
3626 */
3627extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003628
Allan Stephensc98da842016-11-11 15:45:03 -05003629/**
3630 * @} end defgroup heap_apis
3631 */
3632
Benjamin Walshacc68c12017-01-29 18:57:45 -05003633/* polling API - PRIVATE */
3634
Benjamin Walshb0179862017-02-02 16:39:57 -05003635#ifdef CONFIG_POLL
3636#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3637#else
3638#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3639#endif
3640
Benjamin Walshacc68c12017-01-29 18:57:45 -05003641/* private - implementation data created as needed, per-type */
3642struct _poller {
3643 struct k_thread *thread;
3644};
3645
3646/* private - types bit positions */
3647enum _poll_types_bits {
3648 /* can be used to ignore an event */
3649 _POLL_TYPE_IGNORE,
3650
3651 /* to be signaled by k_poll_signal() */
3652 _POLL_TYPE_SIGNAL,
3653
3654 /* semaphore availability */
3655 _POLL_TYPE_SEM_AVAILABLE,
3656
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003657 /* queue/fifo/lifo data availability */
3658 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003659
3660 _POLL_NUM_TYPES
3661};
3662
3663#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3664
3665/* private - states bit positions */
3666enum _poll_states_bits {
3667 /* default state when creating event */
3668 _POLL_STATE_NOT_READY,
3669
Benjamin Walshacc68c12017-01-29 18:57:45 -05003670 /* signaled by k_poll_signal() */
3671 _POLL_STATE_SIGNALED,
3672
3673 /* semaphore is available */
3674 _POLL_STATE_SEM_AVAILABLE,
3675
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003676 /* data is available to read on queue/fifo/lifo */
3677 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003678
3679 _POLL_NUM_STATES
3680};
3681
3682#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3683
3684#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003685 (32 - (0 \
3686 + 8 /* tag */ \
3687 + _POLL_NUM_TYPES \
3688 + _POLL_NUM_STATES \
3689 + 1 /* modes */ \
3690 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003691
3692#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3693#error overflow of 32-bit word in struct k_poll_event
3694#endif
3695
3696/* end of polling API - PRIVATE */
3697
3698
3699/**
3700 * @defgroup poll_apis Async polling APIs
3701 * @ingroup kernel_apis
3702 * @{
3703 */
3704
3705/* Public polling API */
3706
3707/* public - values for k_poll_event.type bitfield */
3708#define K_POLL_TYPE_IGNORE 0
3709#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3710#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003711#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3712#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003713
3714/* public - polling modes */
3715enum k_poll_modes {
3716 /* polling thread does not take ownership of objects when available */
3717 K_POLL_MODE_NOTIFY_ONLY = 0,
3718
3719 K_POLL_NUM_MODES
3720};
3721
3722/* public - values for k_poll_event.state bitfield */
3723#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003724#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3725#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003726#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3727#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003728
3729/* public - poll signal object */
3730struct k_poll_signal {
3731 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003732 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003733
3734 /*
3735 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3736 * user resets it to 0.
3737 */
3738 unsigned int signaled;
3739
3740 /* custom result value passed to k_poll_signal() if needed */
3741 int result;
3742};
3743
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003744#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003745 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003746 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003747 .signaled = 0, \
3748 .result = 0, \
3749 }
3750
3751struct k_poll_event {
3752 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003753 sys_dnode_t _node;
3754
3755 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003756 struct _poller *poller;
3757
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003758 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003759 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003760
Benjamin Walshacc68c12017-01-29 18:57:45 -05003761 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003762 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003763
3764 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003765 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003766
3767 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003768 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003769
3770 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003771 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003772
3773 /* per-type data */
3774 union {
3775 void *obj;
3776 struct k_poll_signal *signal;
3777 struct k_sem *sem;
3778 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003779 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003780 };
3781};
3782
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003783#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003784 { \
3785 .poller = NULL, \
3786 .type = event_type, \
3787 .state = K_POLL_STATE_NOT_READY, \
3788 .mode = event_mode, \
3789 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003790 { .obj = event_obj }, \
3791 }
3792
3793#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3794 event_tag) \
3795 { \
3796 .type = event_type, \
3797 .tag = event_tag, \
3798 .state = K_POLL_STATE_NOT_READY, \
3799 .mode = event_mode, \
3800 .unused = 0, \
3801 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003802 }
3803
3804/**
3805 * @brief Initialize one struct k_poll_event instance
3806 *
3807 * After this routine is called on a poll event, the event it ready to be
3808 * placed in an event array to be passed to k_poll().
3809 *
3810 * @param event The event to initialize.
3811 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3812 * values. Only values that apply to the same object being polled
3813 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3814 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003815 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003816 * @param obj Kernel object or poll signal.
3817 *
3818 * @return N/A
3819 */
3820
Kumar Galacc334c72017-04-21 10:55:34 -05003821extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003822 int mode, void *obj);
3823
3824/**
3825 * @brief Wait for one or many of multiple poll events to occur
3826 *
3827 * This routine allows a thread to wait concurrently for one or many of
3828 * multiple poll events to have occurred. Such events can be a kernel object
3829 * being available, like a semaphore, or a poll signal event.
3830 *
3831 * When an event notifies that a kernel object is available, the kernel object
3832 * is not "given" to the thread calling k_poll(): it merely signals the fact
3833 * that the object was available when the k_poll() call was in effect. Also,
3834 * all threads trying to acquire an object the regular way, i.e. by pending on
3835 * the object, have precedence over the thread polling on the object. This
3836 * means that the polling thread will never get the poll event on an object
3837 * until the object becomes available and its pend queue is empty. For this
3838 * reason, the k_poll() call is more effective when the objects being polled
3839 * only have one thread, the polling thread, trying to acquire them.
3840 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003841 * When k_poll() returns 0, the caller should loop on all the events that were
3842 * passed to k_poll() and check the state field for the values that were
3843 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003844 *
3845 * Before being reused for another call to k_poll(), the user has to reset the
3846 * state field to K_POLL_STATE_NOT_READY.
3847 *
3848 * @param events An array of pointers to events to be polled for.
3849 * @param num_events The number of events in the array.
3850 * @param timeout Waiting period for an event to be ready (in milliseconds),
3851 * or one of the special values K_NO_WAIT and K_FOREVER.
3852 *
3853 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003854 * @retval -EAGAIN Waiting period timed out.
3855 */
3856
3857extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003858 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003859
3860/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003861 * @brief Initialize a poll signal object.
3862 *
3863 * Ready a poll signal object to be signaled via k_poll_signal().
3864 *
3865 * @param signal A poll signal.
3866 *
3867 * @return N/A
3868 */
3869
3870extern void k_poll_signal_init(struct k_poll_signal *signal);
3871
3872/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003873 * @brief Signal a poll signal object.
3874 *
3875 * This routine makes ready a poll signal, which is basically a poll event of
3876 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3877 * made ready to run. A @a result value can be specified.
3878 *
3879 * The poll signal contains a 'signaled' field that, when set by
3880 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3881 * be reset by the user before being passed again to k_poll() or k_poll() will
3882 * consider it being signaled, and will return immediately.
3883 *
3884 * @param signal A poll signal.
3885 * @param result The value to store in the result field of the signal.
3886 *
3887 * @retval 0 The signal was delivered successfully.
3888 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3889 */
3890
3891extern int k_poll_signal(struct k_poll_signal *signal, int result);
3892
3893/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003894extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003895
3896/**
3897 * @} end defgroup poll_apis
3898 */
3899
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003900/**
3901 * @brief Make the CPU idle.
3902 *
3903 * This function makes the CPU idle until an event wakes it up.
3904 *
3905 * In a regular system, the idle thread should be the only thread responsible
3906 * for making the CPU idle and triggering any type of power management.
3907 * However, in some more constrained systems, such as a single-threaded system,
3908 * the only thread would be responsible for this if needed.
3909 *
3910 * @return N/A
3911 */
3912extern void k_cpu_idle(void);
3913
3914/**
3915 * @brief Make the CPU idle in an atomic fashion.
3916 *
3917 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3918 * must be done atomically before making the CPU idle.
3919 *
3920 * @param key Interrupt locking key obtained from irq_lock().
3921 *
3922 * @return N/A
3923 */
3924extern void k_cpu_atomic_idle(unsigned int key);
3925
Kumar Galacc334c72017-04-21 10:55:34 -05003926extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003927
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003928#include <arch/cpu.h>
3929
Andrew Boiecdb94d62017-04-18 15:22:05 -07003930#ifdef _ARCH_EXCEPT
3931/* This archtecture has direct support for triggering a CPU exception */
3932#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3933#else
3934
3935#include <misc/printk.h>
3936
3937/* NOTE: This is the implementation for arches that do not implement
3938 * _ARCH_EXCEPT() to generate a real CPU exception.
3939 *
3940 * We won't have a real exception frame to determine the PC value when
3941 * the oops occurred, so print file and line number before we jump into
3942 * the fatal error handler.
3943 */
3944#define _k_except_reason(reason) do { \
3945 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3946 _NanoFatalErrorHandler(reason, &_default_esf); \
3947 CODE_UNREACHABLE; \
3948 } while (0)
3949
3950#endif /* _ARCH__EXCEPT */
3951
3952/**
3953 * @brief Fatally terminate a thread
3954 *
3955 * This should be called when a thread has encountered an unrecoverable
3956 * runtime condition and needs to terminate. What this ultimately
3957 * means is determined by the _fatal_error_handler() implementation, which
3958 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3959 *
3960 * If this is called from ISR context, the default system fatal error handler
3961 * will treat it as an unrecoverable system error, just like k_panic().
3962 */
3963#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3964
3965/**
3966 * @brief Fatally terminate the system
3967 *
3968 * This should be called when the Zephyr kernel has encountered an
3969 * unrecoverable runtime condition and needs to terminate. What this ultimately
3970 * means is determined by the _fatal_error_handler() implementation, which
3971 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3972 */
3973#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3974
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003975/*
3976 * private APIs that are utilized by one or more public APIs
3977 */
3978
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003979#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003980extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003981#else
3982#define _init_static_threads() do { } while ((0))
3983#endif
3984
3985extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003986extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003987
Andrew Boiedc5d9352017-06-02 12:56:47 -07003988/* arch/cpu.h may declare an architecture or platform-specific macro
3989 * for properly declaring stacks, compatible with MMU/MPU constraints if
3990 * enabled
3991 */
3992#ifdef _ARCH_THREAD_STACK_DEFINE
3993#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3994#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3995 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3996#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3997#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07003998static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3999{
4000 return _ARCH_THREAD_STACK_BUFFER(sym);
4001}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004002#else
4003/**
4004 * @brief Declare a toplevel thread stack memory region
4005 *
4006 * This declares a region of memory suitable for use as a thread's stack.
4007 *
4008 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4009 * 'noinit' section so that it isn't zeroed at boot
4010 *
Andrew Boie507852a2017-07-25 18:47:07 -07004011 * The declared symbol will always be a k_thread_stack_t which can be passed to
4012 * k_thread_create, but should otherwise not be manipulated. If the buffer
4013 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004014 *
4015 * It is legal to precede this definition with the 'static' keyword.
4016 *
4017 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4018 * parameter of k_thread_create(), it may not be the same as the
4019 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4020 *
4021 * @param sym Thread stack symbol name
4022 * @param size Size of the stack memory region
4023 */
4024#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004025 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004026
4027/**
4028 * @brief Declare a toplevel array of thread stack memory regions
4029 *
4030 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4031 * definition for additional details and constraints.
4032 *
4033 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4034 * 'noinit' section so that it isn't zeroed at boot
4035 *
4036 * @param sym Thread stack symbol name
4037 * @param nmemb Number of stacks to declare
4038 * @param size Size of the stack memory region
4039 */
4040
4041#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004042 struct _k_thread_stack_element __noinit \
4043 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004044
4045/**
4046 * @brief Declare an embedded stack memory region
4047 *
4048 * Used for stacks embedded within other data structures. Use is highly
4049 * discouraged but in some cases necessary. For memory protection scenarios,
4050 * it is very important that any RAM preceding this member not be writable
4051 * by threads else a stack overflow will lead to silent corruption. In other
4052 * words, the containing data structure should live in RAM owned by the kernel.
4053 *
4054 * @param sym Thread stack symbol name
4055 * @param size Size of the stack memory region
4056 */
4057#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004058 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004059
4060/**
4061 * @brief Return the size in bytes of a stack memory region
4062 *
4063 * Convenience macro for passing the desired stack size to k_thread_create()
4064 * since the underlying implementation may actually create something larger
4065 * (for instance a guard area).
4066 *
4067 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004068 * passed to K_THREAD_STACK_DEFINE.
4069 *
4070 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4071 * it is not guaranteed to return the original value since each array
4072 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004073 *
4074 * @param sym Stack memory symbol
4075 * @return Size of the stack
4076 */
4077#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4078
4079/**
4080 * @brief Get a pointer to the physical stack buffer
4081 *
4082 * Convenience macro to get at the real underlying stack buffer used by
4083 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4084 * This is really only intended for diagnostic tools which want to examine
4085 * stack memory contents.
4086 *
4087 * @param sym Declared stack symbol name
4088 * @return The buffer itself, a char *
4089 */
Andrew Boie507852a2017-07-25 18:47:07 -07004090static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4091{
4092 return (char *)sym;
4093}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004094
4095#endif /* _ARCH_DECLARE_STACK */
4096
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004097#ifdef __cplusplus
4098}
4099#endif
4100
Andrew Boiee004dec2016-11-07 09:01:19 -08004101#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4102/*
4103 * Define new and delete operators.
4104 * At this moment, the operators do nothing since objects are supposed
4105 * to be statically allocated.
4106 */
4107inline void operator delete(void *ptr)
4108{
4109 (void)ptr;
4110}
4111
4112inline void operator delete[](void *ptr)
4113{
4114 (void)ptr;
4115}
4116
4117inline void *operator new(size_t size)
4118{
4119 (void)size;
4120 return NULL;
4121}
4122
4123inline void *operator new[](size_t size)
4124{
4125 (void)size;
4126 return NULL;
4127}
4128
4129/* Placement versions of operator new and delete */
4130inline void operator delete(void *ptr1, void *ptr2)
4131{
4132 (void)ptr1;
4133 (void)ptr2;
4134}
4135
4136inline void operator delete[](void *ptr1, void *ptr2)
4137{
4138 (void)ptr1;
4139 (void)ptr2;
4140}
4141
4142inline void *operator new(size_t size, void *ptr)
4143{
4144 (void)size;
4145 return ptr;
4146}
4147
4148inline void *operator new[](size_t size, void *ptr)
4149{
4150 (void)size;
4151 return ptr;
4152}
4153
4154#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4155
Andrew Boiefa94ee72017-09-28 16:54:35 -07004156#include <syscalls/kernel.h>
4157
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004158#endif /* !_ASMLANGUAGE */
4159
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004160#endif /* _kernel__h_ */