blob: 7c1718b9c4f68958cb5605f44de559097570a8a6 [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
300/* timers */
301
302struct k_timer {
303 /*
304 * _timeout structure must be first here if we want to use
305 * dynamic timer allocation. timeout.node is used in the double-linked
306 * list of free timers
307 */
308 struct _timeout timeout;
309
310 /* wait queue for the threads waiting on this timer */
311 _wait_q_t wait_q;
312
313 /* runs in ISR context */
314 void (*handler)(void *);
315 void *handler_arg;
316
317 /* runs in the context of the thread that calls k_timer_stop() */
318 void (*stop_handler)(void *);
319 void *stop_handler_arg;
320
321 /* timer period */
322 int32_t period;
323
324 /* user supplied data pointer returned to the thread*/
325 void *user_data;
326
327 /* user supplied data pointer */
328 void *user_data_internal;
329
330 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
331};
332
333#define K_TIMER_INITIALIZER(obj) \
334 { \
335 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
336 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
337 }
338
339#define K_TIMER_DEFINE(name) \
340 struct k_timer name = K_TIMER_INITIALIZER(name)
341
342extern void k_timer_init(struct k_timer *timer, void *data);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700343
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400344extern void k_timer_start(struct k_timer *timer,
345 int32_t duration, int32_t period,
346 void (*handler)(void *), void *handler_arg,
347 void (*stop_handler)(void *), void *stop_handler_arg);
348extern void k_timer_restart(struct k_timer *timer, int32_t duration,
349 int32_t period);
350extern void k_timer_stop(struct k_timer *timer);
351extern int k_timer_test(struct k_timer *timer, void **data, int wait);
352extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400353
354
355/**
356 * @brief Get the time elapsed since the system booted (uptime)
357 *
358 * @return The current uptime of the system in ms
359 */
360
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400361extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400362
363/**
364 * @brief Get the lower 32-bit of time elapsed since the system booted (uptime)
365 *
366 * This function is potentially less onerous in both the time it takes to
367 * execute, the interrupt latency it introduces and the amount of 64-bit math
368 * it requires than k_uptime_get(), but it only provides an uptime value of
369 * 32-bits. The user must handle possible rollovers/spillovers.
370 *
371 * At a rate of increment of 1000 per second, it rolls over approximately every
372 * 50 days.
373 *
374 * @return The current uptime of the system in ms
375 */
376
377extern uint32_t k_uptime_get_32(void);
378
379/**
380 * @brief Get the difference between a reference time and the current uptime
381 *
382 * @param reftime A pointer to a reference time. It is updated with the current
383 * uptime upon return.
384 *
385 * @return The delta between the reference time and the current uptime.
386 */
387
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400388extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400389
390/**
391 * @brief Get the difference between a reference time and the current uptime
392 *
393 * The 32-bit version of k_uptime_delta(). It has the same perks and issues as
394 * k_uptime_get_32().
395 *
396 * @param reftime A pointer to a reference time. It is updated with the current
397 * uptime upon return.
398 *
399 * @return The delta between the reference time and the current uptime.
400 */
401
402extern uint32_t k_uptime_delta_32(int64_t *reftime);
403
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400404extern uint32_t k_cycle_get_32(void);
405
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400406/**
407 * data transfers (basic)
408 */
409
410/* fifos */
411
412struct k_fifo {
413 _wait_q_t wait_q;
414 sys_slist_t data_q;
415
416 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
417};
418
419extern void k_fifo_init(struct k_fifo *fifo);
420extern void k_fifo_put(struct k_fifo *fifo, void *data);
421extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
422extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
423extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
424
425#define K_FIFO_INITIALIZER(obj) \
426 { \
427 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400428 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400429 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
430 }
431
432#define K_FIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400433 struct k_fifo name = K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400434
435/* lifos */
436
437struct k_lifo {
438 _wait_q_t wait_q;
439 void *list;
440
441 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
442};
443
444extern void k_lifo_init(struct k_lifo *lifo);
445extern void k_lifo_put(struct k_lifo *lifo, void *data);
446extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
447
448#define K_LIFO_INITIALIZER(obj) \
449 { \
450 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
451 .list = NULL, \
452 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
453 }
454
455#define K_LIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400456 struct k_lifo name = K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400457
458/* stacks */
459
460struct k_stack {
461 _wait_q_t wait_q;
462 uint32_t *base, *next, *top;
463
464 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
465};
466
Allan Stephens018cd9a2016-10-07 15:13:24 -0500467extern void k_stack_init(struct k_stack *stack,
468 uint32_t *buffer, int num_entries);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400469extern void k_stack_push(struct k_stack *stack, uint32_t data);
470extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
471
472#define K_STACK_INITIALIZER(obj, stack_num_entries, stack_buffer) \
473 { \
474 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
475 .base = stack_buffer, \
476 .next = stack_buffer, \
477 .top = stack_buffer + stack_num_entries, \
478 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
479 }
480
481#define K_STACK_DEFINE(name, stack_num_entries) \
482 uint32_t __noinit _k_stack_buf_##name[stack_num_entries]; \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400483 struct k_stack name = \
484 K_STACK_INITIALIZER(name, stack_num_entries, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400485 _k_stack_buf_##name); \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400486
487#define K_STACK_SIZE(stack_num_entries) \
488 (sizeof(struct k_stack) + (stack_num_entries * sizeof(uint32_t)))
489
490/**
491 * workqueues
492 */
493
494struct k_work;
495
496typedef void (*k_work_handler_t)(struct k_work *);
497
498/**
499 * A workqueue is a fiber that executes @ref k_work items that are
500 * queued to it. This is useful for drivers which need to schedule
501 * execution of code which might sleep from ISR context. The actual
502 * fiber identifier is not stored in the structure in order to save
503 * space.
504 */
505struct k_work_q {
506 struct k_fifo fifo;
507};
508
509/**
510 * @brief Work flags.
511 */
512enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300513 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400514};
515
516/**
517 * @brief An item which can be scheduled on a @ref k_work_q.
518 */
519struct k_work {
520 void *_reserved; /* Used by k_fifo implementation. */
521 k_work_handler_t handler;
522 atomic_t flags[1];
523};
524
525/**
526 * @brief Statically initialize work item
527 */
528#define K_WORK_INITIALIZER(work_handler) \
529 { \
530 ._reserved = NULL, \
531 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300532 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400533 }
534
535/**
536 * @brief Dynamically initialize work item
537 */
538static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
539{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300540 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400541 work->handler = handler;
542}
543
544/**
545 * @brief Submit a work item to a workqueue.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300546 *
547 * This procedure schedules a work item to be processed.
548 * In the case where the work item has already been submitted and is pending
549 * execution, calling this function will result in a no-op. In this case, the
550 * work item must not be modified externally (e.g. by the caller of this
551 * function), since that could cause the work item to be processed in a
552 * corrupted state.
553 *
554 * @param work_q to schedule the work item
555 * @param work work item
556 *
557 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400558 */
559static inline void k_work_submit_to_queue(struct k_work_q *work_q,
560 struct k_work *work)
561{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +0300562 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400563 k_fifo_put(&work_q->fifo, work);
564 }
565}
566
567/**
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300568 * @brief Check if work item is pending.
569 */
570static inline int k_work_pending(struct k_work *work)
571{
Iván Briano9c7b5ea2016-10-04 18:11:05 -0300572 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +0300573}
574
575/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400576 * @brief Start a new workqueue. This routine can be called from either
577 * fiber or task context.
578 */
579extern void k_work_q_start(struct k_work_q *work_q,
580 const struct k_thread_config *config);
581
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400582#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400583
584 /*
585 * @brief An item which can be scheduled on a @ref k_work_q with a
586 * delay.
587 */
588struct k_delayed_work {
589 struct k_work work;
590 struct _timeout timeout;
591 struct k_work_q *work_q;
592};
593
594/**
595 * @brief Initialize delayed work
596 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400597extern void k_delayed_work_init(struct k_delayed_work *work,
598 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400599
600/**
601 * @brief Submit a delayed work item to a workqueue.
602 *
603 * This procedure schedules a work item to be processed after a delay.
604 * Once the delay has passed, the work item is submitted to the work queue:
605 * at this point, it is no longer possible to cancel it. Once the work item's
606 * handler is about to be executed, the work is considered complete and can be
607 * resubmitted.
608 *
609 * Care must be taken if the handler blocks or yield as there is no implicit
610 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
611 * it should be explicitly done between the submitter and the handler.
612 *
613 * @param work_q to schedule the work item
614 * @param work Delayed work item
615 * @param ticks Ticks to wait before scheduling the work item
616 *
617 * @return 0 in case of success or negative value in case of error.
618 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400619extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
620 struct k_delayed_work *work,
621 int32_t ticks);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400622
623/**
624 * @brief Cancel a delayed work item
625 *
626 * This procedure cancels a scheduled work item. If the work has been completed
627 * or is idle, this will do nothing. The only case where this can fail is when
628 * the work has been submitted to the work queue, but the handler has not run
629 * yet.
630 *
631 * @param work Delayed work item to be canceled
632 *
633 * @return 0 in case of success or negative value in case of error.
634 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400635extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400636
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400637#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400638
639#if defined(CONFIG_SYSTEM_WORKQUEUE)
640
641extern struct k_work_q k_sys_work_q;
642
643/*
644 * @brief Submit a work item to the system workqueue.
645 *
646 * @ref k_work_submit_to_queue
647 *
648 * When using the system workqueue it is not recommended to block or yield
649 * on the handler since its fiber is shared system wide it may cause
650 * unexpected behavior.
651 */
652static inline void k_work_submit(struct k_work *work)
653{
654 k_work_submit_to_queue(&k_sys_work_q, work);
655}
656
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400657#if defined(CONFIG_SYS_CLOCK_EXISTS)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400658/*
659 * @brief Submit a delayed work item to the system workqueue.
660 *
661 * @ref k_delayed_work_submit_to_queue
662 *
663 * When using the system workqueue it is not recommended to block or yield
664 * on the handler since its fiber is shared system wide it may cause
665 * unexpected behavior.
666 */
667static inline int k_delayed_work_submit(struct k_delayed_work *work,
668 int ticks)
669{
670 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, ticks);
671}
672
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400673#endif /* CONFIG_SYS_CLOCK_EXISTS */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400674#endif /* CONFIG_SYSTEM_WORKQUEUE */
675
676/**
677 * synchronization
678 */
679
680/* mutexes */
681
682struct k_mutex {
683 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400684 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400685 uint32_t lock_count;
686 int owner_orig_prio;
687#ifdef CONFIG_OBJECT_MONITOR
688 int num_lock_state_changes;
689 int num_conflicts;
690#endif
691
692 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
693};
694
695#ifdef CONFIG_OBJECT_MONITOR
696#define _MUTEX_INIT_OBJECT_MONITOR \
697 .num_lock_state_changes = 0, .num_conflicts = 0,
698#else
699#define _MUTEX_INIT_OBJECT_MONITOR
700#endif
701
702#define K_MUTEX_INITIALIZER(obj) \
703 { \
704 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
705 .owner = NULL, \
706 .lock_count = 0, \
707 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
708 _MUTEX_INIT_OBJECT_MONITOR \
709 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
710 }
711
712#define K_MUTEX_DEFINE(name) \
713 struct k_mutex name = K_MUTEX_INITIALIZER(name)
714
715extern void k_mutex_init(struct k_mutex *mutex);
716extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
717extern void k_mutex_unlock(struct k_mutex *mutex);
718
719/* semaphores */
720
721struct k_sem {
722 _wait_q_t wait_q;
723 unsigned int count;
724 unsigned int limit;
725
726 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
727};
728
729extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
730 unsigned int limit);
731extern int k_sem_take(struct k_sem *sem, int32_t timeout);
732extern void k_sem_give(struct k_sem *sem);
733
Benjamin Walsh70c68b92016-09-21 10:37:34 -0400734static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400735{
736 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400737}
738
Tomasz Bursztyka276086d2016-09-21 16:03:21 +0200739static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400740{
741 return sem->count;
742}
743
Peter Mitsis45403672016-09-09 14:24:06 -0400744#ifdef CONFIG_SEMAPHORE_GROUPS
745/**
746 * @brief Take the first available semaphore
747 *
748 * Given a list of semaphore pointers, this routine will attempt to take one
749 * of them, waiting up to a maximum of @a timeout ms to do so. The taken
750 * semaphore is identified by @a sem (set to NULL on error).
751 *
752 * Be aware that the more semaphores specified in the group, the more stack
753 * space is required by the waiting thread.
754 *
755 * @param sem_array Array of semaphore pointers terminated by a K_END entry
756 * @param sem Identifies the semaphore that was taken
757 * @param timeout Maximum number of milliseconds to wait
758 *
759 * @retval 0 A semaphore was successfully taken
760 * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT)
761 * @retval -EAGAIN Time out occurred while waiting for semaphore
762 */
763
764extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem,
765 int32_t timeout);
766
767/**
768 * @brief Give all the semaphores in the group
769 *
770 * This routine will give each semaphore in the array of semaphore pointers.
771 *
772 * @param sem_array Array of semaphore pointers terminated by a K_END entry
773 *
774 * @return N/A
775 */
776extern void k_sem_group_give(struct k_sem *sem_array[]);
777
778/**
779 * @brief Reset the count to zero on each semaphore in the array
780 *
781 * This routine resets the count of each semaphore in the group to zero.
782 * Note that it does NOT have any impact on any thread that might have
783 * been previously pending on any of the semaphores.
784 *
785 * @param sem_array Array of semaphore pointers terminated by a K_END entry
786 *
787 * @return N/A
788 */
789extern void k_sem_group_reset(struct k_sem *sem_array[]);
790#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400791
792#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
793 { \
794 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
795 .count = initial_count, \
796 .limit = count_limit, \
797 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
798 }
799
800#define K_SEM_DEFINE(name, initial_count, count_limit) \
801 struct k_sem name = \
802 K_SEM_INITIALIZER(name, initial_count, count_limit)
803
804/* events */
805
806#define K_EVT_DEFAULT NULL
807#define K_EVT_IGNORE ((void *)(-1))
808
809typedef int (*k_event_handler_t)(struct k_event *);
810
811struct k_event {
812 k_event_handler_t handler;
813 atomic_t send_count;
814 struct k_work work_item;
815 struct k_sem sem;
816
817 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_event);
818};
819
820extern void _k_event_deliver(struct k_work *work);
821
822#define K_EVENT_INITIALIZER(obj, event_handler) \
823 { \
824 .handler = (k_event_handler_t)event_handler, \
825 .send_count = ATOMIC_INIT(0), \
826 .work_item = K_WORK_INITIALIZER(_k_event_deliver), \
827 .sem = K_SEM_INITIALIZER(obj.sem, 0, 1), \
828 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
829 }
830
831#define K_EVENT_DEFINE(name, event_handler) \
832 struct k_event name \
833 __in_section(_k_event_list, event, name) = \
834 K_EVENT_INITIALIZER(name, event_handler)
835
836extern void k_event_init(struct k_event *event, k_event_handler_t handler);
837extern int k_event_recv(struct k_event *event, int32_t timeout);
838extern void k_event_send(struct k_event *event);
839
840/**
841 * data transfers (complex)
842 */
843
844/* message queues */
845
846struct k_msgq {
847 _wait_q_t wait_q;
848 uint32_t msg_size;
849 uint32_t max_msgs;
850 char *buffer_start;
851 char *buffer_end;
852 char *read_ptr;
853 char *write_ptr;
854 uint32_t used_msgs;
855
856 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
857};
858
Peter Mitsis1da807e2016-10-06 11:36:59 -0400859#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400860 { \
861 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -0400862 .max_msgs = q_max_msgs, \
863 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400864 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -0400865 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400866 .read_ptr = q_buffer, \
867 .write_ptr = q_buffer, \
868 .used_msgs = 0, \
869 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
870 }
871
Peter Mitsis1da807e2016-10-06 11:36:59 -0400872/**
873 * @brief Define a message queue
874 *
875 * This declares and initializes a message queue whose buffer is aligned to
876 * a @a q_align -byte boundary. The new message queue can be passed to the
877 * kernel's message queue functions.
878 *
879 * Note that for each of the mesages in the message queue to be aligned to
880 * @a q_align bytes, then @a q_msg_size must be a multiple of @a q_align.
881 *
882 * @param q_name Name of the message queue
883 * @param q_msg_size The size in bytes of each message
884 * @param q_max_msgs Maximum number of messages the queue can hold
885 * @param q_align Alignment of the message queue's buffer (power of 2)
886 */
887#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
888 static char __noinit __aligned(q_align) \
889 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
890 struct k_msgq q_name = \
891 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
892 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400893
Peter Mitsisd7a37502016-10-13 11:37:40 -0400894/**
895 * @brief Initialize a message queue.
896 *
897 * @param q Pointer to the message queue object.
898 * @param buffer Pointer to memory area that holds queued messages.
899 * @param msg_size Message size, in bytes.
900 * @param max_msgs Maximum number of messages that can be queued.
901 *
902 * @return N/A
903 */
Peter Mitsis1da807e2016-10-06 11:36:59 -0400904extern void k_msgq_init(struct k_msgq *q, char *buffer,
905 uint32_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -0400906
907/**
908 * @brief Add a message to a message queue.
909 *
910 * This routine adds an item to the message queue. When the message queue is
911 * full, the routine will wait either for space to become available, or until
912 * the specified time limit is reached.
913 *
914 * @param q Pointer to the message queue object.
915 * @param data Pointer to message data area.
916 * @param timeout Number of milliseconds to wait until space becomes available
917 * to add the message into the message queue, or one of the
918 * special values K_NO_WAIT and K_FOREVER.
919 *
920 * @return 0 if successful, -ENOMSG if failed immediately or after queue purge,
921 * -EAGAIN if timed out
922 *
923 * @sa K_NO_WAIT, K_FOREVER
924 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400925extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -0400926
927/**
928 * @brief Obtain a message from a message queue.
929 *
930 * This routine fetches the oldest item from the message queue. When the message
931 * queue is found empty, the routine will wait either until an item is added to
932 * the message queue or until the specified time limit is reached.
933 *
934 * @param q Pointer to the message queue object.
935 * @param data Pointer to message data area.
936 * @param timeout Number of milliseconds to wait to obtain message, or one of
937 * the special values K_NO_WAIT and K_FOREVER.
938 *
939 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
940 *
941 * @sa K_NO_WAIT, K_FOREVER
942 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400943extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -0400944
945/**
946 * @brief Purge contents of a message queue.
947 *
948 * Discards all messages currently in the message queue, and cancels
949 * any "add message" operations initiated by waiting threads.
950 *
951 * @param q Pointer to the message queue object.
952 *
953 * @return N/A
954 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400955extern void k_msgq_purge(struct k_msgq *q);
956
Peter Mitsis67be2492016-10-07 11:44:34 -0400957/**
958 * @brief Get the number of unused messages
959 *
960 * @param q Message queue to query
961 *
962 * @return Number of unused messages
963 */
964static inline int k_msgq_num_free_get(struct k_msgq *q)
965{
966 return q->max_msgs - q->used_msgs;
967}
968
Peter Mitsisd7a37502016-10-13 11:37:40 -0400969/**
970 * @brief Get the number of used messages
971 *
972 * @param q Message queue to query
973 *
974 * @return Number of used messages
975 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400976static inline int k_msgq_num_used_get(struct k_msgq *q)
977{
978 return q->used_msgs;
979}
980
981struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -0400982 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400983 void *addr_in_pool;
984 void *data;
985 uint32_t req_size;
986};
987
988/* mailboxes */
989
990struct k_mbox_msg {
991 /** internal use only - needed for legacy API support */
992 uint32_t _mailbox;
993 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -0400994 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400995 /** application-defined information value */
996 uint32_t info;
997 /** sender's message data buffer */
998 void *tx_data;
999 /** internal use only - needed for legacy API support */
1000 void *_rx_data;
1001 /** message data block descriptor */
1002 struct k_mem_block tx_block;
1003 /** source thread id */
1004 k_tid_t rx_source_thread;
1005 /** target thread id */
1006 k_tid_t tx_target_thread;
1007 /** internal use only - thread waiting on send (may be a dummy) */
1008 k_tid_t _syncing_thread;
1009#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1010 /** internal use only - semaphore used during asynchronous send */
1011 struct k_sem *_async_sem;
1012#endif
1013};
1014
1015struct k_mbox {
1016 _wait_q_t tx_msg_queue;
1017 _wait_q_t rx_msg_queue;
1018
1019 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
1020};
1021
1022#define K_MBOX_INITIALIZER(obj) \
1023 { \
1024 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
1025 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
1026 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1027 }
1028
Peter Mitsis12092702016-10-14 12:57:23 -04001029/**
1030 * @brief Define a mailbox
1031 *
1032 * This declares and initializes a mailbox. The new mailbox can be passed to
Peter Mitsisd7a37502016-10-13 11:37:40 -04001033 * the kernel's mailbox functions.
Peter Mitsis12092702016-10-14 12:57:23 -04001034 *
1035 * @param name Name of the mailbox
1036 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001037#define K_MBOX_DEFINE(name) \
1038 struct k_mbox name = \
1039 K_MBOX_INITIALIZER(name) \
1040
Peter Mitsis12092702016-10-14 12:57:23 -04001041/**
1042 * @brief Initialize a mailbox.
1043 *
1044 * @param mbox Pointer to the mailbox object
1045 *
1046 * @return N/A
1047 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001048extern void k_mbox_init(struct k_mbox *mbox);
1049
Peter Mitsis12092702016-10-14 12:57:23 -04001050/**
1051 * @brief Send a mailbox message in a synchronous manner.
1052 *
1053 * Sends a message to a mailbox and waits for a receiver to process it.
1054 * The message data may be in a buffer, in a memory pool block, or non-existent
1055 * (i.e. empty message).
1056 *
1057 * @param mbox Pointer to the mailbox object.
1058 * @param tx_msg Pointer to transmit message descriptor.
1059 * @param timeout Maximum time (milliseconds) to wait for the message to be
1060 * received (although not necessarily completely processed).
1061 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long
1062 * as necessary.
1063 *
1064 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1065 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001066extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001067 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001068
1069#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
1070/**
1071 * @brief Send a mailbox message in an asynchronous manner.
1072 *
1073 * Sends a message to a mailbox without waiting for a receiver to process it.
1074 * The message data may be in a buffer, in a memory pool block, or non-existent
1075 * (i.e. an empty message). Optionally, the specified semaphore will be given
1076 * by the mailbox when the message has been both received and disposed of
1077 * by the receiver.
1078 *
1079 * @param mbox Pointer to the mailbox object.
1080 * @param tx_msg Pointer to transmit message descriptor.
1081 * @param sem Semaphore identifier, or NULL if none specified.
1082 *
1083 * @return N/A
1084 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001085extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001086 struct k_sem *sem);
Peter Mitsis12092702016-10-14 12:57:23 -04001087#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001088
Peter Mitsis12092702016-10-14 12:57:23 -04001089/**
1090 * @brief Receive a mailbox message.
1091 *
1092 * Receives a message from a mailbox, then optionally retrieves its data
1093 * and disposes of the message.
1094 *
1095 * @param mbox Pointer to the mailbox object.
1096 * @param rx_msg Pointer to receive message descriptor.
1097 * @param buffer Pointer to buffer to receive data.
1098 * (Use NULL to defer data retrieval and message disposal until later.)
1099 * @param timeout Maximum time (milliseconds) to wait for a message.
1100 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1101 * necessary.
1102 *
1103 * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out
1104 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001105extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001106 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04001107
1108/**
1109 * @brief Retrieve mailbox message data into a buffer.
1110 *
1111 * Completes the processing of a received message by retrieving its data
1112 * into a buffer, then disposing of the message.
1113 *
1114 * Alternatively, this routine can be used to dispose of a received message
1115 * without retrieving its data.
1116 *
1117 * @param rx_msg Pointer to receive message descriptor.
1118 * @param buffer Pointer to buffer to receive data. (Use NULL to discard data.)
1119 *
1120 * @return N/A
1121 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001122extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04001123
1124/**
1125 * @brief Retrieve mailbox message data into a memory pool block.
1126 *
1127 * Completes the processing of a received message by retrieving its data
1128 * into a memory pool block, then disposing of the message. The memory pool
1129 * block that results from successful retrieval must be returned to the pool
1130 * once the data has been processed, even in cases where zero bytes of data
1131 * are retrieved.
1132 *
1133 * Alternatively, this routine can be used to dispose of a received message
1134 * without retrieving its data. In this case there is no need to return a
1135 * memory pool block to the pool.
1136 *
1137 * This routine allocates a new memory pool block for the data only if the
1138 * data is not already in one. If a new block cannot be allocated, the routine
1139 * returns a failure code and the received message is left unchanged. This
1140 * permits the caller to reattempt data retrieval at a later time or to dispose
1141 * of the received message without retrieving its data.
1142 *
1143 * @param rx_msg Pointer to receive message descriptor.
1144 * @param pool Memory pool identifier. (Use NULL to discard data.)
1145 * @param block Pointer to area to hold memory pool block info.
1146 * @param timeout Maximum time (milliseconds) to wait for a memory pool block.
1147 * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as
1148 * necessary.
1149 *
1150 * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out
1151 */
Peter Mitsis40680f62016-10-14 10:04:55 -04001152extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04001153 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001154 struct k_mem_block *block, int32_t timeout);
1155
1156/* pipes */
1157
1158struct k_pipe {
1159 unsigned char *buffer; /* Pipe buffer: may be NULL */
1160 size_t size; /* Buffer size */
1161 size_t bytes_used; /* # bytes used in buffer */
1162 size_t read_index; /* Where in buffer to read from */
1163 size_t write_index; /* Where in buffer to write */
1164
1165 struct {
1166 _wait_q_t readers; /* Reader wait queue */
1167 _wait_q_t writers; /* Writer wait queue */
1168 } wait_q;
1169
1170 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
1171};
1172
Peter Mitsise5d9c582016-10-14 14:44:57 -04001173#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001174 { \
1175 .buffer = pipe_buffer, \
1176 .size = pipe_buffer_size, \
1177 .bytes_used = 0, \
1178 .read_index = 0, \
1179 .write_index = 0, \
1180 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
1181 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
1182 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1183 }
1184
Peter Mitsise5d9c582016-10-14 14:44:57 -04001185#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
1186 static unsigned char __noinit __aligned(pipe_align) \
1187 _k_pipe_buf_##name[pipe_buffer_size]; \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001188 struct k_pipe name = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04001189 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001190
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001191/**
1192 * @brief Runtime initialization of a pipe
1193 *
1194 * @param pipe Pointer to pipe to initialize
1195 * @param buffer Pointer to buffer to use for pipe's ring buffer
1196 * @param size Size of the pipe's ring buffer
1197 *
1198 * @return N/A
1199 */
1200extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
1201 size_t size);
1202
1203/**
1204 * @brief Put a message into the specified pipe
1205 *
1206 * This routine synchronously adds a message into the pipe specified by
1207 * @a pipe. It will wait up to @a timeout for the pipe to accept
Peter Mitsise5d9c582016-10-14 14:44:57 -04001208 * @a bytes_to_write bytes of data. If by @a timeout, the pipe could not
1209 * accept @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001210 * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1211 *
1212 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001213 * @param data Data to put into the pipe
1214 * @param bytes_to_write Desired number of bytes to put into the pipe
1215 * @param bytes_written Number of bytes the pipe accepted
1216 * @param min_xfer Minimum number of bytes accepted for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001217 * @param timeout Maximum number of milliseconds to wait
1218 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001219 * @retval 0 At least @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001220 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001221 * @retval -EAGAIN Fewer than @a min_xfer were sent
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001222 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001223extern int k_pipe_put(struct k_pipe *pipe, void *data,
1224 size_t bytes_to_write, size_t *bytes_written,
1225 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001226
1227/**
1228 * @brief Get a message from the specified pipe
1229 *
1230 * This routine synchronously retrieves a message from the pipe specified by
Peter Mitsise5d9c582016-10-14 14:44:57 -04001231 * @a pipe. It will wait up to @a timeout to retrieve @a bytes_to_read
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001232 * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve
Peter Mitsise5d9c582016-10-14 14:44:57 -04001233 * @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001234 * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
1235 *
1236 * @param pipe Pointer to the pipe
Peter Mitsise5d9c582016-10-14 14:44:57 -04001237 * @param data Location to place retrieved data
1238 * @param bytes_to_read Desired number of bytes to retrieve from the pipe
1239 * @param bytes_read Number of bytes retrieved from the pipe
1240 * @param min_xfer Minimum number of bytes retrieved for success
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001241 * @param timeout Maximum number of milliseconds to wait
1242 *
Peter Mitsise5d9c582016-10-14 14:44:57 -04001243 * @retval 0 At least @a min_xfer were transferred
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001244 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
Peter Mitsise5d9c582016-10-14 14:44:57 -04001245 * @retval -EAGAIN Fewer than @a min_xfer were retrieved
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001246 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04001247extern int k_pipe_get(struct k_pipe *pipe, void *data,
1248 size_t bytes_to_read, size_t *bytes_read,
1249 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001250
Peter Mitsis2fef0232016-10-14 14:53:44 -04001251#if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001252/**
1253 * @brief Send a message to the specified pipe
1254 *
1255 * This routine asynchronously sends a message from the pipe specified by
1256 * @a pipe. Once all @a size bytes have been accepted by the pipe, it will
1257 * free the memory block @a block and give the semaphore @a sem (if specified).
1258 * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight
1259 * at any given time.
1260 *
1261 * @param pipe Pointer to the pipe
1262 * @param block Memory block containing data to send
1263 * @param size Number of data bytes in memory block to send
1264 * @param sem Semaphore to signal upon completion (else NULL)
1265 *
1266 * @retval N/A
1267 */
1268extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1269 size_t size, struct k_sem *sem);
Peter Mitsis2fef0232016-10-14 14:53:44 -04001270#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001271
1272/**
1273 * memory management
1274 */
1275
1276/* memory maps */
1277
1278struct k_mem_map {
1279 _wait_q_t wait_q;
1280 int num_blocks;
1281 int block_size;
1282 char *buffer;
1283 char *free_list;
1284 int num_used;
1285
1286 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_map);
1287};
1288
Peter Mitsis578f9112016-10-07 13:50:31 -04001289#define K_MEM_MAP_INITIALIZER(obj, map_buffer, map_block_size, map_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001290 { \
1291 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1292 .num_blocks = map_num_blocks, \
1293 .block_size = map_block_size, \
1294 .buffer = map_buffer, \
1295 .free_list = NULL, \
1296 .num_used = 0, \
1297 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1298 }
1299
Peter Mitsis578f9112016-10-07 13:50:31 -04001300/**
1301 * @brief Define a memory map
1302 *
1303 * This declares and initializes a memory map whose buffer is aligned to
1304 * a @a map_align -byte boundary. The new memory map can be passed to the
1305 * kernel's memory map functions.
1306 *
1307 * Note that for each of the blocks in the memory map to be aligned to
1308 * @a map_align bytes, then @a map_block_size must be a multiple of
1309 * @a map_align.
1310 *
1311 * @param name Name of the memory map
1312 * @param map_block_size Size of each block in the buffer (in bytes)
1313 * @param map_num_blocks Number blocks in the buffer
1314 * @param map_align Alignment of the memory map's buffer (power of 2)
1315 */
1316#define K_MEM_MAP_DEFINE(name, map_block_size, map_num_blocks, map_align) \
1317 char __aligned(map_align) \
1318 _k_mem_map_buf_##name[(map_num_blocks) * (map_block_size)]; \
1319 struct k_mem_map name \
1320 __in_section(_k_mem_map_ptr, private, mem_map) = \
1321 K_MEM_MAP_INITIALIZER(name, _k_mem_map_buf_##name, \
1322 map_block_size, map_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001323
Peter Mitsis578f9112016-10-07 13:50:31 -04001324extern void k_mem_map_init(struct k_mem_map *map, void *buffer,
1325 int block_size, int num_blocks);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001326extern int k_mem_map_alloc(struct k_mem_map *map, void **mem, int32_t timeout);
1327extern void k_mem_map_free(struct k_mem_map *map, void **mem);
1328
1329static inline int k_mem_map_num_used_get(struct k_mem_map *map)
1330{
1331 return map->num_used;
1332}
1333
Peter Mitsisc001aa82016-10-13 13:53:37 -04001334/**
1335 * @brief Get the number of unused memory blocks
1336 *
1337 * This routine gets the current number of unused memory blocks in the
1338 * specified pool. It should be used for stats purposes only as that value
1339 * may potentially be out-of-date by the time it is used.
1340 *
1341 * @param map Memory map to query
1342 *
1343 * @return Number of unused memory blocks
1344 */
1345static inline int k_mem_map_num_free_get(struct k_mem_map *map)
1346{
1347 return map->num_blocks - map->num_used;
1348}
1349
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001350/* memory pools */
1351
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001352/*
1353 * Memory pool requires a buffer and two arrays of structures for the
1354 * memory block accounting:
1355 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
1356 * status of four blocks of memory.
1357 */
1358struct k_mem_pool_quad_block {
1359 char *mem_blocks; /* pointer to the first of four memory blocks */
1360 uint32_t mem_status; /* four bits. If bit is set, memory block is
1361 allocated */
1362};
1363/*
1364 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
1365 * blocks of one size. Block sizes go from maximal to minimal. Next memory
1366 * block size is 4 times less than the previous one and thus requires 4 times
1367 * bigger array of k_mem_pool_quad_block structures to keep track of the
1368 * memory blocks.
1369 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001370
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001371/*
1372 * The array of k_mem_pool_block_set keeps the information of each array of
1373 * k_mem_pool_quad_block structures
1374 */
1375struct k_mem_pool_block_set {
1376 int block_size; /* memory block size */
1377 int nr_of_entries; /* nr of quad block structures in the array */
1378 struct k_mem_pool_quad_block *quad_block;
1379 int count;
1380};
1381
1382/* Memory pool descriptor */
1383struct k_mem_pool {
1384 int max_block_size;
1385 int min_block_size;
1386 int nr_of_maxblocks;
1387 int nr_of_block_sets;
1388 struct k_mem_pool_block_set *block_set;
1389 char *bufblock;
1390 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001391 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
1392};
1393
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001394#ifdef CONFIG_ARM
1395#define _SECTION_TYPE_SIGN "%"
1396#else
1397#define _SECTION_TYPE_SIGN "@"
1398#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001399
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001400/*
1401 * Static memory pool initialization
1402 */
1403/*
1404 * Use .altmacro to be able to recalculate values and pass them as string
1405 * arguments when calling assembler macros resursively
1406 */
1407__asm__(".altmacro\n\t");
1408
1409/*
1410 * Recursively calls a macro
1411 * The followig global symbols need to be initialized:
1412 * __memory_pool_max_block_size - maximal size of the memory block
1413 * __memory_pool_min_block_size - minimal size of the memory block
1414 * Notes:
1415 * Global symbols are used due the fact that assembler macro allows only
1416 * one argument be passed with the % conversion
1417 * Some assemblers do not get division operation ("/"). To avoid it >> 2
1418 * is used instead of / 4.
1419 * n_max argument needs to go first in the invoked macro, as some
1420 * assemblers concatenate \name and %(\n_max * 4) arguments
1421 * if \name goes first
1422 */
1423__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
1424 ".ifge __memory_pool_max_block_size >> 2 -"
1425 " __memory_pool_min_block_size\n\t\t"
1426 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
1427 "\\macro_name %(\\n_max * 4) \\name\n\t"
1428 ".endif\n\t"
1429 ".endm\n");
1430
1431/*
1432 * Build quad blocks
1433 * Macro allocates space in memory for the array of k_mem_pool_quad_block
1434 * structures and recursively calls itself for the next array, 4 times
1435 * larger.
1436 * The followig global symbols need to be initialized:
1437 * __memory_pool_max_block_size - maximal size of the memory block
1438 * __memory_pool_min_block_size - minimal size of the memory block
1439 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
1440 */
1441__asm__(".macro _build_quad_blocks n_max, name\n\t"
1442 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
1443 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
1444 ".if \\n_max % 4\n\t\t"
1445 ".skip __memory_pool_quad_block_size\n\t"
1446 ".endif\n\t"
1447 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
1448 ".endm\n");
1449
1450/*
1451 * Build block sets and initialize them
1452 * Macro initializes the k_mem_pool_block_set structure and
1453 * recursively calls itself for the next one.
1454 * The followig global symbols need to be initialized:
1455 * __memory_pool_max_block_size - maximal size of the memory block
1456 * __memory_pool_min_block_size - minimal size of the memory block
1457 * __memory_pool_block_set_count, the number of the elements in the
1458 * block set array must be set to 0. Macro calculates it's real
1459 * value.
1460 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
1461 * structures, _build_quad_blocks must be called prior it.
1462 */
1463__asm__(".macro _build_block_set n_max, name\n\t"
1464 ".int __memory_pool_max_block_size\n\t" /* block_size */
1465 ".if \\n_max % 4\n\t\t"
1466 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
1467 ".else\n\t\t"
1468 ".int \\n_max >> 2\n\t"
1469 ".endif\n\t"
1470 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
1471 ".int 0\n\t" /* count */
1472 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
1473 "__do_recurse _build_block_set \\name \\n_max\n\t"
1474 ".endm\n");
1475
1476/*
1477 * Build a memory pool structure and initialize it
1478 * Macro uses __memory_pool_block_set_count global symbol,
1479 * block set addresses and buffer address, it may be called only after
1480 * _build_block_set
1481 */
1482__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
1483 ".pushsection ._k_memory_pool,\"aw\","
1484 _SECTION_TYPE_SIGN "progbits\n\t"
1485 ".globl \\name\n\t"
1486 "\\name:\n\t"
1487 ".int \\max_size\n\t" /* max_block_size */
1488 ".int \\min_size\n\t" /* min_block_size */
1489 ".int \\n_max\n\t" /* nr_of_maxblocks */
1490 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
1491 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
1492 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
1493 ".int 0\n\t" /* wait_q->head */
1494 ".int 0\n\t" /* wait_q->next */
1495 ".popsection\n\t"
1496 ".endm\n");
1497
1498#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
1499 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1500 _SECTION_TYPE_SIGN "progbits\n\t"); \
1501 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
1502 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1503 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
1504 STRINGIFY(name) "\n\t"); \
1505 __asm__(".popsection\n\t")
1506
1507#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
1508 __asm__("__memory_pool_block_set_count = 0\n\t"); \
1509 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1510 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1511 _SECTION_TYPE_SIGN "progbits\n\t"); \
1512 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
1513 __asm__("_build_block_set " STRINGIFY(n_max) " " \
1514 STRINGIFY(name) "\n\t"); \
1515 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
1516 __asm__(".int __memory_pool_block_set_count\n\t"); \
1517 __asm__(".popsection\n\t"); \
1518 extern uint32_t _mem_pool_block_set_count_##name; \
1519 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
1520
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001521#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
1522 char __noinit __aligned(align) \
1523 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001524
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001525/**
1526 * @brief Define a memory pool
1527 *
1528 * This declares and initializes a memory pool whose buffer is aligned to
1529 * a @a align -byte boundary. The new memory pool can be passed to the
1530 * kernel's memory pool functions.
1531 *
1532 * Note that for each of the minimum sized blocks to be aligned to @a align
1533 * bytes, then @a min_size must be a multiple of @a align.
1534 *
1535 * @param name Name of the memory pool
1536 * @param min_size Minimum block size in the pool
1537 * @param max_size Maximum block size in the pool
1538 * @param n_max Number of maximum sized blocks in the pool
1539 * @param align Alignment of the memory pool's buffer
1540 */
1541#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001542 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
1543 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04001544 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001545 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
1546 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
1547 extern struct k_mem_pool name
1548
1549/*
1550 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
1551 * to __memory_pool_quad_block_size absolute symbol.
1552 * This function does not get called, but compiler calculates the value and
1553 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
1554 */
1555static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
1556{
1557 __asm__(".globl __memory_pool_quad_block_size\n\t"
1558 "__memory_pool_quad_block_size = %c0\n\t"
1559 :
1560 : "n"(sizeof(struct k_mem_pool_quad_block)));
1561}
1562
Peter Mitsis937042c2016-10-13 13:18:26 -04001563/**
1564 * @brief Allocate memory from a memory pool
1565 *
1566 * @param pool Pointer to the memory pool object
1567 * @param block Pointer to the allocated memory's block descriptor
1568 * @param size Minimum number of bytes to allocate
1569 * @param timeout Maximum time (milliseconds) to wait for operation to
1570 * complete. Use K_NO_WAIT to return immediately, or K_FOREVER
1571 * to wait as long as necessary.
1572 *
1573 * @return 0 on success, -ENOMEM on failure
1574 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001575extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis937042c2016-10-13 13:18:26 -04001576 int size, int32_t timeout);
1577
1578/**
1579 * @brief Return previously allocated memory to its memory pool
1580 *
1581 * @param block Pointer to allocated memory's block descriptor
1582 *
1583 * @return N/A
1584 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001585extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04001586
1587/**
1588 * @brief Defragment the specified memory pool
1589 *
1590 * @param pool Pointer to the memory pool object
1591 *
1592 * @return N/A
1593 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001594extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04001595
1596/**
1597 * @brief Allocate memory from heap pool
1598 *
1599 * This routine provides traditional malloc semantics; internally it uses
1600 * the memory pool APIs on a dedicated HEAP pool
1601 *
1602 * @param size Size of memory requested by the caller (in bytes)
1603 *
1604 * @return Address of the allocated memory on success; otherwise NULL
1605 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001606extern void *k_malloc(uint32_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04001607
1608/**
1609 * @brief Free memory allocated through k_malloc()
1610 *
1611 * @param ptr Pointer to previously allocated memory
1612 *
1613 * @return N/A
1614 */
1615extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001616
1617/*
1618 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
1619 * hook into the device subsystem, which itself uses nanokernel semaphores,
1620 * and thus currently requires the definition of nano_sem.
1621 */
1622#include <legacy.h>
1623#include <arch/cpu.h>
1624
1625/*
1626 * private APIs that are utilized by one or more public APIs
1627 */
1628
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001629extern int _is_thread_essential(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001630extern void _init_static_threads(void);
1631
1632#ifdef __cplusplus
1633}
1634#endif
1635
1636#endif /* _kernel__h_ */