blob: a0bce6e8ad3a6f77c59fe4f2d018a7a3ff9191fc [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
55#define K_OBJ(name, size) char name[size] __aligned(4)
56
57#if CONFIG_NUM_COOP_PRIORITIES > 0
58#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
59#else
60#define K_HIGHEST_THREAD_PRIO 0
61#endif
62
63#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
64#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
65#else
66#define K_LOWEST_THREAD_PRIO -1
67#endif
68
69#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
70#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
71
72typedef sys_dlist_t _wait_q_t;
73
74#ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS
75#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next
76#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL,
77#else
78#define _DEBUG_TRACING_KERNEL_OBJECTS_INIT
79#define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type)
80#endif
81
82#define k_thread tcs
83struct tcs;
84struct k_mutex;
85struct k_sem;
86struct k_event;
87struct k_msgq;
88struct k_mbox;
89struct k_pipe;
90struct k_fifo;
91struct k_lifo;
92struct k_stack;
93struct k_mem_map;
94struct k_mem_pool;
95struct k_timer;
96
97typedef struct tcs *k_tid_t;
98typedef struct k_mem_pool *k_mem_pool_t;
99
100/* threads/scheduler/execution contexts */
101
102enum execution_context_types {
103 K_ISR = 0,
104 K_COOP_THREAD,
105 K_PREEMPT_THREAD,
106};
107
108struct k_thread_config {
109 char *stack;
110 unsigned stack_size;
111 unsigned prio;
112};
113
114typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
115extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size,
116 void (*entry)(void *, void *, void*),
117 void *p1, void *p2, void *p3,
118 int32_t prio, uint32_t options, int32_t delay);
119
120extern void k_sleep(int32_t duration);
121extern void k_busy_wait(uint32_t usec_to_wait);
122extern void k_yield(void);
123extern void k_wakeup(k_tid_t thread);
124extern k_tid_t k_current_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400125extern int k_current_priority_get(void);
126extern int k_thread_cancel(k_tid_t thread);
127
128extern void k_thread_abort(k_tid_t thread);
129
130#define K_THREAD_GROUP_EXE 0x1
131#define K_THREAD_GROUP_SYS 0x2
132#define K_THREAD_GROUP_FPU 0x4
133
134/* XXX - doesn't work because CONFIG_ARCH is a string */
135#if 0
136/* arch-specific groups */
137#if CONFIG_ARCH == "x86"
138#define K_THREAD_GROUP_SSE 0x4
139#endif
140#endif
141
142#ifdef CONFIG_NANO_TIMEOUTS
143#define _THREAD_TIMEOUT_INIT(obj) \
144 (obj).nano_timeout = { \
145 .node = { {0}, {0} }, \
146 .tcs = NULL, \
147 .wait_q = NULL, \
148 .delta_ticks_from_prev = -1, \
149 },
150#else
151#define _THREAD_TIMEOUT_INIT(obj)
152#endif
153
154#ifdef CONFIG_ERRNO
155#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
156#else
157#define _THREAD_ERRNO_INIT(obj)
158#endif
159
160struct k_thread_static_init {
161 uint32_t init_groups;
162 int init_prio;
163 void (*init_entry)(void *, void *, void *);
164 void *init_p1;
165 void *init_p2;
166 void *init_p3;
167 void (*init_abort)(void);
168 union {
169 char *init_stack;
170 struct k_thread *thread;
171 };
172 unsigned int init_stack_size;
173};
174
175#define K_THREAD_INITIALIZER(stack, stack_size, \
176 entry, p1, p2, p3, \
177 abort, prio, groups) \
178 { \
179 .init_groups = (groups), \
180 .init_prio = (prio), \
181 .init_entry = entry, \
182 .init_p1 = (void *)p1, \
183 .init_p2 = (void *)p2, \
184 .init_p3 = (void *)p3, \
185 .init_abort = abort, \
186 .init_stack = (stack), \
187 .init_stack_size = (stack_size), \
188 }
189
190/*
191 * Define thread initializer object and initialize it
192 * NOTE: For thread group functions thread initializers must be organized
193 * in array and thus should not have gaps between them.
194 * On x86 by default compiler aligns them by 32 byte boundary. To prevent
195 * this 32-bit alignment in specified here.
196 * k_thread_static_init structure sise needs to be kept 32-bit aligned as well
197 */
198#define K_THREAD_OBJ_DEFINE(name, stack_size, \
199 entry, p1, p2, p3, \
200 abort, prio, groups) \
201 extern void entry(void *, void *, void *); \
202 char __noinit __stack _k_thread_obj_##name[stack_size]; \
203 struct k_thread_static_init _k_thread_init_##name __aligned(4) \
204 __in_section(_k_task_list, private, task) = \
205 K_THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
206 entry, p1, p2, p3, abort, prio, groups)
207
208#define K_THREAD_DEFINE(name, stack_size, entry, p1, p2, p3, \
209 abort, prio, groups) \
210 K_THREAD_OBJ_DEFINE(name, stack_size, entry, p1, p2, p3, \
211 abort, prio, groups); \
212 k_tid_t const name = (k_tid_t)_k_thread_obj_##name
213
214/* extern int k_thread_prio_get(k_tid_t thread); in sched.h */
215extern void k_thread_priority_set(k_tid_t thread, int prio);
216
217#if 0
218extern int k_thread_suspend(k_tid_t thread);
219extern int k_thread_resume(k_tid_t thread);
220extern int k_thread_entry_set(k_tid_t thread,
221 void (*entry)(void*, void*, void*);
222extern int k_thread_abort_handler_set(k_tid_t thread,
223 void (*handler)(void));
224#endif
225
226extern void k_sched_time_slice_set(int32_t slice, int prio);
227extern int k_workload_get(void);
228extern void k_workload_time_slice_set(int32_t slice);
229
230extern int k_am_in_isr(void);
231
232extern void k_thread_custom_data_set(void *value);
233extern void *k_thread_custom_data_get(void);
234
235/**
236 * kernel timing
237 */
238
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400239#include <sys_clock.h>
240
241/* private internal time manipulation (users should never play with ticks) */
242
243static int64_t __ticks_to_ms(int64_t ticks)
244{
245 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
246}
247
248
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400249/* timeouts */
250
251struct _timeout;
252typedef void (*_timeout_func_t)(struct _timeout *t);
253
254struct _timeout {
255 sys_dlist_t node;
256 struct tcs *tcs;
257 sys_dlist_t *wait_q;
258 int32_t delta_ticks_from_prev;
259 _timeout_func_t func;
260};
261
262/* timers */
263
264struct k_timer {
265 /*
266 * _timeout structure must be first here if we want to use
267 * dynamic timer allocation. timeout.node is used in the double-linked
268 * list of free timers
269 */
270 struct _timeout timeout;
271
272 /* wait queue for the threads waiting on this timer */
273 _wait_q_t wait_q;
274
275 /* runs in ISR context */
276 void (*handler)(void *);
277 void *handler_arg;
278
279 /* runs in the context of the thread that calls k_timer_stop() */
280 void (*stop_handler)(void *);
281 void *stop_handler_arg;
282
283 /* timer period */
284 int32_t period;
285
286 /* user supplied data pointer returned to the thread*/
287 void *user_data;
288
289 /* user supplied data pointer */
290 void *user_data_internal;
291
292 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
293};
294
295#define K_TIMER_INITIALIZER(obj) \
296 { \
297 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
298 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
299 }
300
301#define K_TIMER_DEFINE(name) \
302 struct k_timer name = K_TIMER_INITIALIZER(name)
303
304extern void k_timer_init(struct k_timer *timer, void *data);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700305
306#if (CONFIG_NUM_DYNAMIC_TIMERS > 0)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400307extern struct k_timer *k_timer_alloc(void);
308extern void k_timer_free(struct k_timer *timer);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700309#endif
310
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400311extern void k_timer_start(struct k_timer *timer,
312 int32_t duration, int32_t period,
313 void (*handler)(void *), void *handler_arg,
314 void (*stop_handler)(void *), void *stop_handler_arg);
315extern void k_timer_restart(struct k_timer *timer, int32_t duration,
316 int32_t period);
317extern void k_timer_stop(struct k_timer *timer);
318extern int k_timer_test(struct k_timer *timer, void **data, int wait);
319extern int32_t k_timer_remaining_get(struct k_timer *timer);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400320
321
322/**
323 * @brief Get the time elapsed since the system booted (uptime)
324 *
325 * @return The current uptime of the system in ms
326 */
327
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400328extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400329
330/**
331 * @brief Get the lower 32-bit of time elapsed since the system booted (uptime)
332 *
333 * This function is potentially less onerous in both the time it takes to
334 * execute, the interrupt latency it introduces and the amount of 64-bit math
335 * it requires than k_uptime_get(), but it only provides an uptime value of
336 * 32-bits. The user must handle possible rollovers/spillovers.
337 *
338 * At a rate of increment of 1000 per second, it rolls over approximately every
339 * 50 days.
340 *
341 * @return The current uptime of the system in ms
342 */
343
344extern uint32_t k_uptime_get_32(void);
345
346/**
347 * @brief Get the difference between a reference time and the current uptime
348 *
349 * @param reftime A pointer to a reference time. It is updated with the current
350 * uptime upon return.
351 *
352 * @return The delta between the reference time and the current uptime.
353 */
354
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400355extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400356
357/**
358 * @brief Get the difference between a reference time and the current uptime
359 *
360 * The 32-bit version of k_uptime_delta(). It has the same perks and issues as
361 * k_uptime_get_32().
362 *
363 * @param reftime A pointer to a reference time. It is updated with the current
364 * uptime upon return.
365 *
366 * @return The delta between the reference time and the current uptime.
367 */
368
369extern uint32_t k_uptime_delta_32(int64_t *reftime);
370
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400371extern bool k_timer_pool_is_empty(void);
372
373extern uint32_t k_cycle_get_32(void);
374
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400375/**
376 * data transfers (basic)
377 */
378
379/* fifos */
380
381struct k_fifo {
382 _wait_q_t wait_q;
383 sys_slist_t data_q;
384
385 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
386};
387
388extern void k_fifo_init(struct k_fifo *fifo);
389extern void k_fifo_put(struct k_fifo *fifo, void *data);
390extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
391extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
392extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
393
394#define K_FIFO_INITIALIZER(obj) \
395 { \
396 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh9091e5d2016-09-30 10:42:47 -0400397 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400398 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
399 }
400
401#define K_FIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400402 struct k_fifo name = K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400403
404/* lifos */
405
406struct k_lifo {
407 _wait_q_t wait_q;
408 void *list;
409
410 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
411};
412
413extern void k_lifo_init(struct k_lifo *lifo);
414extern void k_lifo_put(struct k_lifo *lifo, void *data);
415extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
416
417#define K_LIFO_INITIALIZER(obj) \
418 { \
419 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
420 .list = NULL, \
421 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
422 }
423
424#define K_LIFO_DEFINE(name) \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400425 struct k_lifo name = K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400426
427/* stacks */
428
429struct k_stack {
430 _wait_q_t wait_q;
431 uint32_t *base, *next, *top;
432
433 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
434};
435
436extern void k_stack_init(struct k_stack *stack, int num_entries);
437extern void k_stack_init_with_buffer(struct k_stack *stack, int num_entries,
438 uint32_t *buffer);
439extern void k_stack_push(struct k_stack *stack, uint32_t data);
440extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
441
442#define K_STACK_INITIALIZER(obj, stack_num_entries, stack_buffer) \
443 { \
444 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
445 .base = stack_buffer, \
446 .next = stack_buffer, \
447 .top = stack_buffer + stack_num_entries, \
448 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
449 }
450
451#define K_STACK_DEFINE(name, stack_num_entries) \
452 uint32_t __noinit _k_stack_buf_##name[stack_num_entries]; \
Benjamin Walsh0bee91d2016-09-15 17:16:38 -0400453 struct k_stack name = \
454 K_STACK_INITIALIZER(name, stack_num_entries, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400455 _k_stack_buf_##name); \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400456
457#define K_STACK_SIZE(stack_num_entries) \
458 (sizeof(struct k_stack) + (stack_num_entries * sizeof(uint32_t)))
459
460/**
461 * workqueues
462 */
463
464struct k_work;
465
466typedef void (*k_work_handler_t)(struct k_work *);
467
468/**
469 * A workqueue is a fiber that executes @ref k_work items that are
470 * queued to it. This is useful for drivers which need to schedule
471 * execution of code which might sleep from ISR context. The actual
472 * fiber identifier is not stored in the structure in order to save
473 * space.
474 */
475struct k_work_q {
476 struct k_fifo fifo;
477};
478
479/**
480 * @brief Work flags.
481 */
482enum {
483 K_WORK_STATE_IDLE, /* Work item idle state */
484};
485
486/**
487 * @brief An item which can be scheduled on a @ref k_work_q.
488 */
489struct k_work {
490 void *_reserved; /* Used by k_fifo implementation. */
491 k_work_handler_t handler;
492 atomic_t flags[1];
493};
494
495/**
496 * @brief Statically initialize work item
497 */
498#define K_WORK_INITIALIZER(work_handler) \
499 { \
500 ._reserved = NULL, \
501 .handler = work_handler, \
502 .flags = { 1 } \
503 }
504
505/**
506 * @brief Dynamically initialize work item
507 */
508static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
509{
510 atomic_set_bit(work->flags, K_WORK_STATE_IDLE);
511 work->handler = handler;
512}
513
514/**
515 * @brief Submit a work item to a workqueue.
516 */
517static inline void k_work_submit_to_queue(struct k_work_q *work_q,
518 struct k_work *work)
519{
520 if (!atomic_test_and_clear_bit(work->flags, K_WORK_STATE_IDLE)) {
521 __ASSERT_NO_MSG(0);
522 } else {
523 k_fifo_put(&work_q->fifo, work);
524 }
525}
526
527/**
528 * @brief Start a new workqueue. This routine can be called from either
529 * fiber or task context.
530 */
531extern void k_work_q_start(struct k_work_q *work_q,
532 const struct k_thread_config *config);
533
534#if defined(CONFIG_NANO_TIMEOUTS)
535
536 /*
537 * @brief An item which can be scheduled on a @ref k_work_q with a
538 * delay.
539 */
540struct k_delayed_work {
541 struct k_work work;
542 struct _timeout timeout;
543 struct k_work_q *work_q;
544};
545
546/**
547 * @brief Initialize delayed work
548 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400549extern void k_delayed_work_init(struct k_delayed_work *work,
550 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400551
552/**
553 * @brief Submit a delayed work item to a workqueue.
554 *
555 * This procedure schedules a work item to be processed after a delay.
556 * Once the delay has passed, the work item is submitted to the work queue:
557 * at this point, it is no longer possible to cancel it. Once the work item's
558 * handler is about to be executed, the work is considered complete and can be
559 * resubmitted.
560 *
561 * Care must be taken if the handler blocks or yield as there is no implicit
562 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
563 * it should be explicitly done between the submitter and the handler.
564 *
565 * @param work_q to schedule the work item
566 * @param work Delayed work item
567 * @param ticks Ticks to wait before scheduling the work item
568 *
569 * @return 0 in case of success or negative value in case of error.
570 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400571extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
572 struct k_delayed_work *work,
573 int32_t ticks);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400574
575/**
576 * @brief Cancel a delayed work item
577 *
578 * This procedure cancels a scheduled work item. If the work has been completed
579 * or is idle, this will do nothing. The only case where this can fail is when
580 * the work has been submitted to the work queue, but the handler has not run
581 * yet.
582 *
583 * @param work Delayed work item to be canceled
584 *
585 * @return 0 in case of success or negative value in case of error.
586 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400587extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400588
589#endif /* CONFIG_NANO_TIMEOUTS */
590
591#if defined(CONFIG_SYSTEM_WORKQUEUE)
592
593extern struct k_work_q k_sys_work_q;
594
595/*
596 * @brief Submit a work item to the system workqueue.
597 *
598 * @ref k_work_submit_to_queue
599 *
600 * When using the system workqueue it is not recommended to block or yield
601 * on the handler since its fiber is shared system wide it may cause
602 * unexpected behavior.
603 */
604static inline void k_work_submit(struct k_work *work)
605{
606 k_work_submit_to_queue(&k_sys_work_q, work);
607}
608
609#if defined(CONFIG_NANO_TIMEOUTS)
610/*
611 * @brief Submit a delayed work item to the system workqueue.
612 *
613 * @ref k_delayed_work_submit_to_queue
614 *
615 * When using the system workqueue it is not recommended to block or yield
616 * on the handler since its fiber is shared system wide it may cause
617 * unexpected behavior.
618 */
619static inline int k_delayed_work_submit(struct k_delayed_work *work,
620 int ticks)
621{
622 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, ticks);
623}
624
625#endif /* CONFIG_NANO_TIMEOUTS */
626#endif /* CONFIG_SYSTEM_WORKQUEUE */
627
628/**
629 * synchronization
630 */
631
632/* mutexes */
633
634struct k_mutex {
635 _wait_q_t wait_q;
636 struct tcs *owner;
637 uint32_t lock_count;
638 int owner_orig_prio;
639#ifdef CONFIG_OBJECT_MONITOR
640 int num_lock_state_changes;
641 int num_conflicts;
642#endif
643
644 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
645};
646
647#ifdef CONFIG_OBJECT_MONITOR
648#define _MUTEX_INIT_OBJECT_MONITOR \
649 .num_lock_state_changes = 0, .num_conflicts = 0,
650#else
651#define _MUTEX_INIT_OBJECT_MONITOR
652#endif
653
654#define K_MUTEX_INITIALIZER(obj) \
655 { \
656 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
657 .owner = NULL, \
658 .lock_count = 0, \
659 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
660 _MUTEX_INIT_OBJECT_MONITOR \
661 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
662 }
663
664#define K_MUTEX_DEFINE(name) \
665 struct k_mutex name = K_MUTEX_INITIALIZER(name)
666
667extern void k_mutex_init(struct k_mutex *mutex);
668extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
669extern void k_mutex_unlock(struct k_mutex *mutex);
670
671/* semaphores */
672
673struct k_sem {
674 _wait_q_t wait_q;
675 unsigned int count;
676 unsigned int limit;
677
678 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
679};
680
681extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
682 unsigned int limit);
683extern int k_sem_take(struct k_sem *sem, int32_t timeout);
684extern void k_sem_give(struct k_sem *sem);
685
Benjamin Walsh70c68b92016-09-21 10:37:34 -0400686static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400687{
688 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400689}
690
Tomasz Bursztyka276086d2016-09-21 16:03:21 +0200691static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400692{
693 return sem->count;
694}
695
Peter Mitsis45403672016-09-09 14:24:06 -0400696#ifdef CONFIG_SEMAPHORE_GROUPS
697/**
698 * @brief Take the first available semaphore
699 *
700 * Given a list of semaphore pointers, this routine will attempt to take one
701 * of them, waiting up to a maximum of @a timeout ms to do so. The taken
702 * semaphore is identified by @a sem (set to NULL on error).
703 *
704 * Be aware that the more semaphores specified in the group, the more stack
705 * space is required by the waiting thread.
706 *
707 * @param sem_array Array of semaphore pointers terminated by a K_END entry
708 * @param sem Identifies the semaphore that was taken
709 * @param timeout Maximum number of milliseconds to wait
710 *
711 * @retval 0 A semaphore was successfully taken
712 * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT)
713 * @retval -EAGAIN Time out occurred while waiting for semaphore
714 */
715
716extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem,
717 int32_t timeout);
718
719/**
720 * @brief Give all the semaphores in the group
721 *
722 * This routine will give each semaphore in the array of semaphore pointers.
723 *
724 * @param sem_array Array of semaphore pointers terminated by a K_END entry
725 *
726 * @return N/A
727 */
728extern void k_sem_group_give(struct k_sem *sem_array[]);
729
730/**
731 * @brief Reset the count to zero on each semaphore in the array
732 *
733 * This routine resets the count of each semaphore in the group to zero.
734 * Note that it does NOT have any impact on any thread that might have
735 * been previously pending on any of the semaphores.
736 *
737 * @param sem_array Array of semaphore pointers terminated by a K_END entry
738 *
739 * @return N/A
740 */
741extern void k_sem_group_reset(struct k_sem *sem_array[]);
742#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400743
744#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
745 { \
746 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
747 .count = initial_count, \
748 .limit = count_limit, \
749 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
750 }
751
752#define K_SEM_DEFINE(name, initial_count, count_limit) \
753 struct k_sem name = \
754 K_SEM_INITIALIZER(name, initial_count, count_limit)
755
756/* events */
757
758#define K_EVT_DEFAULT NULL
759#define K_EVT_IGNORE ((void *)(-1))
760
761typedef int (*k_event_handler_t)(struct k_event *);
762
763struct k_event {
764 k_event_handler_t handler;
765 atomic_t send_count;
766 struct k_work work_item;
767 struct k_sem sem;
768
769 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_event);
770};
771
772extern void _k_event_deliver(struct k_work *work);
773
774#define K_EVENT_INITIALIZER(obj, event_handler) \
775 { \
776 .handler = (k_event_handler_t)event_handler, \
777 .send_count = ATOMIC_INIT(0), \
778 .work_item = K_WORK_INITIALIZER(_k_event_deliver), \
779 .sem = K_SEM_INITIALIZER(obj.sem, 0, 1), \
780 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
781 }
782
783#define K_EVENT_DEFINE(name, event_handler) \
784 struct k_event name \
785 __in_section(_k_event_list, event, name) = \
786 K_EVENT_INITIALIZER(name, event_handler)
787
788extern void k_event_init(struct k_event *event, k_event_handler_t handler);
789extern int k_event_recv(struct k_event *event, int32_t timeout);
790extern void k_event_send(struct k_event *event);
791
792/**
793 * data transfers (complex)
794 */
795
796/* message queues */
797
798struct k_msgq {
799 _wait_q_t wait_q;
800 uint32_t msg_size;
801 uint32_t max_msgs;
802 char *buffer_start;
803 char *buffer_end;
804 char *read_ptr;
805 char *write_ptr;
806 uint32_t used_msgs;
807
808 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
809};
810
811#define K_MSGQ_INITIALIZER(obj, q_depth, q_width, q_buffer) \
812 { \
813 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
814 .max_msgs = q_depth, \
815 .msg_size = q_width, \
816 .buffer_start = q_buffer, \
817 .buffer_end = q_buffer + (q_depth * q_width), \
818 .read_ptr = q_buffer, \
819 .write_ptr = q_buffer, \
820 .used_msgs = 0, \
821 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
822 }
823
824#define K_MSGQ_DEFINE(name, q_depth, q_width) \
825 static char __noinit _k_fifo_buf_##name[(q_depth) * (q_width)]; \
826 struct k_msgq name = \
827 K_MSGQ_INITIALIZER(name, q_depth, q_width, _k_fifo_buf_##name)
828
829#define K_MSGQ_SIZE(q_depth, q_width) \
830 ((sizeof(struct k_msgq)) + ((q_width) * (q_depth)))
831
Benjamin Walsh72e5a392016-09-30 11:32:33 -0400832extern void k_msgq_init(struct k_msgq *q, uint32_t msg_size,
833 uint32_t max_msgs, char *buffer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400834extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
835extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
836extern void k_msgq_purge(struct k_msgq *q);
837
838static inline int k_msgq_num_used_get(struct k_msgq *q)
839{
840 return q->used_msgs;
841}
842
843struct k_mem_block {
844 k_mem_pool_t pool_id;
845 void *addr_in_pool;
846 void *data;
847 uint32_t req_size;
848};
849
850/* mailboxes */
851
852struct k_mbox_msg {
853 /** internal use only - needed for legacy API support */
854 uint32_t _mailbox;
855 /** size of message (in bytes) */
856 uint32_t size;
857 /** application-defined information value */
858 uint32_t info;
859 /** sender's message data buffer */
860 void *tx_data;
861 /** internal use only - needed for legacy API support */
862 void *_rx_data;
863 /** message data block descriptor */
864 struct k_mem_block tx_block;
865 /** source thread id */
866 k_tid_t rx_source_thread;
867 /** target thread id */
868 k_tid_t tx_target_thread;
869 /** internal use only - thread waiting on send (may be a dummy) */
870 k_tid_t _syncing_thread;
871#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
872 /** internal use only - semaphore used during asynchronous send */
873 struct k_sem *_async_sem;
874#endif
875};
876
877struct k_mbox {
878 _wait_q_t tx_msg_queue;
879 _wait_q_t rx_msg_queue;
880
881 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
882};
883
884#define K_MBOX_INITIALIZER(obj) \
885 { \
886 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
887 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
888 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
889 }
890
891#define K_MBOX_DEFINE(name) \
892 struct k_mbox name = \
893 K_MBOX_INITIALIZER(name) \
894
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400895extern void k_mbox_init(struct k_mbox *mbox);
896
897extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *msg,
898 int32_t timeout);
899extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *msg,
900 struct k_sem *sem);
901
902extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *msg,
903 void *buffer, int32_t timeout);
904extern void k_mbox_data_get(struct k_mbox_msg *msg, void *buffer);
905extern int k_mbox_data_block_get(struct k_mbox_msg *msg, k_mem_pool_t pool,
906 struct k_mem_block *block, int32_t timeout);
907
908/* pipes */
909
910struct k_pipe {
911 unsigned char *buffer; /* Pipe buffer: may be NULL */
912 size_t size; /* Buffer size */
913 size_t bytes_used; /* # bytes used in buffer */
914 size_t read_index; /* Where in buffer to read from */
915 size_t write_index; /* Where in buffer to write */
916
917 struct {
918 _wait_q_t readers; /* Reader wait queue */
919 _wait_q_t writers; /* Writer wait queue */
920 } wait_q;
921
922 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
923};
924
925#define K_PIPE_INITIALIZER(obj, pipe_buffer_size, pipe_buffer) \
926 { \
927 .buffer = pipe_buffer, \
928 .size = pipe_buffer_size, \
929 .bytes_used = 0, \
930 .read_index = 0, \
931 .write_index = 0, \
932 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
933 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
934 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
935 }
936
937#define K_PIPE_DEFINE(name, pipe_buffer_size) \
938 static unsigned char __noinit _k_pipe_buf_##name[pipe_buffer_size]; \
939 struct k_pipe name = \
940 K_PIPE_INITIALIZER(name, pipe_buffer_size, _k_pipe_buf_##name)
941
942#define K_PIPE_SIZE(buffer_size) (sizeof(struct k_pipe) + buffer_size)
943
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400944/**
945 * @brief Runtime initialization of a pipe
946 *
947 * @param pipe Pointer to pipe to initialize
948 * @param buffer Pointer to buffer to use for pipe's ring buffer
949 * @param size Size of the pipe's ring buffer
950 *
951 * @return N/A
952 */
953extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
954 size_t size);
955
956/**
957 * @brief Put a message into the specified pipe
958 *
959 * This routine synchronously adds a message into the pipe specified by
960 * @a pipe. It will wait up to @a timeout for the pipe to accept
961 * @a num_bytes_to_write bytes of data. If by @a timeout, the pipe could not
962 * accept @a min_bytes bytes of data, it fails. Fewer than @a min_bytes will
963 * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
964 *
965 * @param pipe Pointer to the pipe
966 * @param buffer Data to put into the pipe
967 * @param num_bytes_to_write Desired number of bytes to put into the pipe
968 * @param num_bytes_written Number of bytes the pipe accepted
969 * @param min_bytes Minimum number of bytes accepted for success
970 * @param timeout Maximum number of milliseconds to wait
971 *
972 * @retval 0 At least @a min_bytes were sent
973 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
974 * @retval -EAGAIN Fewer than @a min_bytes were sent
975 */
976extern int k_pipe_put(struct k_pipe *pipe, void *buffer,
977 size_t num_bytes_to_write, size_t *num_bytes_written,
978 size_t min_bytes, int32_t timeout);
979
980/**
981 * @brief Get a message from the specified pipe
982 *
983 * This routine synchronously retrieves a message from the pipe specified by
984 * @a pipe. It will wait up to @a timeout to retrieve @a num_bytes_to_read
985 * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve
986 * @a min_bytes bytes of data, it fails. Fewer than @a min_bytes will
987 * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
988 *
989 * @param pipe Pointer to the pipe
990 * @param buffer Location to place retrieved data
991 * @param num_bytes_to_read Desired number of bytes to retrieve from the pipe
992 * @param num_bytes_read Number of bytes retrieved from the pipe
993 * @param min_bytes Minimum number of bytes retrieved for success
994 * @param timeout Maximum number of milliseconds to wait
995 *
996 * @retval 0 At least @a min_bytes were transferred
997 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
998 * @retval -EAGAIN Fewer than @a min_bytes were retrieved
999 */
1000extern int k_pipe_get(struct k_pipe *pipe, void *buffer,
1001 size_t num_bytes_to_read, size_t *num_bytes_read,
1002 size_t min_bytes, int32_t timeout);
1003
1004/**
1005 * @brief Send a message to the specified pipe
1006 *
1007 * This routine asynchronously sends a message from the pipe specified by
1008 * @a pipe. Once all @a size bytes have been accepted by the pipe, it will
1009 * free the memory block @a block and give the semaphore @a sem (if specified).
1010 * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight
1011 * at any given time.
1012 *
1013 * @param pipe Pointer to the pipe
1014 * @param block Memory block containing data to send
1015 * @param size Number of data bytes in memory block to send
1016 * @param sem Semaphore to signal upon completion (else NULL)
1017 *
1018 * @retval N/A
1019 */
1020extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
1021 size_t size, struct k_sem *sem);
1022
1023/**
1024 * memory management
1025 */
1026
1027/* memory maps */
1028
1029struct k_mem_map {
1030 _wait_q_t wait_q;
1031 int num_blocks;
1032 int block_size;
1033 char *buffer;
1034 char *free_list;
1035 int num_used;
1036
1037 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_map);
1038};
1039
1040#define K_MEM_MAP_INITIALIZER(obj, map_num_blocks, map_block_size, \
1041 map_buffer) \
1042 { \
1043 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1044 .num_blocks = map_num_blocks, \
1045 .block_size = map_block_size, \
1046 .buffer = map_buffer, \
1047 .free_list = NULL, \
1048 .num_used = 0, \
1049 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
1050 }
1051
1052#define K_MEM_MAP_DEFINE(name, map_num_blocks, map_block_size) \
1053 char _k_mem_map_buf_##name[(map_num_blocks) * (map_block_size)]; \
1054 struct k_mem_map name \
1055 __in_section(_k_mem_map_ptr, private, mem_map) = \
1056 K_MEM_MAP_INITIALIZER(name, map_num_blocks, \
1057 map_block_size, _k_mem_map_buf_##name)
1058
1059#define K_MEM_MAP_SIZE(map_num_blocks, map_block_size) \
1060 (sizeof(struct k_mem_map) + ((map_num_blocks) * (map_block_size)))
1061
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001062extern void k_mem_map_init(struct k_mem_map *map, int num_blocks,
1063 int block_size, void *buffer);
1064extern int k_mem_map_alloc(struct k_mem_map *map, void **mem, int32_t timeout);
1065extern void k_mem_map_free(struct k_mem_map *map, void **mem);
1066
1067static inline int k_mem_map_num_used_get(struct k_mem_map *map)
1068{
1069 return map->num_used;
1070}
1071
1072/* memory pools */
1073
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001074/*
1075 * Memory pool requires a buffer and two arrays of structures for the
1076 * memory block accounting:
1077 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
1078 * status of four blocks of memory.
1079 */
1080struct k_mem_pool_quad_block {
1081 char *mem_blocks; /* pointer to the first of four memory blocks */
1082 uint32_t mem_status; /* four bits. If bit is set, memory block is
1083 allocated */
1084};
1085/*
1086 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
1087 * blocks of one size. Block sizes go from maximal to minimal. Next memory
1088 * block size is 4 times less than the previous one and thus requires 4 times
1089 * bigger array of k_mem_pool_quad_block structures to keep track of the
1090 * memory blocks.
1091 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001092
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001093/*
1094 * The array of k_mem_pool_block_set keeps the information of each array of
1095 * k_mem_pool_quad_block structures
1096 */
1097struct k_mem_pool_block_set {
1098 int block_size; /* memory block size */
1099 int nr_of_entries; /* nr of quad block structures in the array */
1100 struct k_mem_pool_quad_block *quad_block;
1101 int count;
1102};
1103
1104/* Memory pool descriptor */
1105struct k_mem_pool {
1106 int max_block_size;
1107 int min_block_size;
1108 int nr_of_maxblocks;
1109 int nr_of_block_sets;
1110 struct k_mem_pool_block_set *block_set;
1111 char *bufblock;
1112 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001113 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
1114};
1115
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001116#ifdef CONFIG_ARM
1117#define _SECTION_TYPE_SIGN "%"
1118#else
1119#define _SECTION_TYPE_SIGN "@"
1120#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001121
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001122/*
1123 * Static memory pool initialization
1124 */
1125/*
1126 * Use .altmacro to be able to recalculate values and pass them as string
1127 * arguments when calling assembler macros resursively
1128 */
1129__asm__(".altmacro\n\t");
1130
1131/*
1132 * Recursively calls a macro
1133 * The followig global symbols need to be initialized:
1134 * __memory_pool_max_block_size - maximal size of the memory block
1135 * __memory_pool_min_block_size - minimal size of the memory block
1136 * Notes:
1137 * Global symbols are used due the fact that assembler macro allows only
1138 * one argument be passed with the % conversion
1139 * Some assemblers do not get division operation ("/"). To avoid it >> 2
1140 * is used instead of / 4.
1141 * n_max argument needs to go first in the invoked macro, as some
1142 * assemblers concatenate \name and %(\n_max * 4) arguments
1143 * if \name goes first
1144 */
1145__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
1146 ".ifge __memory_pool_max_block_size >> 2 -"
1147 " __memory_pool_min_block_size\n\t\t"
1148 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
1149 "\\macro_name %(\\n_max * 4) \\name\n\t"
1150 ".endif\n\t"
1151 ".endm\n");
1152
1153/*
1154 * Build quad blocks
1155 * Macro allocates space in memory for the array of k_mem_pool_quad_block
1156 * structures and recursively calls itself for the next array, 4 times
1157 * larger.
1158 * The followig global symbols need to be initialized:
1159 * __memory_pool_max_block_size - maximal size of the memory block
1160 * __memory_pool_min_block_size - minimal size of the memory block
1161 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
1162 */
1163__asm__(".macro _build_quad_blocks n_max, name\n\t"
1164 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
1165 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
1166 ".if \\n_max % 4\n\t\t"
1167 ".skip __memory_pool_quad_block_size\n\t"
1168 ".endif\n\t"
1169 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
1170 ".endm\n");
1171
1172/*
1173 * Build block sets and initialize them
1174 * Macro initializes the k_mem_pool_block_set structure and
1175 * recursively calls itself for the next one.
1176 * The followig global symbols need to be initialized:
1177 * __memory_pool_max_block_size - maximal size of the memory block
1178 * __memory_pool_min_block_size - minimal size of the memory block
1179 * __memory_pool_block_set_count, the number of the elements in the
1180 * block set array must be set to 0. Macro calculates it's real
1181 * value.
1182 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
1183 * structures, _build_quad_blocks must be called prior it.
1184 */
1185__asm__(".macro _build_block_set n_max, name\n\t"
1186 ".int __memory_pool_max_block_size\n\t" /* block_size */
1187 ".if \\n_max % 4\n\t\t"
1188 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
1189 ".else\n\t\t"
1190 ".int \\n_max >> 2\n\t"
1191 ".endif\n\t"
1192 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
1193 ".int 0\n\t" /* count */
1194 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
1195 "__do_recurse _build_block_set \\name \\n_max\n\t"
1196 ".endm\n");
1197
1198/*
1199 * Build a memory pool structure and initialize it
1200 * Macro uses __memory_pool_block_set_count global symbol,
1201 * block set addresses and buffer address, it may be called only after
1202 * _build_block_set
1203 */
1204__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
1205 ".pushsection ._k_memory_pool,\"aw\","
1206 _SECTION_TYPE_SIGN "progbits\n\t"
1207 ".globl \\name\n\t"
1208 "\\name:\n\t"
1209 ".int \\max_size\n\t" /* max_block_size */
1210 ".int \\min_size\n\t" /* min_block_size */
1211 ".int \\n_max\n\t" /* nr_of_maxblocks */
1212 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
1213 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
1214 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
1215 ".int 0\n\t" /* wait_q->head */
1216 ".int 0\n\t" /* wait_q->next */
1217 ".popsection\n\t"
1218 ".endm\n");
1219
1220#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
1221 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1222 _SECTION_TYPE_SIGN "progbits\n\t"); \
1223 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
1224 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1225 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
1226 STRINGIFY(name) "\n\t"); \
1227 __asm__(".popsection\n\t")
1228
1229#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
1230 __asm__("__memory_pool_block_set_count = 0\n\t"); \
1231 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
1232 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
1233 _SECTION_TYPE_SIGN "progbits\n\t"); \
1234 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
1235 __asm__("_build_block_set " STRINGIFY(n_max) " " \
1236 STRINGIFY(name) "\n\t"); \
1237 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
1238 __asm__(".int __memory_pool_block_set_count\n\t"); \
1239 __asm__(".popsection\n\t"); \
1240 extern uint32_t _mem_pool_block_set_count_##name; \
1241 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
1242
1243#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max) \
1244 char __noinit _mem_pool_buffer_##name[(max_size) * (n_max)]
1245
1246#define K_MEMORY_POOL_DEFINE(name, min_size, max_size, n_max) \
1247 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
1248 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
1249 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max); \
1250 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
1251 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
1252 extern struct k_mem_pool name
1253
1254/*
1255 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
1256 * to __memory_pool_quad_block_size absolute symbol.
1257 * This function does not get called, but compiler calculates the value and
1258 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
1259 */
1260static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
1261{
1262 __asm__(".globl __memory_pool_quad_block_size\n\t"
1263 "__memory_pool_quad_block_size = %c0\n\t"
1264 :
1265 : "n"(sizeof(struct k_mem_pool_quad_block)));
1266}
1267
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001268#define K_MEM_POOL_SIZE(max_block_size, num_max_blocks) \
1269 (sizeof(struct k_mem_pool) + ((max_block_size) * (num_max_blocks)))
1270
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001271extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001272 int size, int32_t timeout);
1273extern void k_mem_pool_free(struct k_mem_block *block);
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04001274extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001275extern void *k_malloc(uint32_t size);
1276extern void k_free(void *p);
1277
1278/*
1279 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
1280 * hook into the device subsystem, which itself uses nanokernel semaphores,
1281 * and thus currently requires the definition of nano_sem.
1282 */
1283#include <legacy.h>
1284#include <arch/cpu.h>
1285
1286/*
1287 * private APIs that are utilized by one or more public APIs
1288 */
1289
1290extern struct k_thread_static_init _k_task_list_start[];
1291extern struct k_thread_static_init _k_task_list_end[];
1292
1293#define _FOREACH_STATIC_THREAD(thread_init) \
1294 for (struct k_thread_static_init *thread_init = _k_task_list_start; \
1295 thread_init < _k_task_list_end; thread_init++)
1296
1297extern int _is_thread_essential(void);
1298static inline int is_in_any_group(struct k_thread_static_init *thread_init,
1299 uint32_t groups)
1300{
1301 return !!(thread_init->init_groups & groups);
1302}
1303extern void _init_static_threads(void);
1304
1305#ifdef __cplusplus
1306}
1307#endif
1308
1309#endif /* _kernel__h_ */