blob: ab109788c9a7efaded6f4f7804ba4af0c1fa00aa [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @file
19 *
20 * @brief Public kernel APIs.
21 */
22
23#ifndef _kernel__h_
24#define _kernel__h_
25
26#include <stddef.h>
27#include <stdint.h>
28#include <toolchain.h>
29#include <sections.h>
30#include <atomic.h>
31#include <errno.h>
32#include <misc/__assert.h>
33#include <misc/dlist.h>
34#include <misc/slist.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40#ifdef CONFIG_KERNEL_V2_DEBUG
41#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
42#else
43#define K_DEBUG(fmt, ...)
44#endif
45
46#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
47#define K_PRIO_PREEMPT(x) (x)
48
49#define K_FOREVER (-1)
50#define K_NO_WAIT 0
51
52#define K_ANY NULL
53#define K_END NULL
54
Benjamin Walsh456c6da2016-09-02 18:55:39 -040055#if CONFIG_NUM_COOP_PRIORITIES > 0
56#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
57#else
58#define K_HIGHEST_THREAD_PRIO 0
59#endif
60
61#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
62#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
63#else
64#define K_LOWEST_THREAD_PRIO -1
65#endif
66
67#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
68#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
69
70typedef sys_dlist_t _wait_q_t;
71
72#ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS
73#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next
74#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL,
75#else
76#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT
77#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type)
78#endif
79
80#define k_thread tcs
81struct tcs;
82struct k_mutex;
83struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040084struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040085struct k_msgq;
86struct k_mbox;
87struct k_pipe;
88struct k_fifo;
89struct k_lifo;
90struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -040091struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040092struct k_mem_pool;
93struct k_timer;
94
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040095typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096
97/* threads/scheduler/execution contexts */
98
99enum execution_context_types {
100 K_ISR = 0,
101 K_COOP_THREAD,
102 K_PREEMPT_THREAD,
103};
104
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400106
107/**
108 * @brief Initialize and start a thread with an optional delay
109 *
110 * This routine initializes a thread and optionally delays its execution.
111 * It is not ISR-callable.
112 *
113 * If a thread of priority higher than the current thread is spawned, and the
114 * current thread id preemptible, the current thread is preempted by the new
115 * thread.
116 *
117 * @param stack Pointer to the stack space.
118 * @param stack_size Stack size in bytes.
119 * @param entry Thread entry function.
120 * @param p1 1st entry point parameter.
121 * @param p2 2nd entry point parameter.
122 * @param p3 3rd entry point parameter.
123 * @param prio The thread's priority.
124 * @param options Not used currently.
125 * @param delay Duration of execution delay in milliseconds
126 *
127 * @return Kernel thread identifier
128 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400129extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size,
130 void (*entry)(void *, void *, void*),
131 void *p1, void *p2, void *p3,
132 int32_t prio, uint32_t options, int32_t delay);
133
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400134/**
135 * @brief Put the current thread to sleep
136 *
137 * This routine puts the currently thread to sleep for the specified
138 * number of milliseconds.
139 *
140 * @param duration Number of milliseconds the thread is to sleep
141 *
142 * @return N/A
143 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400144extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400145
146/**
147 * @brief Cause the current thread to busy wait
148 *
149 * This routine causes the current thread to execute a "do nothing" loop for
150 * a specified period of microseconds.
151 *
152 * @warning This routine utilizes the system clock, so it must not be invoked
153 * until the system clock is fully operational or while interrupts are
154 * locked.
155 *
156 * @return N/A
157 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400158extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400159
160/**
161 * @brief Yield the current thread
162 *
163 * Calling this routine results in the current thread yielding to another
164 * thread of the same or higher priority. If there are no other ready threads
165 * of the same or higher priority, the routine will return immediately.
166 *
167 * @return N/A
168 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400169extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400170
171/**
172 * @brief Wake the specified thread from sleep
173 *
174 * This routine wakes the thread specified by @a thread from its sleep.
175 *
176 * @param thread Identifies thread to wake
177 *
178 * @return N/A
179 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400180extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400181
182/**
183 * @brief Obtain the thread ID of the currently executing thread
184 *
185 * @return Current thread ID
186 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400187extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400188
189/**
190 * @brief Cancel a delayed thread start
191 *
192 * @param thread Delayed thread ID
193 *
194 * @retval 0 on success
195 * @retval -EINVAL Thread has already started or not delayed
196 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400197extern int k_thread_cancel(k_tid_t thread);
198
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400199/**
200 * @brief Abort a thread
201 *
202 * Execution of @a thread is immediately permanently cancelled. @a thread is
203 * taken off the ready queue if ready, or out of any wait queues and/or
204 * timeout queues it might be currently queued on. However, objects it might
205 * currently owned, such as mutexes, are not released. It is up to the
206 * subsystems managing the objects to handle this.
207 *
208 * @param thread Thread to abort
209 *
210 * @return N/A
211 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400212extern void k_thread_abort(k_tid_t thread);
213
214#define K_THREAD_GROUP_EXE 0x1
215#define K_THREAD_GROUP_SYS 0x2
216#define K_THREAD_GROUP_FPU 0x4
217
218/* XXX - doesn't work because CONFIG_ARCH is a string */
219#if 0
220/* arch-specific groups */
221#if CONFIG_ARCH == "x86"
222#define K_THREAD_GROUP_SSE 0x4
223#endif
224#endif
225
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400226#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400227#define _THREAD_TIMEOUT_INIT(obj) \
228 (obj).nano_timeout = { \
229 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400230 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400231 .wait_q = NULL, \
232 .delta_ticks_from_prev = -1, \
233 },
234#else
235#define _THREAD_TIMEOUT_INIT(obj)
236#endif
237
238#ifdef CONFIG_ERRNO
239#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
240#else
241#define _THREAD_ERRNO_INIT(obj)
242#endif
243
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400244struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400245 union {
246 char *init_stack;
247 struct k_thread *thread;
248 };
249 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500250 void (*init_entry)(void *, void *, void *);
251 void *init_p1;
252 void *init_p2;
253 void *init_p3;
254 int init_prio;
255 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400256 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500257 void (*init_abort)(void);
258 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400259};
260
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400261#define _THREAD_INITIALIZER(stack, stack_size, \
262 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500263 prio, options, delay, abort, groups) \
264 { \
265 .init_stack = (stack), \
266 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400267 .init_entry = (void (*)(void *, void *, void *))entry, \
268 .init_p1 = (void *)p1, \
269 .init_p2 = (void *)p2, \
270 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500271 .init_prio = (prio), \
272 .init_options = (options), \
273 .init_delay = (delay), \
274 .init_abort = (abort), \
275 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400276 }
277
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400278/**
Allan Stephens6cfe1322016-10-26 10:16:51 -0500279 * @brief Define a static thread.
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400280 *
281 * @internal It has been observed that the x86 compiler by default aligns
282 * these _static_thread_data structures to 32-byte boundaries, thereby
283 * wasting space. To work around this, force a 4-byte alignment.
284 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500285#define K_THREAD_DEFINE(name, stack_size, \
286 entry, p1, p2, p3, \
287 prio, options, delay) \
288 char __noinit __stack _k_thread_obj_##name[stack_size]; \
289 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500290 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500291 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
292 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500293 NULL, 0); \
294 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400295
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400296/**
297 * @brief Get a thread's priority
298 *
299 * @param thread ID of thread to query
300 *
301 * @return Specified thread's priority
302 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500303extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400304
305/**
306 * @brief Set the priority of a thread
307 *
308 * This routine immediately changes the priority of the specified thread.
309 *
310 * Rescheduling can occur immediately depending on the priority @a thread is
311 * set to:
312 *
313 * - If its priority is raised above the priority of the caller of this
314 * function, and the caller is preemptible, @a thread will be scheduled in.
315 *
316 * - If the caller operates on itself, it lowers its priority below that of
317 * other threads in the system, and the caller is preemptible, the thread of
318 * highest priority will be scheduled in.
319 *
320 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
321 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
322 * highest priority.
323 *
324 * @param thread Thread whose priority is to be set.
325 * @param prio New priority.
326 *
327 * @warning Changing the priority of a thread currently involved in mutex
328 * priority inheritance may result in undefined behavior.
329 *
330 * @return N/A
331 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400332extern void k_thread_priority_set(k_tid_t thread, int prio);
333
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400334/**
335 * @brief Suspend a thread
336 *
337 * Remove @a thread from scheduling decisions. All other internal operations
338 * on @a thread will still be performed: any timeout it is on keeps ticking
339 * and delivered upon expiry, objects it is waiting on are still handed to it,
340 * etc.
341 *
342 * @param thread Thread to suspend
343 *
344 * @return N/A
345 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400346extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400347
348/**
349 * @brief Resume a previously suspended thread
350 *
351 * Resume using @a thread in scheduling decisions.
352 *
353 * @param thread Thread to resume
354 *
355 * @return N/A
356 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400357extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400358
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400359/**
360 * @brief Set time-slicing period and scope
361 *
362 * This routine controls how thread time slicing is performed by the scheduler
363 * on preemptible threads; it specifes the maximum time slice length (in
364 * milliseconds) and the highest thread priority level for which time slicing
365 * is performed.
366 *
367 * To enable time slicing, a non-zero time slice length must be specified.
368 * The scheduler then ensures that no executing thread runs for more than the
369 * specified number of milliseconds before giving other threads of that priority
370 * a chance to execute. (However, any thread whose priority is higher than the
371 * specified thread priority level is exempted, and may execute as long as
372 * desired without being pre-empted due to time slicing.)
373 *
374 * Time slicing limits only the maximum amount of time a thread may continuously
375 * execute. Once the scheduler selects a thread for execution, there is no
376 * minimum guaranteed time the thread will execute before threads of greater or
377 * equal priority are scheduled.
378 *
379 * When the currently-executing thread is the only one of that priority eligible
380 * for execution, this routine has no effect; the thread is immediately
381 * rescheduled after the slice period expires.
382 *
383 * To disable timeslicing, call the API with both parameters set to zero.
384 *
385 * @return N/A
386 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400387extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400388
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400389/**
390 * @brief Determine if code is running at interrupt level
391 *
392 * @return 0 if invoked by a thread, or non-zero if invoked by an ISR
393 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400394extern int k_am_in_isr(void);
395
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400396/**
397 * @brief Set thread's custom data
398 *
399 * This routine sets the custom data value for the current thread. Custom
400 * data is not used by the kernel itself, and is freely available for the
401 * thread to use as it sees fit.
402 *
403 * This provides a skeleton upon which to build thread-local storage.
404 *
405 * @param value New value to set the thread's custom data to.
406 *
407 * @return N/A
408 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400409extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400410
411/**
412 * @brief Get thread's custom data
413 *
414 * This function returns the custom data value for the current thread.
415 *
416 * @return current custom data value
417 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400418extern void *k_thread_custom_data_get(void);
419
420/**
421 * kernel timing
422 */
423
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400424#include <sys_clock.h>
425
426/* private internal time manipulation (users should never play with ticks) */
427
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500428/* added tick needed to account for tick in progress */
429#define _TICK_ALIGN 1
430
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400431static int64_t __ticks_to_ms(int64_t ticks)
432{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400433#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400434 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400435#else
436 __ASSERT(ticks == 0, "");
437 return 0;
438#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400439}
440
441
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400442/* timeouts */
443
444struct _timeout;
445typedef void (*_timeout_func_t)(struct _timeout *t);
446
447struct _timeout {
448 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400449 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400450 sys_dlist_t *wait_q;
451 int32_t delta_ticks_from_prev;
452 _timeout_func_t func;
453};
454
Allan Stephens45bfa372016-10-12 12:39:42 -0500455
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400456/* timers */
457
458struct k_timer {
459 /*
460 * _timeout structure must be first here if we want to use
461 * dynamic timer allocation. timeout.node is used in the double-linked
462 * list of free timers
463 */
464 struct _timeout timeout;
465
Allan Stephens45bfa372016-10-12 12:39:42 -0500466 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400467 _wait_q_t wait_q;
468
469 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500470 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400471
472 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500473 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400474
475 /* timer period */
476 int32_t period;
477
Allan Stephens45bfa372016-10-12 12:39:42 -0500478 /* timer status */
479 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400480
Allan Stephens45bfa372016-10-12 12:39:42 -0500481 /* used to support legacy timer APIs */
482 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400483
484 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
485};
486
Allan Stephens1342adb2016-11-03 13:54:53 -0500487#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400488 { \
Allan Stephens1342adb2016-11-03 13:54:53 -0500489 .timeout.delta_ticks_from_prev = -1, \
490 .timeout.wait_q = NULL, \
491 .timeout.thread = NULL, \
492 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400493 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500494 .expiry_fn = expiry, \
495 .stop_fn = stop, \
496 .status = 0, \
497 ._legacy_data = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400498 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
499 }
500
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400501/**
502 * @brief Statically define a timer and initialize it
503 *
504 * If the timer is to be accessed outside the module where it is defined, it
505 * can be declared via
506 *
507 * extern struct k_timer @a name;
508 *
509 * @param name Name of the timer variable.
Allan Stephens1342adb2016-11-03 13:54:53 -0500510 * @param expiry_fn Function to invoke each time timer expires.
511 * @param stop_fn Function to invoke if timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400512 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500513#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500514 struct k_timer name \
515 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500516 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400517
Allan Stephens45bfa372016-10-12 12:39:42 -0500518/**
519 * @brief Initialize a timer.
520 *
521 * This routine must be called before the timer is used.
522 *
523 * @param timer Address of timer.
524 * @param expiry_fn Function to invoke each time timer expires.
525 * @param stop_fn Function to invoke if timer is stopped while running.
526 *
527 * @return N/A
528 */
529extern void k_timer_init(struct k_timer *timer,
530 void (*expiry_fn)(struct k_timer *),
531 void (*stop_fn)(struct k_timer *));
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700532
Allan Stephens45bfa372016-10-12 12:39:42 -0500533/**
534 * @brief Start a timer.
535 *
536 * This routine starts a timer, and resets its status to zero. The timer
537 * begins counting down using the specified duration and period values.
538 *
539 * Attempting to start a timer that is already running is permitted.
540 * The timer's status is reset to zero and the timer begins counting down
541 * using the new duration and period values.
542 *
543 * @param timer Address of timer.
544 * @param duration Initial timer duration (in milliseconds).
545 * @param period Timer period (in milliseconds).
546 *
547 * @return N/A
548 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400549extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500550 int32_t duration, int32_t period);
551
552/**
553 * @brief Stop a timer.
554 *
555 * This routine stops a running timer prematurely. The timer's stop function,
556 * if one exists, is invoked by the caller.
557 *
558 * Attempting to stop a timer that is not running is permitted, but has no
559 * effect on the timer since it is already stopped.
560 *
561 * @param timer Address of timer.
562 *
563 * @return N/A
564 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400565extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500566
567/**
568 * @brief Read timer status.
569 *
570 * This routine reads the timer's status, which indicates the number of times
571 * it has expired since its status was last read.
572 *
573 * Calling this routine resets the timer's status to zero.
574 *
575 * @param timer Address of timer.
576 *
577 * @return Timer status.
578 */
579extern uint32_t k_timer_status_get(struct k_timer *timer);
580
581/**
582 * @brief Synchronize thread to timer expiration.
583 *
584 * This routine blocks the calling thread until the timer's status is non-zero
585 * (indicating that it has expired at least once since it was last examined)
586 * or the timer is stopped. If the timer status is already non-zero,
587 * or the timer is already stopped, the caller continues without waiting.
588 *
589 * Calling this routine resets the timer's status to zero.
590 *
591 * This routine must not be used by interrupt handlers, since they are not
592 * allowed to block.
593 *
594 * @param timer Address of timer.
595 *
596 * @return Timer status.
597 */
598extern uint32_t k_timer_status_sync(struct k_timer *timer);
599
600/**
601 * @brief Get timer remaining before next timer expiration.
602 *
603 * This routine computes the (approximate) time remaining before a running
604 * timer next expires. If the timer is not running, it returns zero.
605 *
606 * @param timer Address of timer.
607 *
608 * @return Remaining time (in milliseconds).
609 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400610extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400611
612
Allan Stephens45bfa372016-10-12 12:39:42 -0500613/* kernel clocks */
614
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400615/**
616 * @brief Get the time elapsed since the system booted (uptime)
617 *
618 * @return The current uptime of the system in ms
619 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400620extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400621
622/**
623 * @brief Get the lower 32-bit of time elapsed since the system booted (uptime)
624 *
625 * This function is potentially less onerous in both the time it takes to
626 * execute, the interrupt latency it introduces and the amount of 64-bit math
627 * it requires than k_uptime_get(), but it only provides an uptime value of
628 * 32-bits. The user must handle possible rollovers/spillovers.
629 *
630 * At a rate of increment of 1000 per second, it rolls over approximately every
631 * 50 days.
632 *
633 * @return The current uptime of the system in ms
634 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400635extern uint32_t k_uptime_get_32(void);
636
637/**
638 * @brief Get the difference between a reference time and the current uptime
639 *
640 * @param reftime A pointer to a reference time. It is updated with the current
641 * uptime upon return.
642 *
643 * @return The delta between the reference time and the current uptime.
644 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400645extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400646
647/**
648 * @brief Get the difference between a reference time and the current uptime
649 *
650 * The 32-bit version of k_uptime_delta(). It has the same perks and issues as
651 * k_uptime_get_32().
652 *
653 * @param reftime A pointer to a reference time. It is updated with the current
654 * uptime upon return.
655 *
656 * @return The delta between the reference time and the current uptime.
657 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400658extern uint32_t k_uptime_delta_32(int64_t *reftime);
659
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400660/**
661 * @brief Read the platform's timer hardware
662 *
663 * This routine returns the current time in terms of timer hardware clock
664 * cycles.
665 *
666 * @return up counter of elapsed clock cycles
667 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400668extern uint32_t k_cycle_get_32(void);
669
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400670/**
671 * data transfers (basic)
672 */
673
674/* fifos */
675
676struct k_fifo {
677 _wait_q_t wait_q;
678 sys_slist_t data_q;
679
680 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
681};
682
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400683/**
684 * @brief Initialize a kernel FIFO object.
685 *
686 * This routine initializes a kernel FIFO object structure. It must not be
687 * called from an ISR.
688 *
689 * @param fifo FIFO to initialize.
690 *
691 * @return N/A
692 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400693extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400694
695/**
696 * @brief Add an element to the end of a FIFO.
697 *
698 * This routine adds an element to the end of a FIFO. FIFO data items must be
699 * aligned on a 4-byte boundary, as the kernel reserves the first 32 bits of
700 * each item for use as a pointer to the next data item in the FIFO's link
701 * list. Each data item added to the FIFO must include and reserve these first
702 * 32 bits.
703 *
704 * @param fifo FIFO on which to interact.
705 * @param data Data to send.
706 *
707 * @return N/A
708 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400709extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400710
711/**
712 * @brief Atomically add a list of elements to the end of a FIFO.
713 *
714 * This routine adds a list of elements in one shot to the end of a FIFO
715 * object. If threads are pending on the FIFO object, they become ready to run.
716 * If this API is called from a preemptible thread, the highest priority one
717 * will preempt the running thread once the put operation is complete.
718 *
719 * If enough threads are waiting on the FIFO, the address of each element given
720 * to threads is returned to the waiting thread. The remaining elements are
721 * linked to the end of the list.
722 *
723 * The list must be a singly-linked list, where each element only has a pointer
724 * to the next one. The list must be NULL-terminated.
725 *
726 * @param fifo FIFO on which to interact.
727 * @param head head of singly-linked list
728 * @param tail tail of singly-linked list
729 *
730 * @return N/A
731 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400732extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400733
734/**
735 * @brief Atomically add a list of elements to the end of a FIFO.
736 *
737 * See k_fifo_put_list for the description of the behaviour.
738 *
739 * It takes a pointer to a sys_slist_t object instead of the head and tail of
740 * a custom singly-linked list. The sys_slist_t object is invalid afterwards
741 * and must be re-initialized via sys_slist_init().
742 *
743 * @param fifo FIFO on which to interact.
744 * @param list pointer to singly-linked list
745 *
746 * @return N/A
747 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400748extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400749
750/**
751 * @brief Get an element from the head of a FIFO.
752 *
753 * If no element is available, the function returns NULL. The first word in
754 * the element contains invalid data because its memory location was used to
755 * store a pointer to the next element in the linked list.
756 *
757 * @param fifo FIFO on which to interact.
758 * @param timeout Number of milliseconds to wait for item if FIFO is empty,
759 * or one of the special values K_NO_WAIT and K_FOREVER.
760 *
761 * @warning If it is to be called from the context of an ISR, then @a
762 * timeout must be set to K_NO_WAIT.
763 *
764 * @return Pointer to head element in the list when available.
765 * NULL Otherwise.
766 *
767 * @sa K_NO_WAIT, K_FOREVER
768 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400769extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
770
771#define K_FIFO_INITIALIZER(obj) \
772 { \
773 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400774 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400775 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
776 }
777
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400778/**
779 * @brief Statically define a FIFO and initialize it
780 *
781 * If the FIFO is to be accessed outside the module where it is defined, it
782 * can be declared via
783 *
784 * extern struct k_fifo @a name;
785 *
786 * @param name Name of the FIFO variable.
787 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400788#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500789 struct k_fifo name \
790 __in_section(_k_fifo, static, name) = \
791 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400792
793/* lifos */
794
795struct k_lifo {
796 _wait_q_t wait_q;
797 void *list;
798
799 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
800};
801
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400802/**
803 * @brief Initialize a kernel linked list LIFO object.
804 *
805 * This routine initializes a kernel LIFO object structure. It must not be
806 * called from an ISR.
807 *
808 * @param lifo LIFO to initialize.
809 *
810 * @return N/A
811 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400812extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813
814/**
815 * @brief Prepend an element to a LIFO
816 *
817 * This routine prepends an element to a LIFO. LIFO data items must be
818 * aligned on a 4-byte boundary, as the kernel reserves the first 32 bits of
819 * each item for use as a pointer to the next data item in the LIFO's link
820 * list. Each data item added to the LIFO must include and reserve these first
821 * 32 bits.
822 *
823 * @param lifo LIFO on which to interact.
824 * @param data Data to send.
825 *
826 * @return N/A
827 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400828extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829
830/**
831 * @brief Get the first element from a LIFO.
832 *
833 * If no element is available, the function returns NULL. The first word in
834 * the element contains invalid data because its memory location was used to
835 * store a pointer to the next element in the linked list.
836 *
837 * @param lifo LIFO on which to interact.
838 * @param timeout Number of milliseconds to wait for item if LIFO is empty,
839 * or one of the special values K_NO_WAIT and K_FOREVER.
840 *
841 * @warning If it is to be called from the context of an ISR, then @a
842 * timeout must be set to K_NO_WAIT.
843 *
844 * @return Pointer to head element in the list when available.
845 * NULL Otherwise.
846 *
847 * @sa K_NO_WAIT, K_FOREVER
848 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400849extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
850
851#define K_LIFO_INITIALIZER(obj) \
852 { \
853 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
854 .list = NULL, \
855 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
856 }
857
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858/**
859 * @brief Statically define a LIFO and initialize it
860 *
861 * If the LIFO is to be accessed outside the module where it is defined, it
862 * can be declared via
863 *
864 * extern struct k_lifo @a name;
865 *
866 * @param name Name of the LIFO variable.
867 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400868#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500869 struct k_lifo name \
870 __in_section(_k_lifo, static, name) = \
871 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400872
873/* stacks */
874
875struct k_stack {
876 _wait_q_t wait_q;
877 uint32_t *base, *next, *top;
878
879 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
880};
881
Allan Stephens018cd9a2016-10-07 15:13:24 -0500882extern void k_stack_init(struct k_stack *stack,
883 uint32_t *buffer, int num_entries);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400884extern void k_stack_push(struct k_stack *stack, uint32_t data);
885extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
886
Peter Mitsis602e6a82016-10-17 11:48:43 -0400887#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400888 { \
889 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
890 .base = stack_buffer, \
891 .next = stack_buffer, \
892 .top = stack_buffer + stack_num_entries, \
893 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
894 }
895
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400896/**
897 * @brief Statically define a stack object and initialize it
898 *
899 * If the stack is to be accessed outside the module where it is defined, it
900 * can be declared via
901 *
902 * extern struct k_stack @a name;
903 *
904 * @param name Name of the stack object variable.
905 * @param stack_num_entries Number of entries in the stack object
906 */
Peter Mitsis602e6a82016-10-17 11:48:43 -0400907#define K_STACK_DEFINE(name, stack_num_entries) \
908 uint32_t __noinit \
909 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500910 struct k_stack name \
911 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -0400912 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
913 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400914
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400915/**
916 * workqueues
917 */
918
919struct k_work;
920
921typedef void (*k_work_handler_t)(struct k_work *);
922
923/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400924 * A workqueue is a thread that executes @ref k_work items that are
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400925 * queued to it. This is useful for drivers which need to schedule
926 * execution of code which might sleep from ISR context. The actual
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400927 * thread identifier is not stored in the structure in order to save
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400928 * space.
929 */
930struct k_work_q {
931 struct k_fifo fifo;
932};
933
934/**
935 * @brief Work flags.
936 */
937enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300938 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400939};
940
941/**
942 * @brief An item which can be scheduled on a @ref k_work_q.
943 */
944struct k_work {
945 void *_reserved; /* Used by k_fifo implementation. */
946 k_work_handler_t handler;
947 atomic_t flags[1];
948};
949
950/**
951 * @brief Statically initialize work item
952 */
953#define K_WORK_INITIALIZER(work_handler) \
954 { \
955 ._reserved = NULL, \
956 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300957 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400958 }
959
960/**
961 * @brief Dynamically initialize work item
962 */
963static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
964{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300965 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400966 work->handler = handler;
967}
968
969/**
970 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300971 *
972 * This procedure schedules a work item to be processed.
973 * In the case where the work item has already been submitted and is pending
974 * execution, calling this function will result in a no-op. In this case, the
975 * work item must not be modified externally (e.g. by the caller of this
976 * function), since that could cause the work item to be processed in a
977 * corrupted state.
978 *
979 * @param work_q to schedule the work item
980 * @param work work item
981 *
982 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400983 */
984static inline void k_work_submit_to_queue(struct k_work_q *work_q,
985 struct k_work *work)
986{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300987 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400988 k_fifo_put(&work_q->fifo, work);
989 }
990}
991
992/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300993 * @brief Check if work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400994 *
995 * @param work Work item to query
996 *
997 * @return K_WORK_STATE_PENDING if pending, 0 if not
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300998 */
999static inline int k_work_pending(struct k_work *work)
1000{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001001 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001002}
1003
1004/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001005 * @brief Start a new workqueue.
1006 *
1007 * This routine must not be called from an ISR.
1008 *
1009 * @param work_q Pointer to Work queue
1010 * @param stack Pointer to work queue thread's stack
1011 * @param stack_size Size of the work queue thread's stack
1012 * @param prio Priority of the work queue's thread
1013 *
1014 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001015 */
Allan Stephens904cf972016-10-07 13:59:23 -05001016extern void k_work_q_start(struct k_work_q *work_q, char *stack,
1017 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001018
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001019#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001020
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001021/**
1022 * @brief An item which can be scheduled on a @ref k_work_q with a delay
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001023 */
1024struct k_delayed_work {
1025 struct k_work work;
1026 struct _timeout timeout;
1027 struct k_work_q *work_q;
1028};
1029
1030/**
1031 * @brief Initialize delayed work
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001032 *
1033 * Initialize a delayed work item.
1034 *
1035 * @param work Delayed work item
1036 * @param handler Routine invoked when processing delayed work item
1037 *
1038 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001039 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001040extern void k_delayed_work_init(struct k_delayed_work *work,
1041 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001042
1043/**
1044 * @brief Submit a delayed work item to a workqueue.
1045 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001046 * This routine schedules a work item to be processed after a delay.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001047 * Once the delay has passed, the work item is submitted to the work queue:
1048 * at this point, it is no longer possible to cancel it. Once the work item's
1049 * handler is about to be executed, the work is considered complete and can be
1050 * resubmitted.
1051 *
1052 * Care must be taken if the handler blocks or yield as there is no implicit
1053 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
1054 * it should be explicitly done between the submitter and the handler.
1055 *
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001056 * @param work_q Workqueue to schedule the work item
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001057 * @param work Delayed work item
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001058 * @param delay Delay before scheduling the work item (in milliseconds)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001059 *
1060 * @return 0 in case of success or negative value in case of error.
1061 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001062extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1063 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001064 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001065
1066/**
1067 * @brief Cancel a delayed work item
1068 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001069 * This routine cancels a scheduled work item. If the work has been completed
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001070 * or is idle, this will do nothing. The only case where this can fail is when
1071 * the work has been submitted to the work queue, but the handler has not run
1072 * yet.
1073 *
1074 * @param work Delayed work item to be canceled
1075 *
1076 * @return 0 in case of success or negative value in case of error.
1077 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001078extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001079
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001080#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001081
1082#if defined(CONFIG_SYSTEM_WORKQUEUE)
1083
1084extern struct k_work_q k_sys_work_q;
1085
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001086/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001087 * @brief Submit a work item to the system workqueue.
1088 *
1089 * @ref k_work_submit_to_queue
1090 *
1091 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001092 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001093 * unexpected behavior.
1094 */
1095static inline void k_work_submit(struct k_work *work)
1096{
1097 k_work_submit_to_queue(&k_sys_work_q, work);
1098}
1099
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001100#if defined(CONFIG_SYS_CLOCK_EXISTS)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001101/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001102 * @brief Submit a delayed work item to the system workqueue.
1103 *
1104 * @ref k_delayed_work_submit_to_queue
1105 *
1106 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001107 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001108 * unexpected behavior.
1109 */
1110static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001111 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001112{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001113 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001114}
1115
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001116#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001117#endif /* CONFIG_SYSTEM_WORKQUEUE */
1118
1119/**
1120 * synchronization
1121 */
1122
1123/* mutexes */
1124
1125struct k_mutex {
1126 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001127 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001128 uint32_t lock_count;
1129 int owner_orig_prio;
1130#ifdef CONFIG_OBJECT_MONITOR
1131 int num_lock_state_changes;
1132 int num_conflicts;
1133#endif
1134
1135 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
1136};
1137
1138#ifdef CONFIG_OBJECT_MONITOR
1139#define _MUTEX_INIT_OBJECT_MONITOR \
1140 .num_lock_state_changes = 0, .num_conflicts = 0,
1141#else
1142#define _MUTEX_INIT_OBJECT_MONITOR
1143#endif
1144
1145#define K_MUTEX_INITIALIZER(obj) \
1146 { \
1147 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1148 .owner = NULL, \
1149 .lock_count = 0, \
1150 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1151 _MUTEX_INIT_OBJECT_MONITOR \
1152 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1153 }
1154
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001155/**
1156 * @brief Statically define a mutex object and initialize it
1157 *
1158 * If the mutex is to be accessed outside the module where it is defined, it
1159 * can be declared via
1160 *
1161 * extern struct k_mutex @a name;
1162 *
1163 * @param name Name of the mutex object variable.
1164 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001165#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001166 struct k_mutex name \
1167 __in_section(_k_mutex, static, name) = \
1168 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001169
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001170/**
1171 * @brief Initialize a mutex
1172 *
1173 * Upon initialization, the mutex is available and does not have an owner.
1174 *
1175 * @param mutex Mutex to initialize
1176 *
1177 * @return N/A
1178 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001179extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001180
1181/**
1182 * @brief Lock a mutex
1183 *
1184 * This routine locks mutex @a mutex. When the mutex is locked by another
1185 * thread, the thread calling this function will either wait until the mutex
1186 * becomes available, or until a specified timeout expires.
1187 *
1188 * A thread is permitted to lock a mutex it has already locked; in such a case,
1189 * this routine immediately succeeds and the lock count is increased by 1.
1190 *
1191 * @param mutex Pointer to a mutex object.
1192 * @param timeout Number of milliseconds to wait if mutex is unavailable,
1193 * or one of the special values K_NO_WAIT and K_FOREVER.
1194 *
1195 * @retval 0 When semaphore is obtained successfully.
1196 * @retval -EBUSY Failed to immediately lock mutex when @a timeout is K_NO_WAIT.
1197 * @retval -EAGAIN When timeout expires.
1198 *
1199 * @sa K_NO_WAIT, K_FOREVER
1200 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001201extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001202
1203/**
1204 * @brief Unlock a mutex
1205 *
1206 * This routine unlocks mutex @a mutex. The mutex must already be locked by the
1207 * requesting thread.
1208 *
1209 * The mutex cannot be claimed by another thread until it has been unlocked by
1210 * the requesting thread as many times as it was previously locked by that
1211 * thread.
1212 *
1213 * @param mutex Mutex name.
1214 *
1215 * @return N/A
1216 */
1217
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001218extern void k_mutex_unlock(struct k_mutex *mutex);
1219
1220/* semaphores */
1221
1222struct k_sem {
1223 _wait_q_t wait_q;
1224 unsigned int count;
1225 unsigned int limit;
1226
1227 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
1228};
1229
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001230/**
1231 * @brief Initialize a semaphore object.
1232 *
1233 * An initial count and a count limit can be specified. The count will never go
1234 * over the count limit if the semaphore is given multiple times without being
1235 * taken.
1236 *
1237 * Cannot be called from ISR.
1238 *
1239 * @param sem Pointer to a semaphore object.
1240 * @param initial_count Initial count.
1241 * @param limit Highest value the count can take during operation.
1242 *
1243 * @return N/A
1244 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001245extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1246 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001247
1248/**
1249 * @brief Take a semaphore, possibly pending if not available.
1250 *
1251 * The current execution context tries to obtain the semaphore. If the
1252 * semaphore is unavailable and a timeout other than K_NO_WAIT is specified,
1253 * the context will pend.
1254 *
1255 * @param sem Pointer to a semaphore object.
1256 * @param timeout Number of milliseconds to wait if semaphore is unavailable,
1257 * or one of the special values K_NO_WAIT and K_FOREVER.
1258 *
1259 * @warning If it is called from the context of an ISR, then the only legal
1260 * value for @a timeout is K_NO_WAIT.
1261 *
1262 * @retval 0 When semaphore is obtained successfully.
1263 * @retval -EAGAIN When timeout expires.
1264 * @retval -EBUSY When unavailable and the timeout is K_NO_WAIT.
1265 *
1266 * @sa K_NO_WAIT, K_FOREVER
1267 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001268extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001269
1270/**
1271 * @brief Give a semaphore.
1272 *
1273 * Increase the semaphore's internal count by 1, up to its limit, if no thread
1274 * is waiting on the semaphore; otherwise, wake up the first thread in the
1275 * semaphore's waiting queue.
1276 *
1277 * If the latter case, and if the current context is preemptible, the thread
1278 * that is taken off the wait queue will be scheduled in and will preempt the
1279 * current thread.
1280 *
1281 * @param sem Pointer to a semaphore object.
1282 *
1283 * @return N/A
1284 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001285extern void k_sem_give(struct k_sem *sem);
1286
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001287/**
1288 * @brief Reset a semaphore's count to zero.
1289 *
1290 * The only effect is that the count is set to zero. There is no other
1291 * side-effect to calling this function.
1292 *
1293 * @param sem Pointer to a semaphore object.
1294 *
1295 * @return N/A
1296 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001297static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001298{
1299 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001300}
1301
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001302/**
1303 * @brief Get a semaphore's count.
1304 *
1305 * Note there is no guarantee the count has not changed by the time this
1306 * function returns.
1307 *
1308 * @param sem Pointer to a semaphore object.
1309 *
1310 * @return The current semaphore count.
1311 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001312static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001313{
1314 return sem->count;
1315}
1316
Peter Mitsis45403672016-09-09 14:24:06 -04001317#ifdef CONFIG_SEMAPHORE_GROUPS
1318/**
1319 * @brief Take the first available semaphore
1320 *
1321 * Given a list of semaphore pointers, this routine will attempt to take one
1322 * of them, waiting up to a maximum of @a timeout ms to do so. The taken
1323 * semaphore is identified by @a sem (set to NULL on error).
1324 *
1325 * Be aware that the more semaphores specified in the group, the more stack
1326 * space is required by the waiting thread.
1327 *
1328 * @param sem_array Array of semaphore pointers terminated by a K_END entry
1329 * @param sem Identifies the semaphore that was taken
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001330 * @param timeout Number of milliseconds to wait if semaphores are unavailable,
1331 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsis45403672016-09-09 14:24:06 -04001332 *
1333 * @retval 0 A semaphore was successfully taken
1334 * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT)
1335 * @retval -EAGAIN Time out occurred while waiting for semaphore
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001336 *
1337 * @sa K_NO_WAIT, K_FOREVER
Peter Mitsis45403672016-09-09 14:24:06 -04001338 */
1339
1340extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem,
1341 int32_t timeout);
1342
1343/**
1344 * @brief Give all the semaphores in the group
1345 *
1346 * This routine will give each semaphore in the array of semaphore pointers.
1347 *
1348 * @param sem_array Array of semaphore pointers terminated by a K_END entry
1349 *
1350 * @return N/A
1351 */
1352extern void k_sem_group_give(struct k_sem *sem_array[]);
1353
1354/**
1355 * @brief Reset the count to zero on each semaphore in the array
1356 *
1357 * This routine resets the count of each semaphore in the group to zero.
1358 * Note that it does NOT have any impact on any thread that might have
1359 * been previously pending on any of the semaphores.
1360 *
1361 * @param sem_array Array of semaphore pointers terminated by a K_END entry
1362 *
1363 * @return N/A
1364 */
1365extern void k_sem_group_reset(struct k_sem *sem_array[]);
1366#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001367
1368#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1369 { \
1370 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1371 .count = initial_count, \
1372 .limit = count_limit, \
1373 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1374 }
1375
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001376/**
1377 * @def K_SEM_DEFINE
1378 *
1379 * @brief Statically define and initialize a global semaphore.
1380 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001381 * Create a global semaphore named @a name. It is initialized as if k_sem_init()
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001382 * was called on it. If the semaphore is to be accessed outside the module
1383 * where it is defined, it can be declared via
1384 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001385 * extern struct k_sem @a name;
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001386 *
1387 * @param name Name of the semaphore variable.
1388 * @param initial_count Initial count.
1389 * @param count_limit Highest value the count can take during operation.
1390 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001391#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001392 struct k_sem name \
1393 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001394 K_SEM_INITIALIZER(name, initial_count, count_limit)
1395
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001396/* alerts */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001397
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001398#define K_ALERT_DEFAULT NULL
1399#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001400
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001401typedef int (*k_alert_handler_t)(struct k_alert *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001402
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001403struct k_alert {
1404 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001405 atomic_t send_count;
1406 struct k_work work_item;
1407 struct k_sem sem;
1408
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001409 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001410};
1411
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001412extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001413
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001414#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001415 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001416 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001417 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001418 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001419 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001420 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1421 }
1422
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001423/**
1424 * @brief Statically define and initialize a global alert
1425 *
1426 * Create a global alert named @a name. It is initialized as if k_alert_init()
1427 * was called on it. If the alert is to be accessed outside the module
1428 * where it is defined, it can be declared via
1429 *
1430 * extern struct k_alert @a name;
1431 *
1432 * @param name Alert name
1433 * @param alert_handler Handler to invoke after the delivery of the alert
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001434 * @param max_num_pending_alerts Maximum number of concurrent pending alerts
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001435 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001436#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001437 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001438 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001439 K_ALERT_INITIALIZER(name, alert_handler, \
1440 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001441
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001442/**
1443 * @brief Initialize an alert object.
1444 *
1445 * This routine initializes a kernel alert object structure. It must not be
1446 * called from an ISR.
1447 *
1448 * @param alert Pointer to the alert object
1449 * @param handler Routine to invoke after delivery of alert
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001450 * @param max_num_pending_alerts Maximum number of concurrent pending alerts
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001451 *
1452 * @return N/A
1453 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001454extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1455 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001456
1457/**
1458 * @brief Receive an alert
1459 *
1460 * The current execution context tries to receive the alert. If the
1461 * semaphore is unavailable and a timeout other than K_NO_WAIT is specified,
1462 * the context will pend.
1463 *
1464 * @param alert Pointer to a alert object.
1465 * @param timeout Number of milliseconds to wait if alert is unavailable,
1466 * or one of the special values K_NO_WAIT and K_FOREVER.
1467 *
1468 * @warning If it is called from the context of an ISR, then the only legal
1469 * value for @a timeout is K_NO_WAIT.
1470 *
1471 * @retval 0 When alert is received successfully.
1472 * @retval -EAGAIN When timeout expires.
1473 * @retval -EBUSY When unavailable and the timeout is K_NO_WAIT.
1474 *
1475 * @sa K_NO_WAIT, K_FOREVER
1476 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001477extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001478
1479/**
1480 * @brief Signal an alert
1481 *
1482 * This routine signals the specified alert. If an alert handler is installed
1483 * for that alert, it will run. If no alert handler is installed, any thread
1484 * waiting on the alert is released.
1485 *
1486 * @param alert Alert to signal
1487 *
1488 * @return N/A
1489 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001490extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001491
1492/**
1493 * data transfers (complex)
1494 */
1495
1496/* message queues */
1497
1498struct k_msgq {
1499 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001500 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001501 uint32_t max_msgs;
1502 char *buffer_start;
1503 char *buffer_end;
1504 char *read_ptr;
1505 char *write_ptr;
1506 uint32_t used_msgs;
1507
1508 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1509};
1510
Peter Mitsis1da807e2016-10-06 11:36:59 -04001511#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001512 { \
1513 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001514 .max_msgs = q_max_msgs, \
1515 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001516 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001517 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001518 .read_ptr = q_buffer, \
1519 .write_ptr = q_buffer, \
1520 .used_msgs = 0, \
1521 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1522 }
1523
Peter Mitsis1da807e2016-10-06 11:36:59 -04001524/**
1525 * @brief Define a message queue
1526 *
1527 * This declares and initializes a message queue whose buffer is aligned to
1528 * a @a q_align -byte boundary. The new message queue can be passed to the
1529 * kernel's message queue functions.
1530 *
1531 * Note that for each of the mesages in the message queue to be aligned to
1532 * @a q_align bytes, then @a q_msg_size must be a multiple of @a q_align.
1533 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001534 * If the message queue is to be accessed outside the module where it is
1535 * defined, it can be declared via
1536 *
1537 * extern struct k_msgq @a name;
1538 *
Peter Mitsis1da807e2016-10-06 11:36:59 -04001539 * @param q_name Name of the message queue
1540 * @param q_msg_size The size in bytes of each message
1541 * @param q_max_msgs Maximum number of messages the queue can hold
1542 * @param q_align Alignment of the message queue's buffer (power of 2)
1543 */
1544#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1545 static char __noinit __aligned(q_align) \
1546 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001547 struct k_msgq q_name \
1548 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001549 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1550 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001551
Peter Mitsisd7a37502016-10-13 11:37:40 -04001552/**
1553 * @brief Initialize a message queue.
1554 *
1555 * @param q Pointer to the message queue object.
1556 * @param buffer Pointer to memory area that holds queued messages.
1557 * @param msg_size Message size, in bytes.
1558 * @param max_msgs Maximum number of messages that can be queued.
1559 *
1560 * @return N/A
1561 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001562extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001563 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001564
1565/**
1566 * @brief Add a message to a message queue.
1567 *
1568 * This routine adds an item to the message queue. When the message queue is
1569 * full, the routine will wait either for space to become available, or until
1570 * the specified time limit is reached.
1571 *
1572 * @param q Pointer to the message queue object.
1573 * @param data Pointer to message data area.
1574 * @param timeout Number of milliseconds to wait until space becomes available
1575 * to add the message into the message queue, or one of the
1576 * special values K_NO_WAIT and K_FOREVER.
1577 *
1578 * @return 0 if successful, -ENOMSG if failed immediately or after queue purge,
1579 * -EAGAIN if timed out
1580 *
1581 * @sa K_NO_WAIT, K_FOREVER
1582 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001583extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001584
1585/**
1586 * @brief Obtain a message from a message queue.
1587 *
1588 * This routine fetches the oldest item from the message queue. When the message
1589 * queue is found empty, the routine will wait either until an item is added to
1590 * the message queue or until the specified time limit is reached.
1591 *
1592 * @param q Pointer to the message queue object.
1593 * @param data Pointer to message data area.
1594 * @param timeout Number of milliseconds to wait to obtain message, or one of
1595 * the special values K_NO_WAIT and K_FOREVER.
1596 *
1597 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1598 *
1599 * @sa K_NO_WAIT, K_FOREVER
1600 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001601extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001602
1603/**
1604 * @brief Purge contents of a message queue.
1605 *
1606 * Discards all messages currently in the message queue, and cancels
1607 * any "add message" operations initiated by waiting threads.
1608 *
1609 * @param q Pointer to the message queue object.
1610 *
1611 * @return N/A
1612 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001613extern void k_msgq_purge(struct k_msgq *q);
1614
Peter Mitsis67be2492016-10-07 11:44:34 -04001615/**
1616 * @brief Get the number of unused messages
1617 *
1618 * @param q Message queue to query
1619 *
1620 * @return Number of unused messages
1621 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001622static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001623{
1624 return q->max_msgs - q->used_msgs;
1625}
1626
Peter Mitsisd7a37502016-10-13 11:37:40 -04001627/**
1628 * @brief Get the number of used messages
1629 *
1630 * @param q Message queue to query
1631 *
1632 * @return Number of used messages
1633 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001634static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001635{
1636 return q->used_msgs;
1637}
1638
1639struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001640 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001641 void *addr_in_pool;
1642 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04001643 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001644};
1645
1646/* mailboxes */
1647
1648struct k_mbox_msg {
1649 /** internal use only - needed for legacy API support */
1650 uint32_t _mailbox;
1651 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04001652 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001653 /** application-defined information value */
1654 uint32_t info;
1655 /** sender's message data buffer */
1656 void *tx_data;
1657 /** internal use only - needed for legacy API support */
1658 void *_rx_data;
1659 /** message data block descriptor */
1660 struct k_mem_block tx_block;
1661 /** source thread id */
1662 k_tid_t rx_source_thread;
1663 /** target thread id */
1664 k_tid_t tx_target_thread;
1665 /** internal use only - thread waiting on send (may be a dummy) */
1666 k_tid_t _syncing_thread;
1667#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1668 /** internal use only - semaphore used during asynchronous send */
1669 struct k_sem *_async_sem;
1670#endif
1671};
1672
1673struct k_mbox {
1674 _wait_q_t tx_msg_queue;
1675 _wait_q_t rx_msg_queue;
1676
1677 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1678};
1679
1680#define K_MBOX_INITIALIZER(obj) \
1681 { \
1682 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1683 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1684 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1685 }
1686
Peter Mitsis12092702016-10-14 12:57:23 -04001687/**
1688 * @brief Define a mailbox
1689 *
1690 * This declares and initializes a mailbox. The new mailbox can be passed to
Peter Mitsisd7a37502016-10-13 11:37:40 -04001691 * the kernel's mailbox functions.
Peter Mitsis12092702016-10-14 12:57:23 -04001692 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001693 * If the mailbox is to be accessed outside the module where it is defined, it
1694 * can be declared via
1695 *
1696 * extern struct k_mbox @a name;
1697 *
Peter Mitsis12092702016-10-14 12:57:23 -04001698 * @param name Name of the mailbox
1699 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001700#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001701 struct k_mbox name \
1702 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001703 K_MBOX_INITIALIZER(name) \
1704
Peter Mitsis12092702016-10-14 12:57:23 -04001705/**
1706 * @brief Initialize a mailbox.
1707 *
1708 * @param mbox Pointer to the mailbox object
1709 *
1710 * @return N/A
1711 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001712extern void k_mbox_init(struct k_mbox *mbox);
1713
Peter Mitsis12092702016-10-14 12:57:23 -04001714/**
1715 * @brief Send a mailbox message in a synchronous manner.
1716 *
1717 * Sends a message to a mailbox and waits for a receiver to process it.
1718 * The message data may be in a buffer, in a memory pool block, or non-existent
1719 * (i.e. empty message).
1720 *
1721 * @param mbox Pointer to the mailbox object.
1722 * @param tx_msg Pointer to transmit message descriptor.
1723 * @param timeout Maximum time (milliseconds) to wait for the message to be
1724 * received (although not necessarily completely processed).
1725 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long
1726 * as necessary.
1727 *
1728 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1729 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001730extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001731 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001732
1733#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1734/**
1735 * @brief Send a mailbox message in an asynchronous manner.
1736 *
1737 * Sends a message to a mailbox without waiting for a receiver to process it.
1738 * The message data may be in a buffer, in a memory pool block, or non-existent
1739 * (i.e. an empty message). Optionally, the specified semaphore will be given
1740 * by the mailbox when the message has been both received and disposed of
1741 * by the receiver.
1742 *
1743 * @param mbox Pointer to the mailbox object.
1744 * @param tx_msg Pointer to transmit message descriptor.
1745 * @param sem Semaphore identifier, or NULL if none specified.
1746 *
1747 * @return N/A
1748 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001749extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001750 struct k_sem *sem);
Peter Mitsis12092702016-10-14 12:57:23 -04001751#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001752
Peter Mitsis12092702016-10-14 12:57:23 -04001753/**
1754 * @brief Receive a mailbox message.
1755 *
1756 * Receives a message from a mailbox, then optionally retrieves its data
1757 * and disposes of the message.
1758 *
1759 * @param mbox Pointer to the mailbox object.
1760 * @param rx_msg Pointer to receive message descriptor.
1761 * @param buffer Pointer to buffer to receive data.
1762 * (Use NULL to defer data retrieval and message disposal until later.)
1763 * @param timeout Maximum time (milliseconds) to wait for a message.
1764 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1765 * necessary.
1766 *
1767 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1768 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001769extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001770 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001771
1772/**
1773 * @brief Retrieve mailbox message data into a buffer.
1774 *
1775 * Completes the processing of a received message by retrieving its data
1776 * into a buffer, then disposing of the message.
1777 *
1778 * Alternatively, this routine can be used to dispose of a received message
1779 * without retrieving its data.
1780 *
1781 * @param rx_msg Pointer to receive message descriptor.
1782 * @param buffer Pointer to buffer to receive data. (Use NULL to discard data.)
1783 *
1784 * @return N/A
1785 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001786extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04001787
1788/**
1789 * @brief Retrieve mailbox message data into a memory pool block.
1790 *
1791 * Completes the processing of a received message by retrieving its data
1792 * into a memory pool block, then disposing of the message. The memory pool
1793 * block that results from successful retrieval must be returned to the pool
1794 * once the data has been processed, even in cases where zero bytes of data
1795 * are retrieved.
1796 *
1797 * Alternatively, this routine can be used to dispose of a received message
1798 * without retrieving its data. In this case there is no need to return a
1799 * memory pool block to the pool.
1800 *
1801 * This routine allocates a new memory pool block for the data only if the
1802 * data is not already in one. If a new block cannot be allocated, the routine
1803 * returns a failure code and the received message is left unchanged. This
1804 * permits the caller to reattempt data retrieval at a later time or to dispose
1805 * of the received message without retrieving its data.
1806 *
1807 * @param rx_msg Pointer to receive message descriptor.
1808 * @param pool Memory pool identifier. (Use NULL to discard data.)
1809 * @param block Pointer to area to hold memory pool block info.
1810 * @param timeout Maximum time (milliseconds) to wait for a memory pool block.
1811 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1812 * necessary.
1813 *
1814 * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
1815 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001816extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001817 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001818 struct k_mem_block *block, int32_t timeout);
1819
1820/* pipes */
1821
1822struct k_pipe {
1823 unsigned char *buffer; /* Pipe buffer: may be NULL */
1824 size_t size; /* Buffer size */
1825 size_t bytes_used; /* # bytes used in buffer */
1826 size_t read_index; /* Where in buffer to read from */
1827 size_t write_index; /* Where in buffer to write */
1828
1829 struct {
1830 _wait_q_t readers; /* Reader wait queue */
1831 _wait_q_t writers; /* Writer wait queue */
1832 } wait_q;
1833
1834 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
1835};
1836
Peter Mitsise5d9c582016-10-14 14:44:57 -04001837#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001838 { \
1839 .buffer = pipe_buffer, \
1840 .size = pipe_buffer_size, \
1841 .bytes_used = 0, \
1842 .read_index = 0, \
1843 .write_index = 0, \
1844 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
1845 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
1846 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1847 }
1848
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001849/**
1850 * @brief Define a pipe
1851 *
1852 * This declares and initializes a pipe. The new pipe can be passed to
1853 * the kernel's pipe functions.
1854 *
1855 * If the pipe is to be accessed outside the module where it is defined, it
1856 * can be declared via
1857 *
1858 * extern struct k_pipe @a name;
1859 *
1860 * @param name Name of the mailbox
1861 * @param pipe_buffer_size Size of the pipe's buffer (may be zero)
1862 * @param pipe_align Alignment of the pipe's buffer
1863 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001864#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
1865 static unsigned char __noinit __aligned(pipe_align) \
1866 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001867 struct k_pipe name \
1868 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04001869 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001870
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001871/**
1872 * @brief Runtime initialization of a pipe
1873 *
1874 * @param pipe Pointer to pipe to initialize
1875 * @param buffer Pointer to buffer to use for pipe's ring buffer
1876 * @param size Size of the pipe's ring buffer
1877 *
1878 * @return N/A
1879 */
1880extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
1881 size_t size);
1882
1883/**
1884 * @brief Put a message into the specified pipe
1885 *
1886 * This routine synchronously adds a message into the pipe specified by
1887 * @a pipe. It will wait up to @a timeout for the pipe to accept
Peter Mitsise5d9c582016-10-14 14:44:57 -04001888 * @a bytes_to_write bytes of data. If by @a timeout, the pipe could not
1889 * accept @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001890 * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1891 *
1892 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001893 * @param data Data to put into the pipe
1894 * @param bytes_to_write Desired number of bytes to put into the pipe
1895 * @param bytes_written Number of bytes the pipe accepted
1896 * @param min_xfer Minimum number of bytes accepted for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001897 * @param timeout Maximum number of milliseconds to wait
1898 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001899 * @retval 0 At least @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001900 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001901 * @retval -EAGAIN Fewer than @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001902 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001903extern int k_pipe_put(struct k_pipe *pipe, void *data,
1904 size_t bytes_to_write, size_t *bytes_written,
1905 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001906
1907/**
1908 * @brief Get a message from the specified pipe
1909 *
1910 * This routine synchronously retrieves a message from the pipe specified by
Peter Mitsise5d9c582016-10-14 14:44:57 -04001911 * @a pipe. It will wait up to @a timeout to retrieve @a bytes_to_read
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001912 * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve
Peter Mitsise5d9c582016-10-14 14:44:57 -04001913 * @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001914 * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1915 *
1916 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001917 * @param data Location to place retrieved data
1918 * @param bytes_to_read Desired number of bytes to retrieve from the pipe
1919 * @param bytes_read Number of bytes retrieved from the pipe
1920 * @param min_xfer Minimum number of bytes retrieved for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001921 * @param timeout Maximum number of milliseconds to wait
1922 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001923 * @retval 0 At least @a min_xfer were transferred
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001924 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001925 * @retval -EAGAIN Fewer than @a min_xfer were retrieved
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001926 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001927extern int k_pipe_get(struct k_pipe *pipe, void *data,
1928 size_t bytes_to_read, size_t *bytes_read,
1929 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001930
Peter Mitsis2fef0232016-10-14 14:53:44 -04001931#if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001932/**
1933 * @brief Send a message to the specified pipe
1934 *
1935 * This routine asynchronously sends a message from the pipe specified by
1936 * @a pipe. Once all @a size bytes have been accepted by the pipe, it will
1937 * free the memory block @a block and give the semaphore @a sem (if specified).
1938 * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight
1939 * at any given time.
1940 *
1941 * @param pipe Pointer to the pipe
1942 * @param block Memory block containing data to send
1943 * @param size Number of data bytes in memory block to send
1944 * @param sem Semaphore to signal upon completion (else NULL)
1945 *
1946 * @retval N/A
1947 */
1948extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1949 size_t size, struct k_sem *sem);
Peter Mitsis2fef0232016-10-14 14:53:44 -04001950#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001951
1952/**
1953 * memory management
1954 */
1955
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001956/* memory slabs */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001957
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001958struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001959 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001960 uint32_t num_blocks;
1961 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001962 char *buffer;
1963 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001964 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001965
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001966 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001967};
1968
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001969#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
1970 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001971 { \
1972 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001973 .num_blocks = slab_num_blocks, \
1974 .block_size = slab_block_size, \
1975 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001976 .free_list = NULL, \
1977 .num_used = 0, \
1978 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1979 }
1980
Peter Mitsis578f9112016-10-07 13:50:31 -04001981/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001982 * @brief Define a memory slab allocator
Peter Mitsis578f9112016-10-07 13:50:31 -04001983 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001984 * This declares and initializes a slab allocator whose buffer is aligned to
1985 * a @a slab_align -byte boundary. The new slab allocator can be passed to the
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001986 * kernel's memory slab functions.
Peter Mitsis578f9112016-10-07 13:50:31 -04001987 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001988 * Note that for each of the blocks in the memory slab to be aligned to
1989 * @a slab_align bytes, then @a slab_block_size must be a multiple of
1990 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04001991 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992 * If the slab allocator is to be accessed outside the module where it is
1993 * defined, it can be declared via
1994 *
1995 * extern struct k_mem_slab @a name;
1996 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001997 * @param name Name of the memory slab
1998 * @param slab_block_size Size of each block in the buffer (in bytes)
1999 * @param slab_num_blocks Number blocks in the buffer
2000 * @param slab_align Alignment of the memory slab's buffer (power of 2)
Peter Mitsis578f9112016-10-07 13:50:31 -04002001 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002002#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2003 char __noinit __aligned(slab_align) \
2004 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2005 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002006 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002007 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2008 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002009
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002010/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002011 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002012 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002013 * Initializes the memory slab and creates its list of free blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002014 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002015 * @param slab Pointer to the memory slab object
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002016 * @param buffer Pointer to buffer used for the blocks.
2017 * @param block_size Size of each block, in bytes.
2018 * @param num_blocks Number of blocks.
2019 *
2020 * @return N/A
2021 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002022extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002023 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002024
2025/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002026 * @brief Allocate a memory slab block.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002027 *
2028 * Takes a block from the list of unused blocks.
2029 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002030 * @param slab Pointer to memory slab object.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002031 * @param mem Pointer to area to receive block address.
2032 * @param timeout Maximum time (milliseconds) to wait for allocation to
2033 * complete. Use K_NO_WAIT to return immediately, or K_FOREVER to wait
2034 * as long as necessary.
2035 *
2036 * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
2037 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002038extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2039 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002040
2041/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002042 * @brief Free a memory slab block.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002043 *
2044 * Gives block to a waiting thread if there is one, otherwise returns it to
2045 * the list of unused blocks.
2046 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002047 * @param slab Pointer to memory slab object.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002048 * @param mem Pointer to area to containing block address.
2049 *
2050 * @return N/A
2051 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002052extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002053
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002054/**
2055 * @brief Get the number of used memory blocks
2056 *
2057 * This routine gets the current number of used memory blocks in the
2058 * specified pool. It should be used for stats purposes only as that
2059 * value may potentially be out-of-date by the time it is used.
2060 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002061 * @param slab Memory slab to query
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002062 *
2063 * @return Number of used memory blocks
2064 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002065static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002066{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002067 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002068}
2069
Peter Mitsisc001aa82016-10-13 13:53:37 -04002070/**
2071 * @brief Get the number of unused memory blocks
2072 *
2073 * This routine gets the current number of unused memory blocks in the
2074 * specified pool. It should be used for stats purposes only as that value
2075 * may potentially be out-of-date by the time it is used.
2076 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002077 * @param slab Memory slab to query
Peter Mitsisc001aa82016-10-13 13:53:37 -04002078 *
2079 * @return Number of unused memory blocks
2080 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002081static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002082{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002083 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002084}
2085
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002086/* memory pools */
2087
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002088/*
2089 * Memory pool requires a buffer and two arrays of structures for the
2090 * memory block accounting:
2091 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2092 * status of four blocks of memory.
2093 */
2094struct k_mem_pool_quad_block {
2095 char *mem_blocks; /* pointer to the first of four memory blocks */
2096 uint32_t mem_status; /* four bits. If bit is set, memory block is
2097 allocated */
2098};
2099/*
2100 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2101 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2102 * block size is 4 times less than the previous one and thus requires 4 times
2103 * bigger array of k_mem_pool_quad_block structures to keep track of the
2104 * memory blocks.
2105 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002106
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002107/*
2108 * The array of k_mem_pool_block_set keeps the information of each array of
2109 * k_mem_pool_quad_block structures
2110 */
2111struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002112 size_t block_size; /* memory block size */
2113 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002114 struct k_mem_pool_quad_block *quad_block;
2115 int count;
2116};
2117
2118/* Memory pool descriptor */
2119struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002120 size_t max_block_size;
2121 size_t min_block_size;
2122 uint32_t nr_of_maxblocks;
2123 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002124 struct k_mem_pool_block_set *block_set;
2125 char *bufblock;
2126 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002127 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2128};
2129
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002130#ifdef CONFIG_ARM
2131#define _SECTION_TYPE_SIGN "%"
2132#else
2133#define _SECTION_TYPE_SIGN "@"
2134#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002135
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002136/*
2137 * Static memory pool initialization
2138 */
2139/*
2140 * Use .altmacro to be able to recalculate values and pass them as string
2141 * arguments when calling assembler macros resursively
2142 */
2143__asm__(".altmacro\n\t");
2144
2145/*
2146 * Recursively calls a macro
2147 * The followig global symbols need to be initialized:
2148 * __memory_pool_max_block_size - maximal size of the memory block
2149 * __memory_pool_min_block_size - minimal size of the memory block
2150 * Notes:
2151 * Global symbols are used due the fact that assembler macro allows only
2152 * one argument be passed with the % conversion
2153 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2154 * is used instead of / 4.
2155 * n_max argument needs to go first in the invoked macro, as some
2156 * assemblers concatenate \name and %(\n_max * 4) arguments
2157 * if \name goes first
2158 */
2159__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2160 ".ifge __memory_pool_max_block_size >> 2 -"
2161 " __memory_pool_min_block_size\n\t\t"
2162 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2163 "\\macro_name %(\\n_max * 4) \\name\n\t"
2164 ".endif\n\t"
2165 ".endm\n");
2166
2167/*
2168 * Build quad blocks
2169 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2170 * structures and recursively calls itself for the next array, 4 times
2171 * larger.
2172 * The followig global symbols need to be initialized:
2173 * __memory_pool_max_block_size - maximal size of the memory block
2174 * __memory_pool_min_block_size - minimal size of the memory block
2175 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2176 */
2177__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002178 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002179 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2180 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2181 ".if \\n_max % 4\n\t\t"
2182 ".skip __memory_pool_quad_block_size\n\t"
2183 ".endif\n\t"
2184 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2185 ".endm\n");
2186
2187/*
2188 * Build block sets and initialize them
2189 * Macro initializes the k_mem_pool_block_set structure and
2190 * recursively calls itself for the next one.
2191 * The followig global symbols need to be initialized:
2192 * __memory_pool_max_block_size - maximal size of the memory block
2193 * __memory_pool_min_block_size - minimal size of the memory block
2194 * __memory_pool_block_set_count, the number of the elements in the
2195 * block set array must be set to 0. Macro calculates it's real
2196 * value.
2197 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2198 * structures, _build_quad_blocks must be called prior it.
2199 */
2200__asm__(".macro _build_block_set n_max, name\n\t"
2201 ".int __memory_pool_max_block_size\n\t" /* block_size */
2202 ".if \\n_max % 4\n\t\t"
2203 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2204 ".else\n\t\t"
2205 ".int \\n_max >> 2\n\t"
2206 ".endif\n\t"
2207 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2208 ".int 0\n\t" /* count */
2209 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2210 "__do_recurse _build_block_set \\name \\n_max\n\t"
2211 ".endm\n");
2212
2213/*
2214 * Build a memory pool structure and initialize it
2215 * Macro uses __memory_pool_block_set_count global symbol,
2216 * block set addresses and buffer address, it may be called only after
2217 * _build_block_set
2218 */
2219__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002220 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002221 _SECTION_TYPE_SIGN "progbits\n\t"
2222 ".globl \\name\n\t"
2223 "\\name:\n\t"
2224 ".int \\max_size\n\t" /* max_block_size */
2225 ".int \\min_size\n\t" /* min_block_size */
2226 ".int \\n_max\n\t" /* nr_of_maxblocks */
2227 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2228 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2229 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2230 ".int 0\n\t" /* wait_q->head */
2231 ".int 0\n\t" /* wait_q->next */
2232 ".popsection\n\t"
2233 ".endm\n");
2234
2235#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2236 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2237 _SECTION_TYPE_SIGN "progbits\n\t"); \
2238 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2239 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2240 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2241 STRINGIFY(name) "\n\t"); \
2242 __asm__(".popsection\n\t")
2243
2244#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2245 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2246 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2247 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2248 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002249 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002250 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2251 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2252 STRINGIFY(name) "\n\t"); \
2253 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2254 __asm__(".int __memory_pool_block_set_count\n\t"); \
2255 __asm__(".popsection\n\t"); \
2256 extern uint32_t _mem_pool_block_set_count_##name; \
2257 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2258
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002259#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2260 char __noinit __aligned(align) \
2261 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002262
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002263/**
2264 * @brief Define a memory pool
2265 *
2266 * This declares and initializes a memory pool whose buffer is aligned to
2267 * a @a align -byte boundary. The new memory pool can be passed to the
2268 * kernel's memory pool functions.
2269 *
2270 * Note that for each of the minimum sized blocks to be aligned to @a align
2271 * bytes, then @a min_size must be a multiple of @a align.
2272 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002273 * If the pool is to be accessed outside the module where it is defined, it
2274 * can be declared via
2275 *
2276 * extern struct k_mem_pool @a name;
2277 *
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002278 * @param name Name of the memory pool
2279 * @param min_size Minimum block size in the pool
2280 * @param max_size Maximum block size in the pool
2281 * @param n_max Number of maximum sized blocks in the pool
2282 * @param align Alignment of the memory pool's buffer
2283 */
2284#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002285 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2286 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002287 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002288 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2289 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2290 extern struct k_mem_pool name
2291
2292/*
2293 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2294 * to __memory_pool_quad_block_size absolute symbol.
2295 * This function does not get called, but compiler calculates the value and
2296 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2297 */
2298static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2299{
2300 __asm__(".globl __memory_pool_quad_block_size\n\t"
Andrew Boie431607c2016-10-25 11:47:52 -07002301#ifdef CONFIG_NIOS2
2302 "__memory_pool_quad_block_size = %0\n\t"
2303#else
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002304 "__memory_pool_quad_block_size = %c0\n\t"
Andrew Boie431607c2016-10-25 11:47:52 -07002305#endif
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002306 :
2307 : "n"(sizeof(struct k_mem_pool_quad_block)));
2308}
2309
Peter Mitsis937042c2016-10-13 13:18:26 -04002310/**
2311 * @brief Allocate memory from a memory pool
2312 *
2313 * @param pool Pointer to the memory pool object
2314 * @param block Pointer to the allocated memory's block descriptor
2315 * @param size Minimum number of bytes to allocate
2316 * @param timeout Maximum time (milliseconds) to wait for operation to
2317 * complete. Use K_NO_WAIT to return immediately, or K_FOREVER
2318 * to wait as long as necessary.
2319 *
2320 * @return 0 on success, -ENOMEM on failure
2321 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002322extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002323 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002324
2325/**
2326 * @brief Return previously allocated memory to its memory pool
2327 *
2328 * @param block Pointer to allocated memory's block descriptor
2329 *
2330 * @return N/A
2331 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002332extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002333
2334/**
2335 * @brief Defragment the specified memory pool
2336 *
2337 * @param pool Pointer to the memory pool object
2338 *
2339 * @return N/A
2340 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002341extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002342
2343/**
Allan Stephens480a1312016-10-13 15:44:48 -05002344 * @brief Allocate memory from heap
Peter Mitsis937042c2016-10-13 13:18:26 -04002345 *
Allan Stephens480a1312016-10-13 15:44:48 -05002346 * This routine provides traditional malloc() semantics. The memory is
2347 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002348 *
2349 * @param size Size of memory requested by the caller (in bytes)
2350 *
2351 * @return Address of the allocated memory on success; otherwise NULL
2352 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002353extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002354
2355/**
Allan Stephens480a1312016-10-13 15:44:48 -05002356 * @brief Free memory allocated from heap
2357 *
2358 * This routine provides traditional free() semantics. The memory being
2359 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002360 *
2361 * @param ptr Pointer to previously allocated memory
2362 *
2363 * @return N/A
2364 */
2365extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002366
2367/*
2368 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2369 * hook into the device subsystem, which itself uses nanokernel semaphores,
2370 * and thus currently requires the definition of nano_sem.
2371 */
2372#include <legacy.h>
2373#include <arch/cpu.h>
2374
2375/*
2376 * private APIs that are utilized by one or more public APIs
2377 */
2378
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002379extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002380extern void _init_static_threads(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05002381extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002382
2383#ifdef __cplusplus
2384}
2385#endif
2386
2387#endif /* _kernel__h_ */