blob: 2cc3cfed9868c60ca303220d405385394d967786 [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;
84struct k_event;
85struct k_msgq;
86struct k_mbox;
87struct k_pipe;
88struct k_fifo;
89struct k_lifo;
90struct k_stack;
91struct k_mem_map;
92struct 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
105struct k_thread_config {
106 char *stack;
107 unsigned stack_size;
108 unsigned prio;
109};
110
111typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
112extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size,
113 void (*entry)(void *, void *, void*),
114 void *p1, void *p2, void *p3,
115 int32_t prio, uint32_t options, int32_t delay);
116
117extern void k_sleep(int32_t duration);
118extern void k_busy_wait(uint32_t usec_to_wait);
119extern void k_yield(void);
120extern void k_wakeup(k_tid_t thread);
121extern k_tid_t k_current_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122extern int k_thread_cancel(k_tid_t thread);
123
124extern void k_thread_abort(k_tid_t thread);
125
126#define K_THREAD_GROUP_EXE 0x1
127#define K_THREAD_GROUP_SYS 0x2
128#define K_THREAD_GROUP_FPU 0x4
129
130/* XXX - doesn't work because CONFIG_ARCH is a string */
131#if 0
132/* arch-specific groups */
133#if CONFIG_ARCH == "x86"
134#define K_THREAD_GROUP_SSE 0x4
135#endif
136#endif
137
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400138#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400139#define _THREAD_TIMEOUT_INIT(obj) \
140 (obj).nano_timeout = { \
141 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400142 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400143 .wait_q = NULL, \
144 .delta_ticks_from_prev = -1, \
145 },
146#else
147#define _THREAD_TIMEOUT_INIT(obj)
148#endif
149
150#ifdef CONFIG_ERRNO
151#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
152#else
153#define _THREAD_ERRNO_INIT(obj)
154#endif
155
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400156struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400157 uint32_t init_groups;
158 int init_prio;
159 void (*init_entry)(void *, void *, void *);
160 void *init_p1;
161 void *init_p2;
162 void *init_p3;
163 void (*init_abort)(void);
164 union {
165 char *init_stack;
166 struct k_thread *thread;
167 };
168 unsigned int init_stack_size;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400169 int32_t init_delay;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400170};
171
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400172/*
173 * Common macro used by both K_THREAD_INITIALIZER()
174 * and _MDEF_THREAD_INITIALIZER().
175 */
176#define _THREAD_INITIALIZER(stack, stack_size, \
177 entry, p1, p2, p3, \
178 abort, prio) \
179 .init_prio = (prio), \
180 .init_entry = (void (*)(void *, void *, void *))entry, \
181 .init_p1 = (void *)p1, \
182 .init_p2 = (void *)p2, \
183 .init_p3 = (void *)p3, \
184 .init_abort = abort, \
185 .init_stack = (stack), \
186 .init_stack_size = (stack_size),
187
188/**
189 * @brief Thread initializer macro
190 *
191 * This macro is to only be used with statically defined threads that were not
192 * defined in the MDEF file. As such the associated threads can not belong to
193 * any thread group.
194 */
195#define K_THREAD_INITIALIZER(stack, stack_size, \
196 entry, p1, p2, p3, \
197 abort, prio, delay) \
198 { \
199 _THREAD_INITIALIZER(stack, stack_size, \
200 entry, p1, p2, p3, \
201 abort, prio) \
202 .init_groups = 0, \
203 .init_delay = (delay), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400204 }
205
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400206/**
207 * @brief Thread initializer macro
208 *
209 * This macro is to only be used with statically defined threads that were
210 * defined with legacy APIs (including the MDEF file). As such the associated
211 * threads may belong to one or more thread groups.
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400212 */
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400213#define _MDEF_THREAD_INITIALIZER(stack, stack_size, \
214 entry, p1, p2, p3, \
215 abort, prio, groups) \
216 { \
217 _THREAD_INITIALIZER(stack, stack_size, \
218 entry, p1, p2, p3, \
219 abort, prio) \
220 .init_groups = (groups), \
221 .init_delay = K_FOREVER, \
222 }
223
224/**
225 * @brief Define thread initializer and initialize it.
226 *
227 * @internal It has been observed that the x86 compiler by default aligns
228 * these _static_thread_data structures to 32-byte boundaries, thereby
229 * wasting space. To work around this, force a 4-byte alignment.
230 */
231#define K_THREAD_DEFINE(name, stack_size, \
232 entry, p1, p2, p3, \
233 abort, prio, delay) \
234 char __noinit __stack _k_thread_obj_##name[stack_size]; \
235 struct _static_thread_data _k_thread_data_##name __aligned(4) \
236 __in_section(_k_task_list, private, task) = \
237 K_THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
238 entry, p1, p2, p3, abort, prio, delay)
239
240/**
241 * @brief Define thread initializer for MDEF defined thread and initialize it.
242 *
243 * @ref K_THREAD_DEFINE
244 */
245#define _MDEF_THREAD_DEFINE(name, stack_size, \
246 entry, p1, p2, p3, \
247 abort, prio, groups) \
248 char __noinit __stack _k_thread_obj_##name[stack_size]; \
249 struct _static_thread_data _k_thread_data_##name __aligned(4) \
250 __in_section(_k_task_list, private, task) = \
251 _MDEF_THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400252 entry, p1, p2, p3, abort, prio, groups)
253
Allan Stephens399d0ad2016-10-07 13:41:34 -0500254extern int k_thread_priority_get(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400255extern void k_thread_priority_set(k_tid_t thread, int prio);
256
Benjamin Walsh71d52282016-09-29 10:49:48 -0400257extern void k_thread_suspend(k_tid_t thread);
258extern void k_thread_resume(k_tid_t thread);
259extern void k_thread_abort_handler_set(void (*handler)(void));
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400260
261extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400262
263extern int k_am_in_isr(void);
264
265extern void k_thread_custom_data_set(void *value);
266extern void *k_thread_custom_data_get(void);
267
268/**
269 * kernel timing
270 */
271
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400272#include <sys_clock.h>
273
274/* private internal time manipulation (users should never play with ticks) */
275
276static int64_t __ticks_to_ms(int64_t ticks)
277{
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400278#if CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400279 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400280#else
281 __ASSERT(ticks == 0, "");
282 return 0;
283#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400284}
285
286
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400287/* timeouts */
288
289struct _timeout;
290typedef void (*_timeout_func_t)(struct _timeout *t);
291
292struct _timeout {
293 sys_dlist_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400294 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400295 sys_dlist_t *wait_q;
296 int32_t delta_ticks_from_prev;
297 _timeout_func_t func;
298};
299
Allan Stephens45bfa372016-10-12 12:39:42 -0500300
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400301/* timers */
302
303struct k_timer {
304 /*
305 * _timeout structure must be first here if we want to use
306 * dynamic timer allocation. timeout.node is used in the double-linked
307 * list of free timers
308 */
309 struct _timeout timeout;
310
Allan Stephens45bfa372016-10-12 12:39:42 -0500311 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400312 _wait_q_t wait_q;
313
314 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500315 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400316
317 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500318 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400319
320 /* timer period */
321 int32_t period;
322
Allan Stephens45bfa372016-10-12 12:39:42 -0500323 /* timer status */
324 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400325
Allan Stephens45bfa372016-10-12 12:39:42 -0500326 /* used to support legacy timer APIs */
327 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400328
329 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
330};
331
332#define K_TIMER_INITIALIZER(obj) \
333 { \
334 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
335 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
336 }
337
338#define K_TIMER_DEFINE(name) \
339 struct k_timer name = K_TIMER_INITIALIZER(name)
340
Allan Stephens45bfa372016-10-12 12:39:42 -0500341/**
342 * @brief Initialize a timer.
343 *
344 * This routine must be called before the timer is used.
345 *
346 * @param timer Address of timer.
347 * @param expiry_fn Function to invoke each time timer expires.
348 * @param stop_fn Function to invoke if timer is stopped while running.
349 *
350 * @return N/A
351 */
352extern void k_timer_init(struct k_timer *timer,
353 void (*expiry_fn)(struct k_timer *),
354 void (*stop_fn)(struct k_timer *));
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700355
Allan Stephens45bfa372016-10-12 12:39:42 -0500356/**
357 * @brief Start a timer.
358 *
359 * This routine starts a timer, and resets its status to zero. The timer
360 * begins counting down using the specified duration and period values.
361 *
362 * Attempting to start a timer that is already running is permitted.
363 * The timer's status is reset to zero and the timer begins counting down
364 * using the new duration and period values.
365 *
366 * @param timer Address of timer.
367 * @param duration Initial timer duration (in milliseconds).
368 * @param period Timer period (in milliseconds).
369 *
370 * @return N/A
371 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400372extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500373 int32_t duration, int32_t period);
374
375/**
376 * @brief Stop a timer.
377 *
378 * This routine stops a running timer prematurely. The timer's stop function,
379 * if one exists, is invoked by the caller.
380 *
381 * Attempting to stop a timer that is not running is permitted, but has no
382 * effect on the timer since it is already stopped.
383 *
384 * @param timer Address of timer.
385 *
386 * @return N/A
387 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400388extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500389
390/**
391 * @brief Read timer status.
392 *
393 * This routine reads the timer's status, which indicates the number of times
394 * it has expired since its status was last read.
395 *
396 * Calling this routine resets the timer's status to zero.
397 *
398 * @param timer Address of timer.
399 *
400 * @return Timer status.
401 */
402extern uint32_t k_timer_status_get(struct k_timer *timer);
403
404/**
405 * @brief Synchronize thread to timer expiration.
406 *
407 * This routine blocks the calling thread until the timer's status is non-zero
408 * (indicating that it has expired at least once since it was last examined)
409 * or the timer is stopped. If the timer status is already non-zero,
410 * or the timer is already stopped, the caller continues without waiting.
411 *
412 * Calling this routine resets the timer's status to zero.
413 *
414 * This routine must not be used by interrupt handlers, since they are not
415 * allowed to block.
416 *
417 * @param timer Address of timer.
418 *
419 * @return Timer status.
420 */
421extern uint32_t k_timer_status_sync(struct k_timer *timer);
422
423/**
424 * @brief Get timer remaining before next timer expiration.
425 *
426 * This routine computes the (approximate) time remaining before a running
427 * timer next expires. If the timer is not running, it returns zero.
428 *
429 * @param timer Address of timer.
430 *
431 * @return Remaining time (in milliseconds).
432 */
433
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400434extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400435
436
Allan Stephens45bfa372016-10-12 12:39:42 -0500437/* kernel clocks */
438
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400439/**
440 * @brief Get the time elapsed since the system booted (uptime)
441 *
442 * @return The current uptime of the system in ms
443 */
444
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400445extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400446
447/**
448 * @brief Get the lower 32-bit of time elapsed since the system booted (uptime)
449 *
450 * This function is potentially less onerous in both the time it takes to
451 * execute, the interrupt latency it introduces and the amount of 64-bit math
452 * it requires than k_uptime_get(), but it only provides an uptime value of
453 * 32-bits. The user must handle possible rollovers/spillovers.
454 *
455 * At a rate of increment of 1000 per second, it rolls over approximately every
456 * 50 days.
457 *
458 * @return The current uptime of the system in ms
459 */
460
461extern uint32_t k_uptime_get_32(void);
462
463/**
464 * @brief Get the difference between a reference time and the current uptime
465 *
466 * @param reftime A pointer to a reference time. It is updated with the current
467 * uptime upon return.
468 *
469 * @return The delta between the reference time and the current uptime.
470 */
471
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400472extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400473
474/**
475 * @brief Get the difference between a reference time and the current uptime
476 *
477 * The 32-bit version of k_uptime_delta(). It has the same perks and issues as
478 * k_uptime_get_32().
479 *
480 * @param reftime A pointer to a reference time. It is updated with the current
481 * uptime upon return.
482 *
483 * @return The delta between the reference time and the current uptime.
484 */
485
486extern uint32_t k_uptime_delta_32(int64_t *reftime);
487
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400488extern uint32_t k_cycle_get_32(void);
489
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400490/**
491 * data transfers (basic)
492 */
493
494/* fifos */
495
496struct k_fifo {
497 _wait_q_t wait_q;
498 sys_slist_t data_q;
499
500 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
501};
502
503extern void k_fifo_init(struct k_fifo *fifo);
504extern void k_fifo_put(struct k_fifo *fifo, void *data);
505extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
506extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
507extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
508
509#define K_FIFO_INITIALIZER(obj) \
510 { \
511 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400512 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400513 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
514 }
515
516#define K_FIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400517 struct k_fifo name = K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400518
519/* lifos */
520
521struct k_lifo {
522 _wait_q_t wait_q;
523 void *list;
524
525 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
526};
527
528extern void k_lifo_init(struct k_lifo *lifo);
529extern void k_lifo_put(struct k_lifo *lifo, void *data);
530extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
531
532#define K_LIFO_INITIALIZER(obj) \
533 { \
534 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
535 .list = NULL, \
536 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
537 }
538
539#define K_LIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400540 struct k_lifo name = K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400541
542/* stacks */
543
544struct k_stack {
545 _wait_q_t wait_q;
546 uint32_t *base, *next, *top;
547
548 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
549};
550
Allan Stephens018cd9a2016-10-07 15:13:24 -0500551extern void k_stack_init(struct k_stack *stack,
552 uint32_t *buffer, int num_entries);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400553extern void k_stack_push(struct k_stack *stack, uint32_t data);
554extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
555
556#define K_STACK_INITIALIZER(obj, stack_num_entries, stack_buffer) \
557 { \
558 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
559 .base = stack_buffer, \
560 .next = stack_buffer, \
561 .top = stack_buffer + stack_num_entries, \
562 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
563 }
564
565#define K_STACK_DEFINE(name, stack_num_entries) \
566 uint32_t __noinit _k_stack_buf_##name[stack_num_entries]; \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400567 struct k_stack name = \
568 K_STACK_INITIALIZER(name, stack_num_entries, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400569 _k_stack_buf_##name); \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400570
571#define K_STACK_SIZE(stack_num_entries) \
572 (sizeof(struct k_stack) + (stack_num_entries * sizeof(uint32_t)))
573
574/**
575 * workqueues
576 */
577
578struct k_work;
579
580typedef void (*k_work_handler_t)(struct k_work *);
581
582/**
583 * A workqueue is a fiber that executes @ref k_work items that are
584 * queued to it. This is useful for drivers which need to schedule
585 * execution of code which might sleep from ISR context. The actual
586 * fiber identifier is not stored in the structure in order to save
587 * space.
588 */
589struct k_work_q {
590 struct k_fifo fifo;
591};
592
593/**
594 * @brief Work flags.
595 */
596enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300597 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400598};
599
600/**
601 * @brief An item which can be scheduled on a @ref k_work_q.
602 */
603struct k_work {
604 void *_reserved; /* Used by k_fifo implementation. */
605 k_work_handler_t handler;
606 atomic_t flags[1];
607};
608
609/**
610 * @brief Statically initialize work item
611 */
612#define K_WORK_INITIALIZER(work_handler) \
613 { \
614 ._reserved = NULL, \
615 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300616 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400617 }
618
619/**
620 * @brief Dynamically initialize work item
621 */
622static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
623{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300624 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400625 work->handler = handler;
626}
627
628/**
629 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300630 *
631 * This procedure schedules a work item to be processed.
632 * In the case where the work item has already been submitted and is pending
633 * execution, calling this function will result in a no-op. In this case, the
634 * work item must not be modified externally (e.g. by the caller of this
635 * function), since that could cause the work item to be processed in a
636 * corrupted state.
637 *
638 * @param work_q to schedule the work item
639 * @param work work item
640 *
641 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400642 */
643static inline void k_work_submit_to_queue(struct k_work_q *work_q,
644 struct k_work *work)
645{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300646 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400647 k_fifo_put(&work_q->fifo, work);
648 }
649}
650
651/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300652 * @brief Check if work item is pending.
653 */
654static inline int k_work_pending(struct k_work *work)
655{
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300656 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300657}
658
659/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400660 * @brief Start a new workqueue. This routine can be called from either
661 * fiber or task context.
662 */
663extern void k_work_q_start(struct k_work_q *work_q,
664 const struct k_thread_config *config);
665
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400666#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400667
668 /*
669 * @brief An item which can be scheduled on a @ref k_work_q with a
670 * delay.
671 */
672struct k_delayed_work {
673 struct k_work work;
674 struct _timeout timeout;
675 struct k_work_q *work_q;
676};
677
678/**
679 * @brief Initialize delayed work
680 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400681extern void k_delayed_work_init(struct k_delayed_work *work,
682 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400683
684/**
685 * @brief Submit a delayed work item to a workqueue.
686 *
687 * This procedure schedules a work item to be processed after a delay.
688 * Once the delay has passed, the work item is submitted to the work queue:
689 * at this point, it is no longer possible to cancel it. Once the work item's
690 * handler is about to be executed, the work is considered complete and can be
691 * resubmitted.
692 *
693 * Care must be taken if the handler blocks or yield as there is no implicit
694 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
695 * it should be explicitly done between the submitter and the handler.
696 *
697 * @param work_q to schedule the work item
698 * @param work Delayed work item
699 * @param ticks Ticks to wait before scheduling the work item
700 *
701 * @return 0 in case of success or negative value in case of error.
702 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400703extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
704 struct k_delayed_work *work,
705 int32_t ticks);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400706
707/**
708 * @brief Cancel a delayed work item
709 *
710 * This procedure cancels a scheduled work item. If the work has been completed
711 * or is idle, this will do nothing. The only case where this can fail is when
712 * the work has been submitted to the work queue, but the handler has not run
713 * yet.
714 *
715 * @param work Delayed work item to be canceled
716 *
717 * @return 0 in case of success or negative value in case of error.
718 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400719extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400720
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400721#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400722
723#if defined(CONFIG_SYSTEM_WORKQUEUE)
724
725extern struct k_work_q k_sys_work_q;
726
727/*
728 * @brief Submit a work item to the system workqueue.
729 *
730 * @ref k_work_submit_to_queue
731 *
732 * When using the system workqueue it is not recommended to block or yield
733 * on the handler since its fiber is shared system wide it may cause
734 * unexpected behavior.
735 */
736static inline void k_work_submit(struct k_work *work)
737{
738 k_work_submit_to_queue(&k_sys_work_q, work);
739}
740
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400741#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400742/*
743 * @brief Submit a delayed work item to the system workqueue.
744 *
745 * @ref k_delayed_work_submit_to_queue
746 *
747 * When using the system workqueue it is not recommended to block or yield
748 * on the handler since its fiber is shared system wide it may cause
749 * unexpected behavior.
750 */
751static inline int k_delayed_work_submit(struct k_delayed_work *work,
752 int ticks)
753{
754 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, ticks);
755}
756
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400757#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400758#endif /* CONFIG_SYSTEM_WORKQUEUE */
759
760/**
761 * synchronization
762 */
763
764/* mutexes */
765
766struct k_mutex {
767 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400768 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400769 uint32_t lock_count;
770 int owner_orig_prio;
771#ifdef CONFIG_OBJECT_MONITOR
772 int num_lock_state_changes;
773 int num_conflicts;
774#endif
775
776 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
777};
778
779#ifdef CONFIG_OBJECT_MONITOR
780#define _MUTEX_INIT_OBJECT_MONITOR \
781 .num_lock_state_changes = 0, .num_conflicts = 0,
782#else
783#define _MUTEX_INIT_OBJECT_MONITOR
784#endif
785
786#define K_MUTEX_INITIALIZER(obj) \
787 { \
788 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
789 .owner = NULL, \
790 .lock_count = 0, \
791 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
792 _MUTEX_INIT_OBJECT_MONITOR \
793 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
794 }
795
796#define K_MUTEX_DEFINE(name) \
797 struct k_mutex name = K_MUTEX_INITIALIZER(name)
798
799extern void k_mutex_init(struct k_mutex *mutex);
800extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
801extern void k_mutex_unlock(struct k_mutex *mutex);
802
803/* semaphores */
804
805struct k_sem {
806 _wait_q_t wait_q;
807 unsigned int count;
808 unsigned int limit;
809
810 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
811};
812
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400813/**
814 * @brief Initialize a semaphore object.
815 *
816 * An initial count and a count limit can be specified. The count will never go
817 * over the count limit if the semaphore is given multiple times without being
818 * taken.
819 *
820 * Cannot be called from ISR.
821 *
822 * @param sem Pointer to a semaphore object.
823 * @param initial_count Initial count.
824 * @param limit Highest value the count can take during operation.
825 *
826 * @return N/A
827 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400828extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
829 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400830
831/**
832 * @brief Take a semaphore, possibly pending if not available.
833 *
834 * The current execution context tries to obtain the semaphore. If the
835 * semaphore is unavailable and a timeout other than K_NO_WAIT is specified,
836 * the context will pend.
837 *
838 * @param sem Pointer to a semaphore object.
839 * @param timeout Number of milliseconds to wait if semaphore is unavailable,
840 * or one of the special values K_NO_WAIT and K_FOREVER.
841 *
842 * @warning If it is called from the context of an ISR, then the only legal
843 * value for @a timeout is K_NO_WAIT.
844 *
845 * @retval 0 When semaphore is obtained successfully.
846 * @retval -EAGAIN When timeout expires.
847 * @retval -EBUSY When unavailable and the timeout is K_NO_WAIT.
848 *
849 * @sa K_NO_WAIT, K_FOREVER
850 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400851extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400852
853/**
854 * @brief Give a semaphore.
855 *
856 * Increase the semaphore's internal count by 1, up to its limit, if no thread
857 * is waiting on the semaphore; otherwise, wake up the first thread in the
858 * semaphore's waiting queue.
859 *
860 * If the latter case, and if the current context is preemptible, the thread
861 * that is taken off the wait queue will be scheduled in and will preempt the
862 * current thread.
863 *
864 * @param sem Pointer to a semaphore object.
865 *
866 * @return N/A
867 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400868extern void k_sem_give(struct k_sem *sem);
869
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400870/**
871 * @brief Reset a semaphore's count to zero.
872 *
873 * The only effect is that the count is set to zero. There is no other
874 * side-effect to calling this function.
875 *
876 * @param sem Pointer to a semaphore object.
877 *
878 * @return N/A
879 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -0400880static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400881{
882 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400883}
884
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400885/**
886 * @brief Get a semaphore's count.
887 *
888 * Note there is no guarantee the count has not changed by the time this
889 * function returns.
890 *
891 * @param sem Pointer to a semaphore object.
892 *
893 * @return The current semaphore count.
894 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +0200895static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400896{
897 return sem->count;
898}
899
Peter Mitsis45403672016-09-09 14:24:06 -0400900#ifdef CONFIG_SEMAPHORE_GROUPS
901/**
902 * @brief Take the first available semaphore
903 *
904 * Given a list of semaphore pointers, this routine will attempt to take one
905 * of them, waiting up to a maximum of @a timeout ms to do so. The taken
906 * semaphore is identified by @a sem (set to NULL on error).
907 *
908 * Be aware that the more semaphores specified in the group, the more stack
909 * space is required by the waiting thread.
910 *
911 * @param sem_array Array of semaphore pointers terminated by a K_END entry
912 * @param sem Identifies the semaphore that was taken
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400913 * @param timeout Number of milliseconds to wait if semaphores are unavailable,
914 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsis45403672016-09-09 14:24:06 -0400915 *
916 * @retval 0 A semaphore was successfully taken
917 * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT)
918 * @retval -EAGAIN Time out occurred while waiting for semaphore
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400919 *
920 * @sa K_NO_WAIT, K_FOREVER
Peter Mitsis45403672016-09-09 14:24:06 -0400921 */
922
923extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem,
924 int32_t timeout);
925
926/**
927 * @brief Give all the semaphores in the group
928 *
929 * This routine will give each semaphore in the array of semaphore pointers.
930 *
931 * @param sem_array Array of semaphore pointers terminated by a K_END entry
932 *
933 * @return N/A
934 */
935extern void k_sem_group_give(struct k_sem *sem_array[]);
936
937/**
938 * @brief Reset the count to zero on each semaphore in the array
939 *
940 * This routine resets the count of each semaphore in the group to zero.
941 * Note that it does NOT have any impact on any thread that might have
942 * been previously pending on any of the semaphores.
943 *
944 * @param sem_array Array of semaphore pointers terminated by a K_END entry
945 *
946 * @return N/A
947 */
948extern void k_sem_group_reset(struct k_sem *sem_array[]);
949#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400950
951#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
952 { \
953 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
954 .count = initial_count, \
955 .limit = count_limit, \
956 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
957 }
958
Benjamin Walshb9c1a062016-10-15 17:12:35 -0400959/**
960 * @def K_SEM_DEFINE
961 *
962 * @brief Statically define and initialize a global semaphore.
963 *
964 * Create a global semaphore named @name. It is initialized as if k_sem_init()
965 * was called on it. If the semaphore is to be accessed outside the module
966 * where it is defined, it can be declared via
967 *
968 * extern struct k_sem @name;
969 *
970 * @param name Name of the semaphore variable.
971 * @param initial_count Initial count.
972 * @param count_limit Highest value the count can take during operation.
973 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400974#define K_SEM_DEFINE(name, initial_count, count_limit) \
975 struct k_sem name = \
976 K_SEM_INITIALIZER(name, initial_count, count_limit)
977
978/* events */
979
980#define K_EVT_DEFAULT NULL
981#define K_EVT_IGNORE ((void *)(-1))
982
983typedef int (*k_event_handler_t)(struct k_event *);
984
985struct k_event {
986 k_event_handler_t handler;
987 atomic_t send_count;
988 struct k_work work_item;
989 struct k_sem sem;
990
991 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_event);
992};
993
994extern void _k_event_deliver(struct k_work *work);
995
996#define K_EVENT_INITIALIZER(obj, event_handler) \
997 { \
998 .handler = (k_event_handler_t)event_handler, \
999 .send_count = ATOMIC_INIT(0), \
1000 .work_item = K_WORK_INITIALIZER(_k_event_deliver), \
1001 .sem = K_SEM_INITIALIZER(obj.sem, 0, 1), \
1002 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1003 }
1004
1005#define K_EVENT_DEFINE(name, event_handler) \
1006 struct k_event name \
1007 __in_section(_k_event_list, event, name) = \
1008 K_EVENT_INITIALIZER(name, event_handler)
1009
1010extern void k_event_init(struct k_event *event, k_event_handler_t handler);
1011extern int k_event_recv(struct k_event *event, int32_t timeout);
1012extern void k_event_send(struct k_event *event);
1013
1014/**
1015 * data transfers (complex)
1016 */
1017
1018/* message queues */
1019
1020struct k_msgq {
1021 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001022 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001023 uint32_t max_msgs;
1024 char *buffer_start;
1025 char *buffer_end;
1026 char *read_ptr;
1027 char *write_ptr;
1028 uint32_t used_msgs;
1029
1030 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
1031};
1032
Peter Mitsis1da807e2016-10-06 11:36:59 -04001033#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001034 { \
1035 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001036 .max_msgs = q_max_msgs, \
1037 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001038 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001039 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001040 .read_ptr = q_buffer, \
1041 .write_ptr = q_buffer, \
1042 .used_msgs = 0, \
1043 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1044 }
1045
Peter Mitsis1da807e2016-10-06 11:36:59 -04001046/**
1047 * @brief Define a message queue
1048 *
1049 * This declares and initializes a message queue whose buffer is aligned to
1050 * a @a q_align -byte boundary. The new message queue can be passed to the
1051 * kernel's message queue functions.
1052 *
1053 * Note that for each of the mesages in the message queue to be aligned to
1054 * @a q_align bytes, then @a q_msg_size must be a multiple of @a q_align.
1055 *
1056 * @param q_name Name of the message queue
1057 * @param q_msg_size The size in bytes of each message
1058 * @param q_max_msgs Maximum number of messages the queue can hold
1059 * @param q_align Alignment of the message queue's buffer (power of 2)
1060 */
1061#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
1062 static char __noinit __aligned(q_align) \
1063 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
1064 struct k_msgq q_name = \
1065 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
1066 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001067
Peter Mitsisd7a37502016-10-13 11:37:40 -04001068/**
1069 * @brief Initialize a message queue.
1070 *
1071 * @param q Pointer to the message queue object.
1072 * @param buffer Pointer to memory area that holds queued messages.
1073 * @param msg_size Message size, in bytes.
1074 * @param max_msgs Maximum number of messages that can be queued.
1075 *
1076 * @return N/A
1077 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04001078extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001079 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001080
1081/**
1082 * @brief Add a message to a message queue.
1083 *
1084 * This routine adds an item to the message queue. When the message queue is
1085 * full, the routine will wait either for space to become available, or until
1086 * the specified time limit is reached.
1087 *
1088 * @param q Pointer to the message queue object.
1089 * @param data Pointer to message data area.
1090 * @param timeout Number of milliseconds to wait until space becomes available
1091 * to add the message into the message queue, or one of the
1092 * special values K_NO_WAIT and K_FOREVER.
1093 *
1094 * @return 0 if successful, -ENOMSG if failed immediately or after queue purge,
1095 * -EAGAIN if timed out
1096 *
1097 * @sa K_NO_WAIT, K_FOREVER
1098 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001099extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001100
1101/**
1102 * @brief Obtain a message from a message queue.
1103 *
1104 * This routine fetches the oldest item from the message queue. When the message
1105 * queue is found empty, the routine will wait either until an item is added to
1106 * the message queue or until the specified time limit is reached.
1107 *
1108 * @param q Pointer to the message queue object.
1109 * @param data Pointer to message data area.
1110 * @param timeout Number of milliseconds to wait to obtain message, or one of
1111 * the special values K_NO_WAIT and K_FOREVER.
1112 *
1113 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1114 *
1115 * @sa K_NO_WAIT, K_FOREVER
1116 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001117extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04001118
1119/**
1120 * @brief Purge contents of a message queue.
1121 *
1122 * Discards all messages currently in the message queue, and cancels
1123 * any "add message" operations initiated by waiting threads.
1124 *
1125 * @param q Pointer to the message queue object.
1126 *
1127 * @return N/A
1128 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001129extern void k_msgq_purge(struct k_msgq *q);
1130
Peter Mitsis67be2492016-10-07 11:44:34 -04001131/**
1132 * @brief Get the number of unused messages
1133 *
1134 * @param q Message queue to query
1135 *
1136 * @return Number of unused messages
1137 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001138static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04001139{
1140 return q->max_msgs - q->used_msgs;
1141}
1142
Peter Mitsisd7a37502016-10-13 11:37:40 -04001143/**
1144 * @brief Get the number of used messages
1145 *
1146 * @param q Message queue to query
1147 *
1148 * @return Number of used messages
1149 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001150static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001151{
1152 return q->used_msgs;
1153}
1154
1155struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001156 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001157 void *addr_in_pool;
1158 void *data;
1159 uint32_t req_size;
1160};
1161
1162/* mailboxes */
1163
1164struct k_mbox_msg {
1165 /** internal use only - needed for legacy API support */
1166 uint32_t _mailbox;
1167 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04001168 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001169 /** application-defined information value */
1170 uint32_t info;
1171 /** sender's message data buffer */
1172 void *tx_data;
1173 /** internal use only - needed for legacy API support */
1174 void *_rx_data;
1175 /** message data block descriptor */
1176 struct k_mem_block tx_block;
1177 /** source thread id */
1178 k_tid_t rx_source_thread;
1179 /** target thread id */
1180 k_tid_t tx_target_thread;
1181 /** internal use only - thread waiting on send (may be a dummy) */
1182 k_tid_t _syncing_thread;
1183#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1184 /** internal use only - semaphore used during asynchronous send */
1185 struct k_sem *_async_sem;
1186#endif
1187};
1188
1189struct k_mbox {
1190 _wait_q_t tx_msg_queue;
1191 _wait_q_t rx_msg_queue;
1192
1193 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1194};
1195
1196#define K_MBOX_INITIALIZER(obj) \
1197 { \
1198 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1199 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1200 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1201 }
1202
Peter Mitsis12092702016-10-14 12:57:23 -04001203/**
1204 * @brief Define a mailbox
1205 *
1206 * This declares and initializes a mailbox. The new mailbox can be passed to
Peter Mitsisd7a37502016-10-13 11:37:40 -04001207 * the kernel's mailbox functions.
Peter Mitsis12092702016-10-14 12:57:23 -04001208 *
1209 * @param name Name of the mailbox
1210 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001211#define K_MBOX_DEFINE(name) \
1212 struct k_mbox name = \
1213 K_MBOX_INITIALIZER(name) \
1214
Peter Mitsis12092702016-10-14 12:57:23 -04001215/**
1216 * @brief Initialize a mailbox.
1217 *
1218 * @param mbox Pointer to the mailbox object
1219 *
1220 * @return N/A
1221 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001222extern void k_mbox_init(struct k_mbox *mbox);
1223
Peter Mitsis12092702016-10-14 12:57:23 -04001224/**
1225 * @brief Send a mailbox message in a synchronous manner.
1226 *
1227 * Sends a message to a mailbox and waits for a receiver to process it.
1228 * The message data may be in a buffer, in a memory pool block, or non-existent
1229 * (i.e. empty message).
1230 *
1231 * @param mbox Pointer to the mailbox object.
1232 * @param tx_msg Pointer to transmit message descriptor.
1233 * @param timeout Maximum time (milliseconds) to wait for the message to be
1234 * received (although not necessarily completely processed).
1235 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long
1236 * as necessary.
1237 *
1238 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1239 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001240extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001241 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001242
1243#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1244/**
1245 * @brief Send a mailbox message in an asynchronous manner.
1246 *
1247 * Sends a message to a mailbox without waiting for a receiver to process it.
1248 * The message data may be in a buffer, in a memory pool block, or non-existent
1249 * (i.e. an empty message). Optionally, the specified semaphore will be given
1250 * by the mailbox when the message has been both received and disposed of
1251 * by the receiver.
1252 *
1253 * @param mbox Pointer to the mailbox object.
1254 * @param tx_msg Pointer to transmit message descriptor.
1255 * @param sem Semaphore identifier, or NULL if none specified.
1256 *
1257 * @return N/A
1258 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001259extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001260 struct k_sem *sem);
Peter Mitsis12092702016-10-14 12:57:23 -04001261#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001262
Peter Mitsis12092702016-10-14 12:57:23 -04001263/**
1264 * @brief Receive a mailbox message.
1265 *
1266 * Receives a message from a mailbox, then optionally retrieves its data
1267 * and disposes of the message.
1268 *
1269 * @param mbox Pointer to the mailbox object.
1270 * @param rx_msg Pointer to receive message descriptor.
1271 * @param buffer Pointer to buffer to receive data.
1272 * (Use NULL to defer data retrieval and message disposal until later.)
1273 * @param timeout Maximum time (milliseconds) to wait for a message.
1274 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1275 * necessary.
1276 *
1277 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1278 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001279extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001280 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001281
1282/**
1283 * @brief Retrieve mailbox message data into a buffer.
1284 *
1285 * Completes the processing of a received message by retrieving its data
1286 * into a buffer, then disposing of the message.
1287 *
1288 * Alternatively, this routine can be used to dispose of a received message
1289 * without retrieving its data.
1290 *
1291 * @param rx_msg Pointer to receive message descriptor.
1292 * @param buffer Pointer to buffer to receive data. (Use NULL to discard data.)
1293 *
1294 * @return N/A
1295 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001296extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04001297
1298/**
1299 * @brief Retrieve mailbox message data into a memory pool block.
1300 *
1301 * Completes the processing of a received message by retrieving its data
1302 * into a memory pool block, then disposing of the message. The memory pool
1303 * block that results from successful retrieval must be returned to the pool
1304 * once the data has been processed, even in cases where zero bytes of data
1305 * are retrieved.
1306 *
1307 * Alternatively, this routine can be used to dispose of a received message
1308 * without retrieving its data. In this case there is no need to return a
1309 * memory pool block to the pool.
1310 *
1311 * This routine allocates a new memory pool block for the data only if the
1312 * data is not already in one. If a new block cannot be allocated, the routine
1313 * returns a failure code and the received message is left unchanged. This
1314 * permits the caller to reattempt data retrieval at a later time or to dispose
1315 * of the received message without retrieving its data.
1316 *
1317 * @param rx_msg Pointer to receive message descriptor.
1318 * @param pool Memory pool identifier. (Use NULL to discard data.)
1319 * @param block Pointer to area to hold memory pool block info.
1320 * @param timeout Maximum time (milliseconds) to wait for a memory pool block.
1321 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1322 * necessary.
1323 *
1324 * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
1325 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001326extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001327 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001328 struct k_mem_block *block, int32_t timeout);
1329
1330/* pipes */
1331
1332struct k_pipe {
1333 unsigned char *buffer; /* Pipe buffer: may be NULL */
1334 size_t size; /* Buffer size */
1335 size_t bytes_used; /* # bytes used in buffer */
1336 size_t read_index; /* Where in buffer to read from */
1337 size_t write_index; /* Where in buffer to write */
1338
1339 struct {
1340 _wait_q_t readers; /* Reader wait queue */
1341 _wait_q_t writers; /* Writer wait queue */
1342 } wait_q;
1343
1344 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
1345};
1346
Peter Mitsise5d9c582016-10-14 14:44:57 -04001347#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001348 { \
1349 .buffer = pipe_buffer, \
1350 .size = pipe_buffer_size, \
1351 .bytes_used = 0, \
1352 .read_index = 0, \
1353 .write_index = 0, \
1354 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
1355 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
1356 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1357 }
1358
Peter Mitsise5d9c582016-10-14 14:44:57 -04001359#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
1360 static unsigned char __noinit __aligned(pipe_align) \
1361 _k_pipe_buf_##name[pipe_buffer_size]; \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001362 struct k_pipe name = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04001363 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001364
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001365/**
1366 * @brief Runtime initialization of a pipe
1367 *
1368 * @param pipe Pointer to pipe to initialize
1369 * @param buffer Pointer to buffer to use for pipe's ring buffer
1370 * @param size Size of the pipe's ring buffer
1371 *
1372 * @return N/A
1373 */
1374extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
1375 size_t size);
1376
1377/**
1378 * @brief Put a message into the specified pipe
1379 *
1380 * This routine synchronously adds a message into the pipe specified by
1381 * @a pipe. It will wait up to @a timeout for the pipe to accept
Peter Mitsise5d9c582016-10-14 14:44:57 -04001382 * @a bytes_to_write bytes of data. If by @a timeout, the pipe could not
1383 * accept @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001384 * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1385 *
1386 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001387 * @param data Data to put into the pipe
1388 * @param bytes_to_write Desired number of bytes to put into the pipe
1389 * @param bytes_written Number of bytes the pipe accepted
1390 * @param min_xfer Minimum number of bytes accepted for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001391 * @param timeout Maximum number of milliseconds to wait
1392 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001393 * @retval 0 At least @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001394 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001395 * @retval -EAGAIN Fewer than @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001396 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001397extern int k_pipe_put(struct k_pipe *pipe, void *data,
1398 size_t bytes_to_write, size_t *bytes_written,
1399 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001400
1401/**
1402 * @brief Get a message from the specified pipe
1403 *
1404 * This routine synchronously retrieves a message from the pipe specified by
Peter Mitsise5d9c582016-10-14 14:44:57 -04001405 * @a pipe. It will wait up to @a timeout to retrieve @a bytes_to_read
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001406 * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve
Peter Mitsise5d9c582016-10-14 14:44:57 -04001407 * @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001408 * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1409 *
1410 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001411 * @param data Location to place retrieved data
1412 * @param bytes_to_read Desired number of bytes to retrieve from the pipe
1413 * @param bytes_read Number of bytes retrieved from the pipe
1414 * @param min_xfer Minimum number of bytes retrieved for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001415 * @param timeout Maximum number of milliseconds to wait
1416 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001417 * @retval 0 At least @a min_xfer were transferred
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001418 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001419 * @retval -EAGAIN Fewer than @a min_xfer were retrieved
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001420 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001421extern int k_pipe_get(struct k_pipe *pipe, void *data,
1422 size_t bytes_to_read, size_t *bytes_read,
1423 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001424
Peter Mitsis2fef0232016-10-14 14:53:44 -04001425#if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001426/**
1427 * @brief Send a message to the specified pipe
1428 *
1429 * This routine asynchronously sends a message from the pipe specified by
1430 * @a pipe. Once all @a size bytes have been accepted by the pipe, it will
1431 * free the memory block @a block and give the semaphore @a sem (if specified).
1432 * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight
1433 * at any given time.
1434 *
1435 * @param pipe Pointer to the pipe
1436 * @param block Memory block containing data to send
1437 * @param size Number of data bytes in memory block to send
1438 * @param sem Semaphore to signal upon completion (else NULL)
1439 *
1440 * @retval N/A
1441 */
1442extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1443 size_t size, struct k_sem *sem);
Peter Mitsis2fef0232016-10-14 14:53:44 -04001444#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001445
1446/**
1447 * memory management
1448 */
1449
1450/* memory maps */
1451
1452struct k_mem_map {
1453 _wait_q_t wait_q;
1454 int num_blocks;
1455 int block_size;
1456 char *buffer;
1457 char *free_list;
1458 int num_used;
1459
1460 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_map);
1461};
1462
Peter Mitsis578f9112016-10-07 13:50:31 -04001463#define K_MEM_MAP_INITIALIZER(obj, map_buffer, map_block_size, map_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001464 { \
1465 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1466 .num_blocks = map_num_blocks, \
1467 .block_size = map_block_size, \
1468 .buffer = map_buffer, \
1469 .free_list = NULL, \
1470 .num_used = 0, \
1471 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1472 }
1473
Peter Mitsis578f9112016-10-07 13:50:31 -04001474/**
1475 * @brief Define a memory map
1476 *
1477 * This declares and initializes a memory map whose buffer is aligned to
1478 * a @a map_align -byte boundary. The new memory map can be passed to the
1479 * kernel's memory map functions.
1480 *
1481 * Note that for each of the blocks in the memory map to be aligned to
1482 * @a map_align bytes, then @a map_block_size must be a multiple of
1483 * @a map_align.
1484 *
1485 * @param name Name of the memory map
1486 * @param map_block_size Size of each block in the buffer (in bytes)
1487 * @param map_num_blocks Number blocks in the buffer
1488 * @param map_align Alignment of the memory map's buffer (power of 2)
1489 */
1490#define K_MEM_MAP_DEFINE(name, map_block_size, map_num_blocks, map_align) \
1491 char __aligned(map_align) \
1492 _k_mem_map_buf_##name[(map_num_blocks) * (map_block_size)]; \
1493 struct k_mem_map name \
1494 __in_section(_k_mem_map_ptr, private, mem_map) = \
1495 K_MEM_MAP_INITIALIZER(name, _k_mem_map_buf_##name, \
1496 map_block_size, map_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001497
Peter Mitsis578f9112016-10-07 13:50:31 -04001498extern void k_mem_map_init(struct k_mem_map *map, void *buffer,
1499 int block_size, int num_blocks);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001500extern int k_mem_map_alloc(struct k_mem_map *map, void **mem, int32_t timeout);
1501extern void k_mem_map_free(struct k_mem_map *map, void **mem);
1502
1503static inline int k_mem_map_num_used_get(struct k_mem_map *map)
1504{
1505 return map->num_used;
1506}
1507
Peter Mitsisc001aa82016-10-13 13:53:37 -04001508/**
1509 * @brief Get the number of unused memory blocks
1510 *
1511 * This routine gets the current number of unused memory blocks in the
1512 * specified pool. It should be used for stats purposes only as that value
1513 * may potentially be out-of-date by the time it is used.
1514 *
1515 * @param map Memory map to query
1516 *
1517 * @return Number of unused memory blocks
1518 */
1519static inline int k_mem_map_num_free_get(struct k_mem_map *map)
1520{
1521 return map->num_blocks - map->num_used;
1522}
1523
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001524/* memory pools */
1525
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001526/*
1527 * Memory pool requires a buffer and two arrays of structures for the
1528 * memory block accounting:
1529 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
1530 * status of four blocks of memory.
1531 */
1532struct k_mem_pool_quad_block {
1533 char *mem_blocks; /* pointer to the first of four memory blocks */
1534 uint32_t mem_status; /* four bits. If bit is set, memory block is
1535 allocated */
1536};
1537/*
1538 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
1539 * blocks of one size. Block sizes go from maximal to minimal. Next memory
1540 * block size is 4 times less than the previous one and thus requires 4 times
1541 * bigger array of k_mem_pool_quad_block structures to keep track of the
1542 * memory blocks.
1543 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001544
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001545/*
1546 * The array of k_mem_pool_block_set keeps the information of each array of
1547 * k_mem_pool_quad_block structures
1548 */
1549struct k_mem_pool_block_set {
1550 int block_size; /* memory block size */
1551 int nr_of_entries; /* nr of quad block structures in the array */
1552 struct k_mem_pool_quad_block *quad_block;
1553 int count;
1554};
1555
1556/* Memory pool descriptor */
1557struct k_mem_pool {
1558 int max_block_size;
1559 int min_block_size;
1560 int nr_of_maxblocks;
1561 int nr_of_block_sets;
1562 struct k_mem_pool_block_set *block_set;
1563 char *bufblock;
1564 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001565 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
1566};
1567
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001568#ifdef CONFIG_ARM
1569#define _SECTION_TYPE_SIGN "%"
1570#else
1571#define _SECTION_TYPE_SIGN "@"
1572#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001573
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001574/*
1575 * Static memory pool initialization
1576 */
1577/*
1578 * Use .altmacro to be able to recalculate values and pass them as string
1579 * arguments when calling assembler macros resursively
1580 */
1581__asm__(".altmacro\n\t");
1582
1583/*
1584 * Recursively calls a macro
1585 * The followig global symbols need to be initialized:
1586 * __memory_pool_max_block_size - maximal size of the memory block
1587 * __memory_pool_min_block_size - minimal size of the memory block
1588 * Notes:
1589 * Global symbols are used due the fact that assembler macro allows only
1590 * one argument be passed with the % conversion
1591 * Some assemblers do not get division operation ("/"). To avoid it >> 2
1592 * is used instead of / 4.
1593 * n_max argument needs to go first in the invoked macro, as some
1594 * assemblers concatenate \name and %(\n_max * 4) arguments
1595 * if \name goes first
1596 */
1597__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
1598 ".ifge __memory_pool_max_block_size >> 2 -"
1599 " __memory_pool_min_block_size\n\t\t"
1600 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
1601 "\\macro_name %(\\n_max * 4) \\name\n\t"
1602 ".endif\n\t"
1603 ".endm\n");
1604
1605/*
1606 * Build quad blocks
1607 * Macro allocates space in memory for the array of k_mem_pool_quad_block
1608 * structures and recursively calls itself for the next array, 4 times
1609 * larger.
1610 * The followig global symbols need to be initialized:
1611 * __memory_pool_max_block_size - maximal size of the memory block
1612 * __memory_pool_min_block_size - minimal size of the memory block
1613 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
1614 */
1615__asm__(".macro _build_quad_blocks n_max, name\n\t"
1616 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
1617 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
1618 ".if \\n_max % 4\n\t\t"
1619 ".skip __memory_pool_quad_block_size\n\t"
1620 ".endif\n\t"
1621 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
1622 ".endm\n");
1623
1624/*
1625 * Build block sets and initialize them
1626 * Macro initializes the k_mem_pool_block_set structure and
1627 * recursively calls itself for the next one.
1628 * The followig global symbols need to be initialized:
1629 * __memory_pool_max_block_size - maximal size of the memory block
1630 * __memory_pool_min_block_size - minimal size of the memory block
1631 * __memory_pool_block_set_count, the number of the elements in the
1632 * block set array must be set to 0. Macro calculates it's real
1633 * value.
1634 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
1635 * structures, _build_quad_blocks must be called prior it.
1636 */
1637__asm__(".macro _build_block_set n_max, name\n\t"
1638 ".int __memory_pool_max_block_size\n\t" /* block_size */
1639 ".if \\n_max % 4\n\t\t"
1640 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
1641 ".else\n\t\t"
1642 ".int \\n_max >> 2\n\t"
1643 ".endif\n\t"
1644 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
1645 ".int 0\n\t" /* count */
1646 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
1647 "__do_recurse _build_block_set \\name \\n_max\n\t"
1648 ".endm\n");
1649
1650/*
1651 * Build a memory pool structure and initialize it
1652 * Macro uses __memory_pool_block_set_count global symbol,
1653 * block set addresses and buffer address, it may be called only after
1654 * _build_block_set
1655 */
1656__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
1657 ".pushsection ._k_memory_pool,\"aw\","
1658 _SECTION_TYPE_SIGN "progbits\n\t"
1659 ".globl \\name\n\t"
1660 "\\name:\n\t"
1661 ".int \\max_size\n\t" /* max_block_size */
1662 ".int \\min_size\n\t" /* min_block_size */
1663 ".int \\n_max\n\t" /* nr_of_maxblocks */
1664 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
1665 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
1666 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
1667 ".int 0\n\t" /* wait_q->head */
1668 ".int 0\n\t" /* wait_q->next */
1669 ".popsection\n\t"
1670 ".endm\n");
1671
1672#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
1673 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1674 _SECTION_TYPE_SIGN "progbits\n\t"); \
1675 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
1676 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1677 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
1678 STRINGIFY(name) "\n\t"); \
1679 __asm__(".popsection\n\t")
1680
1681#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
1682 __asm__("__memory_pool_block_set_count = 0\n\t"); \
1683 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1684 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1685 _SECTION_TYPE_SIGN "progbits\n\t"); \
1686 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
1687 __asm__("_build_block_set " STRINGIFY(n_max) " " \
1688 STRINGIFY(name) "\n\t"); \
1689 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
1690 __asm__(".int __memory_pool_block_set_count\n\t"); \
1691 __asm__(".popsection\n\t"); \
1692 extern uint32_t _mem_pool_block_set_count_##name; \
1693 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
1694
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001695#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
1696 char __noinit __aligned(align) \
1697 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001698
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001699/**
1700 * @brief Define a memory pool
1701 *
1702 * This declares and initializes a memory pool whose buffer is aligned to
1703 * a @a align -byte boundary. The new memory pool can be passed to the
1704 * kernel's memory pool functions.
1705 *
1706 * Note that for each of the minimum sized blocks to be aligned to @a align
1707 * bytes, then @a min_size must be a multiple of @a align.
1708 *
1709 * @param name Name of the memory pool
1710 * @param min_size Minimum block size in the pool
1711 * @param max_size Maximum block size in the pool
1712 * @param n_max Number of maximum sized blocks in the pool
1713 * @param align Alignment of the memory pool's buffer
1714 */
1715#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001716 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
1717 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001718 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001719 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
1720 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
1721 extern struct k_mem_pool name
1722
1723/*
1724 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
1725 * to __memory_pool_quad_block_size absolute symbol.
1726 * This function does not get called, but compiler calculates the value and
1727 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
1728 */
1729static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
1730{
1731 __asm__(".globl __memory_pool_quad_block_size\n\t"
1732 "__memory_pool_quad_block_size = %c0\n\t"
1733 :
1734 : "n"(sizeof(struct k_mem_pool_quad_block)));
1735}
1736
Peter Mitsis937042c2016-10-13 13:18:26 -04001737/**
1738 * @brief Allocate memory from a memory pool
1739 *
1740 * @param pool Pointer to the memory pool object
1741 * @param block Pointer to the allocated memory's block descriptor
1742 * @param size Minimum number of bytes to allocate
1743 * @param timeout Maximum time (milliseconds) to wait for operation to
1744 * complete. Use K_NO_WAIT to return immediately, or K_FOREVER
1745 * to wait as long as necessary.
1746 *
1747 * @return 0 on success, -ENOMEM on failure
1748 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001749extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis937042c2016-10-13 13:18:26 -04001750 int size, int32_t timeout);
1751
1752/**
1753 * @brief Return previously allocated memory to its memory pool
1754 *
1755 * @param block Pointer to allocated memory's block descriptor
1756 *
1757 * @return N/A
1758 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001759extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04001760
1761/**
1762 * @brief Defragment the specified memory pool
1763 *
1764 * @param pool Pointer to the memory pool object
1765 *
1766 * @return N/A
1767 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001768extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04001769
1770/**
1771 * @brief Allocate memory from heap pool
1772 *
1773 * This routine provides traditional malloc semantics; internally it uses
1774 * the memory pool APIs on a dedicated HEAP pool
1775 *
1776 * @param size Size of memory requested by the caller (in bytes)
1777 *
1778 * @return Address of the allocated memory on success; otherwise NULL
1779 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001780extern void *k_malloc(uint32_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04001781
1782/**
1783 * @brief Free memory allocated through k_malloc()
1784 *
1785 * @param ptr Pointer to previously allocated memory
1786 *
1787 * @return N/A
1788 */
1789extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001790
1791/*
1792 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
1793 * hook into the device subsystem, which itself uses nanokernel semaphores,
1794 * and thus currently requires the definition of nano_sem.
1795 */
1796#include <legacy.h>
1797#include <arch/cpu.h>
1798
1799/*
1800 * private APIs that are utilized by one or more public APIs
1801 */
1802
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001803extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001804extern void _init_static_threads(void);
1805
1806#ifdef __cplusplus
1807}
1808#endif
1809
1810#endif /* _kernel__h_ */