blob: c400ab1bc0db3ab7e523dd0925e002a00ae40bcd [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 Boie99280232017-09-29 14:17:47 -07002508__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2509 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002510
2511/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002512 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002514 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002516 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2517 *
2518 * @param sem Address of the semaphore.
2519 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002520 * or one of the special values K_NO_WAIT and K_FOREVER.
2521 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002522 * @note When porting code from the nanokernel legacy API to the new API, be
2523 * careful with the return value of this function. The return value is the
2524 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2525 * non-zero means failure, while the nano_sem_take family returns 1 for success
2526 * and 0 for failure.
2527 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002528 * @retval 0 Semaphore taken.
2529 * @retval -EBUSY Returned without waiting.
2530 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002531 */
Andrew Boie99280232017-09-29 14:17:47 -07002532__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002533
2534/**
2535 * @brief Give a semaphore.
2536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * This routine gives @a sem, unless the semaphore is already at its maximum
2538 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002542 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002543 *
2544 * @return N/A
2545 */
Andrew Boie99280232017-09-29 14:17:47 -07002546__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002547
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002548/**
2549 * @brief Reset a semaphore's count to zero.
2550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002551 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002553 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002554 *
2555 * @return N/A
2556 */
Andrew Boie99280232017-09-29 14:17:47 -07002557__syscall_inline void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002558
2559static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002560{
2561 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562}
2563
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002564/**
2565 * @brief Get a semaphore's count.
2566 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002567 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002569 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002570 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002571 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002572 */
Andrew Boie99280232017-09-29 14:17:47 -07002573__syscall_inline unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002574
2575static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002576{
2577 return sem->count;
2578}
2579
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002580/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002581 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002583 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002584 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002585 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002586 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002587 * @param name Name of the semaphore.
2588 * @param initial_count Initial semaphore count.
2589 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002590 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002591#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002592 struct k_sem name \
2593 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002594 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595
Allan Stephensc98da842016-11-11 15:45:03 -05002596/**
2597 * @} end defgroup semaphore_apis
2598 */
2599
2600/**
2601 * @defgroup alert_apis Alert APIs
2602 * @ingroup kernel_apis
2603 * @{
2604 */
2605
Allan Stephens5eceb852016-11-16 10:16:30 -05002606/**
2607 * @typedef k_alert_handler_t
2608 * @brief Alert handler function type.
2609 *
2610 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002611 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002612 * and is only invoked if the alert has been initialized with one.
2613 *
2614 * @param alert Address of the alert.
2615 *
2616 * @return 0 if alert has been consumed; non-zero if alert should pend.
2617 */
2618typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002619
2620/**
2621 * @} end defgroup alert_apis
2622 */
2623
2624/**
2625 * @cond INTERNAL_HIDDEN
2626 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002627
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002628#define K_ALERT_DEFAULT NULL
2629#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002630
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002631struct k_alert {
2632 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002633 atomic_t send_count;
2634 struct k_work work_item;
2635 struct k_sem sem;
2636
Anas Nashif2f203c22016-12-18 06:57:45 -05002637 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002638};
2639
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002640extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002641
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002642#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002643 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002644 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002645 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002646 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2647 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002648 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002649 }
2650
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002651#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2652
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002653/**
Allan Stephensc98da842016-11-11 15:45:03 -05002654 * INTERNAL_HIDDEN @endcond
2655 */
2656
2657/**
2658 * @addtogroup alert_apis
2659 * @{
2660 */
2661
2662/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002663 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002664 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002665 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002667 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002669 * @param name Name of the alert.
2670 * @param alert_handler Action to take when alert is sent. Specify either
2671 * the address of a function to be invoked by the system workqueue
2672 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2673 * K_ALERT_DEFAULT (which causes the alert to pend).
2674 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002675 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002676#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002677 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002678 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002679 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002680 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002681
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002683 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002685 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002687 * @param alert Address of the alert.
2688 * @param handler Action to take when alert is sent. Specify either the address
2689 * of a function to be invoked by the system workqueue thread,
2690 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2691 * K_ALERT_DEFAULT (which causes the alert to pend).
2692 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002693 *
2694 * @return N/A
2695 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002696extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2697 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002698
2699/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002700 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002702 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002704 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2705 *
2706 * @param alert Address of the alert.
2707 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002708 * or one of the special values K_NO_WAIT and K_FOREVER.
2709 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002710 * @retval 0 Alert received.
2711 * @retval -EBUSY Returned without waiting.
2712 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002713 */
Kumar Galacc334c72017-04-21 10:55:34 -05002714extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002715
2716/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002717 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002719 * This routine signals @a alert. The action specified for @a alert will
2720 * be taken, which may trigger the execution of an alert handler function
2721 * and/or cause the alert to pend (assuming the alert has not reached its
2722 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002724 * @note Can be called by ISRs.
2725 *
2726 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002727 *
2728 * @return N/A
2729 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002730extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002731
2732/**
Allan Stephensc98da842016-11-11 15:45:03 -05002733 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002734 */
2735
Allan Stephensc98da842016-11-11 15:45:03 -05002736/**
2737 * @cond INTERNAL_HIDDEN
2738 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002739
2740struct k_msgq {
2741 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002742 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002743 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744 char *buffer_start;
2745 char *buffer_end;
2746 char *read_ptr;
2747 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002748 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002749
Anas Nashif2f203c22016-12-18 06:57:45 -05002750 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751};
2752
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002753#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002754 { \
2755 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002756 .max_msgs = q_max_msgs, \
2757 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002758 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002759 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002760 .read_ptr = q_buffer, \
2761 .write_ptr = q_buffer, \
2762 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002763 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002764 }
2765
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002766#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2767
Peter Mitsis1da807e2016-10-06 11:36:59 -04002768/**
Allan Stephensc98da842016-11-11 15:45:03 -05002769 * INTERNAL_HIDDEN @endcond
2770 */
2771
2772/**
2773 * @defgroup msgq_apis Message Queue APIs
2774 * @ingroup kernel_apis
2775 * @{
2776 */
2777
2778/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002779 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002781 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2782 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002783 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2784 * message is similarly aligned to this boundary, @a q_msg_size must also be
2785 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002787 * The message queue can be accessed outside the module where it is defined
2788 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002789 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002790 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002791 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002792 * @param q_name Name of the message queue.
2793 * @param q_msg_size Message size (in bytes).
2794 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002795 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002796 */
2797#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2798 static char __noinit __aligned(q_align) \
2799 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002800 struct k_msgq q_name \
2801 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002802 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002803 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804
Peter Mitsisd7a37502016-10-13 11:37:40 -04002805/**
2806 * @brief Initialize a message queue.
2807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002808 * This routine initializes a message queue object, prior to its first use.
2809 *
Allan Stephensda827222016-11-09 14:23:58 -06002810 * The message queue's ring buffer must contain space for @a max_msgs messages,
2811 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2812 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2813 * that each message is similarly aligned to this boundary, @a q_msg_size
2814 * must also be a multiple of N.
2815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002816 * @param q Address of the message queue.
2817 * @param buffer Pointer to ring buffer that holds queued messages.
2818 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002819 * @param max_msgs Maximum number of messages that can be queued.
2820 *
2821 * @return N/A
2822 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002823extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002824 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002825
2826/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002827 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002829 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002830 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002831 * @note Can be called by ISRs.
2832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002833 * @param q Address of the message queue.
2834 * @param data Pointer to the message.
2835 * @param timeout Waiting period to add the message (in milliseconds),
2836 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002837 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002838 * @retval 0 Message sent.
2839 * @retval -ENOMSG Returned without waiting or queue purged.
2840 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002841 */
Kumar Galacc334c72017-04-21 10:55:34 -05002842extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002843
2844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002845 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002847 * This routine receives a message from message queue @a q in a "first in,
2848 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002849 *
Allan Stephensc98da842016-11-11 15:45:03 -05002850 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * @param q Address of the message queue.
2853 * @param data Address of area to hold the received message.
2854 * @param timeout Waiting period to receive the message (in milliseconds),
2855 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002856 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002857 * @retval 0 Message received.
2858 * @retval -ENOMSG Returned without waiting.
2859 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002860 */
Kumar Galacc334c72017-04-21 10:55:34 -05002861extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002862
2863/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002864 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002866 * This routine discards all unreceived messages in a message queue's ring
2867 * buffer. Any threads that are blocked waiting to send a message to the
2868 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002870 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002871 *
2872 * @return N/A
2873 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874extern void k_msgq_purge(struct k_msgq *q);
2875
Peter Mitsis67be2492016-10-07 11:44:34 -04002876/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002877 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002879 * This routine returns the number of unused entries in a message queue's
2880 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * @param q Address of the message queue.
2883 *
2884 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002885 */
Kumar Galacc334c72017-04-21 10:55:34 -05002886static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002887{
2888 return q->max_msgs - q->used_msgs;
2889}
2890
Peter Mitsisd7a37502016-10-13 11:37:40 -04002891/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002892 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002894 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002896 * @param q Address of the message queue.
2897 *
2898 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002899 */
Kumar Galacc334c72017-04-21 10:55:34 -05002900static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002901{
2902 return q->used_msgs;
2903}
2904
Allan Stephensc98da842016-11-11 15:45:03 -05002905/**
2906 * @} end defgroup msgq_apis
2907 */
2908
2909/**
2910 * @defgroup mem_pool_apis Memory Pool APIs
2911 * @ingroup kernel_apis
2912 * @{
2913 */
2914
Andy Ross73cb9582017-05-09 10:42:39 -07002915/* Note on sizing: the use of a 20 bit field for block means that,
2916 * assuming a reasonable minimum block size of 16 bytes, we're limited
2917 * to 16M of memory managed by a single pool. Long term it would be
2918 * good to move to a variable bit size based on configuration.
2919 */
2920struct k_mem_block_id {
2921 u32_t pool : 8;
2922 u32_t level : 4;
2923 u32_t block : 20;
2924};
2925
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002926struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002927 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002928 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002929};
2930
Allan Stephensc98da842016-11-11 15:45:03 -05002931/**
2932 * @} end defgroup mem_pool_apis
2933 */
2934
2935/**
2936 * @defgroup mailbox_apis Mailbox APIs
2937 * @ingroup kernel_apis
2938 * @{
2939 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002940
2941struct k_mbox_msg {
2942 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002943 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002945 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002946 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002947 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002948 /** sender's message data buffer */
2949 void *tx_data;
2950 /** internal use only - needed for legacy API support */
2951 void *_rx_data;
2952 /** message data block descriptor */
2953 struct k_mem_block tx_block;
2954 /** source thread id */
2955 k_tid_t rx_source_thread;
2956 /** target thread id */
2957 k_tid_t tx_target_thread;
2958 /** internal use only - thread waiting on send (may be a dummy) */
2959 k_tid_t _syncing_thread;
2960#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2961 /** internal use only - semaphore used during asynchronous send */
2962 struct k_sem *_async_sem;
2963#endif
2964};
2965
Allan Stephensc98da842016-11-11 15:45:03 -05002966/**
2967 * @cond INTERNAL_HIDDEN
2968 */
2969
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002970struct k_mbox {
2971 _wait_q_t tx_msg_queue;
2972 _wait_q_t rx_msg_queue;
2973
Anas Nashif2f203c22016-12-18 06:57:45 -05002974 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002975};
2976
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002977#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002978 { \
2979 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2980 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002981 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002982 }
2983
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002984#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2985
Peter Mitsis12092702016-10-14 12:57:23 -04002986/**
Allan Stephensc98da842016-11-11 15:45:03 -05002987 * INTERNAL_HIDDEN @endcond
2988 */
2989
2990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002991 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002993 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002994 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002995 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002997 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002998 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002999#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003000 struct k_mbox name \
3001 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003002 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003003
Peter Mitsis12092702016-10-14 12:57:23 -04003004/**
3005 * @brief Initialize a mailbox.
3006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003007 * This routine initializes a mailbox object, prior to its first use.
3008 *
3009 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003010 *
3011 * @return N/A
3012 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003013extern void k_mbox_init(struct k_mbox *mbox);
3014
Peter Mitsis12092702016-10-14 12:57:23 -04003015/**
3016 * @brief Send a mailbox message in a synchronous manner.
3017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003018 * This routine sends a message to @a mbox and waits for a receiver to both
3019 * receive and process it. The message data may be in a buffer, in a memory
3020 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003022 * @param mbox Address of the mailbox.
3023 * @param tx_msg Address of the transmit message descriptor.
3024 * @param timeout Waiting period for the message to be received (in
3025 * milliseconds), or one of the special values K_NO_WAIT
3026 * and K_FOREVER. Once the message has been received,
3027 * this routine waits as long as necessary for the message
3028 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003029 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003030 * @retval 0 Message sent.
3031 * @retval -ENOMSG Returned without waiting.
3032 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003033 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003034extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003035 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003036
Peter Mitsis12092702016-10-14 12:57:23 -04003037/**
3038 * @brief Send a mailbox message in an asynchronous manner.
3039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003040 * This routine sends a message to @a mbox without waiting for a receiver
3041 * to process it. The message data may be in a buffer, in a memory pool block,
3042 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3043 * will be given when the message has been both received and completely
3044 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003046 * @param mbox Address of the mailbox.
3047 * @param tx_msg Address of the transmit message descriptor.
3048 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003049 *
3050 * @return N/A
3051 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003052extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003053 struct k_sem *sem);
3054
Peter Mitsis12092702016-10-14 12:57:23 -04003055/**
3056 * @brief Receive a mailbox message.
3057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003058 * This routine receives a message from @a mbox, then optionally retrieves
3059 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003061 * @param mbox Address of the mailbox.
3062 * @param rx_msg Address of the receive message descriptor.
3063 * @param buffer Address of the buffer to receive data, or NULL to defer data
3064 * retrieval and message disposal until later.
3065 * @param timeout Waiting period for a message to be received (in
3066 * milliseconds), or one of the special values K_NO_WAIT
3067 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003068 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003069 * @retval 0 Message received.
3070 * @retval -ENOMSG Returned without waiting.
3071 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003072 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003073extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003074 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003075
3076/**
3077 * @brief Retrieve mailbox message data into a buffer.
3078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003079 * This routine completes the processing of a received message by retrieving
3080 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003081 *
3082 * Alternatively, this routine can be used to dispose of a received message
3083 * without retrieving its data.
3084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003085 * @param rx_msg Address of the receive message descriptor.
3086 * @param buffer Address of the buffer to receive data, or NULL to discard
3087 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003088 *
3089 * @return N/A
3090 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003091extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003092
3093/**
3094 * @brief Retrieve mailbox message data into a memory pool block.
3095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003096 * This routine completes the processing of a received message by retrieving
3097 * its data into a memory pool block, then disposing of the message.
3098 * The memory pool block that results from successful retrieval must be
3099 * returned to the pool once the data has been processed, even in cases
3100 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003101 *
3102 * Alternatively, this routine can be used to dispose of a received message
3103 * without retrieving its data. In this case there is no need to return a
3104 * memory pool block to the pool.
3105 *
3106 * This routine allocates a new memory pool block for the data only if the
3107 * data is not already in one. If a new block cannot be allocated, the routine
3108 * returns a failure code and the received message is left unchanged. This
3109 * permits the caller to reattempt data retrieval at a later time or to dispose
3110 * of the received message without retrieving its data.
3111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003112 * @param rx_msg Address of a receive message descriptor.
3113 * @param pool Address of memory pool, or NULL to discard data.
3114 * @param block Address of the area to hold memory pool block info.
3115 * @param timeout Waiting period to wait for a memory pool block (in
3116 * milliseconds), or one of the special values K_NO_WAIT
3117 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003118 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003119 * @retval 0 Data retrieved.
3120 * @retval -ENOMEM Returned without waiting.
3121 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003122 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003123extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003124 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003125 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003126
Allan Stephensc98da842016-11-11 15:45:03 -05003127/**
3128 * @} end defgroup mailbox_apis
3129 */
3130
3131/**
3132 * @cond INTERNAL_HIDDEN
3133 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003134
3135struct k_pipe {
3136 unsigned char *buffer; /* Pipe buffer: may be NULL */
3137 size_t size; /* Buffer size */
3138 size_t bytes_used; /* # bytes used in buffer */
3139 size_t read_index; /* Where in buffer to read from */
3140 size_t write_index; /* Where in buffer to write */
3141
3142 struct {
3143 _wait_q_t readers; /* Reader wait queue */
3144 _wait_q_t writers; /* Writer wait queue */
3145 } wait_q;
3146
Anas Nashif2f203c22016-12-18 06:57:45 -05003147 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003148};
3149
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003150#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003151 { \
3152 .buffer = pipe_buffer, \
3153 .size = pipe_buffer_size, \
3154 .bytes_used = 0, \
3155 .read_index = 0, \
3156 .write_index = 0, \
3157 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3158 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003159 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003160 }
3161
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003162#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3163
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003164/**
Allan Stephensc98da842016-11-11 15:45:03 -05003165 * INTERNAL_HIDDEN @endcond
3166 */
3167
3168/**
3169 * @defgroup pipe_apis Pipe APIs
3170 * @ingroup kernel_apis
3171 * @{
3172 */
3173
3174/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003175 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003177 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003178 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003179 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003181 * @param name Name of the pipe.
3182 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3183 * or zero if no ring buffer is used.
3184 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003185 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003186#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3187 static unsigned char __noinit __aligned(pipe_align) \
3188 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003189 struct k_pipe name \
3190 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003191 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003192
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003193/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003194 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003196 * This routine initializes a pipe object, prior to its first use.
3197 *
3198 * @param pipe Address of the pipe.
3199 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3200 * is used.
3201 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3202 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003203 *
3204 * @return N/A
3205 */
3206extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3207 size_t size);
3208
3209/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003212 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003213 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003214 * @param pipe Address of the pipe.
3215 * @param data Address of data to write.
3216 * @param bytes_to_write Size of data (in bytes).
3217 * @param bytes_written Address of area to hold the number of bytes written.
3218 * @param min_xfer Minimum number of bytes to write.
3219 * @param timeout Waiting period to wait for the data to be written (in
3220 * milliseconds), or one of the special values K_NO_WAIT
3221 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003222 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003223 * @retval 0 At least @a min_xfer bytes of data were written.
3224 * @retval -EIO Returned without waiting; zero data bytes were written.
3225 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003226 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003227 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003228extern int k_pipe_put(struct k_pipe *pipe, void *data,
3229 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003230 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003231
3232/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003233 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003235 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003237 * @param pipe Address of the pipe.
3238 * @param data Address to place the data read from pipe.
3239 * @param bytes_to_read Maximum number of data bytes to read.
3240 * @param bytes_read Address of area to hold the number of bytes read.
3241 * @param min_xfer Minimum number of data bytes to read.
3242 * @param timeout Waiting period to wait for the data to be read (in
3243 * milliseconds), or one of the special values K_NO_WAIT
3244 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003245 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003246 * @retval 0 At least @a min_xfer bytes of data were read.
3247 * @retval -EIO Returned without waiting; zero data bytes were read.
3248 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003249 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003250 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003251extern int k_pipe_get(struct k_pipe *pipe, void *data,
3252 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003253 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254
3255/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003256 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003257 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003258 * This routine writes the data contained in a memory block to @a pipe.
3259 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003260 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003262 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003263 * @param block Memory block containing data to send
3264 * @param size Number of data bytes in memory block to send
3265 * @param sem Semaphore to signal upon completion (else NULL)
3266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003267 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003268 */
3269extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3270 size_t size, struct k_sem *sem);
3271
3272/**
Allan Stephensc98da842016-11-11 15:45:03 -05003273 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003274 */
3275
Allan Stephensc98da842016-11-11 15:45:03 -05003276/**
3277 * @cond INTERNAL_HIDDEN
3278 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003279
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003280struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003281 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003282 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003283 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003284 char *buffer;
3285 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003286 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003287
Anas Nashif2f203c22016-12-18 06:57:45 -05003288 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003289};
3290
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003291#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003292 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003293 { \
3294 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003295 .num_blocks = slab_num_blocks, \
3296 .block_size = slab_block_size, \
3297 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003298 .free_list = NULL, \
3299 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003300 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003301 }
3302
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003303#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3304
3305
Peter Mitsis578f9112016-10-07 13:50:31 -04003306/**
Allan Stephensc98da842016-11-11 15:45:03 -05003307 * INTERNAL_HIDDEN @endcond
3308 */
3309
3310/**
3311 * @defgroup mem_slab_apis Memory Slab APIs
3312 * @ingroup kernel_apis
3313 * @{
3314 */
3315
3316/**
Allan Stephensda827222016-11-09 14:23:58 -06003317 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003318 *
Allan Stephensda827222016-11-09 14:23:58 -06003319 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003320 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003321 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3322 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003323 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003324 *
Allan Stephensda827222016-11-09 14:23:58 -06003325 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003326 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003327 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003328 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003330 * @param name Name of the memory slab.
3331 * @param slab_block_size Size of each memory block (in bytes).
3332 * @param slab_num_blocks Number memory blocks.
3333 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003334 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003335#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3336 char __noinit __aligned(slab_align) \
3337 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3338 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003339 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003340 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003341 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003342
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003343/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003344 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003345 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003346 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003347 *
Allan Stephensda827222016-11-09 14:23:58 -06003348 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3349 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3350 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3351 * To ensure that each memory block is similarly aligned to this boundary,
3352 * @a slab_block_size must also be a multiple of N.
3353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003354 * @param slab Address of the memory slab.
3355 * @param buffer Pointer to buffer used for the memory blocks.
3356 * @param block_size Size of each memory block (in bytes).
3357 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003358 *
3359 * @return N/A
3360 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003361extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003362 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003363
3364/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * @param slab Address of the memory slab.
3370 * @param mem Pointer to block address area.
3371 * @param timeout Maximum time to wait for operation to complete
3372 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3373 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003374 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003375 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003376 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003377 * @retval -ENOMEM Returned without waiting.
3378 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003379 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003380extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003381 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003382
3383/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003384 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003386 * This routine releases a previously allocated memory block back to its
3387 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003389 * @param slab Address of the memory slab.
3390 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003391 *
3392 * @return N/A
3393 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003394extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003395
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003396/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003397 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003399 * This routine gets the number of memory blocks that are currently
3400 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003402 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003405 */
Kumar Galacc334c72017-04-21 10:55:34 -05003406static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003407{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003408 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003409}
3410
Peter Mitsisc001aa82016-10-13 13:53:37 -04003411/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003412 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003414 * This routine gets the number of memory blocks that are currently
3415 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003417 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003419 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003420 */
Kumar Galacc334c72017-04-21 10:55:34 -05003421static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003422{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003423 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003424}
3425
Allan Stephensc98da842016-11-11 15:45:03 -05003426/**
3427 * @} end defgroup mem_slab_apis
3428 */
3429
3430/**
3431 * @cond INTERNAL_HIDDEN
3432 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003433
Andy Ross73cb9582017-05-09 10:42:39 -07003434struct k_mem_pool_lvl {
3435 union {
3436 u32_t *bits_p;
3437 u32_t bits;
3438 };
3439 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003440};
3441
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003442struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003443 void *buf;
3444 size_t max_sz;
3445 u16_t n_max;
3446 u8_t n_levels;
3447 u8_t max_inline_level;
3448 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003449 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003450};
3451
Andy Ross73cb9582017-05-09 10:42:39 -07003452#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003453
Andy Ross73cb9582017-05-09 10:42:39 -07003454#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3455
3456#define _MPOOL_LVLS(maxsz, minsz) \
3457 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3458 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3459 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3460 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3461 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3462 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3463 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3464 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3465 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3466 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3467 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3468 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3469 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3470 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3471 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3472 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3473
3474/* Rounds the needed bits up to integer multiples of u32_t */
3475#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3476 ((((n_max) << (2*(l))) + 31) / 32)
3477
3478/* One word gets stored free unioned with the pointer, otherwise the
3479 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003480 */
Andy Ross73cb9582017-05-09 10:42:39 -07003481#define _MPOOL_LBIT_WORDS(n_max, l) \
3482 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3483 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003484
Andy Ross73cb9582017-05-09 10:42:39 -07003485/* How many bytes for the bitfields of a single level? */
3486#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3487 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3488 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003489
Andy Ross73cb9582017-05-09 10:42:39 -07003490/* Size of the bitmap array that follows the buffer in allocated memory */
3491#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3492 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3493 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3494 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3495 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3496 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3497 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3498 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3499 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3500 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3501 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3502 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3503 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3504 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3505 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3506 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3507 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003508
3509/**
Allan Stephensc98da842016-11-11 15:45:03 -05003510 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003511 */
3512
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003513/**
Allan Stephensc98da842016-11-11 15:45:03 -05003514 * @addtogroup mem_pool_apis
3515 * @{
3516 */
3517
3518/**
3519 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003521 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3522 * long. The memory pool allows blocks to be repeatedly partitioned into
3523 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003524 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003525 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003526 * If the pool is to be accessed outside the module where it is defined, it
3527 * can be declared via
3528 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003529 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003531 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003532 * @param minsz Size of the smallest blocks in the pool (in bytes).
3533 * @param maxsz Size of the largest blocks in the pool (in bytes).
3534 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003535 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003536 */
Andy Ross73cb9582017-05-09 10:42:39 -07003537#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3538 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3539 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3540 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3541 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3542 .buf = _mpool_buf_##name, \
3543 .max_sz = maxsz, \
3544 .n_max = nmax, \
3545 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3546 .levels = _mpool_lvls_##name, \
3547 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003548
Peter Mitsis937042c2016-10-13 13:18:26 -04003549/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003550 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003552 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003554 * @param pool Address of the memory pool.
3555 * @param block Pointer to block descriptor for the allocated memory.
3556 * @param size Amount of memory to allocate (in bytes).
3557 * @param timeout Maximum time to wait for operation to complete
3558 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3559 * or K_FOREVER to wait as long as necessary.
3560 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003561 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003562 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003563 * @retval -ENOMEM Returned without waiting.
3564 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003565 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003566extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003567 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003568
3569/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003570 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003572 * This routine releases a previously allocated memory block back to its
3573 * memory pool.
3574 *
3575 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003576 *
3577 * @return N/A
3578 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003579extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003580
3581/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003582 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003583 *
Andy Ross73cb9582017-05-09 10:42:39 -07003584 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003585 *
Andy Ross73cb9582017-05-09 10:42:39 -07003586 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003587 *
3588 * @return N/A
3589 */
Andy Ross73cb9582017-05-09 10:42:39 -07003590static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003591
3592/**
Allan Stephensc98da842016-11-11 15:45:03 -05003593 * @} end addtogroup mem_pool_apis
3594 */
3595
3596/**
3597 * @defgroup heap_apis Heap Memory Pool APIs
3598 * @ingroup kernel_apis
3599 * @{
3600 */
3601
3602/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003603 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003605 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003606 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003608 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003610 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003611 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003612extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003613
3614/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003615 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003616 *
3617 * This routine provides traditional free() semantics. The memory being
3618 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003619 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003620 * If @a ptr is NULL, no operation is performed.
3621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003622 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003623 *
3624 * @return N/A
3625 */
3626extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003627
Allan Stephensc98da842016-11-11 15:45:03 -05003628/**
3629 * @} end defgroup heap_apis
3630 */
3631
Benjamin Walshacc68c12017-01-29 18:57:45 -05003632/* polling API - PRIVATE */
3633
Benjamin Walshb0179862017-02-02 16:39:57 -05003634#ifdef CONFIG_POLL
3635#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3636#else
3637#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3638#endif
3639
Benjamin Walshacc68c12017-01-29 18:57:45 -05003640/* private - implementation data created as needed, per-type */
3641struct _poller {
3642 struct k_thread *thread;
3643};
3644
3645/* private - types bit positions */
3646enum _poll_types_bits {
3647 /* can be used to ignore an event */
3648 _POLL_TYPE_IGNORE,
3649
3650 /* to be signaled by k_poll_signal() */
3651 _POLL_TYPE_SIGNAL,
3652
3653 /* semaphore availability */
3654 _POLL_TYPE_SEM_AVAILABLE,
3655
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003656 /* queue/fifo/lifo data availability */
3657 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003658
3659 _POLL_NUM_TYPES
3660};
3661
3662#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3663
3664/* private - states bit positions */
3665enum _poll_states_bits {
3666 /* default state when creating event */
3667 _POLL_STATE_NOT_READY,
3668
Benjamin Walshacc68c12017-01-29 18:57:45 -05003669 /* signaled by k_poll_signal() */
3670 _POLL_STATE_SIGNALED,
3671
3672 /* semaphore is available */
3673 _POLL_STATE_SEM_AVAILABLE,
3674
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003675 /* data is available to read on queue/fifo/lifo */
3676 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003677
3678 _POLL_NUM_STATES
3679};
3680
3681#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3682
3683#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003684 (32 - (0 \
3685 + 8 /* tag */ \
3686 + _POLL_NUM_TYPES \
3687 + _POLL_NUM_STATES \
3688 + 1 /* modes */ \
3689 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003690
3691#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3692#error overflow of 32-bit word in struct k_poll_event
3693#endif
3694
3695/* end of polling API - PRIVATE */
3696
3697
3698/**
3699 * @defgroup poll_apis Async polling APIs
3700 * @ingroup kernel_apis
3701 * @{
3702 */
3703
3704/* Public polling API */
3705
3706/* public - values for k_poll_event.type bitfield */
3707#define K_POLL_TYPE_IGNORE 0
3708#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3709#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003710#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3711#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003712
3713/* public - polling modes */
3714enum k_poll_modes {
3715 /* polling thread does not take ownership of objects when available */
3716 K_POLL_MODE_NOTIFY_ONLY = 0,
3717
3718 K_POLL_NUM_MODES
3719};
3720
3721/* public - values for k_poll_event.state bitfield */
3722#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003723#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3724#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003725#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3726#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003727
3728/* public - poll signal object */
3729struct k_poll_signal {
3730 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003731 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003732
3733 /*
3734 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3735 * user resets it to 0.
3736 */
3737 unsigned int signaled;
3738
3739 /* custom result value passed to k_poll_signal() if needed */
3740 int result;
3741};
3742
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003743#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003744 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003745 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003746 .signaled = 0, \
3747 .result = 0, \
3748 }
3749
3750struct k_poll_event {
3751 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003752 sys_dnode_t _node;
3753
3754 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003755 struct _poller *poller;
3756
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003757 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003758 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003759
Benjamin Walshacc68c12017-01-29 18:57:45 -05003760 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003761 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003762
3763 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003764 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003765
3766 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003767 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003768
3769 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003770 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003771
3772 /* per-type data */
3773 union {
3774 void *obj;
3775 struct k_poll_signal *signal;
3776 struct k_sem *sem;
3777 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003778 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003779 };
3780};
3781
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003782#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003783 { \
3784 .poller = NULL, \
3785 .type = event_type, \
3786 .state = K_POLL_STATE_NOT_READY, \
3787 .mode = event_mode, \
3788 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003789 { .obj = event_obj }, \
3790 }
3791
3792#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3793 event_tag) \
3794 { \
3795 .type = event_type, \
3796 .tag = event_tag, \
3797 .state = K_POLL_STATE_NOT_READY, \
3798 .mode = event_mode, \
3799 .unused = 0, \
3800 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003801 }
3802
3803/**
3804 * @brief Initialize one struct k_poll_event instance
3805 *
3806 * After this routine is called on a poll event, the event it ready to be
3807 * placed in an event array to be passed to k_poll().
3808 *
3809 * @param event The event to initialize.
3810 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3811 * values. Only values that apply to the same object being polled
3812 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3813 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003814 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003815 * @param obj Kernel object or poll signal.
3816 *
3817 * @return N/A
3818 */
3819
Kumar Galacc334c72017-04-21 10:55:34 -05003820extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003821 int mode, void *obj);
3822
3823/**
3824 * @brief Wait for one or many of multiple poll events to occur
3825 *
3826 * This routine allows a thread to wait concurrently for one or many of
3827 * multiple poll events to have occurred. Such events can be a kernel object
3828 * being available, like a semaphore, or a poll signal event.
3829 *
3830 * When an event notifies that a kernel object is available, the kernel object
3831 * is not "given" to the thread calling k_poll(): it merely signals the fact
3832 * that the object was available when the k_poll() call was in effect. Also,
3833 * all threads trying to acquire an object the regular way, i.e. by pending on
3834 * the object, have precedence over the thread polling on the object. This
3835 * means that the polling thread will never get the poll event on an object
3836 * until the object becomes available and its pend queue is empty. For this
3837 * reason, the k_poll() call is more effective when the objects being polled
3838 * only have one thread, the polling thread, trying to acquire them.
3839 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003840 * When k_poll() returns 0, the caller should loop on all the events that were
3841 * passed to k_poll() and check the state field for the values that were
3842 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003843 *
3844 * Before being reused for another call to k_poll(), the user has to reset the
3845 * state field to K_POLL_STATE_NOT_READY.
3846 *
3847 * @param events An array of pointers to events to be polled for.
3848 * @param num_events The number of events in the array.
3849 * @param timeout Waiting period for an event to be ready (in milliseconds),
3850 * or one of the special values K_NO_WAIT and K_FOREVER.
3851 *
3852 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003853 * @retval -EAGAIN Waiting period timed out.
3854 */
3855
3856extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003857 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003858
3859/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003860 * @brief Initialize a poll signal object.
3861 *
3862 * Ready a poll signal object to be signaled via k_poll_signal().
3863 *
3864 * @param signal A poll signal.
3865 *
3866 * @return N/A
3867 */
3868
3869extern void k_poll_signal_init(struct k_poll_signal *signal);
3870
3871/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003872 * @brief Signal a poll signal object.
3873 *
3874 * This routine makes ready a poll signal, which is basically a poll event of
3875 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3876 * made ready to run. A @a result value can be specified.
3877 *
3878 * The poll signal contains a 'signaled' field that, when set by
3879 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3880 * be reset by the user before being passed again to k_poll() or k_poll() will
3881 * consider it being signaled, and will return immediately.
3882 *
3883 * @param signal A poll signal.
3884 * @param result The value to store in the result field of the signal.
3885 *
3886 * @retval 0 The signal was delivered successfully.
3887 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3888 */
3889
3890extern int k_poll_signal(struct k_poll_signal *signal, int result);
3891
3892/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003893extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003894
3895/**
3896 * @} end defgroup poll_apis
3897 */
3898
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003899/**
3900 * @brief Make the CPU idle.
3901 *
3902 * This function makes the CPU idle until an event wakes it up.
3903 *
3904 * In a regular system, the idle thread should be the only thread responsible
3905 * for making the CPU idle and triggering any type of power management.
3906 * However, in some more constrained systems, such as a single-threaded system,
3907 * the only thread would be responsible for this if needed.
3908 *
3909 * @return N/A
3910 */
3911extern void k_cpu_idle(void);
3912
3913/**
3914 * @brief Make the CPU idle in an atomic fashion.
3915 *
3916 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3917 * must be done atomically before making the CPU idle.
3918 *
3919 * @param key Interrupt locking key obtained from irq_lock().
3920 *
3921 * @return N/A
3922 */
3923extern void k_cpu_atomic_idle(unsigned int key);
3924
Kumar Galacc334c72017-04-21 10:55:34 -05003925extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003926
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003927#include <arch/cpu.h>
3928
Andrew Boiecdb94d62017-04-18 15:22:05 -07003929#ifdef _ARCH_EXCEPT
3930/* This archtecture has direct support for triggering a CPU exception */
3931#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3932#else
3933
3934#include <misc/printk.h>
3935
3936/* NOTE: This is the implementation for arches that do not implement
3937 * _ARCH_EXCEPT() to generate a real CPU exception.
3938 *
3939 * We won't have a real exception frame to determine the PC value when
3940 * the oops occurred, so print file and line number before we jump into
3941 * the fatal error handler.
3942 */
3943#define _k_except_reason(reason) do { \
3944 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3945 _NanoFatalErrorHandler(reason, &_default_esf); \
3946 CODE_UNREACHABLE; \
3947 } while (0)
3948
3949#endif /* _ARCH__EXCEPT */
3950
3951/**
3952 * @brief Fatally terminate a thread
3953 *
3954 * This should be called when a thread has encountered an unrecoverable
3955 * runtime condition and needs to terminate. What this ultimately
3956 * means is determined by the _fatal_error_handler() implementation, which
3957 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3958 *
3959 * If this is called from ISR context, the default system fatal error handler
3960 * will treat it as an unrecoverable system error, just like k_panic().
3961 */
3962#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3963
3964/**
3965 * @brief Fatally terminate the system
3966 *
3967 * This should be called when the Zephyr kernel has encountered an
3968 * unrecoverable runtime condition and needs to terminate. What this ultimately
3969 * means is determined by the _fatal_error_handler() implementation, which
3970 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3971 */
3972#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3973
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003974/*
3975 * private APIs that are utilized by one or more public APIs
3976 */
3977
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003978#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003979extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003980#else
3981#define _init_static_threads() do { } while ((0))
3982#endif
3983
3984extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003985extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003986
Andrew Boiedc5d9352017-06-02 12:56:47 -07003987/* arch/cpu.h may declare an architecture or platform-specific macro
3988 * for properly declaring stacks, compatible with MMU/MPU constraints if
3989 * enabled
3990 */
3991#ifdef _ARCH_THREAD_STACK_DEFINE
3992#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3993#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3994 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3995#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3996#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07003997static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3998{
3999 return _ARCH_THREAD_STACK_BUFFER(sym);
4000}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004001#else
4002/**
4003 * @brief Declare a toplevel thread stack memory region
4004 *
4005 * This declares a region of memory suitable for use as a thread's stack.
4006 *
4007 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4008 * 'noinit' section so that it isn't zeroed at boot
4009 *
Andrew Boie507852a2017-07-25 18:47:07 -07004010 * The declared symbol will always be a k_thread_stack_t which can be passed to
4011 * k_thread_create, but should otherwise not be manipulated. If the buffer
4012 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004013 *
4014 * It is legal to precede this definition with the 'static' keyword.
4015 *
4016 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4017 * parameter of k_thread_create(), it may not be the same as the
4018 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4019 *
4020 * @param sym Thread stack symbol name
4021 * @param size Size of the stack memory region
4022 */
4023#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004024 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004025
4026/**
4027 * @brief Declare a toplevel array of thread stack memory regions
4028 *
4029 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4030 * definition for additional details and constraints.
4031 *
4032 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4033 * 'noinit' section so that it isn't zeroed at boot
4034 *
4035 * @param sym Thread stack symbol name
4036 * @param nmemb Number of stacks to declare
4037 * @param size Size of the stack memory region
4038 */
4039
4040#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004041 struct _k_thread_stack_element __noinit \
4042 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004043
4044/**
4045 * @brief Declare an embedded stack memory region
4046 *
4047 * Used for stacks embedded within other data structures. Use is highly
4048 * discouraged but in some cases necessary. For memory protection scenarios,
4049 * it is very important that any RAM preceding this member not be writable
4050 * by threads else a stack overflow will lead to silent corruption. In other
4051 * words, the containing data structure should live in RAM owned by the kernel.
4052 *
4053 * @param sym Thread stack symbol name
4054 * @param size Size of the stack memory region
4055 */
4056#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004057 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004058
4059/**
4060 * @brief Return the size in bytes of a stack memory region
4061 *
4062 * Convenience macro for passing the desired stack size to k_thread_create()
4063 * since the underlying implementation may actually create something larger
4064 * (for instance a guard area).
4065 *
4066 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004067 * passed to K_THREAD_STACK_DEFINE.
4068 *
4069 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4070 * it is not guaranteed to return the original value since each array
4071 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004072 *
4073 * @param sym Stack memory symbol
4074 * @return Size of the stack
4075 */
4076#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4077
4078/**
4079 * @brief Get a pointer to the physical stack buffer
4080 *
4081 * Convenience macro to get at the real underlying stack buffer used by
4082 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4083 * This is really only intended for diagnostic tools which want to examine
4084 * stack memory contents.
4085 *
4086 * @param sym Declared stack symbol name
4087 * @return The buffer itself, a char *
4088 */
Andrew Boie507852a2017-07-25 18:47:07 -07004089static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4090{
4091 return (char *)sym;
4092}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004093
4094#endif /* _ARCH_DECLARE_STACK */
4095
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004096#ifdef __cplusplus
4097}
4098#endif
4099
Andrew Boiee004dec2016-11-07 09:01:19 -08004100#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4101/*
4102 * Define new and delete operators.
4103 * At this moment, the operators do nothing since objects are supposed
4104 * to be statically allocated.
4105 */
4106inline void operator delete(void *ptr)
4107{
4108 (void)ptr;
4109}
4110
4111inline void operator delete[](void *ptr)
4112{
4113 (void)ptr;
4114}
4115
4116inline void *operator new(size_t size)
4117{
4118 (void)size;
4119 return NULL;
4120}
4121
4122inline void *operator new[](size_t size)
4123{
4124 (void)size;
4125 return NULL;
4126}
4127
4128/* Placement versions of operator new and delete */
4129inline void operator delete(void *ptr1, void *ptr2)
4130{
4131 (void)ptr1;
4132 (void)ptr2;
4133}
4134
4135inline void operator delete[](void *ptr1, void *ptr2)
4136{
4137 (void)ptr1;
4138 (void)ptr2;
4139}
4140
4141inline void *operator new(size_t size, void *ptr)
4142{
4143 (void)size;
4144 return ptr;
4145}
4146
4147inline void *operator new[](size_t size, void *ptr)
4148{
4149 (void)size;
4150 return ptr;
4151}
4152
4153#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4154
Andrew Boiefa94ee72017-09-28 16:54:35 -07004155#include <syscalls/kernel.h>
4156
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004157#endif /* !_ASMLANGUAGE */
4158
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004159#endif /* _kernel__h_ */