blob: 1712442374c5f382dfec83de265fd17f7221dac7 [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
487#define K_TIMER_INITIALIZER(obj) \
488 { \
489 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
490 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
491 }
492
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400493/**
494 * @brief Statically define a timer and initialize it
495 *
496 * If the timer is to be accessed outside the module where it is defined, it
497 * can be declared via
498 *
499 * extern struct k_timer @a name;
500 *
501 * @param name Name of the timer variable.
502 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400503#define K_TIMER_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500504 struct k_timer name \
505 __in_section(_k_timer, static, name) = \
506 K_TIMER_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400507
Allan Stephens45bfa372016-10-12 12:39:42 -0500508/**
509 * @brief Initialize a timer.
510 *
511 * This routine must be called before the timer is used.
512 *
513 * @param timer Address of timer.
514 * @param expiry_fn Function to invoke each time timer expires.
515 * @param stop_fn Function to invoke if timer is stopped while running.
516 *
517 * @return N/A
518 */
519extern void k_timer_init(struct k_timer *timer,
520 void (*expiry_fn)(struct k_timer *),
521 void (*stop_fn)(struct k_timer *));
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700522
Allan Stephens45bfa372016-10-12 12:39:42 -0500523/**
524 * @brief Start a timer.
525 *
526 * This routine starts a timer, and resets its status to zero. The timer
527 * begins counting down using the specified duration and period values.
528 *
529 * Attempting to start a timer that is already running is permitted.
530 * The timer's status is reset to zero and the timer begins counting down
531 * using the new duration and period values.
532 *
533 * @param timer Address of timer.
534 * @param duration Initial timer duration (in milliseconds).
535 * @param period Timer period (in milliseconds).
536 *
537 * @return N/A
538 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400539extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500540 int32_t duration, int32_t period);
541
542/**
543 * @brief Stop a timer.
544 *
545 * This routine stops a running timer prematurely. The timer's stop function,
546 * if one exists, is invoked by the caller.
547 *
548 * Attempting to stop a timer that is not running is permitted, but has no
549 * effect on the timer since it is already stopped.
550 *
551 * @param timer Address of timer.
552 *
553 * @return N/A
554 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400555extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500556
557/**
558 * @brief Read timer status.
559 *
560 * This routine reads the timer's status, which indicates the number of times
561 * it has expired since its status was last read.
562 *
563 * Calling this routine resets the timer's status to zero.
564 *
565 * @param timer Address of timer.
566 *
567 * @return Timer status.
568 */
569extern uint32_t k_timer_status_get(struct k_timer *timer);
570
571/**
572 * @brief Synchronize thread to timer expiration.
573 *
574 * This routine blocks the calling thread until the timer's status is non-zero
575 * (indicating that it has expired at least once since it was last examined)
576 * or the timer is stopped. If the timer status is already non-zero,
577 * or the timer is already stopped, the caller continues without waiting.
578 *
579 * Calling this routine resets the timer's status to zero.
580 *
581 * This routine must not be used by interrupt handlers, since they are not
582 * allowed to block.
583 *
584 * @param timer Address of timer.
585 *
586 * @return Timer status.
587 */
588extern uint32_t k_timer_status_sync(struct k_timer *timer);
589
590/**
591 * @brief Get timer remaining before next timer expiration.
592 *
593 * This routine computes the (approximate) time remaining before a running
594 * timer next expires. If the timer is not running, it returns zero.
595 *
596 * @param timer Address of timer.
597 *
598 * @return Remaining time (in milliseconds).
599 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400600extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400601
602
Allan Stephens45bfa372016-10-12 12:39:42 -0500603/* kernel clocks */
604
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400605/**
606 * @brief Get the time elapsed since the system booted (uptime)
607 *
608 * @return The current uptime of the system in ms
609 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400610extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400611
612/**
613 * @brief Get the lower 32-bit of time elapsed since the system booted (uptime)
614 *
615 * This function is potentially less onerous in both the time it takes to
616 * execute, the interrupt latency it introduces and the amount of 64-bit math
617 * it requires than k_uptime_get(), but it only provides an uptime value of
618 * 32-bits. The user must handle possible rollovers/spillovers.
619 *
620 * At a rate of increment of 1000 per second, it rolls over approximately every
621 * 50 days.
622 *
623 * @return The current uptime of the system in ms
624 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400625extern uint32_t k_uptime_get_32(void);
626
627/**
628 * @brief Get the difference between a reference time and the current uptime
629 *
630 * @param reftime A pointer to a reference time. It is updated with the current
631 * uptime upon return.
632 *
633 * @return The delta between the reference time and the current uptime.
634 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400635extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400636
637/**
638 * @brief Get the difference between a reference time and the current uptime
639 *
640 * The 32-bit version of k_uptime_delta(). It has the same perks and issues as
641 * k_uptime_get_32().
642 *
643 * @param reftime A pointer to a reference time. It is updated with the current
644 * uptime upon return.
645 *
646 * @return The delta between the reference time and the current uptime.
647 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400648extern uint32_t k_uptime_delta_32(int64_t *reftime);
649
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400650/**
651 * @brief Read the platform's timer hardware
652 *
653 * This routine returns the current time in terms of timer hardware clock
654 * cycles.
655 *
656 * @return up counter of elapsed clock cycles
657 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400658extern uint32_t k_cycle_get_32(void);
659
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400660/**
661 * data transfers (basic)
662 */
663
664/* fifos */
665
666struct k_fifo {
667 _wait_q_t wait_q;
668 sys_slist_t data_q;
669
670 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
671};
672
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400673/**
674 * @brief Initialize a kernel FIFO object.
675 *
676 * This routine initializes a kernel FIFO object structure. It must not be
677 * called from an ISR.
678 *
679 * @param fifo FIFO to initialize.
680 *
681 * @return N/A
682 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400683extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684
685/**
686 * @brief Add an element to the end of a FIFO.
687 *
688 * This routine adds an element to the end of a FIFO. FIFO data items must be
689 * aligned on a 4-byte boundary, as the kernel reserves the first 32 bits of
690 * each item for use as a pointer to the next data item in the FIFO's link
691 * list. Each data item added to the FIFO must include and reserve these first
692 * 32 bits.
693 *
694 * @param fifo FIFO on which to interact.
695 * @param data Data to send.
696 *
697 * @return N/A
698 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400699extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400700
701/**
702 * @brief Atomically add a list of elements to the end of a FIFO.
703 *
704 * This routine adds a list of elements in one shot to the end of a FIFO
705 * object. If threads are pending on the FIFO object, they become ready to run.
706 * If this API is called from a preemptible thread, the highest priority one
707 * will preempt the running thread once the put operation is complete.
708 *
709 * If enough threads are waiting on the FIFO, the address of each element given
710 * to threads is returned to the waiting thread. The remaining elements are
711 * linked to the end of the list.
712 *
713 * The list must be a singly-linked list, where each element only has a pointer
714 * to the next one. The list must be NULL-terminated.
715 *
716 * @param fifo FIFO on which to interact.
717 * @param head head of singly-linked list
718 * @param tail tail of singly-linked list
719 *
720 * @return N/A
721 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400722extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400723
724/**
725 * @brief Atomically add a list of elements to the end of a FIFO.
726 *
727 * See k_fifo_put_list for the description of the behaviour.
728 *
729 * It takes a pointer to a sys_slist_t object instead of the head and tail of
730 * a custom singly-linked list. The sys_slist_t object is invalid afterwards
731 * and must be re-initialized via sys_slist_init().
732 *
733 * @param fifo FIFO on which to interact.
734 * @param list pointer to singly-linked list
735 *
736 * @return N/A
737 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400738extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400739
740/**
741 * @brief Get an element from the head of a FIFO.
742 *
743 * If no element is available, the function returns NULL. The first word in
744 * the element contains invalid data because its memory location was used to
745 * store a pointer to the next element in the linked list.
746 *
747 * @param fifo FIFO on which to interact.
748 * @param timeout Number of milliseconds to wait for item if FIFO is empty,
749 * or one of the special values K_NO_WAIT and K_FOREVER.
750 *
751 * @warning If it is to be called from the context of an ISR, then @a
752 * timeout must be set to K_NO_WAIT.
753 *
754 * @return Pointer to head element in the list when available.
755 * NULL Otherwise.
756 *
757 * @sa K_NO_WAIT, K_FOREVER
758 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400759extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
760
761#define K_FIFO_INITIALIZER(obj) \
762 { \
763 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400764 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400765 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
766 }
767
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400768/**
769 * @brief Statically define a FIFO and initialize it
770 *
771 * If the FIFO is to be accessed outside the module where it is defined, it
772 * can be declared via
773 *
774 * extern struct k_fifo @a name;
775 *
776 * @param name Name of the FIFO variable.
777 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400778#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500779 struct k_fifo name \
780 __in_section(_k_fifo, static, name) = \
781 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400782
783/* lifos */
784
785struct k_lifo {
786 _wait_q_t wait_q;
787 void *list;
788
789 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
790};
791
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400792/**
793 * @brief Initialize a kernel linked list LIFO object.
794 *
795 * This routine initializes a kernel LIFO object structure. It must not be
796 * called from an ISR.
797 *
798 * @param lifo LIFO to initialize.
799 *
800 * @return N/A
801 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400802extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803
804/**
805 * @brief Prepend an element to a LIFO
806 *
807 * This routine prepends an element to a LIFO. LIFO data items must be
808 * aligned on a 4-byte boundary, as the kernel reserves the first 32 bits of
809 * each item for use as a pointer to the next data item in the LIFO's link
810 * list. Each data item added to the LIFO must include and reserve these first
811 * 32 bits.
812 *
813 * @param lifo LIFO on which to interact.
814 * @param data Data to send.
815 *
816 * @return N/A
817 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400818extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819
820/**
821 * @brief Get the first element from a LIFO.
822 *
823 * If no element is available, the function returns NULL. The first word in
824 * the element contains invalid data because its memory location was used to
825 * store a pointer to the next element in the linked list.
826 *
827 * @param lifo LIFO on which to interact.
828 * @param timeout Number of milliseconds to wait for item if LIFO is empty,
829 * or one of the special values K_NO_WAIT and K_FOREVER.
830 *
831 * @warning If it is to be called from the context of an ISR, then @a
832 * timeout must be set to K_NO_WAIT.
833 *
834 * @return Pointer to head element in the list when available.
835 * NULL Otherwise.
836 *
837 * @sa K_NO_WAIT, K_FOREVER
838 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400839extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
840
841#define K_LIFO_INITIALIZER(obj) \
842 { \
843 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
844 .list = NULL, \
845 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
846 }
847
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848/**
849 * @brief Statically define a LIFO and initialize it
850 *
851 * If the LIFO is to be accessed outside the module where it is defined, it
852 * can be declared via
853 *
854 * extern struct k_lifo @a name;
855 *
856 * @param name Name of the LIFO variable.
857 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400858#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500859 struct k_lifo name \
860 __in_section(_k_lifo, static, name) = \
861 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400862
863/* stacks */
864
865struct k_stack {
866 _wait_q_t wait_q;
867 uint32_t *base, *next, *top;
868
869 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
870};
871
Allan Stephens018cd9a2016-10-07 15:13:24 -0500872extern void k_stack_init(struct k_stack *stack,
873 uint32_t *buffer, int num_entries);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400874extern void k_stack_push(struct k_stack *stack, uint32_t data);
875extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
876
Peter Mitsis602e6a82016-10-17 11:48:43 -0400877#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400878 { \
879 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
880 .base = stack_buffer, \
881 .next = stack_buffer, \
882 .top = stack_buffer + stack_num_entries, \
883 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
884 }
885
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886/**
887 * @brief Statically define a stack object and initialize it
888 *
889 * If the stack is to be accessed outside the module where it is defined, it
890 * can be declared via
891 *
892 * extern struct k_stack @a name;
893 *
894 * @param name Name of the stack object variable.
895 * @param stack_num_entries Number of entries in the stack object
896 */
Peter Mitsis602e6a82016-10-17 11:48:43 -0400897#define K_STACK_DEFINE(name, stack_num_entries) \
898 uint32_t __noinit \
899 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500900 struct k_stack name \
901 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -0400902 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
903 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400904
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400905/**
906 * workqueues
907 */
908
909struct k_work;
910
911typedef void (*k_work_handler_t)(struct k_work *);
912
913/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400914 * A workqueue is a thread that executes @ref k_work items that are
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400915 * queued to it. This is useful for drivers which need to schedule
916 * execution of code which might sleep from ISR context. The actual
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400917 * thread identifier is not stored in the structure in order to save
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400918 * space.
919 */
920struct k_work_q {
921 struct k_fifo fifo;
922};
923
924/**
925 * @brief Work flags.
926 */
927enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300928 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400929};
930
931/**
932 * @brief An item which can be scheduled on a @ref k_work_q.
933 */
934struct k_work {
935 void *_reserved; /* Used by k_fifo implementation. */
936 k_work_handler_t handler;
937 atomic_t flags[1];
938};
939
940/**
941 * @brief Statically initialize work item
942 */
943#define K_WORK_INITIALIZER(work_handler) \
944 { \
945 ._reserved = NULL, \
946 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300947 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400948 }
949
950/**
951 * @brief Dynamically initialize work item
952 */
953static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
954{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300955 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400956 work->handler = handler;
957}
958
959/**
960 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300961 *
962 * This procedure schedules a work item to be processed.
963 * In the case where the work item has already been submitted and is pending
964 * execution, calling this function will result in a no-op. In this case, the
965 * work item must not be modified externally (e.g. by the caller of this
966 * function), since that could cause the work item to be processed in a
967 * corrupted state.
968 *
969 * @param work_q to schedule the work item
970 * @param work work item
971 *
972 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400973 */
974static inline void k_work_submit_to_queue(struct k_work_q *work_q,
975 struct k_work *work)
976{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300977 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400978 k_fifo_put(&work_q->fifo, work);
979 }
980}
981
982/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300983 * @brief Check if work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400984 *
985 * @param work Work item to query
986 *
987 * @return K_WORK_STATE_PENDING if pending, 0 if not
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300988 */
989static inline int k_work_pending(struct k_work *work)
990{
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300991 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300992}
993
994/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995 * @brief Start a new workqueue.
996 *
997 * This routine must not be called from an ISR.
998 *
999 * @param work_q Pointer to Work queue
1000 * @param stack Pointer to work queue thread's stack
1001 * @param stack_size Size of the work queue thread's stack
1002 * @param prio Priority of the work queue's thread
1003 *
1004 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001005 */
Allan Stephens904cf972016-10-07 13:59:23 -05001006extern void k_work_q_start(struct k_work_q *work_q, char *stack,
1007 unsigned stack_size, unsigned prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001008
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001009#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001010
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001011/**
1012 * @brief An item which can be scheduled on a @ref k_work_q with a delay
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001013 */
1014struct k_delayed_work {
1015 struct k_work work;
1016 struct _timeout timeout;
1017 struct k_work_q *work_q;
1018};
1019
1020/**
1021 * @brief Initialize delayed work
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022 *
1023 * Initialize a delayed work item.
1024 *
1025 * @param work Delayed work item
1026 * @param handler Routine invoked when processing delayed work item
1027 *
1028 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001029 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001030extern void k_delayed_work_init(struct k_delayed_work *work,
1031 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001032
1033/**
1034 * @brief Submit a delayed work item to a workqueue.
1035 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001036 * This routine schedules a work item to be processed after a delay.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001037 * Once the delay has passed, the work item is submitted to the work queue:
1038 * at this point, it is no longer possible to cancel it. Once the work item's
1039 * handler is about to be executed, the work is considered complete and can be
1040 * resubmitted.
1041 *
1042 * Care must be taken if the handler blocks or yield as there is no implicit
1043 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
1044 * it should be explicitly done between the submitter and the handler.
1045 *
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001046 * @param work_q Workqueue to schedule the work item
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001047 * @param work Delayed work item
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001048 * @param delay Delay before scheduling the work item (in milliseconds)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001049 *
1050 * @return 0 in case of success or negative value in case of error.
1051 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001052extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1053 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001054 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001055
1056/**
1057 * @brief Cancel a delayed work item
1058 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001059 * This routine cancels a scheduled work item. If the work has been completed
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001060 * or is idle, this will do nothing. The only case where this can fail is when
1061 * the work has been submitted to the work queue, but the handler has not run
1062 * yet.
1063 *
1064 * @param work Delayed work item to be canceled
1065 *
1066 * @return 0 in case of success or negative value in case of error.
1067 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001068extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001069
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001070#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001071
1072#if defined(CONFIG_SYSTEM_WORKQUEUE)
1073
1074extern struct k_work_q k_sys_work_q;
1075
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001076/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001077 * @brief Submit a work item to the system workqueue.
1078 *
1079 * @ref k_work_submit_to_queue
1080 *
1081 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001083 * unexpected behavior.
1084 */
1085static inline void k_work_submit(struct k_work *work)
1086{
1087 k_work_submit_to_queue(&k_sys_work_q, work);
1088}
1089
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001090#if defined(CONFIG_SYS_CLOCK_EXISTS)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001091/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001092 * @brief Submit a delayed work item to the system workqueue.
1093 *
1094 * @ref k_delayed_work_submit_to_queue
1095 *
1096 * When using the system workqueue it is not recommended to block or yield
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001097 * on the handler since its thread is shared system wide it may cause
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001098 * unexpected behavior.
1099 */
1100static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001101 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001102{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001103 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001104}
1105
Benjamin Walsh1a5450b2016-10-06 15:04:23 -04001106#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001107#endif /* CONFIG_SYSTEM_WORKQUEUE */
1108
1109/**
1110 * synchronization
1111 */
1112
1113/* mutexes */
1114
1115struct k_mutex {
1116 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001117 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001118 uint32_t lock_count;
1119 int owner_orig_prio;
1120#ifdef CONFIG_OBJECT_MONITOR
1121 int num_lock_state_changes;
1122 int num_conflicts;
1123#endif
1124
1125 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
1126};
1127
1128#ifdef CONFIG_OBJECT_MONITOR
1129#define _MUTEX_INIT_OBJECT_MONITOR \
1130 .num_lock_state_changes = 0, .num_conflicts = 0,
1131#else
1132#define _MUTEX_INIT_OBJECT_MONITOR
1133#endif
1134
1135#define K_MUTEX_INITIALIZER(obj) \
1136 { \
1137 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1138 .owner = NULL, \
1139 .lock_count = 0, \
1140 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1141 _MUTEX_INIT_OBJECT_MONITOR \
1142 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1143 }
1144
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145/**
1146 * @brief Statically define a mutex object and initialize it
1147 *
1148 * If the mutex is to be accessed outside the module where it is defined, it
1149 * can be declared via
1150 *
1151 * extern struct k_mutex @a name;
1152 *
1153 * @param name Name of the mutex object variable.
1154 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001155#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001156 struct k_mutex name \
1157 __in_section(_k_mutex, static, name) = \
1158 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001159
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001160/**
1161 * @brief Initialize a mutex
1162 *
1163 * Upon initialization, the mutex is available and does not have an owner.
1164 *
1165 * @param mutex Mutex to initialize
1166 *
1167 * @return N/A
1168 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001169extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001170
1171/**
1172 * @brief Lock a mutex
1173 *
1174 * This routine locks mutex @a mutex. When the mutex is locked by another
1175 * thread, the thread calling this function will either wait until the mutex
1176 * becomes available, or until a specified timeout expires.
1177 *
1178 * A thread is permitted to lock a mutex it has already locked; in such a case,
1179 * this routine immediately succeeds and the lock count is increased by 1.
1180 *
1181 * @param mutex Pointer to a mutex object.
1182 * @param timeout Number of milliseconds to wait if mutex is unavailable,
1183 * or one of the special values K_NO_WAIT and K_FOREVER.
1184 *
1185 * @retval 0 When semaphore is obtained successfully.
1186 * @retval -EBUSY Failed to immediately lock mutex when @a timeout is K_NO_WAIT.
1187 * @retval -EAGAIN When timeout expires.
1188 *
1189 * @sa K_NO_WAIT, K_FOREVER
1190 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001191extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001192
1193/**
1194 * @brief Unlock a mutex
1195 *
1196 * This routine unlocks mutex @a mutex. The mutex must already be locked by the
1197 * requesting thread.
1198 *
1199 * The mutex cannot be claimed by another thread until it has been unlocked by
1200 * the requesting thread as many times as it was previously locked by that
1201 * thread.
1202 *
1203 * @param mutex Mutex name.
1204 *
1205 * @return N/A
1206 */
1207
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001208extern void k_mutex_unlock(struct k_mutex *mutex);
1209
1210/* semaphores */
1211
1212struct k_sem {
1213 _wait_q_t wait_q;
1214 unsigned int count;
1215 unsigned int limit;
1216
1217 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
1218};
1219
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001220/**
1221 * @brief Initialize a semaphore object.
1222 *
1223 * An initial count and a count limit can be specified. The count will never go
1224 * over the count limit if the semaphore is given multiple times without being
1225 * taken.
1226 *
1227 * Cannot be called from ISR.
1228 *
1229 * @param sem Pointer to a semaphore object.
1230 * @param initial_count Initial count.
1231 * @param limit Highest value the count can take during operation.
1232 *
1233 * @return N/A
1234 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001235extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1236 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001237
1238/**
1239 * @brief Take a semaphore, possibly pending if not available.
1240 *
1241 * The current execution context tries to obtain the semaphore. If the
1242 * semaphore is unavailable and a timeout other than K_NO_WAIT is specified,
1243 * the context will pend.
1244 *
1245 * @param sem Pointer to a semaphore object.
1246 * @param timeout Number of milliseconds to wait if semaphore is unavailable,
1247 * or one of the special values K_NO_WAIT and K_FOREVER.
1248 *
1249 * @warning If it is called from the context of an ISR, then the only legal
1250 * value for @a timeout is K_NO_WAIT.
1251 *
1252 * @retval 0 When semaphore is obtained successfully.
1253 * @retval -EAGAIN When timeout expires.
1254 * @retval -EBUSY When unavailable and the timeout is K_NO_WAIT.
1255 *
1256 * @sa K_NO_WAIT, K_FOREVER
1257 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001258extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001259
1260/**
1261 * @brief Give a semaphore.
1262 *
1263 * Increase the semaphore's internal count by 1, up to its limit, if no thread
1264 * is waiting on the semaphore; otherwise, wake up the first thread in the
1265 * semaphore's waiting queue.
1266 *
1267 * If the latter case, and if the current context is preemptible, the thread
1268 * that is taken off the wait queue will be scheduled in and will preempt the
1269 * current thread.
1270 *
1271 * @param sem Pointer to a semaphore object.
1272 *
1273 * @return N/A
1274 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001275extern void k_sem_give(struct k_sem *sem);
1276
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001277/**
1278 * @brief Reset a semaphore's count to zero.
1279 *
1280 * The only effect is that the count is set to zero. There is no other
1281 * side-effect to calling this function.
1282 *
1283 * @param sem Pointer to a semaphore object.
1284 *
1285 * @return N/A
1286 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001287static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001288{
1289 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001290}
1291
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001292/**
1293 * @brief Get a semaphore's count.
1294 *
1295 * Note there is no guarantee the count has not changed by the time this
1296 * function returns.
1297 *
1298 * @param sem Pointer to a semaphore object.
1299 *
1300 * @return The current semaphore count.
1301 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001302static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001303{
1304 return sem->count;
1305}
1306
Peter Mitsis45403672016-09-09 14:24:06 -04001307#ifdef CONFIG_SEMAPHORE_GROUPS
1308/**
1309 * @brief Take the first available semaphore
1310 *
1311 * Given a list of semaphore pointers, this routine will attempt to take one
1312 * of them, waiting up to a maximum of @a timeout ms to do so. The taken
1313 * semaphore is identified by @a sem (set to NULL on error).
1314 *
1315 * Be aware that the more semaphores specified in the group, the more stack
1316 * space is required by the waiting thread.
1317 *
1318 * @param sem_array Array of semaphore pointers terminated by a K_END entry
1319 * @param sem Identifies the semaphore that was taken
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001320 * @param timeout Number of milliseconds to wait if semaphores are unavailable,
1321 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsis45403672016-09-09 14:24:06 -04001322 *
1323 * @retval 0 A semaphore was successfully taken
1324 * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT)
1325 * @retval -EAGAIN Time out occurred while waiting for semaphore
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001326 *
1327 * @sa K_NO_WAIT, K_FOREVER
Peter Mitsis45403672016-09-09 14:24:06 -04001328 */
1329
1330extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem,
1331 int32_t timeout);
1332
1333/**
1334 * @brief Give all the semaphores in the group
1335 *
1336 * This routine will give each semaphore in the array of semaphore pointers.
1337 *
1338 * @param sem_array Array of semaphore pointers terminated by a K_END entry
1339 *
1340 * @return N/A
1341 */
1342extern void k_sem_group_give(struct k_sem *sem_array[]);
1343
1344/**
1345 * @brief Reset the count to zero on each semaphore in the array
1346 *
1347 * This routine resets the count of each semaphore in the group to zero.
1348 * Note that it does NOT have any impact on any thread that might have
1349 * been previously pending on any of the semaphores.
1350 *
1351 * @param sem_array Array of semaphore pointers terminated by a K_END entry
1352 *
1353 * @return N/A
1354 */
1355extern void k_sem_group_reset(struct k_sem *sem_array[]);
1356#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001357
1358#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1359 { \
1360 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1361 .count = initial_count, \
1362 .limit = count_limit, \
1363 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1364 }
1365
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001366/**
1367 * @def K_SEM_DEFINE
1368 *
1369 * @brief Statically define and initialize a global semaphore.
1370 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001371 * Create a global semaphore named @a name. It is initialized as if k_sem_init()
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001372 * was called on it. If the semaphore is to be accessed outside the module
1373 * where it is defined, it can be declared via
1374 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001375 * extern struct k_sem @a name;
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001376 *
1377 * @param name Name of the semaphore variable.
1378 * @param initial_count Initial count.
1379 * @param count_limit Highest value the count can take during operation.
1380 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001381#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001382 struct k_sem name \
1383 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001384 K_SEM_INITIALIZER(name, initial_count, count_limit)
1385
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001386/* alerts */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001387
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001388#define K_ALERT_DEFAULT NULL
1389#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001390
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001391typedef int (*k_alert_handler_t)(struct k_alert *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001392
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001393struct k_alert {
1394 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001395 atomic_t send_count;
1396 struct k_work work_item;
1397 struct k_sem sem;
1398
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001399 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001400};
1401
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001402extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001404#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001405 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001406 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001407 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001408 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001409 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001410 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1411 }
1412
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001413/**
1414 * @brief Statically define and initialize a global alert
1415 *
1416 * Create a global alert named @a name. It is initialized as if k_alert_init()
1417 * was called on it. If the alert is to be accessed outside the module
1418 * where it is defined, it can be declared via
1419 *
1420 * extern struct k_alert @a name;
1421 *
1422 * @param name Alert name
1423 * @param alert_handler Handler to invoke after the delivery of the alert
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001424 * @param max_num_pending_alerts Maximum number of concurrent pending alerts
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001425 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001426#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001427 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001428 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001429 K_ALERT_INITIALIZER(name, alert_handler, \
1430 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001431
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001432/**
1433 * @brief Initialize an alert object.
1434 *
1435 * This routine initializes a kernel alert object structure. It must not be
1436 * called from an ISR.
1437 *
1438 * @param alert Pointer to the alert object
1439 * @param handler Routine to invoke after delivery of alert
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001440 * @param max_num_pending_alerts Maximum number of concurrent pending alerts
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001441 *
1442 * @return N/A
1443 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001444extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1445 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001446
1447/**
1448 * @brief Receive an alert
1449 *
1450 * The current execution context tries to receive the alert. If the
1451 * semaphore is unavailable and a timeout other than K_NO_WAIT is specified,
1452 * the context will pend.
1453 *
1454 * @param alert Pointer to a alert object.
1455 * @param timeout Number of milliseconds to wait if alert is unavailable,
1456 * or one of the special values K_NO_WAIT and K_FOREVER.
1457 *
1458 * @warning If it is called from the context of an ISR, then the only legal
1459 * value for @a timeout is K_NO_WAIT.
1460 *
1461 * @retval 0 When alert is received successfully.
1462 * @retval -EAGAIN When timeout expires.
1463 * @retval -EBUSY When unavailable and the timeout is K_NO_WAIT.
1464 *
1465 * @sa K_NO_WAIT, K_FOREVER
1466 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001467extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001468
1469/**
1470 * @brief Signal an alert
1471 *
1472 * This routine signals the specified alert. If an alert handler is installed
1473 * for that alert, it will run. If no alert handler is installed, any thread
1474 * waiting on the alert is released.
1475 *
1476 * @param alert Alert to signal
1477 *
1478 * @return N/A
1479 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001480extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001481
1482/**
1483 * data transfers (complex)
1484 */
1485
1486/* message queues */
1487
1488struct k_msgq {
1489 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001490 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001491 uint32_t max_msgs;
1492 char *buffer_start;
1493 char *buffer_end;
1494 char *read_ptr;
1495 char *write_ptr;
1496 uint32_t used_msgs;
1497
1498 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1499};
1500
Peter Mitsis1da807e2016-10-06 11:36:59 -04001501#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001502 { \
1503 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001504 .max_msgs = q_max_msgs, \
1505 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001506 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001507 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001508 .read_ptr = q_buffer, \
1509 .write_ptr = q_buffer, \
1510 .used_msgs = 0, \
1511 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1512 }
1513
Peter Mitsis1da807e2016-10-06 11:36:59 -04001514/**
1515 * @brief Define a message queue
1516 *
1517 * This declares and initializes a message queue whose buffer is aligned to
1518 * a @a q_align -byte boundary. The new message queue can be passed to the
1519 * kernel's message queue functions.
1520 *
1521 * Note that for each of the mesages in the message queue to be aligned to
1522 * @a q_align bytes, then @a q_msg_size must be a multiple of @a q_align.
1523 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001524 * If the message queue is to be accessed outside the module where it is
1525 * defined, it can be declared via
1526 *
1527 * extern struct k_msgq @a name;
1528 *
Peter Mitsis1da807e2016-10-06 11:36:59 -04001529 * @param q_name Name of the message queue
1530 * @param q_msg_size The size in bytes of each message
1531 * @param q_max_msgs Maximum number of messages the queue can hold
1532 * @param q_align Alignment of the message queue's buffer (power of 2)
1533 */
1534#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1535 static char __noinit __aligned(q_align) \
1536 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001537 struct k_msgq q_name \
1538 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001539 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1540 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001541
Peter Mitsisd7a37502016-10-13 11:37:40 -04001542/**
1543 * @brief Initialize a message queue.
1544 *
1545 * @param q Pointer to the message queue object.
1546 * @param buffer Pointer to memory area that holds queued messages.
1547 * @param msg_size Message size, in bytes.
1548 * @param max_msgs Maximum number of messages that can be queued.
1549 *
1550 * @return N/A
1551 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001552extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001553 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001554
1555/**
1556 * @brief Add a message to a message queue.
1557 *
1558 * This routine adds an item to the message queue. When the message queue is
1559 * full, the routine will wait either for space to become available, or until
1560 * the specified time limit is reached.
1561 *
1562 * @param q Pointer to the message queue object.
1563 * @param data Pointer to message data area.
1564 * @param timeout Number of milliseconds to wait until space becomes available
1565 * to add the message into the message queue, or one of the
1566 * special values K_NO_WAIT and K_FOREVER.
1567 *
1568 * @return 0 if successful, -ENOMSG if failed immediately or after queue purge,
1569 * -EAGAIN if timed out
1570 *
1571 * @sa K_NO_WAIT, K_FOREVER
1572 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001573extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001574
1575/**
1576 * @brief Obtain a message from a message queue.
1577 *
1578 * This routine fetches the oldest item from the message queue. When the message
1579 * queue is found empty, the routine will wait either until an item is added to
1580 * the message queue or until the specified time limit is reached.
1581 *
1582 * @param q Pointer to the message queue object.
1583 * @param data Pointer to message data area.
1584 * @param timeout Number of milliseconds to wait to obtain message, or one of
1585 * the special values K_NO_WAIT and K_FOREVER.
1586 *
1587 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1588 *
1589 * @sa K_NO_WAIT, K_FOREVER
1590 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001591extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001592
1593/**
1594 * @brief Purge contents of a message queue.
1595 *
1596 * Discards all messages currently in the message queue, and cancels
1597 * any "add message" operations initiated by waiting threads.
1598 *
1599 * @param q Pointer to the message queue object.
1600 *
1601 * @return N/A
1602 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001603extern void k_msgq_purge(struct k_msgq *q);
1604
Peter Mitsis67be2492016-10-07 11:44:34 -04001605/**
1606 * @brief Get the number of unused messages
1607 *
1608 * @param q Message queue to query
1609 *
1610 * @return Number of unused messages
1611 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001612static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001613{
1614 return q->max_msgs - q->used_msgs;
1615}
1616
Peter Mitsisd7a37502016-10-13 11:37:40 -04001617/**
1618 * @brief Get the number of used messages
1619 *
1620 * @param q Message queue to query
1621 *
1622 * @return Number of used messages
1623 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001624static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001625{
1626 return q->used_msgs;
1627}
1628
1629struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001630 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001631 void *addr_in_pool;
1632 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04001633 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001634};
1635
1636/* mailboxes */
1637
1638struct k_mbox_msg {
1639 /** internal use only - needed for legacy API support */
1640 uint32_t _mailbox;
1641 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04001642 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001643 /** application-defined information value */
1644 uint32_t info;
1645 /** sender's message data buffer */
1646 void *tx_data;
1647 /** internal use only - needed for legacy API support */
1648 void *_rx_data;
1649 /** message data block descriptor */
1650 struct k_mem_block tx_block;
1651 /** source thread id */
1652 k_tid_t rx_source_thread;
1653 /** target thread id */
1654 k_tid_t tx_target_thread;
1655 /** internal use only - thread waiting on send (may be a dummy) */
1656 k_tid_t _syncing_thread;
1657#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1658 /** internal use only - semaphore used during asynchronous send */
1659 struct k_sem *_async_sem;
1660#endif
1661};
1662
1663struct k_mbox {
1664 _wait_q_t tx_msg_queue;
1665 _wait_q_t rx_msg_queue;
1666
1667 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1668};
1669
1670#define K_MBOX_INITIALIZER(obj) \
1671 { \
1672 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1673 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1674 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1675 }
1676
Peter Mitsis12092702016-10-14 12:57:23 -04001677/**
1678 * @brief Define a mailbox
1679 *
1680 * This declares and initializes a mailbox. The new mailbox can be passed to
Peter Mitsisd7a37502016-10-13 11:37:40 -04001681 * the kernel's mailbox functions.
Peter Mitsis12092702016-10-14 12:57:23 -04001682 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001683 * If the mailbox is to be accessed outside the module where it is defined, it
1684 * can be declared via
1685 *
1686 * extern struct k_mbox @a name;
1687 *
Peter Mitsis12092702016-10-14 12:57:23 -04001688 * @param name Name of the mailbox
1689 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001690#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001691 struct k_mbox name \
1692 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001693 K_MBOX_INITIALIZER(name) \
1694
Peter Mitsis12092702016-10-14 12:57:23 -04001695/**
1696 * @brief Initialize a mailbox.
1697 *
1698 * @param mbox Pointer to the mailbox object
1699 *
1700 * @return N/A
1701 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001702extern void k_mbox_init(struct k_mbox *mbox);
1703
Peter Mitsis12092702016-10-14 12:57:23 -04001704/**
1705 * @brief Send a mailbox message in a synchronous manner.
1706 *
1707 * Sends a message to a mailbox and waits for a receiver to process it.
1708 * The message data may be in a buffer, in a memory pool block, or non-existent
1709 * (i.e. empty message).
1710 *
1711 * @param mbox Pointer to the mailbox object.
1712 * @param tx_msg Pointer to transmit message descriptor.
1713 * @param timeout Maximum time (milliseconds) to wait for the message to be
1714 * received (although not necessarily completely processed).
1715 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long
1716 * as necessary.
1717 *
1718 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1719 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001720extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001721 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001722
1723#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1724/**
1725 * @brief Send a mailbox message in an asynchronous manner.
1726 *
1727 * Sends a message to a mailbox without waiting for a receiver to process it.
1728 * The message data may be in a buffer, in a memory pool block, or non-existent
1729 * (i.e. an empty message). Optionally, the specified semaphore will be given
1730 * by the mailbox when the message has been both received and disposed of
1731 * by the receiver.
1732 *
1733 * @param mbox Pointer to the mailbox object.
1734 * @param tx_msg Pointer to transmit message descriptor.
1735 * @param sem Semaphore identifier, or NULL if none specified.
1736 *
1737 * @return N/A
1738 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001739extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001740 struct k_sem *sem);
Peter Mitsis12092702016-10-14 12:57:23 -04001741#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001742
Peter Mitsis12092702016-10-14 12:57:23 -04001743/**
1744 * @brief Receive a mailbox message.
1745 *
1746 * Receives a message from a mailbox, then optionally retrieves its data
1747 * and disposes of the message.
1748 *
1749 * @param mbox Pointer to the mailbox object.
1750 * @param rx_msg Pointer to receive message descriptor.
1751 * @param buffer Pointer to buffer to receive data.
1752 * (Use NULL to defer data retrieval and message disposal until later.)
1753 * @param timeout Maximum time (milliseconds) to wait for a message.
1754 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1755 * necessary.
1756 *
1757 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1758 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001759extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001760 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001761
1762/**
1763 * @brief Retrieve mailbox message data into a buffer.
1764 *
1765 * Completes the processing of a received message by retrieving its data
1766 * into a buffer, then disposing of the message.
1767 *
1768 * Alternatively, this routine can be used to dispose of a received message
1769 * without retrieving its data.
1770 *
1771 * @param rx_msg Pointer to receive message descriptor.
1772 * @param buffer Pointer to buffer to receive data. (Use NULL to discard data.)
1773 *
1774 * @return N/A
1775 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001776extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04001777
1778/**
1779 * @brief Retrieve mailbox message data into a memory pool block.
1780 *
1781 * Completes the processing of a received message by retrieving its data
1782 * into a memory pool block, then disposing of the message. The memory pool
1783 * block that results from successful retrieval must be returned to the pool
1784 * once the data has been processed, even in cases where zero bytes of data
1785 * are retrieved.
1786 *
1787 * Alternatively, this routine can be used to dispose of a received message
1788 * without retrieving its data. In this case there is no need to return a
1789 * memory pool block to the pool.
1790 *
1791 * This routine allocates a new memory pool block for the data only if the
1792 * data is not already in one. If a new block cannot be allocated, the routine
1793 * returns a failure code and the received message is left unchanged. This
1794 * permits the caller to reattempt data retrieval at a later time or to dispose
1795 * of the received message without retrieving its data.
1796 *
1797 * @param rx_msg Pointer to receive message descriptor.
1798 * @param pool Memory pool identifier. (Use NULL to discard data.)
1799 * @param block Pointer to area to hold memory pool block info.
1800 * @param timeout Maximum time (milliseconds) to wait for a memory pool block.
1801 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1802 * necessary.
1803 *
1804 * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
1805 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001806extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001807 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001808 struct k_mem_block *block, int32_t timeout);
1809
1810/* pipes */
1811
1812struct k_pipe {
1813 unsigned char *buffer; /* Pipe buffer: may be NULL */
1814 size_t size; /* Buffer size */
1815 size_t bytes_used; /* # bytes used in buffer */
1816 size_t read_index; /* Where in buffer to read from */
1817 size_t write_index; /* Where in buffer to write */
1818
1819 struct {
1820 _wait_q_t readers; /* Reader wait queue */
1821 _wait_q_t writers; /* Writer wait queue */
1822 } wait_q;
1823
1824 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
1825};
1826
Peter Mitsise5d9c582016-10-14 14:44:57 -04001827#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001828 { \
1829 .buffer = pipe_buffer, \
1830 .size = pipe_buffer_size, \
1831 .bytes_used = 0, \
1832 .read_index = 0, \
1833 .write_index = 0, \
1834 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
1835 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
1836 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1837 }
1838
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001839/**
1840 * @brief Define a pipe
1841 *
1842 * This declares and initializes a pipe. The new pipe can be passed to
1843 * the kernel's pipe functions.
1844 *
1845 * If the pipe is to be accessed outside the module where it is defined, it
1846 * can be declared via
1847 *
1848 * extern struct k_pipe @a name;
1849 *
1850 * @param name Name of the mailbox
1851 * @param pipe_buffer_size Size of the pipe's buffer (may be zero)
1852 * @param pipe_align Alignment of the pipe's buffer
1853 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001854#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
1855 static unsigned char __noinit __aligned(pipe_align) \
1856 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001857 struct k_pipe name \
1858 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04001859 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001860
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001861/**
1862 * @brief Runtime initialization of a pipe
1863 *
1864 * @param pipe Pointer to pipe to initialize
1865 * @param buffer Pointer to buffer to use for pipe's ring buffer
1866 * @param size Size of the pipe's ring buffer
1867 *
1868 * @return N/A
1869 */
1870extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
1871 size_t size);
1872
1873/**
1874 * @brief Put a message into the specified pipe
1875 *
1876 * This routine synchronously adds a message into the pipe specified by
1877 * @a pipe. It will wait up to @a timeout for the pipe to accept
Peter Mitsise5d9c582016-10-14 14:44:57 -04001878 * @a bytes_to_write bytes of data. If by @a timeout, the pipe could not
1879 * accept @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001880 * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1881 *
1882 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001883 * @param data Data to put into the pipe
1884 * @param bytes_to_write Desired number of bytes to put into the pipe
1885 * @param bytes_written Number of bytes the pipe accepted
1886 * @param min_xfer Minimum number of bytes accepted for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001887 * @param timeout Maximum number of milliseconds to wait
1888 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001889 * @retval 0 At least @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001890 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001891 * @retval -EAGAIN Fewer than @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001892 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001893extern int k_pipe_put(struct k_pipe *pipe, void *data,
1894 size_t bytes_to_write, size_t *bytes_written,
1895 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001896
1897/**
1898 * @brief Get a message from the specified pipe
1899 *
1900 * This routine synchronously retrieves a message from the pipe specified by
Peter Mitsise5d9c582016-10-14 14:44:57 -04001901 * @a pipe. It will wait up to @a timeout to retrieve @a bytes_to_read
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001902 * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve
Peter Mitsise5d9c582016-10-14 14:44:57 -04001903 * @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001904 * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1905 *
1906 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001907 * @param data Location to place retrieved data
1908 * @param bytes_to_read Desired number of bytes to retrieve from the pipe
1909 * @param bytes_read Number of bytes retrieved from the pipe
1910 * @param min_xfer Minimum number of bytes retrieved for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001911 * @param timeout Maximum number of milliseconds to wait
1912 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001913 * @retval 0 At least @a min_xfer were transferred
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001914 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001915 * @retval -EAGAIN Fewer than @a min_xfer were retrieved
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001916 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001917extern int k_pipe_get(struct k_pipe *pipe, void *data,
1918 size_t bytes_to_read, size_t *bytes_read,
1919 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001920
Peter Mitsis2fef0232016-10-14 14:53:44 -04001921#if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001922/**
1923 * @brief Send a message to the specified pipe
1924 *
1925 * This routine asynchronously sends a message from the pipe specified by
1926 * @a pipe. Once all @a size bytes have been accepted by the pipe, it will
1927 * free the memory block @a block and give the semaphore @a sem (if specified).
1928 * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight
1929 * at any given time.
1930 *
1931 * @param pipe Pointer to the pipe
1932 * @param block Memory block containing data to send
1933 * @param size Number of data bytes in memory block to send
1934 * @param sem Semaphore to signal upon completion (else NULL)
1935 *
1936 * @retval N/A
1937 */
1938extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1939 size_t size, struct k_sem *sem);
Peter Mitsis2fef0232016-10-14 14:53:44 -04001940#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001941
1942/**
1943 * memory management
1944 */
1945
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001946/* memory slabs */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001947
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001948struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001949 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001950 uint32_t num_blocks;
1951 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001952 char *buffer;
1953 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04001954 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001955
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001956 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001957};
1958
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001959#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
1960 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001961 { \
1962 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001963 .num_blocks = slab_num_blocks, \
1964 .block_size = slab_block_size, \
1965 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001966 .free_list = NULL, \
1967 .num_used = 0, \
1968 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1969 }
1970
Peter Mitsis578f9112016-10-07 13:50:31 -04001971/**
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001972 * @brief Define a memory slab allocator
Peter Mitsis578f9112016-10-07 13:50:31 -04001973 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001974 * This declares and initializes a slab allocator whose buffer is aligned to
1975 * a @a slab_align -byte boundary. The new slab allocator can be passed to the
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001976 * kernel's memory slab functions.
Peter Mitsis578f9112016-10-07 13:50:31 -04001977 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001978 * Note that for each of the blocks in the memory slab to be aligned to
1979 * @a slab_align bytes, then @a slab_block_size must be a multiple of
1980 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04001981 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001982 * If the slab allocator is to be accessed outside the module where it is
1983 * defined, it can be declared via
1984 *
1985 * extern struct k_mem_slab @a name;
1986 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001987 * @param name Name of the memory slab
1988 * @param slab_block_size Size of each block in the buffer (in bytes)
1989 * @param slab_num_blocks Number blocks in the buffer
1990 * @param slab_align Alignment of the memory slab's buffer (power of 2)
Peter Mitsis578f9112016-10-07 13:50:31 -04001991 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001992#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
1993 char __noinit __aligned(slab_align) \
1994 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
1995 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001996 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04001997 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
1998 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001999
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002000/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002001 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002002 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002003 * Initializes the memory slab and creates its list of free blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002004 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002005 * @param slab Pointer to the memory slab object
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002006 * @param buffer Pointer to buffer used for the blocks.
2007 * @param block_size Size of each block, in bytes.
2008 * @param num_blocks Number of blocks.
2009 *
2010 * @return N/A
2011 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002012extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002013 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002014
2015/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002016 * @brief Allocate a memory slab block.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002017 *
2018 * Takes a block from the list of unused blocks.
2019 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002020 * @param slab Pointer to memory slab object.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002021 * @param mem Pointer to area to receive block address.
2022 * @param timeout Maximum time (milliseconds) to wait for allocation to
2023 * complete. Use K_NO_WAIT to return immediately, or K_FOREVER to wait
2024 * as long as necessary.
2025 *
2026 * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
2027 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002028extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2029 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002030
2031/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002032 * @brief Free a memory slab block.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002033 *
2034 * Gives block to a waiting thread if there is one, otherwise returns it to
2035 * the list of unused blocks.
2036 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002037 * @param slab Pointer to memory slab object.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002038 * @param mem Pointer to area to containing block address.
2039 *
2040 * @return N/A
2041 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002042extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002043
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002044/**
2045 * @brief Get the number of used memory blocks
2046 *
2047 * This routine gets the current number of used memory blocks in the
2048 * specified pool. It should be used for stats purposes only as that
2049 * value may potentially be out-of-date by the time it is used.
2050 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002051 * @param slab Memory slab to query
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002052 *
2053 * @return Number of used memory blocks
2054 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002055static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002056{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002057 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002058}
2059
Peter Mitsisc001aa82016-10-13 13:53:37 -04002060/**
2061 * @brief Get the number of unused memory blocks
2062 *
2063 * This routine gets the current number of unused memory blocks in the
2064 * specified pool. It should be used for stats purposes only as that value
2065 * may potentially be out-of-date by the time it is used.
2066 *
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002067 * @param slab Memory slab to query
Peter Mitsisc001aa82016-10-13 13:53:37 -04002068 *
2069 * @return Number of unused memory blocks
2070 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002071static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002072{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002073 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002074}
2075
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002076/* memory pools */
2077
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002078/*
2079 * Memory pool requires a buffer and two arrays of structures for the
2080 * memory block accounting:
2081 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2082 * status of four blocks of memory.
2083 */
2084struct k_mem_pool_quad_block {
2085 char *mem_blocks; /* pointer to the first of four memory blocks */
2086 uint32_t mem_status; /* four bits. If bit is set, memory block is
2087 allocated */
2088};
2089/*
2090 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2091 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2092 * block size is 4 times less than the previous one and thus requires 4 times
2093 * bigger array of k_mem_pool_quad_block structures to keep track of the
2094 * memory blocks.
2095 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002096
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002097/*
2098 * The array of k_mem_pool_block_set keeps the information of each array of
2099 * k_mem_pool_quad_block structures
2100 */
2101struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002102 size_t block_size; /* memory block size */
2103 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002104 struct k_mem_pool_quad_block *quad_block;
2105 int count;
2106};
2107
2108/* Memory pool descriptor */
2109struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002110 size_t max_block_size;
2111 size_t min_block_size;
2112 uint32_t nr_of_maxblocks;
2113 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002114 struct k_mem_pool_block_set *block_set;
2115 char *bufblock;
2116 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002117 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
2118};
2119
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002120#ifdef CONFIG_ARM
2121#define _SECTION_TYPE_SIGN "%"
2122#else
2123#define _SECTION_TYPE_SIGN "@"
2124#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002125
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002126/*
2127 * Static memory pool initialization
2128 */
2129/*
2130 * Use .altmacro to be able to recalculate values and pass them as string
2131 * arguments when calling assembler macros resursively
2132 */
2133__asm__(".altmacro\n\t");
2134
2135/*
2136 * Recursively calls a macro
2137 * The followig global symbols need to be initialized:
2138 * __memory_pool_max_block_size - maximal size of the memory block
2139 * __memory_pool_min_block_size - minimal size of the memory block
2140 * Notes:
2141 * Global symbols are used due the fact that assembler macro allows only
2142 * one argument be passed with the % conversion
2143 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2144 * is used instead of / 4.
2145 * n_max argument needs to go first in the invoked macro, as some
2146 * assemblers concatenate \name and %(\n_max * 4) arguments
2147 * if \name goes first
2148 */
2149__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2150 ".ifge __memory_pool_max_block_size >> 2 -"
2151 " __memory_pool_min_block_size\n\t\t"
2152 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2153 "\\macro_name %(\\n_max * 4) \\name\n\t"
2154 ".endif\n\t"
2155 ".endm\n");
2156
2157/*
2158 * Build quad blocks
2159 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2160 * structures and recursively calls itself for the next array, 4 times
2161 * larger.
2162 * The followig global symbols need to be initialized:
2163 * __memory_pool_max_block_size - maximal size of the memory block
2164 * __memory_pool_min_block_size - minimal size of the memory block
2165 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2166 */
2167__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002168 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002169 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2170 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2171 ".if \\n_max % 4\n\t\t"
2172 ".skip __memory_pool_quad_block_size\n\t"
2173 ".endif\n\t"
2174 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2175 ".endm\n");
2176
2177/*
2178 * Build block sets and initialize them
2179 * Macro initializes the k_mem_pool_block_set structure and
2180 * recursively calls itself for the next one.
2181 * The followig global symbols need to be initialized:
2182 * __memory_pool_max_block_size - maximal size of the memory block
2183 * __memory_pool_min_block_size - minimal size of the memory block
2184 * __memory_pool_block_set_count, the number of the elements in the
2185 * block set array must be set to 0. Macro calculates it's real
2186 * value.
2187 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2188 * structures, _build_quad_blocks must be called prior it.
2189 */
2190__asm__(".macro _build_block_set n_max, name\n\t"
2191 ".int __memory_pool_max_block_size\n\t" /* block_size */
2192 ".if \\n_max % 4\n\t\t"
2193 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2194 ".else\n\t\t"
2195 ".int \\n_max >> 2\n\t"
2196 ".endif\n\t"
2197 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2198 ".int 0\n\t" /* count */
2199 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2200 "__do_recurse _build_block_set \\name \\n_max\n\t"
2201 ".endm\n");
2202
2203/*
2204 * Build a memory pool structure and initialize it
2205 * Macro uses __memory_pool_block_set_count global symbol,
2206 * block set addresses and buffer address, it may be called only after
2207 * _build_block_set
2208 */
2209__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002210 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002211 _SECTION_TYPE_SIGN "progbits\n\t"
2212 ".globl \\name\n\t"
2213 "\\name:\n\t"
2214 ".int \\max_size\n\t" /* max_block_size */
2215 ".int \\min_size\n\t" /* min_block_size */
2216 ".int \\n_max\n\t" /* nr_of_maxblocks */
2217 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2218 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2219 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2220 ".int 0\n\t" /* wait_q->head */
2221 ".int 0\n\t" /* wait_q->next */
2222 ".popsection\n\t"
2223 ".endm\n");
2224
2225#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2226 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2227 _SECTION_TYPE_SIGN "progbits\n\t"); \
2228 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2229 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2230 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2231 STRINGIFY(name) "\n\t"); \
2232 __asm__(".popsection\n\t")
2233
2234#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2235 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2236 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2237 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2238 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002239 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002240 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2241 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2242 STRINGIFY(name) "\n\t"); \
2243 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2244 __asm__(".int __memory_pool_block_set_count\n\t"); \
2245 __asm__(".popsection\n\t"); \
2246 extern uint32_t _mem_pool_block_set_count_##name; \
2247 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2248
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002249#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2250 char __noinit __aligned(align) \
2251 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002252
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002253/**
2254 * @brief Define a memory pool
2255 *
2256 * This declares and initializes a memory pool whose buffer is aligned to
2257 * a @a align -byte boundary. The new memory pool can be passed to the
2258 * kernel's memory pool functions.
2259 *
2260 * Note that for each of the minimum sized blocks to be aligned to @a align
2261 * bytes, then @a min_size must be a multiple of @a align.
2262 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002263 * If the pool is to be accessed outside the module where it is defined, it
2264 * can be declared via
2265 *
2266 * extern struct k_mem_pool @a name;
2267 *
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002268 * @param name Name of the memory pool
2269 * @param min_size Minimum block size in the pool
2270 * @param max_size Maximum block size in the pool
2271 * @param n_max Number of maximum sized blocks in the pool
2272 * @param align Alignment of the memory pool's buffer
2273 */
2274#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002275 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2276 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002277 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002278 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2279 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2280 extern struct k_mem_pool name
2281
2282/*
2283 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2284 * to __memory_pool_quad_block_size absolute symbol.
2285 * This function does not get called, but compiler calculates the value and
2286 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2287 */
2288static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2289{
2290 __asm__(".globl __memory_pool_quad_block_size\n\t"
Andrew Boie431607c2016-10-25 11:47:52 -07002291#ifdef CONFIG_NIOS2
2292 "__memory_pool_quad_block_size = %0\n\t"
2293#else
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002294 "__memory_pool_quad_block_size = %c0\n\t"
Andrew Boie431607c2016-10-25 11:47:52 -07002295#endif
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002296 :
2297 : "n"(sizeof(struct k_mem_pool_quad_block)));
2298}
2299
Peter Mitsis937042c2016-10-13 13:18:26 -04002300/**
2301 * @brief Allocate memory from a memory pool
2302 *
2303 * @param pool Pointer to the memory pool object
2304 * @param block Pointer to the allocated memory's block descriptor
2305 * @param size Minimum number of bytes to allocate
2306 * @param timeout Maximum time (milliseconds) to wait for operation to
2307 * complete. Use K_NO_WAIT to return immediately, or K_FOREVER
2308 * to wait as long as necessary.
2309 *
2310 * @return 0 on success, -ENOMEM on failure
2311 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002312extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002313 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002314
2315/**
2316 * @brief Return previously allocated memory to its memory pool
2317 *
2318 * @param block Pointer to allocated memory's block descriptor
2319 *
2320 * @return N/A
2321 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002322extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002323
2324/**
2325 * @brief Defragment the specified memory pool
2326 *
2327 * @param pool Pointer to the memory pool object
2328 *
2329 * @return N/A
2330 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002331extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002332
2333/**
Allan Stephens480a1312016-10-13 15:44:48 -05002334 * @brief Allocate memory from heap
Peter Mitsis937042c2016-10-13 13:18:26 -04002335 *
Allan Stephens480a1312016-10-13 15:44:48 -05002336 * This routine provides traditional malloc() semantics. The memory is
2337 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002338 *
2339 * @param size Size of memory requested by the caller (in bytes)
2340 *
2341 * @return Address of the allocated memory on success; otherwise NULL
2342 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002343extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002344
2345/**
Allan Stephens480a1312016-10-13 15:44:48 -05002346 * @brief Free memory allocated from heap
2347 *
2348 * This routine provides traditional free() semantics. The memory being
2349 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002350 *
2351 * @param ptr Pointer to previously allocated memory
2352 *
2353 * @return N/A
2354 */
2355extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002356
2357/*
2358 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2359 * hook into the device subsystem, which itself uses nanokernel semaphores,
2360 * and thus currently requires the definition of nano_sem.
2361 */
2362#include <legacy.h>
2363#include <arch/cpu.h>
2364
2365/*
2366 * private APIs that are utilized by one or more public APIs
2367 */
2368
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002369extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002370extern void _init_static_threads(void);
2371
2372#ifdef __cplusplus
2373}
2374#endif
2375
2376#endif /* _kernel__h_ */