blob: 74dcd2f0324802bd1c079706fc36c52f5f6663fd [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
240/* timeouts */
241
242struct _timeout;
243typedef void (*_timeout_func_t)(struct _timeout *t);
244
245struct _timeout {
246 sys_dlist_t node;
247 struct tcs *tcs;
248 sys_dlist_t *wait_q;
249 int32_t delta_ticks_from_prev;
250 _timeout_func_t func;
251};
252
253/* timers */
254
255struct k_timer {
256 /*
257 * _timeout structure must be first here if we want to use
258 * dynamic timer allocation. timeout.node is used in the double-linked
259 * list of free timers
260 */
261 struct _timeout timeout;
262
263 /* wait queue for the threads waiting on this timer */
264 _wait_q_t wait_q;
265
266 /* runs in ISR context */
267 void (*handler)(void *);
268 void *handler_arg;
269
270 /* runs in the context of the thread that calls k_timer_stop() */
271 void (*stop_handler)(void *);
272 void *stop_handler_arg;
273
274 /* timer period */
275 int32_t period;
276
277 /* user supplied data pointer returned to the thread*/
278 void *user_data;
279
280 /* user supplied data pointer */
281 void *user_data_internal;
282
283 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer);
284};
285
286#define K_TIMER_INITIALIZER(obj) \
287 { \
288 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
289 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
290 }
291
292#define K_TIMER_DEFINE(name) \
293 struct k_timer name = K_TIMER_INITIALIZER(name)
294
295extern void k_timer_init(struct k_timer *timer, void *data);
296extern struct k_timer *k_timer_alloc(void);
297extern void k_timer_free(struct k_timer *timer);
298extern void k_timer_start(struct k_timer *timer,
299 int32_t duration, int32_t period,
300 void (*handler)(void *), void *handler_arg,
301 void (*stop_handler)(void *), void *stop_handler_arg);
302extern void k_timer_restart(struct k_timer *timer, int32_t duration,
303 int32_t period);
304extern void k_timer_stop(struct k_timer *timer);
305extern int k_timer_test(struct k_timer *timer, void **data, int wait);
306extern int32_t k_timer_remaining_get(struct k_timer *timer);
307extern int64_t k_uptime_get(void);
308extern int64_t k_uptime_delta(int64_t *reftime);
309extern bool k_timer_pool_is_empty(void);
310
311extern uint32_t k_cycle_get_32(void);
312
313#if (CONFIG_NUM_DYNAMIC_TIMERS > 0)
314extern void _k_dyamic_timer_init(void);
315#else
316#define _k_dyamic_timer_init()
317#endif
318
319/**
320 * data transfers (basic)
321 */
322
323/* fifos */
324
325struct k_fifo {
326 _wait_q_t wait_q;
327 sys_slist_t data_q;
328
329 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo);
330};
331
332extern void k_fifo_init(struct k_fifo *fifo);
333extern void k_fifo_put(struct k_fifo *fifo, void *data);
334extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
335extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
336extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
337
338#define K_FIFO_INITIALIZER(obj) \
339 { \
340 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
341 .data_q = SYS_DLIST_STATIC_INIT(&obj.data_q), \
342 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
343 }
344
345#define K_FIFO_DEFINE(name) \
346 struct k_fifo _k_fifo_obj_##name = \
347 K_FIFO_INITIALIZER(_k_fifo_obj_##name); \
348 struct k_fifo * const name = &_k_fifo_obj_##name
349
350/* lifos */
351
352struct k_lifo {
353 _wait_q_t wait_q;
354 void *list;
355
356 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo);
357};
358
359extern void k_lifo_init(struct k_lifo *lifo);
360extern void k_lifo_put(struct k_lifo *lifo, void *data);
361extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
362
363#define K_LIFO_INITIALIZER(obj) \
364 { \
365 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
366 .list = NULL, \
367 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
368 }
369
370#define K_LIFO_DEFINE(name) \
371 struct k_lifo _k_lifo_obj_##name = \
372 K_LIFO_INITIALIZER(_k_lifo_obj_##name); \
373 struct k_lifo * const name = &_k_lifo_obj_##name
374
375/* stacks */
376
377struct k_stack {
378 _wait_q_t wait_q;
379 uint32_t *base, *next, *top;
380
381 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack);
382};
383
384extern void k_stack_init(struct k_stack *stack, int num_entries);
385extern void k_stack_init_with_buffer(struct k_stack *stack, int num_entries,
386 uint32_t *buffer);
387extern void k_stack_push(struct k_stack *stack, uint32_t data);
388extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
389
390#define K_STACK_INITIALIZER(obj, stack_num_entries, stack_buffer) \
391 { \
392 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
393 .base = stack_buffer, \
394 .next = stack_buffer, \
395 .top = stack_buffer + stack_num_entries, \
396 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
397 }
398
399#define K_STACK_DEFINE(name, stack_num_entries) \
400 uint32_t __noinit _k_stack_buf_##name[stack_num_entries]; \
401 struct k_stack _k_stack_obj_##name = \
402 K_STACK_INITIALIZER(_k_stack_obj_##name, stack_num_entries, \
403 _k_stack_buf_##name); \
404 struct k_stack * const name = &_k_stack_obj_##name
405
406#define K_STACK_SIZE(stack_num_entries) \
407 (sizeof(struct k_stack) + (stack_num_entries * sizeof(uint32_t)))
408
409/**
410 * workqueues
411 */
412
413struct k_work;
414
415typedef void (*k_work_handler_t)(struct k_work *);
416
417/**
418 * A workqueue is a fiber that executes @ref k_work items that are
419 * queued to it. This is useful for drivers which need to schedule
420 * execution of code which might sleep from ISR context. The actual
421 * fiber identifier is not stored in the structure in order to save
422 * space.
423 */
424struct k_work_q {
425 struct k_fifo fifo;
426};
427
428/**
429 * @brief Work flags.
430 */
431enum {
432 K_WORK_STATE_IDLE, /* Work item idle state */
433};
434
435/**
436 * @brief An item which can be scheduled on a @ref k_work_q.
437 */
438struct k_work {
439 void *_reserved; /* Used by k_fifo implementation. */
440 k_work_handler_t handler;
441 atomic_t flags[1];
442};
443
444/**
445 * @brief Statically initialize work item
446 */
447#define K_WORK_INITIALIZER(work_handler) \
448 { \
449 ._reserved = NULL, \
450 .handler = work_handler, \
451 .flags = { 1 } \
452 }
453
454/**
455 * @brief Dynamically initialize work item
456 */
457static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
458{
459 atomic_set_bit(work->flags, K_WORK_STATE_IDLE);
460 work->handler = handler;
461}
462
463/**
464 * @brief Submit a work item to a workqueue.
465 */
466static inline void k_work_submit_to_queue(struct k_work_q *work_q,
467 struct k_work *work)
468{
469 if (!atomic_test_and_clear_bit(work->flags, K_WORK_STATE_IDLE)) {
470 __ASSERT_NO_MSG(0);
471 } else {
472 k_fifo_put(&work_q->fifo, work);
473 }
474}
475
476/**
477 * @brief Start a new workqueue. This routine can be called from either
478 * fiber or task context.
479 */
480extern void k_work_q_start(struct k_work_q *work_q,
481 const struct k_thread_config *config);
482
483#if defined(CONFIG_NANO_TIMEOUTS)
484
485 /*
486 * @brief An item which can be scheduled on a @ref k_work_q with a
487 * delay.
488 */
489struct k_delayed_work {
490 struct k_work work;
491 struct _timeout timeout;
492 struct k_work_q *work_q;
493};
494
495/**
496 * @brief Initialize delayed work
497 */
498void k_delayed_work_init(struct k_delayed_work *work,
499 k_work_handler_t handler);
500
501/**
502 * @brief Submit a delayed work item to a workqueue.
503 *
504 * This procedure schedules a work item to be processed after a delay.
505 * Once the delay has passed, the work item is submitted to the work queue:
506 * at this point, it is no longer possible to cancel it. Once the work item's
507 * handler is about to be executed, the work is considered complete and can be
508 * resubmitted.
509 *
510 * Care must be taken if the handler blocks or yield as there is no implicit
511 * mutual exclusion mechanism. Such usage is not recommended and if necessary,
512 * it should be explicitly done between the submitter and the handler.
513 *
514 * @param work_q to schedule the work item
515 * @param work Delayed work item
516 * @param ticks Ticks to wait before scheduling the work item
517 *
518 * @return 0 in case of success or negative value in case of error.
519 */
520int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
521 struct k_delayed_work *work,
522 int32_t ticks);
523
524/**
525 * @brief Cancel a delayed work item
526 *
527 * This procedure cancels a scheduled work item. If the work has been completed
528 * or is idle, this will do nothing. The only case where this can fail is when
529 * the work has been submitted to the work queue, but the handler has not run
530 * yet.
531 *
532 * @param work Delayed work item to be canceled
533 *
534 * @return 0 in case of success or negative value in case of error.
535 */
536int k_delayed_work_cancel(struct k_delayed_work *work);
537
538#endif /* CONFIG_NANO_TIMEOUTS */
539
540#if defined(CONFIG_SYSTEM_WORKQUEUE)
541
542extern struct k_work_q k_sys_work_q;
543
544/*
545 * @brief Submit a work item to the system workqueue.
546 *
547 * @ref k_work_submit_to_queue
548 *
549 * When using the system workqueue it is not recommended to block or yield
550 * on the handler since its fiber is shared system wide it may cause
551 * unexpected behavior.
552 */
553static inline void k_work_submit(struct k_work *work)
554{
555 k_work_submit_to_queue(&k_sys_work_q, work);
556}
557
558#if defined(CONFIG_NANO_TIMEOUTS)
559/*
560 * @brief Submit a delayed work item to the system workqueue.
561 *
562 * @ref k_delayed_work_submit_to_queue
563 *
564 * When using the system workqueue it is not recommended to block or yield
565 * on the handler since its fiber is shared system wide it may cause
566 * unexpected behavior.
567 */
568static inline int k_delayed_work_submit(struct k_delayed_work *work,
569 int ticks)
570{
571 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, ticks);
572}
573
574#endif /* CONFIG_NANO_TIMEOUTS */
575#endif /* CONFIG_SYSTEM_WORKQUEUE */
576
577/**
578 * synchronization
579 */
580
581/* mutexes */
582
583struct k_mutex {
584 _wait_q_t wait_q;
585 struct tcs *owner;
586 uint32_t lock_count;
587 int owner_orig_prio;
588#ifdef CONFIG_OBJECT_MONITOR
589 int num_lock_state_changes;
590 int num_conflicts;
591#endif
592
593 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex);
594};
595
596#ifdef CONFIG_OBJECT_MONITOR
597#define _MUTEX_INIT_OBJECT_MONITOR \
598 .num_lock_state_changes = 0, .num_conflicts = 0,
599#else
600#define _MUTEX_INIT_OBJECT_MONITOR
601#endif
602
603#define K_MUTEX_INITIALIZER(obj) \
604 { \
605 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
606 .owner = NULL, \
607 .lock_count = 0, \
608 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
609 _MUTEX_INIT_OBJECT_MONITOR \
610 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
611 }
612
613#define K_MUTEX_DEFINE(name) \
614 struct k_mutex name = K_MUTEX_INITIALIZER(name)
615
616extern void k_mutex_init(struct k_mutex *mutex);
617extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
618extern void k_mutex_unlock(struct k_mutex *mutex);
619
620/* semaphores */
621
622struct k_sem {
623 _wait_q_t wait_q;
624 unsigned int count;
625 unsigned int limit;
626
627 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem);
628};
629
630extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
631 unsigned int limit);
632extern int k_sem_take(struct k_sem *sem, int32_t timeout);
633extern void k_sem_give(struct k_sem *sem);
634
635static inline int k_sem_reset(struct k_sem *sem)
636{
637 sem->count = 0;
638
639 return 0;
640}
641
642static inline int k_sem_count_get(struct k_sem *sem)
643{
644 return sem->count;
645}
646
647extern struct k_sem *k_sem_group_take(struct k_sem **sem_array,
648 int32_t timeout);
649extern void k_sem_group_give(struct k_sem **sem_array);
650extern void k_sem_group_reset(struct k_sem **sem_array);
651
652#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
653 { \
654 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
655 .count = initial_count, \
656 .limit = count_limit, \
657 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
658 }
659
660#define K_SEM_DEFINE(name, initial_count, count_limit) \
661 struct k_sem name = \
662 K_SEM_INITIALIZER(name, initial_count, count_limit)
663
664/* events */
665
666#define K_EVT_DEFAULT NULL
667#define K_EVT_IGNORE ((void *)(-1))
668
669typedef int (*k_event_handler_t)(struct k_event *);
670
671struct k_event {
672 k_event_handler_t handler;
673 atomic_t send_count;
674 struct k_work work_item;
675 struct k_sem sem;
676
677 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_event);
678};
679
680extern void _k_event_deliver(struct k_work *work);
681
682#define K_EVENT_INITIALIZER(obj, event_handler) \
683 { \
684 .handler = (k_event_handler_t)event_handler, \
685 .send_count = ATOMIC_INIT(0), \
686 .work_item = K_WORK_INITIALIZER(_k_event_deliver), \
687 .sem = K_SEM_INITIALIZER(obj.sem, 0, 1), \
688 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
689 }
690
691#define K_EVENT_DEFINE(name, event_handler) \
692 struct k_event name \
693 __in_section(_k_event_list, event, name) = \
694 K_EVENT_INITIALIZER(name, event_handler)
695
696extern void k_event_init(struct k_event *event, k_event_handler_t handler);
697extern int k_event_recv(struct k_event *event, int32_t timeout);
698extern void k_event_send(struct k_event *event);
699
700/**
701 * data transfers (complex)
702 */
703
704/* message queues */
705
706struct k_msgq {
707 _wait_q_t wait_q;
708 uint32_t msg_size;
709 uint32_t max_msgs;
710 char *buffer_start;
711 char *buffer_end;
712 char *read_ptr;
713 char *write_ptr;
714 uint32_t used_msgs;
715
716 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq);
717};
718
719#define K_MSGQ_INITIALIZER(obj, q_depth, q_width, q_buffer) \
720 { \
721 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
722 .max_msgs = q_depth, \
723 .msg_size = q_width, \
724 .buffer_start = q_buffer, \
725 .buffer_end = q_buffer + (q_depth * q_width), \
726 .read_ptr = q_buffer, \
727 .write_ptr = q_buffer, \
728 .used_msgs = 0, \
729 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
730 }
731
732#define K_MSGQ_DEFINE(name, q_depth, q_width) \
733 static char __noinit _k_fifo_buf_##name[(q_depth) * (q_width)]; \
734 struct k_msgq name = \
735 K_MSGQ_INITIALIZER(name, q_depth, q_width, _k_fifo_buf_##name)
736
737#define K_MSGQ_SIZE(q_depth, q_width) \
738 ((sizeof(struct k_msgq)) + ((q_width) * (q_depth)))
739
740void k_msgq_init(struct k_msgq *q, uint32_t msg_size, uint32_t max_msgs,
741 char *buffer);
742extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
743extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
744extern void k_msgq_purge(struct k_msgq *q);
745
746static inline int k_msgq_num_used_get(struct k_msgq *q)
747{
748 return q->used_msgs;
749}
750
751struct k_mem_block {
752 k_mem_pool_t pool_id;
753 void *addr_in_pool;
754 void *data;
755 uint32_t req_size;
756};
757
758/* mailboxes */
759
760struct k_mbox_msg {
761 /** internal use only - needed for legacy API support */
762 uint32_t _mailbox;
763 /** size of message (in bytes) */
764 uint32_t size;
765 /** application-defined information value */
766 uint32_t info;
767 /** sender's message data buffer */
768 void *tx_data;
769 /** internal use only - needed for legacy API support */
770 void *_rx_data;
771 /** message data block descriptor */
772 struct k_mem_block tx_block;
773 /** source thread id */
774 k_tid_t rx_source_thread;
775 /** target thread id */
776 k_tid_t tx_target_thread;
777 /** internal use only - thread waiting on send (may be a dummy) */
778 k_tid_t _syncing_thread;
779#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
780 /** internal use only - semaphore used during asynchronous send */
781 struct k_sem *_async_sem;
782#endif
783};
784
785struct k_mbox {
786 _wait_q_t tx_msg_queue;
787 _wait_q_t rx_msg_queue;
788
789 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox);
790};
791
792#define K_MBOX_INITIALIZER(obj) \
793 { \
794 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
795 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
796 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
797 }
798
799#define K_MBOX_DEFINE(name) \
800 struct k_mbox name = \
801 K_MBOX_INITIALIZER(name) \
802
803#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
804extern void _k_mbox_init(void);
805#else
806#define _k_mbox_init()
807#endif
808
809extern void k_mbox_init(struct k_mbox *mbox);
810
811extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *msg,
812 int32_t timeout);
813extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *msg,
814 struct k_sem *sem);
815
816extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *msg,
817 void *buffer, int32_t timeout);
818extern void k_mbox_data_get(struct k_mbox_msg *msg, void *buffer);
819extern int k_mbox_data_block_get(struct k_mbox_msg *msg, k_mem_pool_t pool,
820 struct k_mem_block *block, int32_t timeout);
821
822/* pipes */
823
824struct k_pipe {
825 unsigned char *buffer; /* Pipe buffer: may be NULL */
826 size_t size; /* Buffer size */
827 size_t bytes_used; /* # bytes used in buffer */
828 size_t read_index; /* Where in buffer to read from */
829 size_t write_index; /* Where in buffer to write */
830
831 struct {
832 _wait_q_t readers; /* Reader wait queue */
833 _wait_q_t writers; /* Writer wait queue */
834 } wait_q;
835
836 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe);
837};
838
839#define K_PIPE_INITIALIZER(obj, pipe_buffer_size, pipe_buffer) \
840 { \
841 .buffer = pipe_buffer, \
842 .size = pipe_buffer_size, \
843 .bytes_used = 0, \
844 .read_index = 0, \
845 .write_index = 0, \
846 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
847 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
848 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
849 }
850
851#define K_PIPE_DEFINE(name, pipe_buffer_size) \
852 static unsigned char __noinit _k_pipe_buf_##name[pipe_buffer_size]; \
853 struct k_pipe name = \
854 K_PIPE_INITIALIZER(name, pipe_buffer_size, _k_pipe_buf_##name)
855
856#define K_PIPE_SIZE(buffer_size) (sizeof(struct k_pipe) + buffer_size)
857
858#if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0)
859extern void _k_pipes_init(void);
860#else
861#define _k_pipes_init() do { } while (0)
862#endif
863
864/**
865 * @brief Runtime initialization of a pipe
866 *
867 * @param pipe Pointer to pipe to initialize
868 * @param buffer Pointer to buffer to use for pipe's ring buffer
869 * @param size Size of the pipe's ring buffer
870 *
871 * @return N/A
872 */
873extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
874 size_t size);
875
876/**
877 * @brief Put a message into the specified pipe
878 *
879 * This routine synchronously adds a message into the pipe specified by
880 * @a pipe. It will wait up to @a timeout for the pipe to accept
881 * @a num_bytes_to_write bytes of data. If by @a timeout, the pipe could not
882 * accept @a min_bytes bytes of data, it fails. Fewer than @a min_bytes will
883 * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
884 *
885 * @param pipe Pointer to the pipe
886 * @param buffer Data to put into the pipe
887 * @param num_bytes_to_write Desired number of bytes to put into the pipe
888 * @param num_bytes_written Number of bytes the pipe accepted
889 * @param min_bytes Minimum number of bytes accepted for success
890 * @param timeout Maximum number of milliseconds to wait
891 *
892 * @retval 0 At least @a min_bytes were sent
893 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
894 * @retval -EAGAIN Fewer than @a min_bytes were sent
895 */
896extern int k_pipe_put(struct k_pipe *pipe, void *buffer,
897 size_t num_bytes_to_write, size_t *num_bytes_written,
898 size_t min_bytes, int32_t timeout);
899
900/**
901 * @brief Get a message from the specified pipe
902 *
903 * This routine synchronously retrieves a message from the pipe specified by
904 * @a pipe. It will wait up to @a timeout to retrieve @a num_bytes_to_read
905 * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve
906 * @a min_bytes bytes of data, it fails. Fewer than @a min_bytes will
907 * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER.
908 *
909 * @param pipe Pointer to the pipe
910 * @param buffer Location to place retrieved data
911 * @param num_bytes_to_read Desired number of bytes to retrieve from the pipe
912 * @param num_bytes_read Number of bytes retrieved from the pipe
913 * @param min_bytes Minimum number of bytes retrieved for success
914 * @param timeout Maximum number of milliseconds to wait
915 *
916 * @retval 0 At least @a min_bytes were transferred
917 * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT)
918 * @retval -EAGAIN Fewer than @a min_bytes were retrieved
919 */
920extern int k_pipe_get(struct k_pipe *pipe, void *buffer,
921 size_t num_bytes_to_read, size_t *num_bytes_read,
922 size_t min_bytes, int32_t timeout);
923
924/**
925 * @brief Send a message to the specified pipe
926 *
927 * This routine asynchronously sends a message from the pipe specified by
928 * @a pipe. Once all @a size bytes have been accepted by the pipe, it will
929 * free the memory block @a block and give the semaphore @a sem (if specified).
930 * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight
931 * at any given time.
932 *
933 * @param pipe Pointer to the pipe
934 * @param block Memory block containing data to send
935 * @param size Number of data bytes in memory block to send
936 * @param sem Semaphore to signal upon completion (else NULL)
937 *
938 * @retval N/A
939 */
940extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
941 size_t size, struct k_sem *sem);
942
943/**
944 * memory management
945 */
946
947/* memory maps */
948
949struct k_mem_map {
950 _wait_q_t wait_q;
951 int num_blocks;
952 int block_size;
953 char *buffer;
954 char *free_list;
955 int num_used;
956
957 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_map);
958};
959
960#define K_MEM_MAP_INITIALIZER(obj, map_num_blocks, map_block_size, \
961 map_buffer) \
962 { \
963 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
964 .num_blocks = map_num_blocks, \
965 .block_size = map_block_size, \
966 .buffer = map_buffer, \
967 .free_list = NULL, \
968 .num_used = 0, \
969 _DEBUG_TRACING_KERNEL_OBJECTS_INIT \
970 }
971
972#define K_MEM_MAP_DEFINE(name, map_num_blocks, map_block_size) \
973 char _k_mem_map_buf_##name[(map_num_blocks) * (map_block_size)]; \
974 struct k_mem_map name \
975 __in_section(_k_mem_map_ptr, private, mem_map) = \
976 K_MEM_MAP_INITIALIZER(name, map_num_blocks, \
977 map_block_size, _k_mem_map_buf_##name)
978
979#define K_MEM_MAP_SIZE(map_num_blocks, map_block_size) \
980 (sizeof(struct k_mem_map) + ((map_num_blocks) * (map_block_size)))
981
982extern void _k_mem_map_init(void);
983
984extern void k_mem_map_init(struct k_mem_map *map, int num_blocks,
985 int block_size, void *buffer);
986extern int k_mem_map_alloc(struct k_mem_map *map, void **mem, int32_t timeout);
987extern void k_mem_map_free(struct k_mem_map *map, void **mem);
988
989static inline int k_mem_map_num_used_get(struct k_mem_map *map)
990{
991 return map->num_used;
992}
993
994/* memory pools */
995
996struct k_mem_pool {
997 _wait_q_t wait_q;
998 int max_block_size;
999 int num_max_blocks;
1000
1001 _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool);
1002};
1003
1004/* cannot initialize pools statically */
1005
1006/* XXX - review this computation */
1007#define K_MEM_POOL_SIZE(max_block_size, num_max_blocks) \
1008 (sizeof(struct k_mem_pool) + ((max_block_size) * (num_max_blocks)))
1009
1010extern void k_mem_pool_init(struct k_mem_pool *mem, int max_block_size,
1011 int num_max_blocks);
1012extern int k_mem_pool_alloc(k_mem_pool_t id, struct k_mem_block *block,
1013 int size, int32_t timeout);
1014extern void k_mem_pool_free(struct k_mem_block *block);
1015extern void k_mem_pool_defrag(k_mem_pool_t id);
1016extern void *k_malloc(uint32_t size);
1017extern void k_free(void *p);
1018
1019/*
1020 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
1021 * hook into the device subsystem, which itself uses nanokernel semaphores,
1022 * and thus currently requires the definition of nano_sem.
1023 */
1024#include <legacy.h>
1025#include <arch/cpu.h>
1026
1027/*
1028 * private APIs that are utilized by one or more public APIs
1029 */
1030
1031extern struct k_thread_static_init _k_task_list_start[];
1032extern struct k_thread_static_init _k_task_list_end[];
1033
1034#define _FOREACH_STATIC_THREAD(thread_init) \
1035 for (struct k_thread_static_init *thread_init = _k_task_list_start; \
1036 thread_init < _k_task_list_end; thread_init++)
1037
1038extern int _is_thread_essential(void);
1039static inline int is_in_any_group(struct k_thread_static_init *thread_init,
1040 uint32_t groups)
1041{
1042 return !!(thread_init->init_groups & groups);
1043}
1044extern void _init_static_threads(void);
1045
1046#ifdef __cplusplus
1047}
1048#endif
1049
1050#endif /* _kernel__h_ */