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 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 55 | #if CONFIG_NUM_COOP_PRIORITIES > 0 |
| 56 | #define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES) |
| 57 | #else |
| 58 | #define K_HIGHEST_THREAD_PRIO 0 |
| 59 | #endif |
| 60 | |
| 61 | #if CONFIG_NUM_PREEMPT_PRIORITIES > 0 |
| 62 | #define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES |
| 63 | #else |
| 64 | #define K_LOWEST_THREAD_PRIO -1 |
| 65 | #endif |
| 66 | |
| 67 | #define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO) |
| 68 | #define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1) |
| 69 | |
| 70 | typedef sys_dlist_t _wait_q_t; |
| 71 | |
| 72 | #ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS |
| 73 | #define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next |
| 74 | #define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL, |
| 75 | #else |
| 76 | #define _DEBUG_TRACING_KERNEL_OBJECTS_INIT |
| 77 | #define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) |
| 78 | #endif |
| 79 | |
| 80 | #define k_thread tcs |
| 81 | struct tcs; |
| 82 | struct k_mutex; |
| 83 | struct k_sem; |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 84 | struct k_alert; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 85 | struct k_msgq; |
| 86 | struct k_mbox; |
| 87 | struct k_pipe; |
| 88 | struct k_fifo; |
| 89 | struct k_lifo; |
| 90 | struct k_stack; |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 91 | struct k_mem_slab; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 92 | struct k_mem_pool; |
| 93 | struct k_timer; |
| 94 | |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 95 | typedef struct k_thread *k_tid_t; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 96 | |
| 97 | /* threads/scheduler/execution contexts */ |
| 98 | |
| 99 | enum execution_context_types { |
| 100 | K_ISR = 0, |
| 101 | K_COOP_THREAD, |
| 102 | K_PREEMPT_THREAD, |
| 103 | }; |
| 104 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 105 | typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * @brief Initialize and start a thread with an optional delay |
| 109 | * |
| 110 | * This routine initializes a thread and optionally delays its execution. |
| 111 | * It is not ISR-callable. |
| 112 | * |
| 113 | * If a thread of priority higher than the current thread is spawned, and the |
| 114 | * current thread id preemptible, the current thread is preempted by the new |
| 115 | * thread. |
| 116 | * |
| 117 | * @param stack Pointer to the stack space. |
| 118 | * @param stack_size Stack size in bytes. |
| 119 | * @param entry Thread entry function. |
| 120 | * @param p1 1st entry point parameter. |
| 121 | * @param p2 2nd entry point parameter. |
| 122 | * @param p3 3rd entry point parameter. |
| 123 | * @param prio The thread's priority. |
| 124 | * @param options Not used currently. |
| 125 | * @param delay Duration of execution delay in milliseconds |
| 126 | * |
| 127 | * @return Kernel thread identifier |
| 128 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 129 | extern k_tid_t k_thread_spawn(char *stack, unsigned stack_size, |
| 130 | void (*entry)(void *, void *, void*), |
| 131 | void *p1, void *p2, void *p3, |
| 132 | int32_t prio, uint32_t options, int32_t delay); |
| 133 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 134 | /** |
| 135 | * @brief Put the current thread to sleep |
| 136 | * |
| 137 | * This routine puts the currently thread to sleep for the specified |
| 138 | * number of milliseconds. |
| 139 | * |
| 140 | * @param duration Number of milliseconds the thread is to sleep |
| 141 | * |
| 142 | * @return N/A |
| 143 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 144 | extern void k_sleep(int32_t duration); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * @brief Cause the current thread to busy wait |
| 148 | * |
| 149 | * This routine causes the current thread to execute a "do nothing" loop for |
| 150 | * a specified period of microseconds. |
| 151 | * |
| 152 | * @warning This routine utilizes the system clock, so it must not be invoked |
| 153 | * until the system clock is fully operational or while interrupts are |
| 154 | * locked. |
| 155 | * |
| 156 | * @return N/A |
| 157 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 158 | extern void k_busy_wait(uint32_t usec_to_wait); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * @brief Yield the current thread |
| 162 | * |
| 163 | * Calling this routine results in the current thread yielding to another |
| 164 | * thread of the same or higher priority. If there are no other ready threads |
| 165 | * of the same or higher priority, the routine will return immediately. |
| 166 | * |
| 167 | * @return N/A |
| 168 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 169 | extern void k_yield(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 170 | |
| 171 | /** |
| 172 | * @brief Wake the specified thread from sleep |
| 173 | * |
| 174 | * This routine wakes the thread specified by @a thread from its sleep. |
| 175 | * |
| 176 | * @param thread Identifies thread to wake |
| 177 | * |
| 178 | * @return N/A |
| 179 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 180 | extern void k_wakeup(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * @brief Obtain the thread ID of the currently executing thread |
| 184 | * |
| 185 | * @return Current thread ID |
| 186 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 187 | extern k_tid_t k_current_get(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * @brief Cancel a delayed thread start |
| 191 | * |
| 192 | * @param thread Delayed thread ID |
| 193 | * |
| 194 | * @retval 0 on success |
| 195 | * @retval -EINVAL Thread has already started or not delayed |
| 196 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 197 | extern int k_thread_cancel(k_tid_t thread); |
| 198 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 199 | /** |
| 200 | * @brief Abort a thread |
| 201 | * |
| 202 | * Execution of @a thread is immediately permanently cancelled. @a thread is |
| 203 | * taken off the ready queue if ready, or out of any wait queues and/or |
| 204 | * timeout queues it might be currently queued on. However, objects it might |
| 205 | * currently owned, such as mutexes, are not released. It is up to the |
| 206 | * subsystems managing the objects to handle this. |
| 207 | * |
| 208 | * @param thread Thread to abort |
| 209 | * |
| 210 | * @return N/A |
| 211 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 212 | extern void k_thread_abort(k_tid_t thread); |
| 213 | |
| 214 | #define K_THREAD_GROUP_EXE 0x1 |
| 215 | #define K_THREAD_GROUP_SYS 0x2 |
| 216 | #define K_THREAD_GROUP_FPU 0x4 |
| 217 | |
| 218 | /* XXX - doesn't work because CONFIG_ARCH is a string */ |
| 219 | #if 0 |
| 220 | /* arch-specific groups */ |
| 221 | #if CONFIG_ARCH == "x86" |
| 222 | #define K_THREAD_GROUP_SSE 0x4 |
| 223 | #endif |
| 224 | #endif |
| 225 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 226 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 227 | #define _THREAD_TIMEOUT_INIT(obj) \ |
| 228 | (obj).nano_timeout = { \ |
| 229 | .node = { {0}, {0} }, \ |
Benjamin Walsh | 055262c | 2016-10-05 17:16:01 -0400 | [diff] [blame] | 230 | .thread = NULL, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 231 | .wait_q = NULL, \ |
| 232 | .delta_ticks_from_prev = -1, \ |
| 233 | }, |
| 234 | #else |
| 235 | #define _THREAD_TIMEOUT_INIT(obj) |
| 236 | #endif |
| 237 | |
| 238 | #ifdef CONFIG_ERRNO |
| 239 | #define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0, |
| 240 | #else |
| 241 | #define _THREAD_ERRNO_INIT(obj) |
| 242 | #endif |
| 243 | |
Peter Mitsis | a04c0d7 | 2016-09-28 19:26:00 -0400 | [diff] [blame] | 244 | struct _static_thread_data { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 245 | union { |
| 246 | char *init_stack; |
| 247 | struct k_thread *thread; |
| 248 | }; |
| 249 | unsigned int init_stack_size; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 250 | void (*init_entry)(void *, void *, void *); |
| 251 | void *init_p1; |
| 252 | void *init_p2; |
| 253 | void *init_p3; |
| 254 | int init_prio; |
| 255 | uint32_t init_options; |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 256 | int32_t init_delay; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 257 | void (*init_abort)(void); |
| 258 | uint32_t init_groups; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 259 | }; |
| 260 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 261 | #define _THREAD_INITIALIZER(stack, stack_size, \ |
| 262 | entry, p1, p2, p3, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 263 | prio, options, delay, abort, groups) \ |
| 264 | { \ |
| 265 | .init_stack = (stack), \ |
| 266 | .init_stack_size = (stack_size), \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 267 | .init_entry = (void (*)(void *, void *, void *))entry, \ |
| 268 | .init_p1 = (void *)p1, \ |
| 269 | .init_p2 = (void *)p2, \ |
| 270 | .init_p3 = (void *)p3, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 271 | .init_prio = (prio), \ |
| 272 | .init_options = (options), \ |
| 273 | .init_delay = (delay), \ |
| 274 | .init_abort = (abort), \ |
| 275 | .init_groups = (groups), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 276 | } |
| 277 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 278 | /** |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 279 | * @brief Define a static thread. |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 280 | * |
| 281 | * @internal It has been observed that the x86 compiler by default aligns |
| 282 | * these _static_thread_data structures to 32-byte boundaries, thereby |
| 283 | * wasting space. To work around this, force a 4-byte alignment. |
| 284 | */ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 285 | #define K_THREAD_DEFINE(name, stack_size, \ |
| 286 | entry, p1, p2, p3, \ |
| 287 | prio, options, delay) \ |
| 288 | char __noinit __stack _k_thread_obj_##name[stack_size]; \ |
| 289 | struct _static_thread_data _k_thread_data_##name __aligned(4) \ |
| 290 | __in_section(_k_task_list, private, task) = \ |
| 291 | _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \ |
| 292 | entry, p1, p2, p3, prio, options, delay, \ |
Allan Stephens | 8809502 | 2016-10-26 14:15:08 -0500 | [diff] [blame] | 293 | NULL, 0); \ |
| 294 | const k_tid_t name = (k_tid_t)_k_thread_obj_##name |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 295 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 296 | /** |
| 297 | * @brief Get a thread's priority |
| 298 | * |
| 299 | * @param thread ID of thread to query |
| 300 | * |
| 301 | * @return Specified thread's priority |
| 302 | */ |
Allan Stephens | 399d0ad | 2016-10-07 13:41:34 -0500 | [diff] [blame] | 303 | extern int k_thread_priority_get(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 304 | |
| 305 | /** |
| 306 | * @brief Set the priority of a thread |
| 307 | * |
| 308 | * This routine immediately changes the priority of the specified thread. |
| 309 | * |
| 310 | * Rescheduling can occur immediately depending on the priority @a thread is |
| 311 | * set to: |
| 312 | * |
| 313 | * - If its priority is raised above the priority of the caller of this |
| 314 | * function, and the caller is preemptible, @a thread will be scheduled in. |
| 315 | * |
| 316 | * - If the caller operates on itself, it lowers its priority below that of |
| 317 | * other threads in the system, and the caller is preemptible, the thread of |
| 318 | * highest priority will be scheduled in. |
| 319 | * |
| 320 | * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to |
| 321 | * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the |
| 322 | * highest priority. |
| 323 | * |
| 324 | * @param thread Thread whose priority is to be set. |
| 325 | * @param prio New priority. |
| 326 | * |
| 327 | * @warning Changing the priority of a thread currently involved in mutex |
| 328 | * priority inheritance may result in undefined behavior. |
| 329 | * |
| 330 | * @return N/A |
| 331 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 332 | extern void k_thread_priority_set(k_tid_t thread, int prio); |
| 333 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 334 | /** |
| 335 | * @brief Suspend a thread |
| 336 | * |
| 337 | * Remove @a thread from scheduling decisions. All other internal operations |
| 338 | * on @a thread will still be performed: any timeout it is on keeps ticking |
| 339 | * and delivered upon expiry, objects it is waiting on are still handed to it, |
| 340 | * etc. |
| 341 | * |
| 342 | * @param thread Thread to suspend |
| 343 | * |
| 344 | * @return N/A |
| 345 | */ |
Benjamin Walsh | 71d5228 | 2016-09-29 10:49:48 -0400 | [diff] [blame] | 346 | extern void k_thread_suspend(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 347 | |
| 348 | /** |
| 349 | * @brief Resume a previously suspended thread |
| 350 | * |
| 351 | * Resume using @a thread in scheduling decisions. |
| 352 | * |
| 353 | * @param thread Thread to resume |
| 354 | * |
| 355 | * @return N/A |
| 356 | */ |
Benjamin Walsh | 71d5228 | 2016-09-29 10:49:48 -0400 | [diff] [blame] | 357 | extern void k_thread_resume(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 358 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 359 | /** |
| 360 | * @brief Set time-slicing period and scope |
| 361 | * |
| 362 | * This routine controls how thread time slicing is performed by the scheduler |
| 363 | * on preemptible threads; it specifes the maximum time slice length (in |
| 364 | * milliseconds) and the highest thread priority level for which time slicing |
| 365 | * is performed. |
| 366 | * |
| 367 | * To enable time slicing, a non-zero time slice length must be specified. |
| 368 | * The scheduler then ensures that no executing thread runs for more than the |
| 369 | * specified number of milliseconds before giving other threads of that priority |
| 370 | * a chance to execute. (However, any thread whose priority is higher than the |
| 371 | * specified thread priority level is exempted, and may execute as long as |
| 372 | * desired without being pre-empted due to time slicing.) |
| 373 | * |
| 374 | * Time slicing limits only the maximum amount of time a thread may continuously |
| 375 | * execute. Once the scheduler selects a thread for execution, there is no |
| 376 | * minimum guaranteed time the thread will execute before threads of greater or |
| 377 | * equal priority are scheduled. |
| 378 | * |
| 379 | * When the currently-executing thread is the only one of that priority eligible |
| 380 | * for execution, this routine has no effect; the thread is immediately |
| 381 | * rescheduled after the slice period expires. |
| 382 | * |
| 383 | * To disable timeslicing, call the API with both parameters set to zero. |
| 384 | * |
| 385 | * @return N/A |
| 386 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 387 | extern void k_sched_time_slice_set(int32_t slice, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 388 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 389 | /** |
| 390 | * @brief Determine if code is running at interrupt level |
| 391 | * |
| 392 | * @return 0 if invoked by a thread, or non-zero if invoked by an ISR |
| 393 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 394 | extern int k_am_in_isr(void); |
| 395 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 396 | /** |
| 397 | * @brief Set thread's custom data |
| 398 | * |
| 399 | * This routine sets the custom data value for the current thread. Custom |
| 400 | * data is not used by the kernel itself, and is freely available for the |
| 401 | * thread to use as it sees fit. |
| 402 | * |
| 403 | * This provides a skeleton upon which to build thread-local storage. |
| 404 | * |
| 405 | * @param value New value to set the thread's custom data to. |
| 406 | * |
| 407 | * @return N/A |
| 408 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 409 | extern void k_thread_custom_data_set(void *value); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 410 | |
| 411 | /** |
| 412 | * @brief Get thread's custom data |
| 413 | * |
| 414 | * This function returns the custom data value for the current thread. |
| 415 | * |
| 416 | * @return current custom data value |
| 417 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 418 | extern void *k_thread_custom_data_get(void); |
| 419 | |
| 420 | /** |
| 421 | * kernel timing |
| 422 | */ |
| 423 | |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 424 | #include <sys_clock.h> |
| 425 | |
| 426 | /* private internal time manipulation (users should never play with ticks) */ |
| 427 | |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 428 | /* added tick needed to account for tick in progress */ |
| 429 | #define _TICK_ALIGN 1 |
| 430 | |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 431 | static int64_t __ticks_to_ms(int64_t ticks) |
| 432 | { |
Benjamin Walsh | 57d55dc | 2016-10-04 16:58:08 -0400 | [diff] [blame] | 433 | #if CONFIG_SYS_CLOCK_EXISTS |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 434 | return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec; |
Benjamin Walsh | 57d55dc | 2016-10-04 16:58:08 -0400 | [diff] [blame] | 435 | #else |
| 436 | __ASSERT(ticks == 0, ""); |
| 437 | return 0; |
| 438 | #endif |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 442 | /* timeouts */ |
| 443 | |
| 444 | struct _timeout; |
| 445 | typedef void (*_timeout_func_t)(struct _timeout *t); |
| 446 | |
| 447 | struct _timeout { |
| 448 | sys_dlist_t node; |
Benjamin Walsh | 055262c | 2016-10-05 17:16:01 -0400 | [diff] [blame] | 449 | struct k_thread *thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 450 | sys_dlist_t *wait_q; |
| 451 | int32_t delta_ticks_from_prev; |
| 452 | _timeout_func_t func; |
| 453 | }; |
| 454 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 455 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 456 | /* timers */ |
| 457 | |
| 458 | struct k_timer { |
| 459 | /* |
| 460 | * _timeout structure must be first here if we want to use |
| 461 | * dynamic timer allocation. timeout.node is used in the double-linked |
| 462 | * list of free timers |
| 463 | */ |
| 464 | struct _timeout timeout; |
| 465 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 466 | /* wait queue for the (single) thread waiting on this timer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 467 | _wait_q_t wait_q; |
| 468 | |
| 469 | /* runs in ISR context */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 470 | void (*expiry_fn)(struct k_timer *); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 471 | |
| 472 | /* runs in the context of the thread that calls k_timer_stop() */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 473 | void (*stop_fn)(struct k_timer *); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 474 | |
| 475 | /* timer period */ |
| 476 | int32_t period; |
| 477 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 478 | /* timer status */ |
| 479 | uint32_t status; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 480 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 481 | /* used to support legacy timer APIs */ |
| 482 | void *_legacy_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 483 | |
| 484 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer); |
| 485 | }; |
| 486 | |
| 487 | #define K_TIMER_INITIALIZER(obj) \ |
| 488 | { \ |
| 489 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 490 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 491 | } |
| 492 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 493 | /** |
| 494 | * @brief Statically define a timer and initialize it |
| 495 | * |
| 496 | * If the timer is to be accessed outside the module where it is defined, it |
| 497 | * can be declared via |
| 498 | * |
| 499 | * extern struct k_timer @a name; |
| 500 | * |
| 501 | * @param name Name of the timer variable. |
| 502 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 503 | #define K_TIMER_DEFINE(name) \ |
| 504 | struct k_timer name = K_TIMER_INITIALIZER(name) |
| 505 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 506 | /** |
| 507 | * @brief Initialize a timer. |
| 508 | * |
| 509 | * This routine must be called before the timer is used. |
| 510 | * |
| 511 | * @param timer Address of timer. |
| 512 | * @param expiry_fn Function to invoke each time timer expires. |
| 513 | * @param stop_fn Function to invoke if timer is stopped while running. |
| 514 | * |
| 515 | * @return N/A |
| 516 | */ |
| 517 | extern void k_timer_init(struct k_timer *timer, |
| 518 | void (*expiry_fn)(struct k_timer *), |
| 519 | void (*stop_fn)(struct k_timer *)); |
Andy Ross | 8d8b2ac | 2016-09-23 10:08:54 -0700 | [diff] [blame] | 520 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 521 | /** |
| 522 | * @brief Start a timer. |
| 523 | * |
| 524 | * This routine starts a timer, and resets its status to zero. The timer |
| 525 | * begins counting down using the specified duration and period values. |
| 526 | * |
| 527 | * Attempting to start a timer that is already running is permitted. |
| 528 | * The timer's status is reset to zero and the timer begins counting down |
| 529 | * using the new duration and period values. |
| 530 | * |
| 531 | * @param timer Address of timer. |
| 532 | * @param duration Initial timer duration (in milliseconds). |
| 533 | * @param period Timer period (in milliseconds). |
| 534 | * |
| 535 | * @return N/A |
| 536 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 537 | extern void k_timer_start(struct k_timer *timer, |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 538 | int32_t duration, int32_t period); |
| 539 | |
| 540 | /** |
| 541 | * @brief Stop a timer. |
| 542 | * |
| 543 | * This routine stops a running timer prematurely. The timer's stop function, |
| 544 | * if one exists, is invoked by the caller. |
| 545 | * |
| 546 | * Attempting to stop a timer that is not running is permitted, but has no |
| 547 | * effect on the timer since it is already stopped. |
| 548 | * |
| 549 | * @param timer Address of timer. |
| 550 | * |
| 551 | * @return N/A |
| 552 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 553 | extern void k_timer_stop(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 554 | |
| 555 | /** |
| 556 | * @brief Read timer status. |
| 557 | * |
| 558 | * This routine reads the timer's status, which indicates the number of times |
| 559 | * it has expired since its status was last read. |
| 560 | * |
| 561 | * Calling this routine resets the timer's status to zero. |
| 562 | * |
| 563 | * @param timer Address of timer. |
| 564 | * |
| 565 | * @return Timer status. |
| 566 | */ |
| 567 | extern uint32_t k_timer_status_get(struct k_timer *timer); |
| 568 | |
| 569 | /** |
| 570 | * @brief Synchronize thread to timer expiration. |
| 571 | * |
| 572 | * This routine blocks the calling thread until the timer's status is non-zero |
| 573 | * (indicating that it has expired at least once since it was last examined) |
| 574 | * or the timer is stopped. If the timer status is already non-zero, |
| 575 | * or the timer is already stopped, the caller continues without waiting. |
| 576 | * |
| 577 | * Calling this routine resets the timer's status to zero. |
| 578 | * |
| 579 | * This routine must not be used by interrupt handlers, since they are not |
| 580 | * allowed to block. |
| 581 | * |
| 582 | * @param timer Address of timer. |
| 583 | * |
| 584 | * @return Timer status. |
| 585 | */ |
| 586 | extern uint32_t k_timer_status_sync(struct k_timer *timer); |
| 587 | |
| 588 | /** |
| 589 | * @brief Get timer remaining before next timer expiration. |
| 590 | * |
| 591 | * This routine computes the (approximate) time remaining before a running |
| 592 | * timer next expires. If the timer is not running, it returns zero. |
| 593 | * |
| 594 | * @param timer Address of timer. |
| 595 | * |
| 596 | * @return Remaining time (in milliseconds). |
| 597 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 598 | extern int32_t k_timer_remaining_get(struct k_timer *timer); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 599 | |
| 600 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 601 | /* kernel clocks */ |
| 602 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 603 | /** |
| 604 | * @brief Get the time elapsed since the system booted (uptime) |
| 605 | * |
| 606 | * @return The current uptime of the system in ms |
| 607 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 608 | extern int64_t k_uptime_get(void); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 609 | |
| 610 | /** |
| 611 | * @brief Get the lower 32-bit of time elapsed since the system booted (uptime) |
| 612 | * |
| 613 | * This function is potentially less onerous in both the time it takes to |
| 614 | * execute, the interrupt latency it introduces and the amount of 64-bit math |
| 615 | * it requires than k_uptime_get(), but it only provides an uptime value of |
| 616 | * 32-bits. The user must handle possible rollovers/spillovers. |
| 617 | * |
| 618 | * At a rate of increment of 1000 per second, it rolls over approximately every |
| 619 | * 50 days. |
| 620 | * |
| 621 | * @return The current uptime of the system in ms |
| 622 | */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 623 | extern uint32_t k_uptime_get_32(void); |
| 624 | |
| 625 | /** |
| 626 | * @brief Get the difference between a reference time and the current uptime |
| 627 | * |
| 628 | * @param reftime A pointer to a reference time. It is updated with the current |
| 629 | * uptime upon return. |
| 630 | * |
| 631 | * @return The delta between the reference time and the current uptime. |
| 632 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 633 | extern int64_t k_uptime_delta(int64_t *reftime); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 634 | |
| 635 | /** |
| 636 | * @brief Get the difference between a reference time and the current uptime |
| 637 | * |
| 638 | * The 32-bit version of k_uptime_delta(). It has the same perks and issues as |
| 639 | * k_uptime_get_32(). |
| 640 | * |
| 641 | * @param reftime A pointer to a reference time. It is updated with the current |
| 642 | * uptime upon return. |
| 643 | * |
| 644 | * @return The delta between the reference time and the current uptime. |
| 645 | */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 646 | extern uint32_t k_uptime_delta_32(int64_t *reftime); |
| 647 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 648 | /** |
| 649 | * @brief Read the platform's timer hardware |
| 650 | * |
| 651 | * This routine returns the current time in terms of timer hardware clock |
| 652 | * cycles. |
| 653 | * |
| 654 | * @return up counter of elapsed clock cycles |
| 655 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 656 | extern uint32_t k_cycle_get_32(void); |
| 657 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 658 | /** |
| 659 | * data transfers (basic) |
| 660 | */ |
| 661 | |
| 662 | /* fifos */ |
| 663 | |
| 664 | struct k_fifo { |
| 665 | _wait_q_t wait_q; |
| 666 | sys_slist_t data_q; |
| 667 | |
| 668 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo); |
| 669 | }; |
| 670 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 671 | /** |
| 672 | * @brief Initialize a kernel FIFO object. |
| 673 | * |
| 674 | * This routine initializes a kernel FIFO object structure. It must not be |
| 675 | * called from an ISR. |
| 676 | * |
| 677 | * @param fifo FIFO to initialize. |
| 678 | * |
| 679 | * @return N/A |
| 680 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 681 | extern void k_fifo_init(struct k_fifo *fifo); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 682 | |
| 683 | /** |
| 684 | * @brief Add an element to the end of a FIFO. |
| 685 | * |
| 686 | * This routine adds an element to the end of a FIFO. FIFO data items must be |
| 687 | * aligned on a 4-byte boundary, as the kernel reserves the first 32 bits of |
| 688 | * each item for use as a pointer to the next data item in the FIFO's link |
| 689 | * list. Each data item added to the FIFO must include and reserve these first |
| 690 | * 32 bits. |
| 691 | * |
| 692 | * @param fifo FIFO on which to interact. |
| 693 | * @param data Data to send. |
| 694 | * |
| 695 | * @return N/A |
| 696 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 697 | extern void k_fifo_put(struct k_fifo *fifo, void *data); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 698 | |
| 699 | /** |
| 700 | * @brief Atomically add a list of elements to the end of a FIFO. |
| 701 | * |
| 702 | * This routine adds a list of elements in one shot to the end of a FIFO |
| 703 | * object. If threads are pending on the FIFO object, they become ready to run. |
| 704 | * If this API is called from a preemptible thread, the highest priority one |
| 705 | * will preempt the running thread once the put operation is complete. |
| 706 | * |
| 707 | * If enough threads are waiting on the FIFO, the address of each element given |
| 708 | * to threads is returned to the waiting thread. The remaining elements are |
| 709 | * linked to the end of the list. |
| 710 | * |
| 711 | * The list must be a singly-linked list, where each element only has a pointer |
| 712 | * to the next one. The list must be NULL-terminated. |
| 713 | * |
| 714 | * @param fifo FIFO on which to interact. |
| 715 | * @param head head of singly-linked list |
| 716 | * @param tail tail of singly-linked list |
| 717 | * |
| 718 | * @return N/A |
| 719 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 720 | extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 721 | |
| 722 | /** |
| 723 | * @brief Atomically add a list of elements to the end of a FIFO. |
| 724 | * |
| 725 | * See k_fifo_put_list for the description of the behaviour. |
| 726 | * |
| 727 | * It takes a pointer to a sys_slist_t object instead of the head and tail of |
| 728 | * a custom singly-linked list. The sys_slist_t object is invalid afterwards |
| 729 | * and must be re-initialized via sys_slist_init(). |
| 730 | * |
| 731 | * @param fifo FIFO on which to interact. |
| 732 | * @param list pointer to singly-linked list |
| 733 | * |
| 734 | * @return N/A |
| 735 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 736 | extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 737 | |
| 738 | /** |
| 739 | * @brief Get an element from the head of a FIFO. |
| 740 | * |
| 741 | * If no element is available, the function returns NULL. The first word in |
| 742 | * the element contains invalid data because its memory location was used to |
| 743 | * store a pointer to the next element in the linked list. |
| 744 | * |
| 745 | * @param fifo FIFO on which to interact. |
| 746 | * @param timeout Number of milliseconds to wait for item if FIFO is empty, |
| 747 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 748 | * |
| 749 | * @warning If it is to be called from the context of an ISR, then @a |
| 750 | * timeout must be set to K_NO_WAIT. |
| 751 | * |
| 752 | * @return Pointer to head element in the list when available. |
| 753 | * NULL Otherwise. |
| 754 | * |
| 755 | * @sa K_NO_WAIT, K_FOREVER |
| 756 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 757 | extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout); |
| 758 | |
| 759 | #define K_FIFO_INITIALIZER(obj) \ |
| 760 | { \ |
| 761 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Benjamin Walsh | 9091e5d | 2016-09-30 10:42:47 -0400 | [diff] [blame] | 762 | .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 763 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 764 | } |
| 765 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 766 | /** |
| 767 | * @brief Statically define a FIFO and initialize it |
| 768 | * |
| 769 | * If the FIFO is to be accessed outside the module where it is defined, it |
| 770 | * can be declared via |
| 771 | * |
| 772 | * extern struct k_fifo @a name; |
| 773 | * |
| 774 | * @param name Name of the FIFO variable. |
| 775 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 776 | #define K_FIFO_DEFINE(name) \ |
Benjamin Walsh | 0bee91d | 2016-09-15 17:16:38 -0400 | [diff] [blame] | 777 | struct k_fifo name = K_FIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 778 | |
| 779 | /* lifos */ |
| 780 | |
| 781 | struct k_lifo { |
| 782 | _wait_q_t wait_q; |
| 783 | void *list; |
| 784 | |
| 785 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo); |
| 786 | }; |
| 787 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 788 | /** |
| 789 | * @brief Initialize a kernel linked list LIFO object. |
| 790 | * |
| 791 | * This routine initializes a kernel LIFO object structure. It must not be |
| 792 | * called from an ISR. |
| 793 | * |
| 794 | * @param lifo LIFO to initialize. |
| 795 | * |
| 796 | * @return N/A |
| 797 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 798 | extern void k_lifo_init(struct k_lifo *lifo); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 799 | |
| 800 | /** |
| 801 | * @brief Prepend an element to a LIFO |
| 802 | * |
| 803 | * This routine prepends an element to a LIFO. LIFO data items must be |
| 804 | * aligned on a 4-byte boundary, as the kernel reserves the first 32 bits of |
| 805 | * each item for use as a pointer to the next data item in the LIFO's link |
| 806 | * list. Each data item added to the LIFO must include and reserve these first |
| 807 | * 32 bits. |
| 808 | * |
| 809 | * @param lifo LIFO on which to interact. |
| 810 | * @param data Data to send. |
| 811 | * |
| 812 | * @return N/A |
| 813 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 814 | extern void k_lifo_put(struct k_lifo *lifo, void *data); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 815 | |
| 816 | /** |
| 817 | * @brief Get the first element from a LIFO. |
| 818 | * |
| 819 | * If no element is available, the function returns NULL. The first word in |
| 820 | * the element contains invalid data because its memory location was used to |
| 821 | * store a pointer to the next element in the linked list. |
| 822 | * |
| 823 | * @param lifo LIFO on which to interact. |
| 824 | * @param timeout Number of milliseconds to wait for item if LIFO is empty, |
| 825 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 826 | * |
| 827 | * @warning If it is to be called from the context of an ISR, then @a |
| 828 | * timeout must be set to K_NO_WAIT. |
| 829 | * |
| 830 | * @return Pointer to head element in the list when available. |
| 831 | * NULL Otherwise. |
| 832 | * |
| 833 | * @sa K_NO_WAIT, K_FOREVER |
| 834 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 835 | extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout); |
| 836 | |
| 837 | #define K_LIFO_INITIALIZER(obj) \ |
| 838 | { \ |
| 839 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 840 | .list = NULL, \ |
| 841 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 842 | } |
| 843 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 844 | /** |
| 845 | * @brief Statically define a LIFO and initialize it |
| 846 | * |
| 847 | * If the LIFO is to be accessed outside the module where it is defined, it |
| 848 | * can be declared via |
| 849 | * |
| 850 | * extern struct k_lifo @a name; |
| 851 | * |
| 852 | * @param name Name of the LIFO variable. |
| 853 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 854 | #define K_LIFO_DEFINE(name) \ |
Benjamin Walsh | 0bee91d | 2016-09-15 17:16:38 -0400 | [diff] [blame] | 855 | struct k_lifo name = K_LIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 856 | |
| 857 | /* stacks */ |
| 858 | |
| 859 | struct k_stack { |
| 860 | _wait_q_t wait_q; |
| 861 | uint32_t *base, *next, *top; |
| 862 | |
| 863 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack); |
| 864 | }; |
| 865 | |
Allan Stephens | 018cd9a | 2016-10-07 15:13:24 -0500 | [diff] [blame] | 866 | extern void k_stack_init(struct k_stack *stack, |
| 867 | uint32_t *buffer, int num_entries); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 868 | extern void k_stack_push(struct k_stack *stack, uint32_t data); |
| 869 | extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout); |
| 870 | |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 871 | #define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 872 | { \ |
| 873 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 874 | .base = stack_buffer, \ |
| 875 | .next = stack_buffer, \ |
| 876 | .top = stack_buffer + stack_num_entries, \ |
| 877 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 878 | } |
| 879 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 880 | /** |
| 881 | * @brief Statically define a stack object and initialize it |
| 882 | * |
| 883 | * If the stack is to be accessed outside the module where it is defined, it |
| 884 | * can be declared via |
| 885 | * |
| 886 | * extern struct k_stack @a name; |
| 887 | * |
| 888 | * @param name Name of the stack object variable. |
| 889 | * @param stack_num_entries Number of entries in the stack object |
| 890 | */ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 891 | #define K_STACK_DEFINE(name, stack_num_entries) \ |
| 892 | uint32_t __noinit \ |
| 893 | _k_stack_buf_##name[stack_num_entries]; \ |
| 894 | struct k_stack name = \ |
| 895 | K_STACK_INITIALIZER(name, _k_stack_buf_##name, \ |
| 896 | stack_num_entries) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 897 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 898 | /** |
| 899 | * workqueues |
| 900 | */ |
| 901 | |
| 902 | struct k_work; |
| 903 | |
| 904 | typedef void (*k_work_handler_t)(struct k_work *); |
| 905 | |
| 906 | /** |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 907 | * A workqueue is a thread that executes @ref k_work items that are |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 908 | * queued to it. This is useful for drivers which need to schedule |
| 909 | * execution of code which might sleep from ISR context. The actual |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 910 | * thread identifier is not stored in the structure in order to save |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 911 | * space. |
| 912 | */ |
| 913 | struct k_work_q { |
| 914 | struct k_fifo fifo; |
| 915 | }; |
| 916 | |
| 917 | /** |
| 918 | * @brief Work flags. |
| 919 | */ |
| 920 | enum { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 921 | K_WORK_STATE_PENDING, /* Work item pending state */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 922 | }; |
| 923 | |
| 924 | /** |
| 925 | * @brief An item which can be scheduled on a @ref k_work_q. |
| 926 | */ |
| 927 | struct k_work { |
| 928 | void *_reserved; /* Used by k_fifo implementation. */ |
| 929 | k_work_handler_t handler; |
| 930 | atomic_t flags[1]; |
| 931 | }; |
| 932 | |
| 933 | /** |
| 934 | * @brief Statically initialize work item |
| 935 | */ |
| 936 | #define K_WORK_INITIALIZER(work_handler) \ |
| 937 | { \ |
| 938 | ._reserved = NULL, \ |
| 939 | .handler = work_handler, \ |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 940 | .flags = { 0 } \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /** |
| 944 | * @brief Dynamically initialize work item |
| 945 | */ |
| 946 | static inline void k_work_init(struct k_work *work, k_work_handler_t handler) |
| 947 | { |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 948 | atomic_clear_bit(work->flags, K_WORK_STATE_PENDING); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 949 | work->handler = handler; |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * @brief Submit a work item to a workqueue. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 954 | * |
| 955 | * This procedure schedules a work item to be processed. |
| 956 | * In the case where the work item has already been submitted and is pending |
| 957 | * execution, calling this function will result in a no-op. In this case, the |
| 958 | * work item must not be modified externally (e.g. by the caller of this |
| 959 | * function), since that could cause the work item to be processed in a |
| 960 | * corrupted state. |
| 961 | * |
| 962 | * @param work_q to schedule the work item |
| 963 | * @param work work item |
| 964 | * |
| 965 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 966 | */ |
| 967 | static inline void k_work_submit_to_queue(struct k_work_q *work_q, |
| 968 | struct k_work *work) |
| 969 | { |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 970 | if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 971 | k_fifo_put(&work_q->fifo, work); |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | /** |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 976 | * @brief Check if work item is pending. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 977 | * |
| 978 | * @param work Work item to query |
| 979 | * |
| 980 | * @return K_WORK_STATE_PENDING if pending, 0 if not |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 981 | */ |
| 982 | static inline int k_work_pending(struct k_work *work) |
| 983 | { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 984 | return atomic_test_bit(work->flags, K_WORK_STATE_PENDING); |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | /** |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 988 | * @brief Start a new workqueue. |
| 989 | * |
| 990 | * This routine must not be called from an ISR. |
| 991 | * |
| 992 | * @param work_q Pointer to Work queue |
| 993 | * @param stack Pointer to work queue thread's stack |
| 994 | * @param stack_size Size of the work queue thread's stack |
| 995 | * @param prio Priority of the work queue's thread |
| 996 | * |
| 997 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 998 | */ |
Allan Stephens | 904cf97 | 2016-10-07 13:59:23 -0500 | [diff] [blame] | 999 | extern void k_work_q_start(struct k_work_q *work_q, char *stack, |
| 1000 | unsigned stack_size, unsigned prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1001 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 1002 | #if defined(CONFIG_SYS_CLOCK_EXISTS) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1003 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1004 | /** |
| 1005 | * @brief An item which can be scheduled on a @ref k_work_q with a delay |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1006 | */ |
| 1007 | struct k_delayed_work { |
| 1008 | struct k_work work; |
| 1009 | struct _timeout timeout; |
| 1010 | struct k_work_q *work_q; |
| 1011 | }; |
| 1012 | |
| 1013 | /** |
| 1014 | * @brief Initialize delayed work |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1015 | * |
| 1016 | * Initialize a delayed work item. |
| 1017 | * |
| 1018 | * @param work Delayed work item |
| 1019 | * @param handler Routine invoked when processing delayed work item |
| 1020 | * |
| 1021 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1022 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1023 | extern void k_delayed_work_init(struct k_delayed_work *work, |
| 1024 | k_work_handler_t handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1025 | |
| 1026 | /** |
| 1027 | * @brief Submit a delayed work item to a workqueue. |
| 1028 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1029 | * This routine schedules a work item to be processed after a delay. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1030 | * Once the delay has passed, the work item is submitted to the work queue: |
| 1031 | * at this point, it is no longer possible to cancel it. Once the work item's |
| 1032 | * handler is about to be executed, the work is considered complete and can be |
| 1033 | * resubmitted. |
| 1034 | * |
| 1035 | * Care must be taken if the handler blocks or yield as there is no implicit |
| 1036 | * mutual exclusion mechanism. Such usage is not recommended and if necessary, |
| 1037 | * it should be explicitly done between the submitter and the handler. |
| 1038 | * |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1039 | * @param work_q Workqueue to schedule the work item |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1040 | * @param work Delayed work item |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1041 | * @param delay Delay before scheduling the work item (in milliseconds) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1042 | * |
| 1043 | * @return 0 in case of success or negative value in case of error. |
| 1044 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1045 | extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q, |
| 1046 | struct k_delayed_work *work, |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1047 | int32_t delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1048 | |
| 1049 | /** |
| 1050 | * @brief Cancel a delayed work item |
| 1051 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1052 | * This routine cancels a scheduled work item. If the work has been completed |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1053 | * or is idle, this will do nothing. The only case where this can fail is when |
| 1054 | * the work has been submitted to the work queue, but the handler has not run |
| 1055 | * yet. |
| 1056 | * |
| 1057 | * @param work Delayed work item to be canceled |
| 1058 | * |
| 1059 | * @return 0 in case of success or negative value in case of error. |
| 1060 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1061 | extern int k_delayed_work_cancel(struct k_delayed_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1062 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 1063 | #endif /* CONFIG_SYS_CLOCK_EXISTS */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1064 | |
| 1065 | #if defined(CONFIG_SYSTEM_WORKQUEUE) |
| 1066 | |
| 1067 | extern struct k_work_q k_sys_work_q; |
| 1068 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1069 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1070 | * @brief Submit a work item to the system workqueue. |
| 1071 | * |
| 1072 | * @ref k_work_submit_to_queue |
| 1073 | * |
| 1074 | * When using the system workqueue it is not recommended to block or yield |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1075 | * on the handler since its thread is shared system wide it may cause |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1076 | * unexpected behavior. |
| 1077 | */ |
| 1078 | static inline void k_work_submit(struct k_work *work) |
| 1079 | { |
| 1080 | k_work_submit_to_queue(&k_sys_work_q, work); |
| 1081 | } |
| 1082 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 1083 | #if defined(CONFIG_SYS_CLOCK_EXISTS) |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1084 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1085 | * @brief Submit a delayed work item to the system workqueue. |
| 1086 | * |
| 1087 | * @ref k_delayed_work_submit_to_queue |
| 1088 | * |
| 1089 | * When using the system workqueue it is not recommended to block or yield |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1090 | * on the handler since its thread is shared system wide it may cause |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1091 | * unexpected behavior. |
| 1092 | */ |
| 1093 | static inline int k_delayed_work_submit(struct k_delayed_work *work, |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1094 | int32_t delay) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1095 | { |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1096 | return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1097 | } |
| 1098 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 1099 | #endif /* CONFIG_SYS_CLOCK_EXISTS */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1100 | #endif /* CONFIG_SYSTEM_WORKQUEUE */ |
| 1101 | |
| 1102 | /** |
| 1103 | * synchronization |
| 1104 | */ |
| 1105 | |
| 1106 | /* mutexes */ |
| 1107 | |
| 1108 | struct k_mutex { |
| 1109 | _wait_q_t wait_q; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 1110 | struct k_thread *owner; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1111 | uint32_t lock_count; |
| 1112 | int owner_orig_prio; |
| 1113 | #ifdef CONFIG_OBJECT_MONITOR |
| 1114 | int num_lock_state_changes; |
| 1115 | int num_conflicts; |
| 1116 | #endif |
| 1117 | |
| 1118 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex); |
| 1119 | }; |
| 1120 | |
| 1121 | #ifdef CONFIG_OBJECT_MONITOR |
| 1122 | #define _MUTEX_INIT_OBJECT_MONITOR \ |
| 1123 | .num_lock_state_changes = 0, .num_conflicts = 0, |
| 1124 | #else |
| 1125 | #define _MUTEX_INIT_OBJECT_MONITOR |
| 1126 | #endif |
| 1127 | |
| 1128 | #define K_MUTEX_INITIALIZER(obj) \ |
| 1129 | { \ |
| 1130 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1131 | .owner = NULL, \ |
| 1132 | .lock_count = 0, \ |
| 1133 | .owner_orig_prio = K_LOWEST_THREAD_PRIO, \ |
| 1134 | _MUTEX_INIT_OBJECT_MONITOR \ |
| 1135 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1136 | } |
| 1137 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1138 | /** |
| 1139 | * @brief Statically define a mutex object and initialize it |
| 1140 | * |
| 1141 | * If the mutex is to be accessed outside the module where it is defined, it |
| 1142 | * can be declared via |
| 1143 | * |
| 1144 | * extern struct k_mutex @a name; |
| 1145 | * |
| 1146 | * @param name Name of the mutex object variable. |
| 1147 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1148 | #define K_MUTEX_DEFINE(name) \ |
| 1149 | struct k_mutex name = K_MUTEX_INITIALIZER(name) |
| 1150 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1151 | /** |
| 1152 | * @brief Initialize a mutex |
| 1153 | * |
| 1154 | * Upon initialization, the mutex is available and does not have an owner. |
| 1155 | * |
| 1156 | * @param mutex Mutex to initialize |
| 1157 | * |
| 1158 | * @return N/A |
| 1159 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1160 | extern void k_mutex_init(struct k_mutex *mutex); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1161 | |
| 1162 | /** |
| 1163 | * @brief Lock a mutex |
| 1164 | * |
| 1165 | * This routine locks mutex @a mutex. When the mutex is locked by another |
| 1166 | * thread, the thread calling this function will either wait until the mutex |
| 1167 | * becomes available, or until a specified timeout expires. |
| 1168 | * |
| 1169 | * A thread is permitted to lock a mutex it has already locked; in such a case, |
| 1170 | * this routine immediately succeeds and the lock count is increased by 1. |
| 1171 | * |
| 1172 | * @param mutex Pointer to a mutex object. |
| 1173 | * @param timeout Number of milliseconds to wait if mutex is unavailable, |
| 1174 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1175 | * |
| 1176 | * @retval 0 When semaphore is obtained successfully. |
| 1177 | * @retval -EBUSY Failed to immediately lock mutex when @a timeout is K_NO_WAIT. |
| 1178 | * @retval -EAGAIN When timeout expires. |
| 1179 | * |
| 1180 | * @sa K_NO_WAIT, K_FOREVER |
| 1181 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1182 | extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1183 | |
| 1184 | /** |
| 1185 | * @brief Unlock a mutex |
| 1186 | * |
| 1187 | * This routine unlocks mutex @a mutex. The mutex must already be locked by the |
| 1188 | * requesting thread. |
| 1189 | * |
| 1190 | * The mutex cannot be claimed by another thread until it has been unlocked by |
| 1191 | * the requesting thread as many times as it was previously locked by that |
| 1192 | * thread. |
| 1193 | * |
| 1194 | * @param mutex Mutex name. |
| 1195 | * |
| 1196 | * @return N/A |
| 1197 | */ |
| 1198 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1199 | extern void k_mutex_unlock(struct k_mutex *mutex); |
| 1200 | |
| 1201 | /* semaphores */ |
| 1202 | |
| 1203 | struct k_sem { |
| 1204 | _wait_q_t wait_q; |
| 1205 | unsigned int count; |
| 1206 | unsigned int limit; |
| 1207 | |
| 1208 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem); |
| 1209 | }; |
| 1210 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1211 | /** |
| 1212 | * @brief Initialize a semaphore object. |
| 1213 | * |
| 1214 | * An initial count and a count limit can be specified. The count will never go |
| 1215 | * over the count limit if the semaphore is given multiple times without being |
| 1216 | * taken. |
| 1217 | * |
| 1218 | * Cannot be called from ISR. |
| 1219 | * |
| 1220 | * @param sem Pointer to a semaphore object. |
| 1221 | * @param initial_count Initial count. |
| 1222 | * @param limit Highest value the count can take during operation. |
| 1223 | * |
| 1224 | * @return N/A |
| 1225 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1226 | extern void k_sem_init(struct k_sem *sem, unsigned int initial_count, |
| 1227 | unsigned int limit); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1228 | |
| 1229 | /** |
| 1230 | * @brief Take a semaphore, possibly pending if not available. |
| 1231 | * |
| 1232 | * The current execution context tries to obtain the semaphore. If the |
| 1233 | * semaphore is unavailable and a timeout other than K_NO_WAIT is specified, |
| 1234 | * the context will pend. |
| 1235 | * |
| 1236 | * @param sem Pointer to a semaphore object. |
| 1237 | * @param timeout Number of milliseconds to wait if semaphore is unavailable, |
| 1238 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1239 | * |
| 1240 | * @warning If it is called from the context of an ISR, then the only legal |
| 1241 | * value for @a timeout is K_NO_WAIT. |
| 1242 | * |
| 1243 | * @retval 0 When semaphore is obtained successfully. |
| 1244 | * @retval -EAGAIN When timeout expires. |
| 1245 | * @retval -EBUSY When unavailable and the timeout is K_NO_WAIT. |
| 1246 | * |
| 1247 | * @sa K_NO_WAIT, K_FOREVER |
| 1248 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1249 | extern int k_sem_take(struct k_sem *sem, int32_t timeout); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1250 | |
| 1251 | /** |
| 1252 | * @brief Give a semaphore. |
| 1253 | * |
| 1254 | * Increase the semaphore's internal count by 1, up to its limit, if no thread |
| 1255 | * is waiting on the semaphore; otherwise, wake up the first thread in the |
| 1256 | * semaphore's waiting queue. |
| 1257 | * |
| 1258 | * If the latter case, and if the current context is preemptible, the thread |
| 1259 | * that is taken off the wait queue will be scheduled in and will preempt the |
| 1260 | * current thread. |
| 1261 | * |
| 1262 | * @param sem Pointer to a semaphore object. |
| 1263 | * |
| 1264 | * @return N/A |
| 1265 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1266 | extern void k_sem_give(struct k_sem *sem); |
| 1267 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1268 | /** |
| 1269 | * @brief Reset a semaphore's count to zero. |
| 1270 | * |
| 1271 | * The only effect is that the count is set to zero. There is no other |
| 1272 | * side-effect to calling this function. |
| 1273 | * |
| 1274 | * @param sem Pointer to a semaphore object. |
| 1275 | * |
| 1276 | * @return N/A |
| 1277 | */ |
Benjamin Walsh | 70c68b9 | 2016-09-21 10:37:34 -0400 | [diff] [blame] | 1278 | static inline void k_sem_reset(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1279 | { |
| 1280 | sem->count = 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1281 | } |
| 1282 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1283 | /** |
| 1284 | * @brief Get a semaphore's count. |
| 1285 | * |
| 1286 | * Note there is no guarantee the count has not changed by the time this |
| 1287 | * function returns. |
| 1288 | * |
| 1289 | * @param sem Pointer to a semaphore object. |
| 1290 | * |
| 1291 | * @return The current semaphore count. |
| 1292 | */ |
Tomasz Bursztyka | 276086d | 2016-09-21 16:03:21 +0200 | [diff] [blame] | 1293 | static inline unsigned int k_sem_count_get(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1294 | { |
| 1295 | return sem->count; |
| 1296 | } |
| 1297 | |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 1298 | #ifdef CONFIG_SEMAPHORE_GROUPS |
| 1299 | /** |
| 1300 | * @brief Take the first available semaphore |
| 1301 | * |
| 1302 | * Given a list of semaphore pointers, this routine will attempt to take one |
| 1303 | * of them, waiting up to a maximum of @a timeout ms to do so. The taken |
| 1304 | * semaphore is identified by @a sem (set to NULL on error). |
| 1305 | * |
| 1306 | * Be aware that the more semaphores specified in the group, the more stack |
| 1307 | * space is required by the waiting thread. |
| 1308 | * |
| 1309 | * @param sem_array Array of semaphore pointers terminated by a K_END entry |
| 1310 | * @param sem Identifies the semaphore that was taken |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1311 | * @param timeout Number of milliseconds to wait if semaphores are unavailable, |
| 1312 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 1313 | * |
| 1314 | * @retval 0 A semaphore was successfully taken |
| 1315 | * @retval -EBUSY No semaphore was available (@a timeout = K_NO_WAIT) |
| 1316 | * @retval -EAGAIN Time out occurred while waiting for semaphore |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1317 | * |
| 1318 | * @sa K_NO_WAIT, K_FOREVER |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 1319 | */ |
| 1320 | |
| 1321 | extern int k_sem_group_take(struct k_sem *sem_array[], struct k_sem **sem, |
| 1322 | int32_t timeout); |
| 1323 | |
| 1324 | /** |
| 1325 | * @brief Give all the semaphores in the group |
| 1326 | * |
| 1327 | * This routine will give each semaphore in the array of semaphore pointers. |
| 1328 | * |
| 1329 | * @param sem_array Array of semaphore pointers terminated by a K_END entry |
| 1330 | * |
| 1331 | * @return N/A |
| 1332 | */ |
| 1333 | extern void k_sem_group_give(struct k_sem *sem_array[]); |
| 1334 | |
| 1335 | /** |
| 1336 | * @brief Reset the count to zero on each semaphore in the array |
| 1337 | * |
| 1338 | * This routine resets the count of each semaphore in the group to zero. |
| 1339 | * Note that it does NOT have any impact on any thread that might have |
| 1340 | * been previously pending on any of the semaphores. |
| 1341 | * |
| 1342 | * @param sem_array Array of semaphore pointers terminated by a K_END entry |
| 1343 | * |
| 1344 | * @return N/A |
| 1345 | */ |
| 1346 | extern void k_sem_group_reset(struct k_sem *sem_array[]); |
| 1347 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1348 | |
| 1349 | #define K_SEM_INITIALIZER(obj, initial_count, count_limit) \ |
| 1350 | { \ |
| 1351 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1352 | .count = initial_count, \ |
| 1353 | .limit = count_limit, \ |
| 1354 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1355 | } |
| 1356 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1357 | /** |
| 1358 | * @def K_SEM_DEFINE |
| 1359 | * |
| 1360 | * @brief Statically define and initialize a global semaphore. |
| 1361 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1362 | * Create a global semaphore named @a name. It is initialized as if k_sem_init() |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1363 | * was called on it. If the semaphore is to be accessed outside the module |
| 1364 | * where it is defined, it can be declared via |
| 1365 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1366 | * extern struct k_sem @a name; |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1367 | * |
| 1368 | * @param name Name of the semaphore variable. |
| 1369 | * @param initial_count Initial count. |
| 1370 | * @param count_limit Highest value the count can take during operation. |
| 1371 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1372 | #define K_SEM_DEFINE(name, initial_count, count_limit) \ |
| 1373 | struct k_sem name = \ |
| 1374 | K_SEM_INITIALIZER(name, initial_count, count_limit) |
| 1375 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1376 | /* alerts */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1377 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1378 | #define K_ALERT_DEFAULT NULL |
| 1379 | #define K_ALERT_IGNORE ((void *)(-1)) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1380 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1381 | typedef int (*k_alert_handler_t)(struct k_alert *); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1382 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1383 | struct k_alert { |
| 1384 | k_alert_handler_t handler; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1385 | atomic_t send_count; |
| 1386 | struct k_work work_item; |
| 1387 | struct k_sem sem; |
| 1388 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1389 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1390 | }; |
| 1391 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1392 | extern void _alert_deliver(struct k_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1393 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1394 | #define K_ALERT_INITIALIZER(obj, alert_handler) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1395 | { \ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1396 | .handler = (k_alert_handler_t)alert_handler, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1397 | .send_count = ATOMIC_INIT(0), \ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1398 | .work_item = K_WORK_INITIALIZER(_alert_deliver), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1399 | .sem = K_SEM_INITIALIZER(obj.sem, 0, 1), \ |
| 1400 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1401 | } |
| 1402 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1403 | /** |
| 1404 | * @brief Statically define and initialize a global alert |
| 1405 | * |
| 1406 | * Create a global alert named @a name. It is initialized as if k_alert_init() |
| 1407 | * was called on it. If the alert is to be accessed outside the module |
| 1408 | * where it is defined, it can be declared via |
| 1409 | * |
| 1410 | * extern struct k_alert @a name; |
| 1411 | * |
| 1412 | * @param name Alert name |
| 1413 | * @param alert_handler Handler to invoke after the delivery of the alert |
| 1414 | */ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1415 | #define K_ALERT_DEFINE(name, alert_handler) \ |
| 1416 | struct k_alert name \ |
| 1417 | __in_section(_k_event_list, alert, name) = \ |
| 1418 | K_ALERT_INITIALIZER(name, alert_handler) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1419 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1420 | /** |
| 1421 | * @brief Initialize an alert object. |
| 1422 | * |
| 1423 | * This routine initializes a kernel alert object structure. It must not be |
| 1424 | * called from an ISR. |
| 1425 | * |
| 1426 | * @param alert Pointer to the alert object |
| 1427 | * @param handler Routine to invoke after delivery of alert |
| 1428 | * |
| 1429 | * @return N/A |
| 1430 | */ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1431 | extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1432 | |
| 1433 | /** |
| 1434 | * @brief Receive an alert |
| 1435 | * |
| 1436 | * The current execution context tries to receive the alert. If the |
| 1437 | * semaphore is unavailable and a timeout other than K_NO_WAIT is specified, |
| 1438 | * the context will pend. |
| 1439 | * |
| 1440 | * @param alert Pointer to a alert object. |
| 1441 | * @param timeout Number of milliseconds to wait if alert is unavailable, |
| 1442 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1443 | * |
| 1444 | * @warning If it is called from the context of an ISR, then the only legal |
| 1445 | * value for @a timeout is K_NO_WAIT. |
| 1446 | * |
| 1447 | * @retval 0 When alert is received successfully. |
| 1448 | * @retval -EAGAIN When timeout expires. |
| 1449 | * @retval -EBUSY When unavailable and the timeout is K_NO_WAIT. |
| 1450 | * |
| 1451 | * @sa K_NO_WAIT, K_FOREVER |
| 1452 | */ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1453 | extern int k_alert_recv(struct k_alert *alert, int32_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1454 | |
| 1455 | /** |
| 1456 | * @brief Signal an alert |
| 1457 | * |
| 1458 | * This routine signals the specified alert. If an alert handler is installed |
| 1459 | * for that alert, it will run. If no alert handler is installed, any thread |
| 1460 | * waiting on the alert is released. |
| 1461 | * |
| 1462 | * @param alert Alert to signal |
| 1463 | * |
| 1464 | * @return N/A |
| 1465 | */ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1466 | extern void k_alert_send(struct k_alert *alert); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1467 | |
| 1468 | /** |
| 1469 | * data transfers (complex) |
| 1470 | */ |
| 1471 | |
| 1472 | /* message queues */ |
| 1473 | |
| 1474 | struct k_msgq { |
| 1475 | _wait_q_t wait_q; |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1476 | size_t msg_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1477 | uint32_t max_msgs; |
| 1478 | char *buffer_start; |
| 1479 | char *buffer_end; |
| 1480 | char *read_ptr; |
| 1481 | char *write_ptr; |
| 1482 | uint32_t used_msgs; |
| 1483 | |
| 1484 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq); |
| 1485 | }; |
| 1486 | |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1487 | #define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1488 | { \ |
| 1489 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1490 | .max_msgs = q_max_msgs, \ |
| 1491 | .msg_size = q_msg_size, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1492 | .buffer_start = q_buffer, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1493 | .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1494 | .read_ptr = q_buffer, \ |
| 1495 | .write_ptr = q_buffer, \ |
| 1496 | .used_msgs = 0, \ |
| 1497 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1498 | } |
| 1499 | |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1500 | /** |
| 1501 | * @brief Define a message queue |
| 1502 | * |
| 1503 | * This declares and initializes a message queue whose buffer is aligned to |
| 1504 | * a @a q_align -byte boundary. The new message queue can be passed to the |
| 1505 | * kernel's message queue functions. |
| 1506 | * |
| 1507 | * Note that for each of the mesages in the message queue to be aligned to |
| 1508 | * @a q_align bytes, then @a q_msg_size must be a multiple of @a q_align. |
| 1509 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1510 | * If the message queue is to be accessed outside the module where it is |
| 1511 | * defined, it can be declared via |
| 1512 | * |
| 1513 | * extern struct k_msgq @a name; |
| 1514 | * |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1515 | * @param q_name Name of the message queue |
| 1516 | * @param q_msg_size The size in bytes of each message |
| 1517 | * @param q_max_msgs Maximum number of messages the queue can hold |
| 1518 | * @param q_align Alignment of the message queue's buffer (power of 2) |
| 1519 | */ |
| 1520 | #define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \ |
| 1521 | static char __noinit __aligned(q_align) \ |
| 1522 | _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \ |
| 1523 | struct k_msgq q_name = \ |
| 1524 | K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \ |
| 1525 | q_msg_size, q_max_msgs) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1526 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1527 | /** |
| 1528 | * @brief Initialize a message queue. |
| 1529 | * |
| 1530 | * @param q Pointer to the message queue object. |
| 1531 | * @param buffer Pointer to memory area that holds queued messages. |
| 1532 | * @param msg_size Message size, in bytes. |
| 1533 | * @param max_msgs Maximum number of messages that can be queued. |
| 1534 | * |
| 1535 | * @return N/A |
| 1536 | */ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1537 | extern void k_msgq_init(struct k_msgq *q, char *buffer, |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1538 | size_t msg_size, uint32_t max_msgs); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1539 | |
| 1540 | /** |
| 1541 | * @brief Add a message to a message queue. |
| 1542 | * |
| 1543 | * This routine adds an item to the message queue. When the message queue is |
| 1544 | * full, the routine will wait either for space to become available, or until |
| 1545 | * the specified time limit is reached. |
| 1546 | * |
| 1547 | * @param q Pointer to the message queue object. |
| 1548 | * @param data Pointer to message data area. |
| 1549 | * @param timeout Number of milliseconds to wait until space becomes available |
| 1550 | * to add the message into the message queue, or one of the |
| 1551 | * special values K_NO_WAIT and K_FOREVER. |
| 1552 | * |
| 1553 | * @return 0 if successful, -ENOMSG if failed immediately or after queue purge, |
| 1554 | * -EAGAIN if timed out |
| 1555 | * |
| 1556 | * @sa K_NO_WAIT, K_FOREVER |
| 1557 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1558 | extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1559 | |
| 1560 | /** |
| 1561 | * @brief Obtain a message from a message queue. |
| 1562 | * |
| 1563 | * This routine fetches the oldest item from the message queue. When the message |
| 1564 | * queue is found empty, the routine will wait either until an item is added to |
| 1565 | * the message queue or until the specified time limit is reached. |
| 1566 | * |
| 1567 | * @param q Pointer to the message queue object. |
| 1568 | * @param data Pointer to message data area. |
| 1569 | * @param timeout Number of milliseconds to wait to obtain message, or one of |
| 1570 | * the special values K_NO_WAIT and K_FOREVER. |
| 1571 | * |
| 1572 | * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out |
| 1573 | * |
| 1574 | * @sa K_NO_WAIT, K_FOREVER |
| 1575 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1576 | extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1577 | |
| 1578 | /** |
| 1579 | * @brief Purge contents of a message queue. |
| 1580 | * |
| 1581 | * Discards all messages currently in the message queue, and cancels |
| 1582 | * any "add message" operations initiated by waiting threads. |
| 1583 | * |
| 1584 | * @param q Pointer to the message queue object. |
| 1585 | * |
| 1586 | * @return N/A |
| 1587 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1588 | extern void k_msgq_purge(struct k_msgq *q); |
| 1589 | |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 1590 | /** |
| 1591 | * @brief Get the number of unused messages |
| 1592 | * |
| 1593 | * @param q Message queue to query |
| 1594 | * |
| 1595 | * @return Number of unused messages |
| 1596 | */ |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1597 | static inline uint32_t k_msgq_num_free_get(struct k_msgq *q) |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 1598 | { |
| 1599 | return q->max_msgs - q->used_msgs; |
| 1600 | } |
| 1601 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1602 | /** |
| 1603 | * @brief Get the number of used messages |
| 1604 | * |
| 1605 | * @param q Message queue to query |
| 1606 | * |
| 1607 | * @return Number of used messages |
| 1608 | */ |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1609 | static inline uint32_t k_msgq_num_used_get(struct k_msgq *q) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1610 | { |
| 1611 | return q->used_msgs; |
| 1612 | } |
| 1613 | |
| 1614 | struct k_mem_block { |
Peter Mitsis | 0cb65c3 | 2016-09-29 14:07:36 -0400 | [diff] [blame] | 1615 | struct k_mem_pool *pool_id; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1616 | void *addr_in_pool; |
| 1617 | void *data; |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 1618 | size_t req_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1619 | }; |
| 1620 | |
| 1621 | /* mailboxes */ |
| 1622 | |
| 1623 | struct k_mbox_msg { |
| 1624 | /** internal use only - needed for legacy API support */ |
| 1625 | uint32_t _mailbox; |
| 1626 | /** size of message (in bytes) */ |
Peter Mitsis | d93078c | 2016-10-14 12:59:37 -0400 | [diff] [blame] | 1627 | size_t size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1628 | /** application-defined information value */ |
| 1629 | uint32_t info; |
| 1630 | /** sender's message data buffer */ |
| 1631 | void *tx_data; |
| 1632 | /** internal use only - needed for legacy API support */ |
| 1633 | void *_rx_data; |
| 1634 | /** message data block descriptor */ |
| 1635 | struct k_mem_block tx_block; |
| 1636 | /** source thread id */ |
| 1637 | k_tid_t rx_source_thread; |
| 1638 | /** target thread id */ |
| 1639 | k_tid_t tx_target_thread; |
| 1640 | /** internal use only - thread waiting on send (may be a dummy) */ |
| 1641 | k_tid_t _syncing_thread; |
| 1642 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 1643 | /** internal use only - semaphore used during asynchronous send */ |
| 1644 | struct k_sem *_async_sem; |
| 1645 | #endif |
| 1646 | }; |
| 1647 | |
| 1648 | struct k_mbox { |
| 1649 | _wait_q_t tx_msg_queue; |
| 1650 | _wait_q_t rx_msg_queue; |
| 1651 | |
| 1652 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox); |
| 1653 | }; |
| 1654 | |
| 1655 | #define K_MBOX_INITIALIZER(obj) \ |
| 1656 | { \ |
| 1657 | .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \ |
| 1658 | .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \ |
| 1659 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1660 | } |
| 1661 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1662 | /** |
| 1663 | * @brief Define a mailbox |
| 1664 | * |
| 1665 | * This declares and initializes a mailbox. The new mailbox can be passed to |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1666 | * the kernel's mailbox functions. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1667 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1668 | * If the mailbox is to be accessed outside the module where it is defined, it |
| 1669 | * can be declared via |
| 1670 | * |
| 1671 | * extern struct k_mbox @a name; |
| 1672 | * |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1673 | * @param name Name of the mailbox |
| 1674 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1675 | #define K_MBOX_DEFINE(name) \ |
| 1676 | struct k_mbox name = \ |
| 1677 | K_MBOX_INITIALIZER(name) \ |
| 1678 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1679 | /** |
| 1680 | * @brief Initialize a mailbox. |
| 1681 | * |
| 1682 | * @param mbox Pointer to the mailbox object |
| 1683 | * |
| 1684 | * @return N/A |
| 1685 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1686 | extern void k_mbox_init(struct k_mbox *mbox); |
| 1687 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1688 | /** |
| 1689 | * @brief Send a mailbox message in a synchronous manner. |
| 1690 | * |
| 1691 | * Sends a message to a mailbox and waits for a receiver to process it. |
| 1692 | * The message data may be in a buffer, in a memory pool block, or non-existent |
| 1693 | * (i.e. empty message). |
| 1694 | * |
| 1695 | * @param mbox Pointer to the mailbox object. |
| 1696 | * @param tx_msg Pointer to transmit message descriptor. |
| 1697 | * @param timeout Maximum time (milliseconds) to wait for the message to be |
| 1698 | * received (although not necessarily completely processed). |
| 1699 | * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long |
| 1700 | * as necessary. |
| 1701 | * |
| 1702 | * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out |
| 1703 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1704 | extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1705 | int32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1706 | |
| 1707 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 1708 | /** |
| 1709 | * @brief Send a mailbox message in an asynchronous manner. |
| 1710 | * |
| 1711 | * Sends a message to a mailbox without waiting for a receiver to process it. |
| 1712 | * The message data may be in a buffer, in a memory pool block, or non-existent |
| 1713 | * (i.e. an empty message). Optionally, the specified semaphore will be given |
| 1714 | * by the mailbox when the message has been both received and disposed of |
| 1715 | * by the receiver. |
| 1716 | * |
| 1717 | * @param mbox Pointer to the mailbox object. |
| 1718 | * @param tx_msg Pointer to transmit message descriptor. |
| 1719 | * @param sem Semaphore identifier, or NULL if none specified. |
| 1720 | * |
| 1721 | * @return N/A |
| 1722 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1723 | extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1724 | struct k_sem *sem); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1725 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1726 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1727 | /** |
| 1728 | * @brief Receive a mailbox message. |
| 1729 | * |
| 1730 | * Receives a message from a mailbox, then optionally retrieves its data |
| 1731 | * and disposes of the message. |
| 1732 | * |
| 1733 | * @param mbox Pointer to the mailbox object. |
| 1734 | * @param rx_msg Pointer to receive message descriptor. |
| 1735 | * @param buffer Pointer to buffer to receive data. |
| 1736 | * (Use NULL to defer data retrieval and message disposal until later.) |
| 1737 | * @param timeout Maximum time (milliseconds) to wait for a message. |
| 1738 | * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as |
| 1739 | * necessary. |
| 1740 | * |
| 1741 | * @return 0 if successful, -ENOMSG if failed immediately, -EAGAIN if timed out |
| 1742 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1743 | extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1744 | void *buffer, int32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1745 | |
| 1746 | /** |
| 1747 | * @brief Retrieve mailbox message data into a buffer. |
| 1748 | * |
| 1749 | * Completes the processing of a received message by retrieving its data |
| 1750 | * into a buffer, then disposing of the message. |
| 1751 | * |
| 1752 | * Alternatively, this routine can be used to dispose of a received message |
| 1753 | * without retrieving its data. |
| 1754 | * |
| 1755 | * @param rx_msg Pointer to receive message descriptor. |
| 1756 | * @param buffer Pointer to buffer to receive data. (Use NULL to discard data.) |
| 1757 | * |
| 1758 | * @return N/A |
| 1759 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1760 | extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 1761 | |
| 1762 | /** |
| 1763 | * @brief Retrieve mailbox message data into a memory pool block. |
| 1764 | * |
| 1765 | * Completes the processing of a received message by retrieving its data |
| 1766 | * into a memory pool block, then disposing of the message. The memory pool |
| 1767 | * block that results from successful retrieval must be returned to the pool |
| 1768 | * once the data has been processed, even in cases where zero bytes of data |
| 1769 | * are retrieved. |
| 1770 | * |
| 1771 | * Alternatively, this routine can be used to dispose of a received message |
| 1772 | * without retrieving its data. In this case there is no need to return a |
| 1773 | * memory pool block to the pool. |
| 1774 | * |
| 1775 | * This routine allocates a new memory pool block for the data only if the |
| 1776 | * data is not already in one. If a new block cannot be allocated, the routine |
| 1777 | * returns a failure code and the received message is left unchanged. This |
| 1778 | * permits the caller to reattempt data retrieval at a later time or to dispose |
| 1779 | * of the received message without retrieving its data. |
| 1780 | * |
| 1781 | * @param rx_msg Pointer to receive message descriptor. |
| 1782 | * @param pool Memory pool identifier. (Use NULL to discard data.) |
| 1783 | * @param block Pointer to area to hold memory pool block info. |
| 1784 | * @param timeout Maximum time (milliseconds) to wait for a memory pool block. |
| 1785 | * Use K_NO_WAIT to return immediately, or K_FOREVER to wait as long as |
| 1786 | * necessary. |
| 1787 | * |
| 1788 | * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out |
| 1789 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 1790 | extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg, |
Peter Mitsis | 0cb65c3 | 2016-09-29 14:07:36 -0400 | [diff] [blame] | 1791 | struct k_mem_pool *pool, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1792 | struct k_mem_block *block, int32_t timeout); |
| 1793 | |
| 1794 | /* pipes */ |
| 1795 | |
| 1796 | struct k_pipe { |
| 1797 | unsigned char *buffer; /* Pipe buffer: may be NULL */ |
| 1798 | size_t size; /* Buffer size */ |
| 1799 | size_t bytes_used; /* # bytes used in buffer */ |
| 1800 | size_t read_index; /* Where in buffer to read from */ |
| 1801 | size_t write_index; /* Where in buffer to write */ |
| 1802 | |
| 1803 | struct { |
| 1804 | _wait_q_t readers; /* Reader wait queue */ |
| 1805 | _wait_q_t writers; /* Writer wait queue */ |
| 1806 | } wait_q; |
| 1807 | |
| 1808 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe); |
| 1809 | }; |
| 1810 | |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1811 | #define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1812 | { \ |
| 1813 | .buffer = pipe_buffer, \ |
| 1814 | .size = pipe_buffer_size, \ |
| 1815 | .bytes_used = 0, \ |
| 1816 | .read_index = 0, \ |
| 1817 | .write_index = 0, \ |
| 1818 | .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \ |
| 1819 | .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \ |
| 1820 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1821 | } |
| 1822 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1823 | /** |
| 1824 | * @brief Define a pipe |
| 1825 | * |
| 1826 | * This declares and initializes a pipe. The new pipe can be passed to |
| 1827 | * the kernel's pipe functions. |
| 1828 | * |
| 1829 | * If the pipe is to be accessed outside the module where it is defined, it |
| 1830 | * can be declared via |
| 1831 | * |
| 1832 | * extern struct k_pipe @a name; |
| 1833 | * |
| 1834 | * @param name Name of the mailbox |
| 1835 | * @param pipe_buffer_size Size of the pipe's buffer (may be zero) |
| 1836 | * @param pipe_align Alignment of the pipe's buffer |
| 1837 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1838 | #define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \ |
| 1839 | static unsigned char __noinit __aligned(pipe_align) \ |
| 1840 | _k_pipe_buf_##name[pipe_buffer_size]; \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1841 | struct k_pipe name = \ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1842 | K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1843 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1844 | /** |
| 1845 | * @brief Runtime initialization of a pipe |
| 1846 | * |
| 1847 | * @param pipe Pointer to pipe to initialize |
| 1848 | * @param buffer Pointer to buffer to use for pipe's ring buffer |
| 1849 | * @param size Size of the pipe's ring buffer |
| 1850 | * |
| 1851 | * @return N/A |
| 1852 | */ |
| 1853 | extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, |
| 1854 | size_t size); |
| 1855 | |
| 1856 | /** |
| 1857 | * @brief Put a message into the specified pipe |
| 1858 | * |
| 1859 | * This routine synchronously adds a message into the pipe specified by |
| 1860 | * @a pipe. It will wait up to @a timeout for the pipe to accept |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1861 | * @a bytes_to_write bytes of data. If by @a timeout, the pipe could not |
| 1862 | * accept @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1863 | * only ever be written to the pipe if K_NO_WAIT < @a timeout < K_FOREVER. |
| 1864 | * |
| 1865 | * @param pipe Pointer to the pipe |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1866 | * @param data Data to put into the pipe |
| 1867 | * @param bytes_to_write Desired number of bytes to put into the pipe |
| 1868 | * @param bytes_written Number of bytes the pipe accepted |
| 1869 | * @param min_xfer Minimum number of bytes accepted for success |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1870 | * @param timeout Maximum number of milliseconds to wait |
| 1871 | * |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1872 | * @retval 0 At least @a min_xfer were sent |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1873 | * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT) |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1874 | * @retval -EAGAIN Fewer than @a min_xfer were sent |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1875 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1876 | extern int k_pipe_put(struct k_pipe *pipe, void *data, |
| 1877 | size_t bytes_to_write, size_t *bytes_written, |
| 1878 | size_t min_xfer, int32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1879 | |
| 1880 | /** |
| 1881 | * @brief Get a message from the specified pipe |
| 1882 | * |
| 1883 | * This routine synchronously retrieves a message from the pipe specified by |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1884 | * @a pipe. It will wait up to @a timeout to retrieve @a bytes_to_read |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1885 | * bytes of data from the pipe. If by @a timeout, the pipe could not retrieve |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1886 | * @a min_xfer bytes of data, it fails. Fewer than @a min_xfer will |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1887 | * only ever be retrieved from the pipe if K_NO_WAIT < @a timeout < K_FOREVER. |
| 1888 | * |
| 1889 | * @param pipe Pointer to the pipe |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1890 | * @param data Location to place retrieved data |
| 1891 | * @param bytes_to_read Desired number of bytes to retrieve from the pipe |
| 1892 | * @param bytes_read Number of bytes retrieved from the pipe |
| 1893 | * @param min_xfer Minimum number of bytes retrieved for success |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1894 | * @param timeout Maximum number of milliseconds to wait |
| 1895 | * |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1896 | * @retval 0 At least @a min_xfer were transferred |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1897 | * @retval -EIO Request can not be satisfied (@a timeout is K_NO_WAIT) |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1898 | * @retval -EAGAIN Fewer than @a min_xfer were retrieved |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1899 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 1900 | extern int k_pipe_get(struct k_pipe *pipe, void *data, |
| 1901 | size_t bytes_to_read, size_t *bytes_read, |
| 1902 | size_t min_xfer, int32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1903 | |
Peter Mitsis | 2fef023 | 2016-10-14 14:53:44 -0400 | [diff] [blame] | 1904 | #if (CONFIG_NUM_PIPE_ASYNC_MSGS > 0) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1905 | /** |
| 1906 | * @brief Send a message to the specified pipe |
| 1907 | * |
| 1908 | * This routine asynchronously sends a message from the pipe specified by |
| 1909 | * @a pipe. Once all @a size bytes have been accepted by the pipe, it will |
| 1910 | * free the memory block @a block and give the semaphore @a sem (if specified). |
| 1911 | * Up to CONFIG_NUM_PIPE_ASYNC_MSGS asynchronous pipe messages can be in-flight |
| 1912 | * at any given time. |
| 1913 | * |
| 1914 | * @param pipe Pointer to the pipe |
| 1915 | * @param block Memory block containing data to send |
| 1916 | * @param size Number of data bytes in memory block to send |
| 1917 | * @param sem Semaphore to signal upon completion (else NULL) |
| 1918 | * |
| 1919 | * @retval N/A |
| 1920 | */ |
| 1921 | extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block, |
| 1922 | size_t size, struct k_sem *sem); |
Peter Mitsis | 2fef023 | 2016-10-14 14:53:44 -0400 | [diff] [blame] | 1923 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1924 | |
| 1925 | /** |
| 1926 | * memory management |
| 1927 | */ |
| 1928 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 1929 | /* memory slabs */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1930 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 1931 | struct k_mem_slab { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1932 | _wait_q_t wait_q; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 1933 | uint32_t num_blocks; |
| 1934 | size_t block_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1935 | char *buffer; |
| 1936 | char *free_list; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 1937 | uint32_t num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1938 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 1939 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1940 | }; |
| 1941 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 1942 | #define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \ |
| 1943 | slab_num_blocks) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1944 | { \ |
| 1945 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 1946 | .num_blocks = slab_num_blocks, \ |
| 1947 | .block_size = slab_block_size, \ |
| 1948 | .buffer = slab_buffer, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1949 | .free_list = NULL, \ |
| 1950 | .num_used = 0, \ |
| 1951 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1952 | } |
| 1953 | |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 1954 | /** |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1955 | * @brief Define a memory slab allocator |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 1956 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1957 | * This declares and initializes a slab allocator whose buffer is aligned to |
| 1958 | * a @a slab_align -byte boundary. The new slab allocator can be passed to the |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 1959 | * kernel's memory slab functions. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 1960 | * |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 1961 | * Note that for each of the blocks in the memory slab to be aligned to |
| 1962 | * @a slab_align bytes, then @a slab_block_size must be a multiple of |
| 1963 | * @a slab_align. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 1964 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1965 | * If the slab allocator is to be accessed outside the module where it is |
| 1966 | * defined, it can be declared via |
| 1967 | * |
| 1968 | * extern struct k_mem_slab @a name; |
| 1969 | * |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 1970 | * @param name Name of the memory slab |
| 1971 | * @param slab_block_size Size of each block in the buffer (in bytes) |
| 1972 | * @param slab_num_blocks Number blocks in the buffer |
| 1973 | * @param slab_align Alignment of the memory slab's buffer (power of 2) |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 1974 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 1975 | #define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \ |
| 1976 | char __noinit __aligned(slab_align) \ |
| 1977 | _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \ |
| 1978 | struct k_mem_slab name \ |
| 1979 | __in_section(_k_mem_map_ptr, private, mem_slab) = \ |
| 1980 | K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \ |
| 1981 | slab_block_size, slab_num_blocks) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1982 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 1983 | /** |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 1984 | * @brief Initialize a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 1985 | * |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 1986 | * Initializes the memory slab and creates its list of free blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 1987 | * |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 1988 | * @param slab Pointer to the memory slab object |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 1989 | * @param buffer Pointer to buffer used for the blocks. |
| 1990 | * @param block_size Size of each block, in bytes. |
| 1991 | * @param num_blocks Number of blocks. |
| 1992 | * |
| 1993 | * @return N/A |
| 1994 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 1995 | extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer, |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 1996 | size_t block_size, uint32_t num_blocks); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 1997 | |
| 1998 | /** |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 1999 | * @brief Allocate a memory slab block. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2000 | * |
| 2001 | * Takes a block from the list of unused blocks. |
| 2002 | * |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2003 | * @param slab Pointer to memory slab object. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2004 | * @param mem Pointer to area to receive block address. |
| 2005 | * @param timeout Maximum time (milliseconds) to wait for allocation to |
| 2006 | * complete. Use K_NO_WAIT to return immediately, or K_FOREVER to wait |
| 2007 | * as long as necessary. |
| 2008 | * |
| 2009 | * @return 0 if successful, -ENOMEM if failed immediately, -EAGAIN if timed out |
| 2010 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2011 | extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem, |
| 2012 | int32_t timeout); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2013 | |
| 2014 | /** |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2015 | * @brief Free a memory slab block. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2016 | * |
| 2017 | * Gives block to a waiting thread if there is one, otherwise returns it to |
| 2018 | * the list of unused blocks. |
| 2019 | * |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2020 | * @param slab Pointer to memory slab object. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2021 | * @param mem Pointer to area to containing block address. |
| 2022 | * |
| 2023 | * @return N/A |
| 2024 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2025 | extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2026 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2027 | /** |
| 2028 | * @brief Get the number of used memory blocks |
| 2029 | * |
| 2030 | * This routine gets the current number of used memory blocks in the |
| 2031 | * specified pool. It should be used for stats purposes only as that |
| 2032 | * value may potentially be out-of-date by the time it is used. |
| 2033 | * |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2034 | * @param slab Memory slab to query |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2035 | * |
| 2036 | * @return Number of used memory blocks |
| 2037 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2038 | static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2039 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2040 | return slab->num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2041 | } |
| 2042 | |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2043 | /** |
| 2044 | * @brief Get the number of unused memory blocks |
| 2045 | * |
| 2046 | * This routine gets the current number of unused memory blocks in the |
| 2047 | * specified pool. It should be used for stats purposes only as that value |
| 2048 | * may potentially be out-of-date by the time it is used. |
| 2049 | * |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2050 | * @param slab Memory slab to query |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2051 | * |
| 2052 | * @return Number of unused memory blocks |
| 2053 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2054 | static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab) |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2055 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2056 | return slab->num_blocks - slab->num_used; |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2057 | } |
| 2058 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2059 | /* memory pools */ |
| 2060 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2061 | /* |
| 2062 | * Memory pool requires a buffer and two arrays of structures for the |
| 2063 | * memory block accounting: |
| 2064 | * A set of arrays of k_mem_pool_quad_block structures where each keeps a |
| 2065 | * status of four blocks of memory. |
| 2066 | */ |
| 2067 | struct k_mem_pool_quad_block { |
| 2068 | char *mem_blocks; /* pointer to the first of four memory blocks */ |
| 2069 | uint32_t mem_status; /* four bits. If bit is set, memory block is |
| 2070 | allocated */ |
| 2071 | }; |
| 2072 | /* |
| 2073 | * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting |
| 2074 | * blocks of one size. Block sizes go from maximal to minimal. Next memory |
| 2075 | * block size is 4 times less than the previous one and thus requires 4 times |
| 2076 | * bigger array of k_mem_pool_quad_block structures to keep track of the |
| 2077 | * memory blocks. |
| 2078 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2079 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2080 | /* |
| 2081 | * The array of k_mem_pool_block_set keeps the information of each array of |
| 2082 | * k_mem_pool_quad_block structures |
| 2083 | */ |
| 2084 | struct k_mem_pool_block_set { |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2085 | size_t block_size; /* memory block size */ |
| 2086 | uint32_t nr_of_entries; /* nr of quad block structures in the array */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2087 | struct k_mem_pool_quad_block *quad_block; |
| 2088 | int count; |
| 2089 | }; |
| 2090 | |
| 2091 | /* Memory pool descriptor */ |
| 2092 | struct k_mem_pool { |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2093 | size_t max_block_size; |
| 2094 | size_t min_block_size; |
| 2095 | uint32_t nr_of_maxblocks; |
| 2096 | uint32_t nr_of_block_sets; |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2097 | struct k_mem_pool_block_set *block_set; |
| 2098 | char *bufblock; |
| 2099 | _wait_q_t wait_q; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2100 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool); |
| 2101 | }; |
| 2102 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2103 | #ifdef CONFIG_ARM |
| 2104 | #define _SECTION_TYPE_SIGN "%" |
| 2105 | #else |
| 2106 | #define _SECTION_TYPE_SIGN "@" |
| 2107 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2108 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2109 | /* |
| 2110 | * Static memory pool initialization |
| 2111 | */ |
| 2112 | /* |
| 2113 | * Use .altmacro to be able to recalculate values and pass them as string |
| 2114 | * arguments when calling assembler macros resursively |
| 2115 | */ |
| 2116 | __asm__(".altmacro\n\t"); |
| 2117 | |
| 2118 | /* |
| 2119 | * Recursively calls a macro |
| 2120 | * The followig global symbols need to be initialized: |
| 2121 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2122 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2123 | * Notes: |
| 2124 | * Global symbols are used due the fact that assembler macro allows only |
| 2125 | * one argument be passed with the % conversion |
| 2126 | * Some assemblers do not get division operation ("/"). To avoid it >> 2 |
| 2127 | * is used instead of / 4. |
| 2128 | * n_max argument needs to go first in the invoked macro, as some |
| 2129 | * assemblers concatenate \name and %(\n_max * 4) arguments |
| 2130 | * if \name goes first |
| 2131 | */ |
| 2132 | __asm__(".macro __do_recurse macro_name, name, n_max\n\t" |
| 2133 | ".ifge __memory_pool_max_block_size >> 2 -" |
| 2134 | " __memory_pool_min_block_size\n\t\t" |
| 2135 | "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t" |
| 2136 | "\\macro_name %(\\n_max * 4) \\name\n\t" |
| 2137 | ".endif\n\t" |
| 2138 | ".endm\n"); |
| 2139 | |
| 2140 | /* |
| 2141 | * Build quad blocks |
| 2142 | * Macro allocates space in memory for the array of k_mem_pool_quad_block |
| 2143 | * structures and recursively calls itself for the next array, 4 times |
| 2144 | * larger. |
| 2145 | * The followig global symbols need to be initialized: |
| 2146 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2147 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2148 | * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block) |
| 2149 | */ |
| 2150 | __asm__(".macro _build_quad_blocks n_max, name\n\t" |
Dmitriy Korovkin | 3c90651 | 2016-10-06 15:50:40 -0400 | [diff] [blame] | 2151 | ".balign 4\n\t" |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2152 | "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t" |
| 2153 | ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t" |
| 2154 | ".if \\n_max % 4\n\t\t" |
| 2155 | ".skip __memory_pool_quad_block_size\n\t" |
| 2156 | ".endif\n\t" |
| 2157 | "__do_recurse _build_quad_blocks \\name \\n_max\n\t" |
| 2158 | ".endm\n"); |
| 2159 | |
| 2160 | /* |
| 2161 | * Build block sets and initialize them |
| 2162 | * Macro initializes the k_mem_pool_block_set structure and |
| 2163 | * recursively calls itself for the next one. |
| 2164 | * The followig global symbols need to be initialized: |
| 2165 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2166 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2167 | * __memory_pool_block_set_count, the number of the elements in the |
| 2168 | * block set array must be set to 0. Macro calculates it's real |
| 2169 | * value. |
| 2170 | * Since the macro initializes pointers to an array of k_mem_pool_quad_block |
| 2171 | * structures, _build_quad_blocks must be called prior it. |
| 2172 | */ |
| 2173 | __asm__(".macro _build_block_set n_max, name\n\t" |
| 2174 | ".int __memory_pool_max_block_size\n\t" /* block_size */ |
| 2175 | ".if \\n_max % 4\n\t\t" |
| 2176 | ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */ |
| 2177 | ".else\n\t\t" |
| 2178 | ".int \\n_max >> 2\n\t" |
| 2179 | ".endif\n\t" |
| 2180 | ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */ |
| 2181 | ".int 0\n\t" /* count */ |
| 2182 | "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t" |
| 2183 | "__do_recurse _build_block_set \\name \\n_max\n\t" |
| 2184 | ".endm\n"); |
| 2185 | |
| 2186 | /* |
| 2187 | * Build a memory pool structure and initialize it |
| 2188 | * Macro uses __memory_pool_block_set_count global symbol, |
| 2189 | * block set addresses and buffer address, it may be called only after |
| 2190 | * _build_block_set |
| 2191 | */ |
| 2192 | __asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t" |
| 2193 | ".pushsection ._k_memory_pool,\"aw\"," |
| 2194 | _SECTION_TYPE_SIGN "progbits\n\t" |
| 2195 | ".globl \\name\n\t" |
| 2196 | "\\name:\n\t" |
| 2197 | ".int \\max_size\n\t" /* max_block_size */ |
| 2198 | ".int \\min_size\n\t" /* min_block_size */ |
| 2199 | ".int \\n_max\n\t" /* nr_of_maxblocks */ |
| 2200 | ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */ |
| 2201 | ".int _mem_pool_block_sets_\\name\n\t" /* block_set */ |
| 2202 | ".int _mem_pool_buffer_\\name\n\t" /* bufblock */ |
| 2203 | ".int 0\n\t" /* wait_q->head */ |
| 2204 | ".int 0\n\t" /* wait_q->next */ |
| 2205 | ".popsection\n\t" |
| 2206 | ".endm\n"); |
| 2207 | |
| 2208 | #define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \ |
| 2209 | __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \ |
| 2210 | _SECTION_TYPE_SIGN "progbits\n\t"); \ |
| 2211 | __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \ |
| 2212 | __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \ |
| 2213 | __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \ |
| 2214 | STRINGIFY(name) "\n\t"); \ |
| 2215 | __asm__(".popsection\n\t") |
| 2216 | |
| 2217 | #define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \ |
| 2218 | __asm__("__memory_pool_block_set_count = 0\n\t"); \ |
| 2219 | __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \ |
| 2220 | __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \ |
| 2221 | _SECTION_TYPE_SIGN "progbits\n\t"); \ |
Dmitriy Korovkin | 3c90651 | 2016-10-06 15:50:40 -0400 | [diff] [blame] | 2222 | __asm__(".balign 4\n\t"); \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2223 | __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \ |
| 2224 | __asm__("_build_block_set " STRINGIFY(n_max) " " \ |
| 2225 | STRINGIFY(name) "\n\t"); \ |
| 2226 | __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \ |
| 2227 | __asm__(".int __memory_pool_block_set_count\n\t"); \ |
| 2228 | __asm__(".popsection\n\t"); \ |
| 2229 | extern uint32_t _mem_pool_block_set_count_##name; \ |
| 2230 | extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[] |
| 2231 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2232 | #define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \ |
| 2233 | char __noinit __aligned(align) \ |
| 2234 | _mem_pool_buffer_##name[(max_size) * (n_max)] |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2235 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2236 | /** |
| 2237 | * @brief Define a memory pool |
| 2238 | * |
| 2239 | * This declares and initializes a memory pool whose buffer is aligned to |
| 2240 | * a @a align -byte boundary. The new memory pool can be passed to the |
| 2241 | * kernel's memory pool functions. |
| 2242 | * |
| 2243 | * Note that for each of the minimum sized blocks to be aligned to @a align |
| 2244 | * bytes, then @a min_size must be a multiple of @a align. |
| 2245 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2246 | * If the pool is to be accessed outside the module where it is defined, it |
| 2247 | * can be declared via |
| 2248 | * |
| 2249 | * extern struct k_mem_pool @a name; |
| 2250 | * |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2251 | * @param name Name of the memory pool |
| 2252 | * @param min_size Minimum block size in the pool |
| 2253 | * @param max_size Maximum block size in the pool |
| 2254 | * @param n_max Number of maximum sized blocks in the pool |
| 2255 | * @param align Alignment of the memory pool's buffer |
| 2256 | */ |
| 2257 | #define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2258 | _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \ |
| 2259 | _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \ |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2260 | _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2261 | __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \ |
| 2262 | STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \ |
| 2263 | extern struct k_mem_pool name |
| 2264 | |
| 2265 | /* |
| 2266 | * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block) |
| 2267 | * to __memory_pool_quad_block_size absolute symbol. |
| 2268 | * This function does not get called, but compiler calculates the value and |
| 2269 | * assigns it to the absolute symbol, that, in turn is used by assembler macros. |
| 2270 | */ |
| 2271 | static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void) |
| 2272 | { |
| 2273 | __asm__(".globl __memory_pool_quad_block_size\n\t" |
Andrew Boie | 431607c | 2016-10-25 11:47:52 -0700 | [diff] [blame] | 2274 | #ifdef CONFIG_NIOS2 |
| 2275 | "__memory_pool_quad_block_size = %0\n\t" |
| 2276 | #else |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2277 | "__memory_pool_quad_block_size = %c0\n\t" |
Andrew Boie | 431607c | 2016-10-25 11:47:52 -0700 | [diff] [blame] | 2278 | #endif |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2279 | : |
| 2280 | : "n"(sizeof(struct k_mem_pool_quad_block))); |
| 2281 | } |
| 2282 | |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2283 | /** |
| 2284 | * @brief Allocate memory from a memory pool |
| 2285 | * |
| 2286 | * @param pool Pointer to the memory pool object |
| 2287 | * @param block Pointer to the allocated memory's block descriptor |
| 2288 | * @param size Minimum number of bytes to allocate |
| 2289 | * @param timeout Maximum time (milliseconds) to wait for operation to |
| 2290 | * complete. Use K_NO_WAIT to return immediately, or K_FOREVER |
| 2291 | * to wait as long as necessary. |
| 2292 | * |
| 2293 | * @return 0 on success, -ENOMEM on failure |
| 2294 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2295 | extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block, |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2296 | size_t size, int32_t timeout); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2297 | |
| 2298 | /** |
| 2299 | * @brief Return previously allocated memory to its memory pool |
| 2300 | * |
| 2301 | * @param block Pointer to allocated memory's block descriptor |
| 2302 | * |
| 2303 | * @return N/A |
| 2304 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2305 | extern void k_mem_pool_free(struct k_mem_block *block); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2306 | |
| 2307 | /** |
| 2308 | * @brief Defragment the specified memory pool |
| 2309 | * |
| 2310 | * @param pool Pointer to the memory pool object |
| 2311 | * |
| 2312 | * @return N/A |
| 2313 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2314 | extern void k_mem_pool_defrag(struct k_mem_pool *pool); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2315 | |
| 2316 | /** |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 2317 | * @brief Allocate memory from heap |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2318 | * |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 2319 | * This routine provides traditional malloc() semantics. The memory is |
| 2320 | * allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2321 | * |
| 2322 | * @param size Size of memory requested by the caller (in bytes) |
| 2323 | * |
| 2324 | * @return Address of the allocated memory on success; otherwise NULL |
| 2325 | */ |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2326 | extern void *k_malloc(size_t size); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2327 | |
| 2328 | /** |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 2329 | * @brief Free memory allocated from heap |
| 2330 | * |
| 2331 | * This routine provides traditional free() semantics. The memory being |
| 2332 | * returned must have been allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2333 | * |
| 2334 | * @param ptr Pointer to previously allocated memory |
| 2335 | * |
| 2336 | * @return N/A |
| 2337 | */ |
| 2338 | extern void k_free(void *ptr); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2339 | |
| 2340 | /* |
| 2341 | * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to |
| 2342 | * hook into the device subsystem, which itself uses nanokernel semaphores, |
| 2343 | * and thus currently requires the definition of nano_sem. |
| 2344 | */ |
| 2345 | #include <legacy.h> |
| 2346 | #include <arch/cpu.h> |
| 2347 | |
| 2348 | /* |
| 2349 | * private APIs that are utilized by one or more public APIs |
| 2350 | */ |
| 2351 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2352 | extern int _is_thread_essential(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2353 | extern void _init_static_threads(void); |
| 2354 | |
| 2355 | #ifdef __cplusplus |
| 2356 | } |
| 2357 | #endif |
| 2358 | |
| 2359 | #endif /* _kernel__h_ */ |