Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1 | /* |
| 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 |
| 37 | extern "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 | |
| 72 | typedef 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 |
| 83 | struct tcs; |
| 84 | struct k_mutex; |
| 85 | struct k_sem; |
| 86 | struct k_event; |
| 87 | struct k_msgq; |
| 88 | struct k_mbox; |
| 89 | struct k_pipe; |
| 90 | struct k_fifo; |
| 91 | struct k_lifo; |
| 92 | struct k_stack; |
| 93 | struct k_mem_map; |
| 94 | struct k_mem_pool; |
| 95 | struct k_timer; |
| 96 | |
| 97 | typedef struct tcs *k_tid_t; |
| 98 | typedef struct k_mem_pool *k_mem_pool_t; |
| 99 | |
| 100 | /* threads/scheduler/execution contexts */ |
| 101 | |
| 102 | enum execution_context_types { |
| 103 | K_ISR = 0, |
| 104 | K_COOP_THREAD, |
| 105 | K_PREEMPT_THREAD, |
| 106 | }; |
| 107 | |
| 108 | struct k_thread_config { |
| 109 | char *stack; |
| 110 | unsigned stack_size; |
| 111 | unsigned prio; |
| 112 | }; |
| 113 | |
| 114 | typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3); |
| 115 | extern 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 | |
| 120 | extern void k_sleep(int32_t duration); |
| 121 | extern void k_busy_wait(uint32_t usec_to_wait); |
| 122 | extern void k_yield(void); |
| 123 | extern void k_wakeup(k_tid_t thread); |
| 124 | extern k_tid_t k_current_get(void); |
| 125 | extern k_tid_t k_current_get(void); |
| 126 | extern int k_current_priority_get(void); |
| 127 | extern int k_thread_cancel(k_tid_t thread); |
| 128 | |
| 129 | extern 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 | |
| 161 | struct 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 */ |
| 216 | extern void k_thread_priority_set(k_tid_t thread, int prio); |
| 217 | |
| 218 | #if 0 |
| 219 | extern int k_thread_suspend(k_tid_t thread); |
| 220 | extern int k_thread_resume(k_tid_t thread); |
| 221 | extern int k_thread_entry_set(k_tid_t thread, |
| 222 | void (*entry)(void*, void*, void*); |
| 223 | extern int k_thread_abort_handler_set(k_tid_t thread, |
| 224 | void (*handler)(void)); |
| 225 | #endif |
| 226 | |
| 227 | extern void k_sched_time_slice_set(int32_t slice, int prio); |
| 228 | extern int k_workload_get(void); |
| 229 | extern void k_workload_time_slice_set(int32_t slice); |
| 230 | |
| 231 | extern int k_am_in_isr(void); |
| 232 | |
| 233 | extern void k_thread_custom_data_set(void *value); |
| 234 | extern void *k_thread_custom_data_get(void); |
| 235 | |
| 236 | /** |
| 237 | * kernel timing |
| 238 | */ |
| 239 | |
| 240 | /* timeouts */ |
| 241 | |
| 242 | struct _timeout; |
| 243 | typedef void (*_timeout_func_t)(struct _timeout *t); |
| 244 | |
| 245 | struct _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 | |
| 255 | struct 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 | |
| 295 | extern void k_timer_init(struct k_timer *timer, void *data); |
| 296 | extern struct k_timer *k_timer_alloc(void); |
| 297 | extern void k_timer_free(struct k_timer *timer); |
| 298 | extern 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); |
| 302 | extern void k_timer_restart(struct k_timer *timer, int32_t duration, |
| 303 | int32_t period); |
| 304 | extern void k_timer_stop(struct k_timer *timer); |
| 305 | extern int k_timer_test(struct k_timer *timer, void **data, int wait); |
| 306 | extern int32_t k_timer_remaining_get(struct k_timer *timer); |
| 307 | extern int64_t k_uptime_get(void); |
| 308 | extern int64_t k_uptime_delta(int64_t *reftime); |
| 309 | extern bool k_timer_pool_is_empty(void); |
| 310 | |
| 311 | extern uint32_t k_cycle_get_32(void); |
| 312 | |
| 313 | #if (CONFIG_NUM_DYNAMIC_TIMERS > 0) |
| 314 | extern 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 | |
| 325 | struct 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 | |
| 332 | extern void k_fifo_init(struct k_fifo *fifo); |
| 333 | extern void k_fifo_put(struct k_fifo *fifo, void *data); |
| 334 | extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail); |
| 335 | extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list); |
| 336 | extern 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) \ |
Benjamin Walsh | 0bee91d | 2016-09-15 17:16:38 -0400 | [diff] [blame] | 346 | struct k_fifo name = K_FIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 347 | |
| 348 | /* lifos */ |
| 349 | |
| 350 | struct k_lifo { |
| 351 | _wait_q_t wait_q; |
| 352 | void *list; |
| 353 | |
| 354 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo); |
| 355 | }; |
| 356 | |
| 357 | extern void k_lifo_init(struct k_lifo *lifo); |
| 358 | extern void k_lifo_put(struct k_lifo *lifo, void *data); |
| 359 | extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout); |
| 360 | |
| 361 | #define K_LIFO_INITIALIZER(obj) \ |
| 362 | { \ |
| 363 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 364 | .list = NULL, \ |
| 365 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 366 | } |
| 367 | |
| 368 | #define K_LIFO_DEFINE(name) \ |
Benjamin Walsh | 0bee91d | 2016-09-15 17:16:38 -0400 | [diff] [blame] | 369 | struct k_lifo name = K_LIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 370 | |
| 371 | /* stacks */ |
| 372 | |
| 373 | struct k_stack { |
| 374 | _wait_q_t wait_q; |
| 375 | uint32_t *base, *next, *top; |
| 376 | |
| 377 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack); |
| 378 | }; |
| 379 | |
| 380 | extern void k_stack_init(struct k_stack *stack, int num_entries); |
| 381 | extern void k_stack_init_with_buffer(struct k_stack *stack, int num_entries, |
| 382 | uint32_t *buffer); |
| 383 | extern void k_stack_push(struct k_stack *stack, uint32_t data); |
| 384 | extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout); |
| 385 | |
| 386 | #define K_STACK_INITIALIZER(obj, stack_num_entries, stack_buffer) \ |
| 387 | { \ |
| 388 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 389 | .base = stack_buffer, \ |
| 390 | .next = stack_buffer, \ |
| 391 | .top = stack_buffer + stack_num_entries, \ |
| 392 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 393 | } |
| 394 | |
| 395 | #define K_STACK_DEFINE(name, stack_num_entries) \ |
| 396 | uint32_t __noinit _k_stack_buf_##name[stack_num_entries]; \ |
Benjamin Walsh | 0bee91d | 2016-09-15 17:16:38 -0400 | [diff] [blame] | 397 | struct k_stack name = \ |
| 398 | K_STACK_INITIALIZER(name, stack_num_entries, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 399 | _k_stack_buf_##name); \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 400 | |
| 401 | #define K_STACK_SIZE(stack_num_entries) \ |
| 402 | (sizeof(struct k_stack) + (stack_num_entries * sizeof(uint32_t))) |
| 403 | |
| 404 | /** |
| 405 | * workqueues |
| 406 | */ |
| 407 | |
| 408 | struct k_work; |
| 409 | |
| 410 | typedef void (*k_work_handler_t)(struct k_work *); |
| 411 | |
| 412 | /** |
| 413 | * A workqueue is a fiber that executes @ref k_work items that are |
| 414 | * queued to it. This is useful for drivers which need to schedule |
| 415 | * execution of code which might sleep from ISR context. The actual |
| 416 | * fiber identifier is not stored in the structure in order to save |
| 417 | * space. |
| 418 | */ |
| 419 | struct k_work_q { |
| 420 | struct k_fifo fifo; |
| 421 | }; |
| 422 | |
| 423 | /** |
| 424 | * @brief Work flags. |
| 425 | */ |
| 426 | enum { |
| 427 | K_WORK_STATE_IDLE, /* Work item idle state */ |
| 428 | }; |
| 429 | |
| 430 | /** |
| 431 | * @brief An item which can be scheduled on a @ref k_work_q. |
| 432 | */ |
| 433 | struct k_work { |
| 434 | void *_reserved; /* Used by k_fifo implementation. */ |
| 435 | k_work_handler_t handler; |
| 436 | atomic_t flags[1]; |
| 437 | }; |
| 438 | |
| 439 | /** |
| 440 | * @brief Statically initialize work item |
| 441 | */ |
| 442 | #define K_WORK_INITIALIZER(work_handler) \ |
| 443 | { \ |
| 444 | ._reserved = NULL, \ |
| 445 | .handler = work_handler, \ |
| 446 | .flags = { 1 } \ |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * @brief Dynamically initialize work item |
| 451 | */ |
| 452 | static inline void k_work_init(struct k_work *work, k_work_handler_t handler) |
| 453 | { |
| 454 | atomic_set_bit(work->flags, K_WORK_STATE_IDLE); |
| 455 | work->handler = handler; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * @brief Submit a work item to a workqueue. |
| 460 | */ |
| 461 | static inline void k_work_submit_to_queue(struct k_work_q *work_q, |
| 462 | struct k_work *work) |
| 463 | { |
| 464 | if (!atomic_test_and_clear_bit(work->flags, K_WORK_STATE_IDLE)) { |
| 465 | __ASSERT_NO_MSG(0); |
| 466 | } else { |
| 467 | k_fifo_put(&work_q->fifo, work); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * @brief Start a new workqueue. This routine can be called from either |
| 473 | * fiber or task context. |
| 474 | */ |
| 475 | extern void k_work_q_start(struct k_work_q *work_q, |
| 476 | const struct k_thread_config *config); |
| 477 | |
| 478 | #if defined(CONFIG_NANO_TIMEOUTS) |
| 479 | |
| 480 | /* |
| 481 | * @brief An item which can be scheduled on a @ref k_work_q with a |
| 482 | * delay. |
| 483 | */ |
| 484 | struct k_delayed_work { |
| 485 | struct k_work work; |
| 486 | struct _timeout timeout; |
| 487 | struct k_work_q *work_q; |
| 488 | }; |
| 489 | |
| 490 | /** |
| 491 | * @brief Initialize delayed work |
| 492 | */ |
| 493 | void k_delayed_work_init(struct k_delayed_work *work, |
| 494 | k_work_handler_t handler); |
| 495 | |
| 496 | /** |
| 497 | * @brief Submit a delayed work item to a workqueue. |
| 498 | * |
| 499 | * This procedure schedules a work item to be processed after a delay. |
| 500 | * Once the delay has passed, the work item is submitted to the work queue: |
| 501 | * at this point, it is no longer possible to cancel it. Once the work item's |
| 502 | * handler is about to be executed, the work is considered complete and can be |
| 503 | * resubmitted. |
| 504 | * |
| 505 | * Care must be taken if the handler blocks or yield as there is no implicit |
| 506 | * mutual exclusion mechanism. Such usage is not recommended and if necessary, |
| 507 | * it should be explicitly done between the submitter and the handler. |
| 508 | * |
| 509 | * @param work_q to schedule the work item |
| 510 | * @param work Delayed work item |
| 511 | * @param ticks Ticks to wait before scheduling the work item |
| 512 | * |
| 513 | * @return 0 in case of success or negative value in case of error. |
| 514 | */ |
| 515 | int k_delayed_work_submit_to_queue(struct k_work_q *work_q, |
| 516 | struct k_delayed_work *work, |
| 517 | int32_t ticks); |
| 518 | |
| 519 | /** |
| 520 | * @brief Cancel a delayed work item |
| 521 | * |
| 522 | * This procedure cancels a scheduled work item. If the work has been completed |
| 523 | * or is idle, this will do nothing. The only case where this can fail is when |
| 524 | * the work has been submitted to the work queue, but the handler has not run |
| 525 | * yet. |
| 526 | * |
| 527 | * @param work Delayed work item to be canceled |
| 528 | * |
| 529 | * @return 0 in case of success or negative value in case of error. |
| 530 | */ |
| 531 | int k_delayed_work_cancel(struct k_delayed_work *work); |
| 532 | |
| 533 | #endif /* CONFIG_NANO_TIMEOUTS */ |
| 534 | |
| 535 | #if defined(CONFIG_SYSTEM_WORKQUEUE) |
| 536 | |
| 537 | extern struct k_work_q k_sys_work_q; |
| 538 | |
| 539 | /* |
| 540 | * @brief Submit a work item to the system workqueue. |
| 541 | * |
| 542 | * @ref k_work_submit_to_queue |
| 543 | * |
| 544 | * When using the system workqueue it is not recommended to block or yield |
| 545 | * on the handler since its fiber is shared system wide it may cause |
| 546 | * unexpected behavior. |
| 547 | */ |
| 548 | static inline void k_work_submit(struct k_work *work) |
| 549 | { |
| 550 | k_work_submit_to_queue(&k_sys_work_q, work); |
| 551 | } |
| 552 | |
| 553 | #if defined(CONFIG_NANO_TIMEOUTS) |
| 554 | /* |
| 555 | * @brief Submit a delayed work item to the system workqueue. |
| 556 | * |
| 557 | * @ref k_delayed_work_submit_to_queue |
| 558 | * |
| 559 | * When using the system workqueue it is not recommended to block or yield |
| 560 | * on the handler since its fiber is shared system wide it may cause |
| 561 | * unexpected behavior. |
| 562 | */ |
| 563 | static inline int k_delayed_work_submit(struct k_delayed_work *work, |
| 564 | int ticks) |
| 565 | { |
| 566 | return k_delayed_work_submit_to_queue(&k_sys_work_q, work, ticks); |
| 567 | } |
| 568 | |
| 569 | #endif /* CONFIG_NANO_TIMEOUTS */ |
| 570 | #endif /* CONFIG_SYSTEM_WORKQUEUE */ |
| 571 | |
| 572 | /** |
| 573 | * synchronization |
| 574 | */ |
| 575 | |
| 576 | /* mutexes */ |
| 577 | |
| 578 | struct k_mutex { |
| 579 | _wait_q_t wait_q; |
| 580 | struct tcs *owner; |
| 581 | uint32_t lock_count; |
| 582 | int owner_orig_prio; |
| 583 | #ifdef CONFIG_OBJECT_MONITOR |
| 584 | int num_lock_state_changes; |
| 585 | int num_conflicts; |
| 586 | #endif |
| 587 | |
| 588 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex); |
| 589 | }; |
| 590 | |
| 591 | #ifdef CONFIG_OBJECT_MONITOR |
| 592 | #define _MUTEX_INIT_OBJECT_MONITOR \ |
| 593 | .num_lock_state_changes = 0, .num_conflicts = 0, |
| 594 | #else |
| 595 | #define _MUTEX_INIT_OBJECT_MONITOR |
| 596 | #endif |
| 597 | |
| 598 | #define K_MUTEX_INITIALIZER(obj) \ |
| 599 | { \ |
| 600 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 601 | .owner = NULL, \ |
| 602 | .lock_count = 0, \ |
| 603 | .owner_orig_prio = K_LOWEST_THREAD_PRIO, \ |
| 604 | _MUTEX_INIT_OBJECT_MONITOR \ |
| 605 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 606 | } |
| 607 | |
| 608 | #define K_MUTEX_DEFINE(name) \ |
| 609 | struct k_mutex name = K_MUTEX_INITIALIZER(name) |
| 610 | |
| 611 | extern void k_mutex_init(struct k_mutex *mutex); |
| 612 | extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout); |
| 613 | extern void k_mutex_unlock(struct k_mutex *mutex); |
| 614 | |
| 615 | /* semaphores */ |
| 616 | |
| 617 | struct k_sem { |
| 618 | _wait_q_t wait_q; |
| 619 | unsigned int count; |
| 620 | unsigned int limit; |
| 621 | |
| 622 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem); |
| 623 | }; |
| 624 | |
| 625 | extern void k_sem_init(struct k_sem *sem, unsigned int initial_count, |
| 626 | unsigned int limit); |
| 627 | extern int k_sem_take(struct k_sem *sem, int32_t timeout); |
| 628 | extern void k_sem_give(struct k_sem *sem); |
| 629 | |
Benjamin Walsh | 70c68b9 | 2016-09-21 10:37:34 -0400 | [diff] [blame^] | 630 | static inline void k_sem_reset(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 631 | { |
| 632 | sem->count = 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 633 | } |
| 634 | |
Tomasz Bursztyka | 276086d | 2016-09-21 16:03:21 +0200 | [diff] [blame] | 635 | static inline unsigned int k_sem_count_get(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 636 | { |
| 637 | return sem->count; |
| 638 | } |
| 639 | |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 640 | #ifdef CONFIG_SEMAPHORE_GROUPS |
| 641 | /** |
| 642 | * @brief Take the first available semaphore |
| 643 | * |
| 644 | * Given a list of semaphore pointers, this routine will attempt to take one |
| 645 | * of them, waiting up to a maximum of @a timeout ms to do so. The taken |
| 646 | * semaphore is identified by @a sem (set to NULL on error). |
| 647 | * |
| 648 | * Be aware that the more semaphores specified in the group, the more stack |
| 649 | * space is required by the waiting thread. |
| 650 | * |
| 651 | * @param sem_array Array of semaphore pointers terminated by a K_END entry |
| 652 | * @param sem Identifies the semaphore that was taken |
| 653 | * @param timeout Maximum number of milliseconds to wait |
| 654 | * |
| 655 | * @retval 0 A semaphore was successfully taken |
| 656 | * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT) |
| 657 | * @retval -EAGAIN Time out occurred while waiting for semaphore |
| 658 | */ |
| 659 | |
| 660 | extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem, |
| 661 | int32_t timeout); |
| 662 | |
| 663 | /** |
| 664 | * @brief Give all the semaphores in the group |
| 665 | * |
| 666 | * This routine will give each semaphore in the array of semaphore pointers. |
| 667 | * |
| 668 | * @param sem_array Array of semaphore pointers terminated by a K_END entry |
| 669 | * |
| 670 | * @return N/A |
| 671 | */ |
| 672 | extern void k_sem_group_give(struct k_sem *sem_array[]); |
| 673 | |
| 674 | /** |
| 675 | * @brief Reset the count to zero on each semaphore in the array |
| 676 | * |
| 677 | * This routine resets the count of each semaphore in the group to zero. |
| 678 | * Note that it does NOT have any impact on any thread that might have |
| 679 | * been previously pending on any of the semaphores. |
| 680 | * |
| 681 | * @param sem_array Array of semaphore pointers terminated by a K_END entry |
| 682 | * |
| 683 | * @return N/A |
| 684 | */ |
| 685 | extern void k_sem_group_reset(struct k_sem *sem_array[]); |
| 686 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 687 | |
| 688 | #define K_SEM_INITIALIZER(obj, initial_count, count_limit) \ |
| 689 | { \ |
| 690 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 691 | .count = initial_count, \ |
| 692 | .limit = count_limit, \ |
| 693 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 694 | } |
| 695 | |
| 696 | #define K_SEM_DEFINE(name, initial_count, count_limit) \ |
| 697 | struct k_sem name = \ |
| 698 | K_SEM_INITIALIZER(name, initial_count, count_limit) |
| 699 | |
| 700 | /* events */ |
| 701 | |
| 702 | #define K_EVT_DEFAULT NULL |
| 703 | #define K_EVT_IGNORE ((void *)(-1)) |
| 704 | |
| 705 | typedef int (*k_event_handler_t)(struct k_event *); |
| 706 | |
| 707 | struct k_event { |
| 708 | k_event_handler_t handler; |
| 709 | atomic_t send_count; |
| 710 | struct k_work work_item; |
| 711 | struct k_sem sem; |
| 712 | |
| 713 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_event); |
| 714 | }; |
| 715 | |
| 716 | extern void _k_event_deliver(struct k_work *work); |
| 717 | |
| 718 | #define K_EVENT_INITIALIZER(obj, event_handler) \ |
| 719 | { \ |
| 720 | .handler = (k_event_handler_t)event_handler, \ |
| 721 | .send_count = ATOMIC_INIT(0), \ |
| 722 | .work_item = K_WORK_INITIALIZER(_k_event_deliver), \ |
| 723 | .sem = K_SEM_INITIALIZER(obj.sem, 0, 1), \ |
| 724 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 725 | } |
| 726 | |
| 727 | #define K_EVENT_DEFINE(name, event_handler) \ |
| 728 | struct k_event name \ |
| 729 | __in_section(_k_event_list, event, name) = \ |
| 730 | K_EVENT_INITIALIZER(name, event_handler) |
| 731 | |
| 732 | extern void k_event_init(struct k_event *event, k_event_handler_t handler); |
| 733 | extern int k_event_recv(struct k_event *event, int32_t timeout); |
| 734 | extern void k_event_send(struct k_event *event); |
| 735 | |
| 736 | /** |
| 737 | * data transfers (complex) |
| 738 | */ |
| 739 | |
| 740 | /* message queues */ |
| 741 | |
| 742 | struct k_msgq { |
| 743 | _wait_q_t wait_q; |
| 744 | uint32_t msg_size; |
| 745 | uint32_t max_msgs; |
| 746 | char *buffer_start; |
| 747 | char *buffer_end; |
| 748 | char *read_ptr; |
| 749 | char *write_ptr; |
| 750 | uint32_t used_msgs; |
| 751 | |
| 752 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq); |
| 753 | }; |
| 754 | |
| 755 | #define K_MSGQ_INITIALIZER(obj, q_depth, q_width, q_buffer) \ |
| 756 | { \ |
| 757 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 758 | .max_msgs = q_depth, \ |
| 759 | .msg_size = q_width, \ |
| 760 | .buffer_start = q_buffer, \ |
| 761 | .buffer_end = q_buffer + (q_depth * q_width), \ |
| 762 | .read_ptr = q_buffer, \ |
| 763 | .write_ptr = q_buffer, \ |
| 764 | .used_msgs = 0, \ |
| 765 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 766 | } |
| 767 | |
| 768 | #define K_MSGQ_DEFINE(name, q_depth, q_width) \ |
| 769 | static char __noinit _k_fifo_buf_##name[(q_depth) * (q_width)]; \ |
| 770 | struct k_msgq name = \ |
| 771 | K_MSGQ_INITIALIZER(name, q_depth, q_width, _k_fifo_buf_##name) |
| 772 | |
| 773 | #define K_MSGQ_SIZE(q_depth, q_width) \ |
| 774 | ((sizeof(struct k_msgq)) + ((q_width) * (q_depth))) |
| 775 | |
| 776 | void k_msgq_init(struct k_msgq *q, uint32_t msg_size, uint32_t max_msgs, |
| 777 | char *buffer); |
| 778 | extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout); |
| 779 | extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout); |
| 780 | extern void k_msgq_purge(struct k_msgq *q); |
| 781 | |
| 782 | static inline int k_msgq_num_used_get(struct k_msgq *q) |
| 783 | { |
| 784 | return q->used_msgs; |
| 785 | } |
| 786 | |
| 787 | struct k_mem_block { |
| 788 | k_mem_pool_t pool_id; |
| 789 | void *addr_in_pool; |
| 790 | void *data; |
| 791 | uint32_t req_size; |
| 792 | }; |
| 793 | |
| 794 | /* mailboxes */ |
| 795 | |
| 796 | struct k_mbox_msg { |
| 797 | /** internal use only - needed for legacy API support */ |
| 798 | uint32_t _mailbox; |
| 799 | /** size of message (in bytes) */ |
| 800 | uint32_t size; |
| 801 | /** application-defined information value */ |
| 802 | uint32_t info; |
| 803 | /** sender's message data buffer */ |
| 804 | void *tx_data; |
| 805 | /** internal use only - needed for legacy API support */ |
| 806 | void *_rx_data; |
| 807 | /** message data block descriptor */ |
| 808 | struct k_mem_block tx_block; |
| 809 | /** source thread id */ |
| 810 | k_tid_t rx_source_thread; |
| 811 | /** target thread id */ |
| 812 | k_tid_t tx_target_thread; |
| 813 | /** internal use only - thread waiting on send (may be a dummy) */ |
| 814 | k_tid_t _syncing_thread; |
| 815 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 816 | /** internal use only - semaphore used during asynchronous send */ |
| 817 | struct k_sem *_async_sem; |
| 818 | #endif |
| 819 | }; |
| 820 | |
| 821 | struct k_mbox { |
| 822 | _wait_q_t tx_msg_queue; |
| 823 | _wait_q_t rx_msg_queue; |
| 824 | |
| 825 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox); |
| 826 | }; |
| 827 | |
| 828 | #define K_MBOX_INITIALIZER(obj) \ |
| 829 | { \ |
| 830 | .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \ |
| 831 | .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \ |
| 832 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 833 | } |
| 834 | |
| 835 | #define K_MBOX_DEFINE(name) \ |
| 836 | struct k_mbox name = \ |
| 837 | K_MBOX_INITIALIZER(name) \ |
| 838 | |
| 839 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 840 | extern void _k_mbox_init(void); |
| 841 | #else |
| 842 | #define _k_mbox_init() |
| 843 | #endif |
| 844 | |
| 845 | extern void k_mbox_init(struct k_mbox *mbox); |
| 846 | |
| 847 | extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *msg, |
| 848 | int32_t timeout); |
| 849 | extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *msg, |
| 850 | struct k_sem *sem); |
| 851 | |
| 852 | extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *msg, |
| 853 | void *buffer, int32_t timeout); |
| 854 | extern void k_mbox_data_get(struct k_mbox_msg *msg, void *buffer); |
| 855 | extern int k_mbox_data_block_get(struct k_mbox_msg *msg, k_mem_pool_t pool, |
| 856 | struct k_mem_block *block, int32_t timeout); |
| 857 | |
| 858 | /* pipes */ |
| 859 | |
| 860 | struct k_pipe { |
| 861 | unsigned char *buffer; /* Pipe buffer: may be NULL */ |
| 862 | size_t size; /* Buffer size */ |
| 863 | size_t bytes_used; /* # bytes used in buffer */ |
| 864 | size_t read_index; /* Where in buffer to read from */ |
| 865 | size_t write_index; /* Where in buffer to write */ |
| 866 | |
| 867 | struct { |
| 868 | _wait_q_t readers; /* Reader wait queue */ |
| 869 | _wait_q_t writers; /* Writer wait queue */ |
| 870 | } wait_q; |
| 871 | |
| 872 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe); |
| 873 | }; |
| 874 | |
| 875 | #define K_PIPE_INITIALIZER(obj, pipe_buffer_size, pipe_buffer) \ |
| 876 | { \ |
| 877 | .buffer = pipe_buffer, \ |
| 878 | .size = pipe_buffer_size, \ |
| 879 | .bytes_used = 0, \ |
| 880 | .read_index = 0, \ |
| 881 | .write_index = 0, \ |
| 882 | .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \ |
| 883 | .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \ |
| 884 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 885 | } |
| 886 | |
| 887 | #define K_PIPE_DEFINE(name, pipe_buffer_size) \ |
| 888 | static unsigned char __noinit _k_pipe_buf_##name[pipe_buffer_size]; \ |
| 889 | struct k_pipe name = \ |
| 890 | K_PIPE_INITIALIZER(name, pipe_buffer_size, _k_pipe_buf_##name) |
| 891 | |
| 892 | #define K_PIPE_SIZE(buffer_size) (sizeof(struct k_pipe) + buffer_size) |
| 893 | |
| 894 | #if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0) |
| 895 | extern void _k_pipes_init(void); |
| 896 | #else |
| 897 | #define _k_pipes_init() do { } while (0) |
| 898 | #endif |
| 899 | |
| 900 | /** |
| 901 | * @brief Runtime initialization of a pipe |
| 902 | * |
| 903 | * @param pipe Pointer to pipe to initialize |
| 904 | * @param buffer Pointer to buffer to use for pipe's ring buffer |
| 905 | * @param size Size of the pipe's ring buffer |
| 906 | * |
| 907 | * @return N/A |
| 908 | */ |
| 909 | extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, |
| 910 | size_t size); |
| 911 | |
| 912 | /** |
| 913 | * @brief Put a message into the specified pipe |
| 914 | * |
| 915 | * This routine synchronously adds a message into the pipe specified by |
| 916 | * @a pipe. It will wait up to @a timeout for the pipe to accept |
| 917 | * @a num_bytes_to_write bytes of data. If by @a timeout, the pipe could not |
| 918 | * accept @a min_bytes bytes of data, it fails. Fewer than @a min_bytes will |
| 919 | * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER. |
| 920 | * |
| 921 | * @param pipe Pointer to the pipe |
| 922 | * @param buffer Data to put into the pipe |
| 923 | * @param num_bytes_to_write Desired number of bytes to put into the pipe |
| 924 | * @param num_bytes_written Number of bytes the pipe accepted |
| 925 | * @param min_bytes Minimum number of bytes accepted for success |
| 926 | * @param timeout Maximum number of milliseconds to wait |
| 927 | * |
| 928 | * @retval 0 At least @a min_bytes were sent |
| 929 | * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT) |
| 930 | * @retval -EAGAIN Fewer than @a min_bytes were sent |
| 931 | */ |
| 932 | extern int k_pipe_put(struct k_pipe *pipe, void *buffer, |
| 933 | size_t num_bytes_to_write, size_t *num_bytes_written, |
| 934 | size_t min_bytes, int32_t timeout); |
| 935 | |
| 936 | /** |
| 937 | * @brief Get a message from the specified pipe |
| 938 | * |
| 939 | * This routine synchronously retrieves a message from the pipe specified by |
| 940 | * @a pipe. It will wait up to @a timeout to retrieve @a num_bytes_to_read |
| 941 | * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve |
| 942 | * @a min_bytes bytes of data, it fails. Fewer than @a min_bytes will |
| 943 | * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER. |
| 944 | * |
| 945 | * @param pipe Pointer to the pipe |
| 946 | * @param buffer Location to place retrieved data |
| 947 | * @param num_bytes_to_read Desired number of bytes to retrieve from the pipe |
| 948 | * @param num_bytes_read Number of bytes retrieved from the pipe |
| 949 | * @param min_bytes Minimum number of bytes retrieved for success |
| 950 | * @param timeout Maximum number of milliseconds to wait |
| 951 | * |
| 952 | * @retval 0 At least @a min_bytes were transferred |
| 953 | * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT) |
| 954 | * @retval -EAGAIN Fewer than @a min_bytes were retrieved |
| 955 | */ |
| 956 | extern int k_pipe_get(struct k_pipe *pipe, void *buffer, |
| 957 | size_t num_bytes_to_read, size_t *num_bytes_read, |
| 958 | size_t min_bytes, int32_t timeout); |
| 959 | |
| 960 | /** |
| 961 | * @brief Send a message to the specified pipe |
| 962 | * |
| 963 | * This routine asynchronously sends a message from the pipe specified by |
| 964 | * @a pipe. Once all @a size bytes have been accepted by the pipe, it will |
| 965 | * free the memory block @a block and give the semaphore @a sem (if specified). |
| 966 | * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight |
| 967 | * at any given time. |
| 968 | * |
| 969 | * @param pipe Pointer to the pipe |
| 970 | * @param block Memory block containing data to send |
| 971 | * @param size Number of data bytes in memory block to send |
| 972 | * @param sem Semaphore to signal upon completion (else NULL) |
| 973 | * |
| 974 | * @retval N/A |
| 975 | */ |
| 976 | extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block, |
| 977 | size_t size, struct k_sem *sem); |
| 978 | |
| 979 | /** |
| 980 | * memory management |
| 981 | */ |
| 982 | |
| 983 | /* memory maps */ |
| 984 | |
| 985 | struct k_mem_map { |
| 986 | _wait_q_t wait_q; |
| 987 | int num_blocks; |
| 988 | int block_size; |
| 989 | char *buffer; |
| 990 | char *free_list; |
| 991 | int num_used; |
| 992 | |
| 993 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_map); |
| 994 | }; |
| 995 | |
| 996 | #define K_MEM_MAP_INITIALIZER(obj, map_num_blocks, map_block_size, \ |
| 997 | map_buffer) \ |
| 998 | { \ |
| 999 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1000 | .num_blocks = map_num_blocks, \ |
| 1001 | .block_size = map_block_size, \ |
| 1002 | .buffer = map_buffer, \ |
| 1003 | .free_list = NULL, \ |
| 1004 | .num_used = 0, \ |
| 1005 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1006 | } |
| 1007 | |
| 1008 | #define K_MEM_MAP_DEFINE(name, map_num_blocks, map_block_size) \ |
| 1009 | char _k_mem_map_buf_##name[(map_num_blocks) * (map_block_size)]; \ |
| 1010 | struct k_mem_map name \ |
| 1011 | __in_section(_k_mem_map_ptr, private, mem_map) = \ |
| 1012 | K_MEM_MAP_INITIALIZER(name, map_num_blocks, \ |
| 1013 | map_block_size, _k_mem_map_buf_##name) |
| 1014 | |
| 1015 | #define K_MEM_MAP_SIZE(map_num_blocks, map_block_size) \ |
| 1016 | (sizeof(struct k_mem_map) + ((map_num_blocks) * (map_block_size))) |
| 1017 | |
| 1018 | extern void _k_mem_map_init(void); |
| 1019 | |
| 1020 | extern void k_mem_map_init(struct k_mem_map *map, int num_blocks, |
| 1021 | int block_size, void *buffer); |
| 1022 | extern int k_mem_map_alloc(struct k_mem_map *map, void **mem, int32_t timeout); |
| 1023 | extern void k_mem_map_free(struct k_mem_map *map, void **mem); |
| 1024 | |
| 1025 | static inline int k_mem_map_num_used_get(struct k_mem_map *map) |
| 1026 | { |
| 1027 | return map->num_used; |
| 1028 | } |
| 1029 | |
| 1030 | /* memory pools */ |
| 1031 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1032 | /* |
| 1033 | * Memory pool requires a buffer and two arrays of structures for the |
| 1034 | * memory block accounting: |
| 1035 | * A set of arrays of k_mem_pool_quad_block structures where each keeps a |
| 1036 | * status of four blocks of memory. |
| 1037 | */ |
| 1038 | struct k_mem_pool_quad_block { |
| 1039 | char *mem_blocks; /* pointer to the first of four memory blocks */ |
| 1040 | uint32_t mem_status; /* four bits. If bit is set, memory block is |
| 1041 | allocated */ |
| 1042 | }; |
| 1043 | /* |
| 1044 | * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting |
| 1045 | * blocks of one size. Block sizes go from maximal to minimal. Next memory |
| 1046 | * block size is 4 times less than the previous one and thus requires 4 times |
| 1047 | * bigger array of k_mem_pool_quad_block structures to keep track of the |
| 1048 | * memory blocks. |
| 1049 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1050 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1051 | /* |
| 1052 | * The array of k_mem_pool_block_set keeps the information of each array of |
| 1053 | * k_mem_pool_quad_block structures |
| 1054 | */ |
| 1055 | struct k_mem_pool_block_set { |
| 1056 | int block_size; /* memory block size */ |
| 1057 | int nr_of_entries; /* nr of quad block structures in the array */ |
| 1058 | struct k_mem_pool_quad_block *quad_block; |
| 1059 | int count; |
| 1060 | }; |
| 1061 | |
| 1062 | /* Memory pool descriptor */ |
| 1063 | struct k_mem_pool { |
| 1064 | int max_block_size; |
| 1065 | int min_block_size; |
| 1066 | int nr_of_maxblocks; |
| 1067 | int nr_of_block_sets; |
| 1068 | struct k_mem_pool_block_set *block_set; |
| 1069 | char *bufblock; |
| 1070 | _wait_q_t wait_q; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1071 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool); |
| 1072 | }; |
| 1073 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1074 | #ifdef CONFIG_ARM |
| 1075 | #define _SECTION_TYPE_SIGN "%" |
| 1076 | #else |
| 1077 | #define _SECTION_TYPE_SIGN "@" |
| 1078 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1079 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1080 | /* |
| 1081 | * Static memory pool initialization |
| 1082 | */ |
| 1083 | /* |
| 1084 | * Use .altmacro to be able to recalculate values and pass them as string |
| 1085 | * arguments when calling assembler macros resursively |
| 1086 | */ |
| 1087 | __asm__(".altmacro\n\t"); |
| 1088 | |
| 1089 | /* |
| 1090 | * Recursively calls a macro |
| 1091 | * The followig global symbols need to be initialized: |
| 1092 | * __memory_pool_max_block_size - maximal size of the memory block |
| 1093 | * __memory_pool_min_block_size - minimal size of the memory block |
| 1094 | * Notes: |
| 1095 | * Global symbols are used due the fact that assembler macro allows only |
| 1096 | * one argument be passed with the % conversion |
| 1097 | * Some assemblers do not get division operation ("/"). To avoid it >> 2 |
| 1098 | * is used instead of / 4. |
| 1099 | * n_max argument needs to go first in the invoked macro, as some |
| 1100 | * assemblers concatenate \name and %(\n_max * 4) arguments |
| 1101 | * if \name goes first |
| 1102 | */ |
| 1103 | __asm__(".macro __do_recurse macro_name, name, n_max\n\t" |
| 1104 | ".ifge __memory_pool_max_block_size >> 2 -" |
| 1105 | " __memory_pool_min_block_size\n\t\t" |
| 1106 | "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t" |
| 1107 | "\\macro_name %(\\n_max * 4) \\name\n\t" |
| 1108 | ".endif\n\t" |
| 1109 | ".endm\n"); |
| 1110 | |
| 1111 | /* |
| 1112 | * Build quad blocks |
| 1113 | * Macro allocates space in memory for the array of k_mem_pool_quad_block |
| 1114 | * structures and recursively calls itself for the next array, 4 times |
| 1115 | * larger. |
| 1116 | * The followig global symbols need to be initialized: |
| 1117 | * __memory_pool_max_block_size - maximal size of the memory block |
| 1118 | * __memory_pool_min_block_size - minimal size of the memory block |
| 1119 | * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block) |
| 1120 | */ |
| 1121 | __asm__(".macro _build_quad_blocks n_max, name\n\t" |
| 1122 | "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t" |
| 1123 | ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t" |
| 1124 | ".if \\n_max % 4\n\t\t" |
| 1125 | ".skip __memory_pool_quad_block_size\n\t" |
| 1126 | ".endif\n\t" |
| 1127 | "__do_recurse _build_quad_blocks \\name \\n_max\n\t" |
| 1128 | ".endm\n"); |
| 1129 | |
| 1130 | /* |
| 1131 | * Build block sets and initialize them |
| 1132 | * Macro initializes the k_mem_pool_block_set structure and |
| 1133 | * recursively calls itself for the next one. |
| 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 | * __memory_pool_block_set_count, the number of the elements in the |
| 1138 | * block set array must be set to 0. Macro calculates it's real |
| 1139 | * value. |
| 1140 | * Since the macro initializes pointers to an array of k_mem_pool_quad_block |
| 1141 | * structures, _build_quad_blocks must be called prior it. |
| 1142 | */ |
| 1143 | __asm__(".macro _build_block_set n_max, name\n\t" |
| 1144 | ".int __memory_pool_max_block_size\n\t" /* block_size */ |
| 1145 | ".if \\n_max % 4\n\t\t" |
| 1146 | ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */ |
| 1147 | ".else\n\t\t" |
| 1148 | ".int \\n_max >> 2\n\t" |
| 1149 | ".endif\n\t" |
| 1150 | ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */ |
| 1151 | ".int 0\n\t" /* count */ |
| 1152 | "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t" |
| 1153 | "__do_recurse _build_block_set \\name \\n_max\n\t" |
| 1154 | ".endm\n"); |
| 1155 | |
| 1156 | /* |
| 1157 | * Build a memory pool structure and initialize it |
| 1158 | * Macro uses __memory_pool_block_set_count global symbol, |
| 1159 | * block set addresses and buffer address, it may be called only after |
| 1160 | * _build_block_set |
| 1161 | */ |
| 1162 | __asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t" |
| 1163 | ".pushsection ._k_memory_pool,\"aw\"," |
| 1164 | _SECTION_TYPE_SIGN "progbits\n\t" |
| 1165 | ".globl \\name\n\t" |
| 1166 | "\\name:\n\t" |
| 1167 | ".int \\max_size\n\t" /* max_block_size */ |
| 1168 | ".int \\min_size\n\t" /* min_block_size */ |
| 1169 | ".int \\n_max\n\t" /* nr_of_maxblocks */ |
| 1170 | ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */ |
| 1171 | ".int _mem_pool_block_sets_\\name\n\t" /* block_set */ |
| 1172 | ".int _mem_pool_buffer_\\name\n\t" /* bufblock */ |
| 1173 | ".int 0\n\t" /* wait_q->head */ |
| 1174 | ".int 0\n\t" /* wait_q->next */ |
| 1175 | ".popsection\n\t" |
| 1176 | ".endm\n"); |
| 1177 | |
| 1178 | #define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \ |
| 1179 | __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \ |
| 1180 | _SECTION_TYPE_SIGN "progbits\n\t"); \ |
| 1181 | __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \ |
| 1182 | __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \ |
| 1183 | __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \ |
| 1184 | STRINGIFY(name) "\n\t"); \ |
| 1185 | __asm__(".popsection\n\t") |
| 1186 | |
| 1187 | #define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \ |
| 1188 | __asm__("__memory_pool_block_set_count = 0\n\t"); \ |
| 1189 | __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \ |
| 1190 | __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \ |
| 1191 | _SECTION_TYPE_SIGN "progbits\n\t"); \ |
| 1192 | __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \ |
| 1193 | __asm__("_build_block_set " STRINGIFY(n_max) " " \ |
| 1194 | STRINGIFY(name) "\n\t"); \ |
| 1195 | __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \ |
| 1196 | __asm__(".int __memory_pool_block_set_count\n\t"); \ |
| 1197 | __asm__(".popsection\n\t"); \ |
| 1198 | extern uint32_t _mem_pool_block_set_count_##name; \ |
| 1199 | extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[] |
| 1200 | |
| 1201 | #define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max) \ |
| 1202 | char __noinit _mem_pool_buffer_##name[(max_size) * (n_max)] |
| 1203 | |
| 1204 | #define K_MEMORY_POOL_DEFINE(name, min_size, max_size, n_max) \ |
| 1205 | _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \ |
| 1206 | _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \ |
| 1207 | _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max); \ |
| 1208 | __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \ |
| 1209 | STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \ |
| 1210 | extern struct k_mem_pool name |
| 1211 | |
| 1212 | /* |
| 1213 | * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block) |
| 1214 | * to __memory_pool_quad_block_size absolute symbol. |
| 1215 | * This function does not get called, but compiler calculates the value and |
| 1216 | * assigns it to the absolute symbol, that, in turn is used by assembler macros. |
| 1217 | */ |
| 1218 | static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void) |
| 1219 | { |
| 1220 | __asm__(".globl __memory_pool_quad_block_size\n\t" |
| 1221 | "__memory_pool_quad_block_size = %c0\n\t" |
| 1222 | : |
| 1223 | : "n"(sizeof(struct k_mem_pool_quad_block))); |
| 1224 | } |
| 1225 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1226 | #define K_MEM_POOL_SIZE(max_block_size, num_max_blocks) \ |
| 1227 | (sizeof(struct k_mem_pool) + ((max_block_size) * (num_max_blocks))) |
| 1228 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1229 | extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1230 | int size, int32_t timeout); |
| 1231 | extern void k_mem_pool_free(struct k_mem_block *block); |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 1232 | extern void k_mem_pool_defrag(struct k_mem_pool *pool); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1233 | extern void *k_malloc(uint32_t size); |
| 1234 | extern void k_free(void *p); |
| 1235 | |
| 1236 | /* |
| 1237 | * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to |
| 1238 | * hook into the device subsystem, which itself uses nanokernel semaphores, |
| 1239 | * and thus currently requires the definition of nano_sem. |
| 1240 | */ |
| 1241 | #include <legacy.h> |
| 1242 | #include <arch/cpu.h> |
| 1243 | |
| 1244 | /* |
| 1245 | * private APIs that are utilized by one or more public APIs |
| 1246 | */ |
| 1247 | |
| 1248 | extern struct k_thread_static_init _k_task_list_start[]; |
| 1249 | extern struct k_thread_static_init _k_task_list_end[]; |
| 1250 | |
| 1251 | #define _FOREACH_STATIC_THREAD(thread_init) \ |
| 1252 | for (struct k_thread_static_init *thread_init = _k_task_list_start; \ |
| 1253 | thread_init < _k_task_list_end; thread_init++) |
| 1254 | |
| 1255 | extern int _is_thread_essential(void); |
| 1256 | static inline int is_in_any_group(struct k_thread_static_init *thread_init, |
| 1257 | uint32_t groups) |
| 1258 | { |
| 1259 | return !!(thread_init->init_groups & groups); |
| 1260 | } |
| 1261 | extern void _init_static_threads(void); |
| 1262 | |
| 1263 | #ifdef __cplusplus |
| 1264 | } |
| 1265 | #endif |
| 1266 | |
| 1267 | #endif /* _kernel__h_ */ |