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