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