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 | |
Anas Nashif | 61f4b24 | 2016-11-18 10:53:59 -0500 | [diff] [blame] | 40 | #ifdef CONFIG_KERNEL_DEBUG |
| 41 | #include <misc/printk.h> |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 42 | #define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__) |
| 43 | #else |
| 44 | #define K_DEBUG(fmt, ...) |
| 45 | #endif |
| 46 | |
| 47 | #define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x))) |
| 48 | #define K_PRIO_PREEMPT(x) (x) |
| 49 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 50 | #define K_ANY NULL |
| 51 | #define K_END NULL |
| 52 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 53 | #if CONFIG_NUM_COOP_PRIORITIES > 0 |
| 54 | #define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES) |
| 55 | #else |
| 56 | #define K_HIGHEST_THREAD_PRIO 0 |
| 57 | #endif |
| 58 | |
| 59 | #if CONFIG_NUM_PREEMPT_PRIORITIES > 0 |
| 60 | #define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES |
| 61 | #else |
| 62 | #define K_LOWEST_THREAD_PRIO -1 |
| 63 | #endif |
| 64 | |
Benjamin Walsh | fab8d92 | 2016-11-08 15:36:36 -0500 | [diff] [blame] | 65 | #define K_IDLE_PRIO K_LOWEST_THREAD_PRIO |
| 66 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 67 | #define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO) |
| 68 | #define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1) |
| 69 | |
| 70 | typedef sys_dlist_t _wait_q_t; |
| 71 | |
| 72 | #ifdef CONFIG_DEBUG_TRACING_KERNEL_OBJECTS |
| 73 | #define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) struct type *__next |
| 74 | #define _DEBUG_TRACING_KERNEL_OBJECTS_INIT .__next = NULL, |
| 75 | #else |
| 76 | #define _DEBUG_TRACING_KERNEL_OBJECTS_INIT |
| 77 | #define _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(type) |
| 78 | #endif |
| 79 | |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 80 | #define tcs k_thread |
| 81 | struct k_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 82 | struct k_mutex; |
| 83 | struct k_sem; |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 84 | struct k_alert; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 85 | struct k_msgq; |
| 86 | struct k_mbox; |
| 87 | struct k_pipe; |
| 88 | struct k_fifo; |
| 89 | struct k_lifo; |
| 90 | struct k_stack; |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 91 | struct k_mem_slab; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 92 | struct k_mem_pool; |
| 93 | struct k_timer; |
| 94 | |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 95 | typedef struct k_thread *k_tid_t; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 96 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 97 | enum execution_context_types { |
| 98 | K_ISR = 0, |
| 99 | K_COOP_THREAD, |
| 100 | K_PREEMPT_THREAD, |
| 101 | }; |
| 102 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 103 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 104 | * @defgroup thread_apis Thread APIs |
| 105 | * @ingroup kernel_apis |
| 106 | * @{ |
| 107 | */ |
| 108 | |
| 109 | /** |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 110 | * @typedef k_thread_entry_t |
| 111 | * @brief Thread entry point function type. |
| 112 | * |
| 113 | * A thread's entry point function is invoked when the thread starts executing. |
| 114 | * Up to 3 argument values can be passed to the function. |
| 115 | * |
| 116 | * The thread terminates execution permanently if the entry point function |
| 117 | * returns. The thread is responsible for releasing any shared resources |
| 118 | * it may own (such as mutexes and dynamically allocated memory), prior to |
| 119 | * returning. |
| 120 | * |
| 121 | * @param p1 First argument. |
| 122 | * @param p2 Second argument. |
| 123 | * @param p3 Third argument. |
| 124 | * |
| 125 | * @return N/A |
| 126 | */ |
| 127 | typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3); |
| 128 | |
| 129 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 130 | * @brief Spawn a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 131 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 132 | * This routine initializes a thread, then schedules it for execution. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 133 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 134 | * The new thread may be scheduled for immediate execution or a delayed start. |
| 135 | * If the newly spawned thread does not have a delayed start the kernel |
| 136 | * scheduler may preempt the current thread to allow the new thread to |
| 137 | * execute. |
| 138 | * |
| 139 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 140 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 141 | * them using "|" (the logical OR operator). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 142 | * |
| 143 | * @param stack Pointer to the stack space. |
| 144 | * @param stack_size Stack size in bytes. |
| 145 | * @param entry Thread entry function. |
| 146 | * @param p1 1st entry point parameter. |
| 147 | * @param p2 2nd entry point parameter. |
| 148 | * @param p3 3rd entry point parameter. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 149 | * @param prio Thread priority. |
| 150 | * @param options Thread options. |
| 151 | * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 152 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 153 | * @return ID of new thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 154 | */ |
Benjamin Walsh | 669360d | 2016-11-14 16:46:14 -0500 | [diff] [blame] | 155 | extern k_tid_t k_thread_spawn(char *stack, size_t stack_size, |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 156 | k_thread_entry_t entry, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 157 | void *p1, void *p2, void *p3, |
Benjamin Walsh | 669360d | 2016-11-14 16:46:14 -0500 | [diff] [blame] | 158 | int prio, uint32_t options, int32_t delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 159 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 160 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 161 | * @brief Put the current thread to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 162 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 163 | * This routine puts the current thread to sleep for @a duration |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 164 | * milliseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 165 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 166 | * @param duration Number of milliseconds to sleep. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 167 | * |
| 168 | * @return N/A |
| 169 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 170 | extern void k_sleep(int32_t duration); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 171 | |
| 172 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 173 | * @brief Cause the current thread to busy wait. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 174 | * |
| 175 | * This routine causes the current thread to execute a "do nothing" loop for |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 176 | * @a usec_to_wait microseconds. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 177 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 178 | * @return N/A |
| 179 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 180 | extern void k_busy_wait(uint32_t usec_to_wait); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 181 | |
| 182 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 183 | * @brief Yield the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 184 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 185 | * This routine causes the current thread to yield execution to another |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 186 | * thread of the same or higher priority. If there are no other ready threads |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 187 | * of the same or higher priority, the routine returns immediately. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 188 | * |
| 189 | * @return N/A |
| 190 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 191 | extern void k_yield(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 192 | |
| 193 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 194 | * @brief Wake up a sleeping thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 195 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 196 | * This routine prematurely wakes up @a thread from sleeping. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 197 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 198 | * If @a thread is not currently sleeping, the routine has no effect. |
| 199 | * |
| 200 | * @param thread ID of thread to wake. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 201 | * |
| 202 | * @return N/A |
| 203 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 204 | extern void k_wakeup(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 205 | |
| 206 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 207 | * @brief Get thread ID of the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 208 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 209 | * @return ID of current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 210 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 211 | extern k_tid_t k_current_get(void); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 212 | |
| 213 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 214 | * @brief Cancel thread performing a delayed start. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 215 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 216 | * This routine prevents @a thread from executing if it has not yet started |
| 217 | * execution. The thread must be re-spawned before it will execute. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 218 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 219 | * @param thread ID of thread to cancel. |
| 220 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 221 | * @retval 0 Thread spawning cancelled. |
| 222 | * @retval -EINVAL Thread has already started executing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 223 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 224 | extern int k_thread_cancel(k_tid_t thread); |
| 225 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 226 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 227 | * @brief Abort a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 228 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 229 | * This routine permanently stops execution of @a thread. The thread is taken |
| 230 | * off all kernel queues it is part of (i.e. the ready queue, the timeout |
| 231 | * queue, or a kernel object wait queue). However, any kernel resources the |
| 232 | * thread might currently own (such as mutexes or memory blocks) are not |
| 233 | * released. It is the responsibility of the caller of this routine to ensure |
| 234 | * all necessary cleanup is performed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 235 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 236 | * @param thread ID of thread to abort. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 237 | * |
| 238 | * @return N/A |
| 239 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 240 | extern void k_thread_abort(k_tid_t thread); |
| 241 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 242 | /** |
| 243 | * @cond INTERNAL_HIDDEN |
| 244 | */ |
| 245 | |
Benjamin Walsh | 1a5450b | 2016-10-06 15:04:23 -0400 | [diff] [blame] | 246 | #ifdef CONFIG_SYS_CLOCK_EXISTS |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 247 | #define _THREAD_TIMEOUT_INIT(obj) \ |
| 248 | (obj).nano_timeout = { \ |
| 249 | .node = { {0}, {0} }, \ |
Benjamin Walsh | 055262c | 2016-10-05 17:16:01 -0400 | [diff] [blame] | 250 | .thread = NULL, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 251 | .wait_q = NULL, \ |
| 252 | .delta_ticks_from_prev = -1, \ |
| 253 | }, |
| 254 | #else |
| 255 | #define _THREAD_TIMEOUT_INIT(obj) |
| 256 | #endif |
| 257 | |
| 258 | #ifdef CONFIG_ERRNO |
| 259 | #define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0, |
| 260 | #else |
| 261 | #define _THREAD_ERRNO_INIT(obj) |
| 262 | #endif |
| 263 | |
Peter Mitsis | a04c0d7 | 2016-09-28 19:26:00 -0400 | [diff] [blame] | 264 | struct _static_thread_data { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 265 | union { |
| 266 | char *init_stack; |
| 267 | struct k_thread *thread; |
| 268 | }; |
| 269 | unsigned int init_stack_size; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 270 | void (*init_entry)(void *, void *, void *); |
| 271 | void *init_p1; |
| 272 | void *init_p2; |
| 273 | void *init_p3; |
| 274 | int init_prio; |
| 275 | uint32_t init_options; |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 276 | int32_t init_delay; |
Allan Stephens | 7c5bffa | 2016-10-26 10:01:28 -0500 | [diff] [blame] | 277 | void (*init_abort)(void); |
| 278 | uint32_t init_groups; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 279 | }; |
| 280 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 281 | #define _THREAD_INITIALIZER(stack, stack_size, \ |
| 282 | entry, p1, p2, p3, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 283 | prio, options, delay, abort, groups) \ |
| 284 | { \ |
| 285 | .init_stack = (stack), \ |
| 286 | .init_stack_size = (stack_size), \ |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 287 | .init_entry = (void (*)(void *, void *, void *))entry, \ |
| 288 | .init_p1 = (void *)p1, \ |
| 289 | .init_p2 = (void *)p2, \ |
| 290 | .init_p3 = (void *)p3, \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 291 | .init_prio = (prio), \ |
| 292 | .init_options = (options), \ |
| 293 | .init_delay = (delay), \ |
| 294 | .init_abort = (abort), \ |
| 295 | .init_groups = (groups), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 298 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 299 | * INTERNAL_HIDDEN @endcond |
| 300 | */ |
| 301 | |
| 302 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 303 | * @brief Statically define and initialize a thread. |
| 304 | * |
| 305 | * The thread may be scheduled for immediate execution or a delayed start. |
| 306 | * |
| 307 | * Thread options are architecture-specific, and can include K_ESSENTIAL, |
| 308 | * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating |
| 309 | * them using "|" (the logical OR operator). |
| 310 | * |
| 311 | * The ID of the thread can be accessed using: |
| 312 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 313 | * @code extern const k_tid_t <name>; @endcode |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 314 | * |
| 315 | * @param name Name of the thread. |
| 316 | * @param stack_size Stack size in bytes. |
| 317 | * @param entry Thread entry function. |
| 318 | * @param p1 1st entry point parameter. |
| 319 | * @param p2 2nd entry point parameter. |
| 320 | * @param p3 3rd entry point parameter. |
| 321 | * @param prio Thread priority. |
| 322 | * @param options Thread options. |
| 323 | * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay). |
Peter Mitsis | b2fd5be | 2016-10-11 12:06:25 -0400 | [diff] [blame] | 324 | * |
| 325 | * @internal It has been observed that the x86 compiler by default aligns |
| 326 | * these _static_thread_data structures to 32-byte boundaries, thereby |
| 327 | * wasting space. To work around this, force a 4-byte alignment. |
| 328 | */ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 329 | #define K_THREAD_DEFINE(name, stack_size, \ |
| 330 | entry, p1, p2, p3, \ |
| 331 | prio, options, delay) \ |
| 332 | char __noinit __stack _k_thread_obj_##name[stack_size]; \ |
| 333 | struct _static_thread_data _k_thread_data_##name __aligned(4) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 334 | __in_section(_static_thread_data, static, name) = \ |
Allan Stephens | 6cfe132 | 2016-10-26 10:16:51 -0500 | [diff] [blame] | 335 | _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \ |
| 336 | entry, p1, p2, p3, prio, options, delay, \ |
Allan Stephens | 8809502 | 2016-10-26 14:15:08 -0500 | [diff] [blame] | 337 | NULL, 0); \ |
| 338 | const k_tid_t name = (k_tid_t)_k_thread_obj_##name |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 339 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 340 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 341 | * @brief Get a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 342 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 343 | * This routine gets the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 344 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 345 | * @param thread ID of thread whose priority is needed. |
| 346 | * |
| 347 | * @return Priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 348 | */ |
Allan Stephens | 399d0ad | 2016-10-07 13:41:34 -0500 | [diff] [blame] | 349 | extern int k_thread_priority_get(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 350 | |
| 351 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 352 | * @brief Set a thread's priority. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 353 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 354 | * This routine immediately changes the priority of @a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 355 | * |
| 356 | * Rescheduling can occur immediately depending on the priority @a thread is |
| 357 | * set to: |
| 358 | * |
| 359 | * - If its priority is raised above the priority of the caller of this |
| 360 | * function, and the caller is preemptible, @a thread will be scheduled in. |
| 361 | * |
| 362 | * - If the caller operates on itself, it lowers its priority below that of |
| 363 | * other threads in the system, and the caller is preemptible, the thread of |
| 364 | * highest priority will be scheduled in. |
| 365 | * |
| 366 | * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to |
| 367 | * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the |
| 368 | * highest priority. |
| 369 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 370 | * @param thread ID of thread whose priority is to be set. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 371 | * @param prio New priority. |
| 372 | * |
| 373 | * @warning Changing the priority of a thread currently involved in mutex |
| 374 | * priority inheritance may result in undefined behavior. |
| 375 | * |
| 376 | * @return N/A |
| 377 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 378 | extern void k_thread_priority_set(k_tid_t thread, int prio); |
| 379 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 380 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 381 | * @brief Suspend a thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 382 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 383 | * This routine prevents the kernel scheduler from making @a thread the |
| 384 | * current thread. All other internal operations on @a thread are still |
| 385 | * performed; for example, any timeout it is waiting on keeps ticking, |
| 386 | * kernel objects it is waiting on are still handed to it, etc. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 387 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 388 | * If @a thread is already suspended, the routine has no effect. |
| 389 | * |
| 390 | * @param thread ID of thread to suspend. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 391 | * |
| 392 | * @return N/A |
| 393 | */ |
Benjamin Walsh | 71d5228 | 2016-09-29 10:49:48 -0400 | [diff] [blame] | 394 | extern void k_thread_suspend(k_tid_t thread); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 395 | |
| 396 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 397 | * @brief Resume a suspended thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 398 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 399 | * This routine allows the kernel scheduler to make @a thread the current |
| 400 | * thread, when it is next eligible for that role. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 401 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 402 | * If @a thread is not currently suspended, the routine has no effect. |
| 403 | * |
| 404 | * @param thread ID of thread to resume. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 405 | * |
| 406 | * @return N/A |
| 407 | */ |
Benjamin Walsh | 71d5228 | 2016-09-29 10:49:48 -0400 | [diff] [blame] | 408 | extern void k_thread_resume(k_tid_t thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 409 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 410 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 411 | * @brief Set time-slicing period and scope. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 412 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 413 | * This routine specifies how the scheduler will perform time slicing of |
| 414 | * preemptible threads. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 415 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 416 | * To enable time slicing, @a slice must be non-zero. The scheduler |
| 417 | * ensures that no thread runs for more than the specified time limit |
| 418 | * before other threads of that priority are given a chance to execute. |
| 419 | * Any thread whose priority is higher than @a prio is exempted, and may |
| 420 | * execute as long as desired without being pre-empted due to time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 421 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 422 | * Time slicing only limits the maximum amount of time a thread may continuously |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 423 | * execute. Once the scheduler selects a thread for execution, there is no |
| 424 | * minimum guaranteed time the thread will execute before threads of greater or |
| 425 | * equal priority are scheduled. |
| 426 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 427 | * When the current thread is the only one of that priority eligible |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 428 | * for execution, this routine has no effect; the thread is immediately |
| 429 | * rescheduled after the slice period expires. |
| 430 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 431 | * To disable timeslicing, set both @a slice and @a prio to zero. |
| 432 | * |
| 433 | * @param slice Maximum time slice length (in milliseconds). |
| 434 | * @param prio Highest thread priority level eligible for time slicing. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 435 | * |
| 436 | * @return N/A |
| 437 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 438 | extern void k_sched_time_slice_set(int32_t slice, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 439 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 440 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 441 | * @} end defgroup thread_apis |
| 442 | */ |
| 443 | |
| 444 | /** |
| 445 | * @addtogroup isr_apis |
| 446 | * @{ |
| 447 | */ |
| 448 | |
| 449 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 450 | * @brief Determine if code is running at interrupt level. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 451 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 452 | * This routine allows the caller to customize its actions, depending on |
| 453 | * whether it is a thread or an ISR. |
| 454 | * |
| 455 | * @note Can be called by ISRs. |
| 456 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 457 | * @return 0 if invoked by a thread. |
| 458 | * @return Non-zero if invoked by an ISR. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 459 | */ |
Benjamin Walsh | c7ba8b1 | 2016-11-08 16:12:59 -0500 | [diff] [blame] | 460 | extern int k_is_in_isr(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 461 | |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 462 | /** |
| 463 | * @brief Determine if code is running in a preemptible thread. |
| 464 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 465 | * This routine allows the caller to customize its actions, depending on |
| 466 | * whether it can be preempted by another thread. The routine returns a 'true' |
| 467 | * value if all of the following conditions are met: |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 468 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 469 | * - The code is running in a thread, not at ISR. |
| 470 | * - The thread's priority is in the preemptible range. |
| 471 | * - The thread has not locked the scheduler. |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 472 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 473 | * @note Can be called by ISRs. |
| 474 | * |
| 475 | * @return 0 if invoked by an ISR or by a cooperative thread. |
Benjamin Walsh | 445830d | 2016-11-10 15:54:27 -0500 | [diff] [blame] | 476 | * @return Non-zero if invoked by a preemptible thread. |
| 477 | */ |
| 478 | extern int k_is_preempt_thread(void); |
| 479 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 480 | /** |
| 481 | * @} end addtogroup isr_apis |
| 482 | */ |
| 483 | |
| 484 | /** |
| 485 | * @addtogroup thread_apis |
| 486 | * @{ |
| 487 | */ |
| 488 | |
| 489 | /** |
| 490 | * @brief Lock the scheduler. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 491 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 492 | * This routine prevents the current thread from being preempted by another |
| 493 | * thread by instructing the scheduler to treat it as a cooperative thread. |
| 494 | * If the thread subsequently performs an operation that makes it unready, |
| 495 | * it will be context switched out in the normal manner. When the thread |
| 496 | * again becomes the current thread, its non-preemptible status is maintained. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 497 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 498 | * This routine can be called recursively. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 499 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 500 | * @note k_sched_lock() and k_sched_unlock() should normally be used |
| 501 | * when the operation being performed can be safely interrupted by ISRs. |
| 502 | * However, if the amount of processing involved is very small, better |
| 503 | * performance may be obtained by using irq_lock() and irq_unlock(). |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 504 | * |
| 505 | * @return N/A |
| 506 | */ |
| 507 | extern void k_sched_lock(void); |
| 508 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 509 | /** |
| 510 | * @brief Unlock the scheduler. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 511 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 512 | * This routine reverses the effect of a previous call to k_sched_lock(). |
| 513 | * A thread must call the routine once for each time it called k_sched_lock() |
| 514 | * before the thread becomes preemptible. |
Benjamin Walsh | d7ad176 | 2016-11-10 14:46:58 -0500 | [diff] [blame] | 515 | * |
| 516 | * @return N/A |
| 517 | */ |
| 518 | extern void k_sched_unlock(void); |
| 519 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 520 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 521 | * @brief Set current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 522 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 523 | * This routine sets the custom data for the current thread to @ value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 524 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 525 | * Custom data is not used by the kernel itself, and is freely available |
| 526 | * for a thread to use as it sees fit. It can be used as a framework |
| 527 | * upon which to build thread-local storage. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 528 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 529 | * @param value New custom data value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 530 | * |
| 531 | * @return N/A |
| 532 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 533 | extern void k_thread_custom_data_set(void *value); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 534 | |
| 535 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 536 | * @brief Get current thread's custom data. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 537 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 538 | * This routine returns the custom data for the current thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 539 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 540 | * @return Current custom data value. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 541 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 542 | extern void *k_thread_custom_data_get(void); |
| 543 | |
| 544 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 545 | * @} end addtogroup thread_apis |
| 546 | */ |
| 547 | |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 548 | #include <sys_clock.h> |
| 549 | |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 550 | /** |
| 551 | * @addtogroup clock_apis |
| 552 | * @{ |
| 553 | */ |
| 554 | |
| 555 | /** |
| 556 | * @brief Generate null timeout delay. |
| 557 | * |
| 558 | * This macro generates a timeout delay that that instructs a kernel API |
| 559 | * not to wait if the requested operation cannot be performed immediately. |
| 560 | * |
| 561 | * @return Timeout delay value. |
| 562 | */ |
| 563 | #define K_NO_WAIT 0 |
| 564 | |
| 565 | /** |
| 566 | * @brief Generate timeout delay from milliseconds. |
| 567 | * |
| 568 | * This macro generates a timeout delay that that instructs a kernel API |
| 569 | * to wait up to @a ms milliseconds to perform the requested operation. |
| 570 | * |
| 571 | * @param ms Duration in milliseconds. |
| 572 | * |
| 573 | * @return Timeout delay value. |
| 574 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 575 | #define K_MSEC(ms) (ms) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 576 | |
| 577 | /** |
| 578 | * @brief Generate timeout delay from seconds. |
| 579 | * |
| 580 | * This macro generates a timeout delay that that instructs a kernel API |
| 581 | * to wait up to @a s seconds to perform the requested operation. |
| 582 | * |
| 583 | * @param s Duration in seconds. |
| 584 | * |
| 585 | * @return Timeout delay value. |
| 586 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 587 | #define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 588 | |
| 589 | /** |
| 590 | * @brief Generate timeout delay from minutes. |
| 591 | * |
| 592 | * This macro generates a timeout delay that that instructs a kernel API |
| 593 | * to wait up to @a m minutes to perform the requested operation. |
| 594 | * |
| 595 | * @param m Duration in minutes. |
| 596 | * |
| 597 | * @return Timeout delay value. |
| 598 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 599 | #define K_MINUTES(m) K_SECONDS((m) * 60) |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 600 | |
| 601 | /** |
| 602 | * @brief Generate timeout delay from hours. |
| 603 | * |
| 604 | * This macro generates a timeout delay that that instructs a kernel API |
| 605 | * to wait up to @a h hours to perform the requested operation. |
| 606 | * |
| 607 | * @param h Duration in hours. |
| 608 | * |
| 609 | * @return Timeout delay value. |
| 610 | */ |
Johan Hedberg | 1447169 | 2016-11-13 10:52:15 +0200 | [diff] [blame] | 611 | #define K_HOURS(h) K_MINUTES((h) * 60) |
| 612 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 613 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 614 | * @brief Generate infinite timeout delay. |
| 615 | * |
| 616 | * This macro generates a timeout delay that that instructs a kernel API |
| 617 | * to wait as long as necessary to perform the requested operation. |
| 618 | * |
| 619 | * @return Timeout delay value. |
| 620 | */ |
| 621 | #define K_FOREVER (-1) |
| 622 | |
| 623 | /** |
| 624 | * @} end addtogroup clock_apis |
| 625 | */ |
| 626 | |
| 627 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 628 | * @cond INTERNAL_HIDDEN |
| 629 | */ |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 630 | |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 631 | /* added tick needed to account for tick in progress */ |
| 632 | #define _TICK_ALIGN 1 |
| 633 | |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 634 | static int64_t __ticks_to_ms(int64_t ticks) |
| 635 | { |
Benjamin Walsh | 57d55dc | 2016-10-04 16:58:08 -0400 | [diff] [blame] | 636 | #if CONFIG_SYS_CLOCK_EXISTS |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 637 | return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec; |
Benjamin Walsh | 57d55dc | 2016-10-04 16:58:08 -0400 | [diff] [blame] | 638 | #else |
| 639 | __ASSERT(ticks == 0, ""); |
| 640 | return 0; |
| 641 | #endif |
Benjamin Walsh | a9604bd | 2016-09-21 11:05:56 -0400 | [diff] [blame] | 642 | } |
| 643 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 644 | /* timeouts */ |
| 645 | |
| 646 | struct _timeout; |
| 647 | typedef void (*_timeout_func_t)(struct _timeout *t); |
| 648 | |
| 649 | struct _timeout { |
| 650 | sys_dlist_t node; |
Benjamin Walsh | 055262c | 2016-10-05 17:16:01 -0400 | [diff] [blame] | 651 | struct k_thread *thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 652 | sys_dlist_t *wait_q; |
| 653 | int32_t delta_ticks_from_prev; |
| 654 | _timeout_func_t func; |
| 655 | }; |
| 656 | |
Johan Hedberg | f99ad3f | 2016-12-09 10:39:49 +0200 | [diff] [blame] | 657 | extern int32_t _timeout_remaining_get(struct _timeout *timeout); |
| 658 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 659 | /** |
| 660 | * INTERNAL_HIDDEN @endcond |
| 661 | */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 662 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 663 | /** |
| 664 | * @cond INTERNAL_HIDDEN |
| 665 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 666 | |
| 667 | struct k_timer { |
| 668 | /* |
| 669 | * _timeout structure must be first here if we want to use |
| 670 | * dynamic timer allocation. timeout.node is used in the double-linked |
| 671 | * list of free timers |
| 672 | */ |
| 673 | struct _timeout timeout; |
| 674 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 675 | /* wait queue for the (single) thread waiting on this timer */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 676 | _wait_q_t wait_q; |
| 677 | |
| 678 | /* runs in ISR context */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 679 | void (*expiry_fn)(struct k_timer *); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 680 | |
| 681 | /* runs in the context of the thread that calls k_timer_stop() */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 682 | void (*stop_fn)(struct k_timer *); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 683 | |
| 684 | /* timer period */ |
| 685 | int32_t period; |
| 686 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 687 | /* timer status */ |
| 688 | uint32_t status; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 689 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 690 | /* used to support legacy timer APIs */ |
| 691 | void *_legacy_data; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 692 | |
| 693 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_timer); |
| 694 | }; |
| 695 | |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 696 | #define K_TIMER_INITIALIZER(obj, expiry, stop) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 697 | { \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 698 | .timeout.delta_ticks_from_prev = -1, \ |
| 699 | .timeout.wait_q = NULL, \ |
| 700 | .timeout.thread = NULL, \ |
| 701 | .timeout.func = _timer_expiration_handler, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 702 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 703 | .expiry_fn = expiry, \ |
| 704 | .stop_fn = stop, \ |
| 705 | .status = 0, \ |
| 706 | ._legacy_data = NULL, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 707 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 708 | } |
| 709 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 710 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 711 | * INTERNAL_HIDDEN @endcond |
| 712 | */ |
| 713 | |
| 714 | /** |
| 715 | * @defgroup timer_apis Timer APIs |
| 716 | * @ingroup kernel_apis |
| 717 | * @{ |
| 718 | */ |
| 719 | |
| 720 | /** |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 721 | * @typedef k_timer_expiry_t |
| 722 | * @brief Timer expiry function type. |
| 723 | * |
| 724 | * A timer's expiry function is executed by the system clock interrupt handler |
| 725 | * each time the timer expires. The expiry function is optional, and is only |
| 726 | * invoked if the timer has been initialized with one. |
| 727 | * |
| 728 | * @param timer Address of timer. |
| 729 | * |
| 730 | * @return N/A |
| 731 | */ |
| 732 | typedef void (*k_timer_expiry_t)(struct k_timer *timer); |
| 733 | |
| 734 | /** |
| 735 | * @typedef k_timer_stop_t |
| 736 | * @brief Timer stop function type. |
| 737 | * |
| 738 | * A timer's stop function is executed if the timer is stopped prematurely. |
| 739 | * The function runs in the context of the thread that stops the timer. |
| 740 | * The stop function is optional, and is only invoked if the timer has been |
| 741 | * initialized with one. |
| 742 | * |
| 743 | * @param timer Address of timer. |
| 744 | * |
| 745 | * @return N/A |
| 746 | */ |
| 747 | typedef void (*k_timer_stop_t)(struct k_timer *timer); |
| 748 | |
| 749 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 750 | * @brief Statically define and initialize a timer. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 751 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 752 | * The timer can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 753 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 754 | * @code extern struct k_timer <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 755 | * |
| 756 | * @param name Name of the timer variable. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 757 | * @param expiry_fn Function to invoke each time the timer expires. |
| 758 | * @param stop_fn Function to invoke if the timer is stopped while running. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 759 | */ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 760 | #define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 761 | struct k_timer name \ |
| 762 | __in_section(_k_timer, static, name) = \ |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 763 | K_TIMER_INITIALIZER(name, expiry_fn, stop_fn) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 764 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 765 | /** |
| 766 | * @brief Initialize a timer. |
| 767 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 768 | * This routine initializes a timer, prior to its first use. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 769 | * |
| 770 | * @param timer Address of timer. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 771 | * @param expiry_fn Function to invoke each time the timer expires. |
| 772 | * @param stop_fn Function to invoke if the timer is stopped while running. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 773 | * |
| 774 | * @return N/A |
| 775 | */ |
| 776 | extern void k_timer_init(struct k_timer *timer, |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 777 | k_timer_expiry_t expiry_fn, |
| 778 | k_timer_stop_t stop_fn); |
Andy Ross | 8d8b2ac | 2016-09-23 10:08:54 -0700 | [diff] [blame] | 779 | |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 780 | /** |
| 781 | * @brief Start a timer. |
| 782 | * |
| 783 | * This routine starts a timer, and resets its status to zero. The timer |
| 784 | * begins counting down using the specified duration and period values. |
| 785 | * |
| 786 | * Attempting to start a timer that is already running is permitted. |
| 787 | * The timer's status is reset to zero and the timer begins counting down |
| 788 | * using the new duration and period values. |
| 789 | * |
| 790 | * @param timer Address of timer. |
| 791 | * @param duration Initial timer duration (in milliseconds). |
| 792 | * @param period Timer period (in milliseconds). |
| 793 | * |
| 794 | * @return N/A |
| 795 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 796 | extern void k_timer_start(struct k_timer *timer, |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 797 | int32_t duration, int32_t period); |
| 798 | |
| 799 | /** |
| 800 | * @brief Stop a timer. |
| 801 | * |
| 802 | * This routine stops a running timer prematurely. The timer's stop function, |
| 803 | * if one exists, is invoked by the caller. |
| 804 | * |
| 805 | * Attempting to stop a timer that is not running is permitted, but has no |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 806 | * effect on the timer. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 807 | * |
| 808 | * @param timer Address of timer. |
| 809 | * |
| 810 | * @return N/A |
| 811 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 812 | extern void k_timer_stop(struct k_timer *timer); |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 813 | |
| 814 | /** |
| 815 | * @brief Read timer status. |
| 816 | * |
| 817 | * This routine reads the timer's status, which indicates the number of times |
| 818 | * it has expired since its status was last read. |
| 819 | * |
| 820 | * Calling this routine resets the timer's status to zero. |
| 821 | * |
| 822 | * @param timer Address of timer. |
| 823 | * |
| 824 | * @return Timer status. |
| 825 | */ |
| 826 | extern uint32_t k_timer_status_get(struct k_timer *timer); |
| 827 | |
| 828 | /** |
| 829 | * @brief Synchronize thread to timer expiration. |
| 830 | * |
| 831 | * This routine blocks the calling thread until the timer's status is non-zero |
| 832 | * (indicating that it has expired at least once since it was last examined) |
| 833 | * or the timer is stopped. If the timer status is already non-zero, |
| 834 | * or the timer is already stopped, the caller continues without waiting. |
| 835 | * |
| 836 | * Calling this routine resets the timer's status to zero. |
| 837 | * |
| 838 | * This routine must not be used by interrupt handlers, since they are not |
| 839 | * allowed to block. |
| 840 | * |
| 841 | * @param timer Address of timer. |
| 842 | * |
| 843 | * @return Timer status. |
| 844 | */ |
| 845 | extern uint32_t k_timer_status_sync(struct k_timer *timer); |
| 846 | |
| 847 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 848 | * @brief Get time remaining before a timer next expires. |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 849 | * |
| 850 | * This routine computes the (approximate) time remaining before a running |
| 851 | * timer next expires. If the timer is not running, it returns zero. |
| 852 | * |
| 853 | * @param timer Address of timer. |
| 854 | * |
| 855 | * @return Remaining time (in milliseconds). |
| 856 | */ |
Johan Hedberg | f99ad3f | 2016-12-09 10:39:49 +0200 | [diff] [blame] | 857 | static inline int32_t k_timer_remaining_get(struct k_timer *timer) |
| 858 | { |
| 859 | return _timeout_remaining_get(&timer->timeout); |
| 860 | } |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 861 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 862 | /** |
| 863 | * @} end defgroup timer_apis |
| 864 | */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 865 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 866 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 867 | * @addtogroup clock_apis |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 868 | * @{ |
| 869 | */ |
Allan Stephens | 45bfa37 | 2016-10-12 12:39:42 -0500 | [diff] [blame] | 870 | |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 871 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 872 | * @brief Get system uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 873 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 874 | * This routine returns the elapsed time since the system booted, |
| 875 | * in milliseconds. |
| 876 | * |
| 877 | * @return Current uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 878 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 879 | extern int64_t k_uptime_get(void); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 880 | |
| 881 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 882 | * @brief Get system uptime (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 883 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 884 | * This routine returns the lower 32-bits of the elapsed time since the system |
| 885 | * booted, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 886 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 887 | * This routine can be more efficient than k_uptime_get(), as it reduces the |
| 888 | * need for interrupt locking and 64-bit math. However, the 32-bit result |
| 889 | * cannot hold a system uptime time larger than approximately 50 days, so the |
| 890 | * caller must handle possible rollovers. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 891 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 892 | * @return Current uptime. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 893 | */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 894 | extern uint32_t k_uptime_get_32(void); |
| 895 | |
| 896 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 897 | * @brief Get elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 898 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 899 | * This routine computes the elapsed time between the current system uptime |
| 900 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 901 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 902 | * @param reftime Pointer to a reference time, which is updated to the current |
| 903 | * uptime upon return. |
| 904 | * |
| 905 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 906 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 907 | extern int64_t k_uptime_delta(int64_t *reftime); |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 908 | |
| 909 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 910 | * @brief Get elapsed time (32-bit version). |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 911 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 912 | * This routine computes the elapsed time between the current system uptime |
| 913 | * and an earlier reference time, in milliseconds. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 914 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 915 | * This routine can be more efficient than k_uptime_delta(), as it reduces the |
| 916 | * need for interrupt locking and 64-bit math. However, the 32-bit result |
| 917 | * cannot hold an elapsed time larger than approximately 50 days, so the |
| 918 | * caller must handle possible rollovers. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 919 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 920 | * @param reftime Pointer to a reference time, which is updated to the current |
| 921 | * uptime upon return. |
| 922 | * |
| 923 | * @return Elapsed time. |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 924 | */ |
Benjamin Walsh | ba5ddc1 | 2016-09-21 16:01:22 -0400 | [diff] [blame] | 925 | extern uint32_t k_uptime_delta_32(int64_t *reftime); |
| 926 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 927 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 928 | * @brief Read the hardware clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 929 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 930 | * This routine returns the current time, as measured by the system's hardware |
| 931 | * clock. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 932 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 933 | * @return Current hardware clock up-counter (in cycles). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 934 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 935 | extern uint32_t k_cycle_get_32(void); |
| 936 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 937 | /** |
Allan Stephens | c2f15a4 | 2016-11-17 12:24:22 -0500 | [diff] [blame] | 938 | * @} end addtogroup clock_apis |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 939 | */ |
| 940 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 941 | /** |
| 942 | * @cond INTERNAL_HIDDEN |
| 943 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 944 | |
| 945 | struct k_fifo { |
| 946 | _wait_q_t wait_q; |
| 947 | sys_slist_t data_q; |
| 948 | |
| 949 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_fifo); |
| 950 | }; |
| 951 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 952 | #define K_FIFO_INITIALIZER(obj) \ |
| 953 | { \ |
| 954 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 955 | .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \ |
| 956 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 957 | } |
| 958 | |
| 959 | /** |
| 960 | * INTERNAL_HIDDEN @endcond |
| 961 | */ |
| 962 | |
| 963 | /** |
| 964 | * @defgroup fifo_apis Fifo APIs |
| 965 | * @ingroup kernel_apis |
| 966 | * @{ |
| 967 | */ |
| 968 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 969 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 970 | * @brief Initialize a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 971 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 972 | * This routine initializes a fifo object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 973 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 974 | * @param fifo Address of the fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 975 | * |
| 976 | * @return N/A |
| 977 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 978 | extern void k_fifo_init(struct k_fifo *fifo); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 979 | |
| 980 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 981 | * @brief Add an element to a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 982 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 983 | * This routine adds a data item to @a fifo. A fifo data item must be |
| 984 | * aligned on a 4-byte boundary, and the first 32 bits of the item are |
| 985 | * reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 986 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 987 | * @note Can be called by ISRs. |
| 988 | * |
| 989 | * @param fifo Address of the fifo. |
| 990 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 991 | * |
| 992 | * @return N/A |
| 993 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 994 | extern void k_fifo_put(struct k_fifo *fifo, void *data); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 995 | |
| 996 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 997 | * @brief Atomically add a list of elements to a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 998 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 999 | * This routine adds a list of data items to @a fifo in one operation. |
| 1000 | * The data items must be in a singly-linked list, with the first 32 bits |
| 1001 | * each data item pointing to the next data item; the list must be |
| 1002 | * NULL-terminated. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1003 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1004 | * @note Can be called by ISRs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1005 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1006 | * @param fifo Address of the fifo. |
| 1007 | * @param head Pointer to first node in singly-linked list. |
| 1008 | * @param tail Pointer to last node in singly-linked list. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1009 | * |
| 1010 | * @return N/A |
| 1011 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1012 | 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] | 1013 | |
| 1014 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1015 | * @brief Atomically add a list of elements to a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1016 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1017 | * This routine adds a list of data items to @a fifo in one operation. |
| 1018 | * The data items must be in a singly-linked list implemented using a |
| 1019 | * sys_slist_t object. Upon completion, the sys_slist_t object is invalid |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1020 | * and must be re-initialized via sys_slist_init(). |
| 1021 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1022 | * @note Can be called by ISRs. |
| 1023 | * |
| 1024 | * @param fifo Address of the fifo. |
| 1025 | * @param list Pointer to sys_slist_t object. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1026 | * |
| 1027 | * @return N/A |
| 1028 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1029 | 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] | 1030 | |
| 1031 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1032 | * @brief Get an element from a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1033 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1034 | * This routine removes a data item from @a fifo in a "first in, first out" |
| 1035 | * manner. The first 32 bits of the data item are reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1036 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1037 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1038 | * |
| 1039 | * @param fifo Address of the fifo. |
| 1040 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1041 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1042 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 1043 | * @return Address of the data item if successful; NULL if returned |
| 1044 | * without waiting, or waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1045 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1046 | extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout); |
| 1047 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1048 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1049 | * @brief Statically define and initialize a fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1050 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1051 | * The fifo can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1052 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1053 | * @code extern struct k_fifo <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1054 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1055 | * @param name Name of the fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1056 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1057 | #define K_FIFO_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1058 | struct k_fifo name \ |
| 1059 | __in_section(_k_fifo, static, name) = \ |
| 1060 | K_FIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1061 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1062 | /** |
| 1063 | * @} end defgroup fifo_apis |
| 1064 | */ |
| 1065 | |
| 1066 | /** |
| 1067 | * @cond INTERNAL_HIDDEN |
| 1068 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1069 | |
| 1070 | struct k_lifo { |
| 1071 | _wait_q_t wait_q; |
| 1072 | void *list; |
| 1073 | |
| 1074 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_lifo); |
| 1075 | }; |
| 1076 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1077 | #define K_LIFO_INITIALIZER(obj) \ |
| 1078 | { \ |
| 1079 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1080 | .list = NULL, \ |
| 1081 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1082 | } |
| 1083 | |
| 1084 | /** |
| 1085 | * INTERNAL_HIDDEN @endcond |
| 1086 | */ |
| 1087 | |
| 1088 | /** |
| 1089 | * @defgroup lifo_apis Lifo APIs |
| 1090 | * @ingroup kernel_apis |
| 1091 | * @{ |
| 1092 | */ |
| 1093 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1094 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1095 | * @brief Initialize a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1096 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1097 | * This routine initializes a lifo object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1098 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1099 | * @param lifo Address of the lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1100 | * |
| 1101 | * @return N/A |
| 1102 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1103 | extern void k_lifo_init(struct k_lifo *lifo); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1104 | |
| 1105 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1106 | * @brief Add an element to a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1107 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1108 | * This routine adds a data item to @a lifo. A lifo data item must be |
| 1109 | * aligned on a 4-byte boundary, and the first 32 bits of the item are |
| 1110 | * reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1111 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1112 | * @note Can be called by ISRs. |
| 1113 | * |
| 1114 | * @param lifo Address of the lifo. |
| 1115 | * @param data Address of the data item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1116 | * |
| 1117 | * @return N/A |
| 1118 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1119 | extern void k_lifo_put(struct k_lifo *lifo, void *data); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1120 | |
| 1121 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1122 | * @brief Get an element from a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1123 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1124 | * This routine removes a data item from @a lifo in a "last in, first out" |
| 1125 | * manner. The first 32 bits of the data item are reserved for the kernel's use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1126 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1127 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1128 | * |
| 1129 | * @param lifo Address of the lifo. |
| 1130 | * @param timeout Waiting period to obtain a data item (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1131 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1132 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 1133 | * @return Address of the data item if successful; NULL if returned |
| 1134 | * without waiting, or waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1135 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1136 | extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout); |
| 1137 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1138 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1139 | * @brief Statically define and initialize a lifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1140 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1141 | * The lifo can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1142 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1143 | * @code extern struct k_lifo <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1144 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1145 | * @param name Name of the fifo. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1146 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1147 | #define K_LIFO_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1148 | struct k_lifo name \ |
| 1149 | __in_section(_k_lifo, static, name) = \ |
| 1150 | K_LIFO_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1151 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1152 | /** |
| 1153 | * @} end defgroup lifo_apis |
| 1154 | */ |
| 1155 | |
| 1156 | /** |
| 1157 | * @cond INTERNAL_HIDDEN |
| 1158 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1159 | |
| 1160 | struct k_stack { |
| 1161 | _wait_q_t wait_q; |
| 1162 | uint32_t *base, *next, *top; |
| 1163 | |
| 1164 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_stack); |
| 1165 | }; |
| 1166 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1167 | #define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \ |
| 1168 | { \ |
| 1169 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1170 | .base = stack_buffer, \ |
| 1171 | .next = stack_buffer, \ |
| 1172 | .top = stack_buffer + stack_num_entries, \ |
| 1173 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1174 | } |
| 1175 | |
| 1176 | /** |
| 1177 | * INTERNAL_HIDDEN @endcond |
| 1178 | */ |
| 1179 | |
| 1180 | /** |
| 1181 | * @defgroup stack_apis Stack APIs |
| 1182 | * @ingroup kernel_apis |
| 1183 | * @{ |
| 1184 | */ |
| 1185 | |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1186 | /** |
| 1187 | * @brief Initialize a stack. |
| 1188 | * |
| 1189 | * This routine initializes a stack object, prior to its first use. |
| 1190 | * |
| 1191 | * @param stack Address of the stack. |
| 1192 | * @param buffer Address of array used to hold stacked values. |
| 1193 | * @param num_entries Maximum number of values that can be stacked. |
| 1194 | * |
| 1195 | * @return N/A |
| 1196 | */ |
Allan Stephens | 018cd9a | 2016-10-07 15:13:24 -0500 | [diff] [blame] | 1197 | extern void k_stack_init(struct k_stack *stack, |
| 1198 | uint32_t *buffer, int num_entries); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1199 | |
| 1200 | /** |
| 1201 | * @brief Push an element onto a stack. |
| 1202 | * |
| 1203 | * This routine adds a 32-bit value @a data to @a stack. |
| 1204 | * |
| 1205 | * @note Can be called by ISRs. |
| 1206 | * |
| 1207 | * @param stack Address of the stack. |
| 1208 | * @param data Value to push onto the stack. |
| 1209 | * |
| 1210 | * @return N/A |
| 1211 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1212 | extern void k_stack_push(struct k_stack *stack, uint32_t data); |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1213 | |
| 1214 | /** |
| 1215 | * @brief Pop an element from a stack. |
| 1216 | * |
| 1217 | * This routine removes a 32-bit value from @a stack in a "last in, first out" |
| 1218 | * manner and stores the value in @a data. |
| 1219 | * |
| 1220 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1221 | * |
| 1222 | * @param stack Address of the stack. |
| 1223 | * @param data Address of area to hold the value popped from the stack. |
| 1224 | * @param timeout Waiting period to obtain a value (in milliseconds), |
| 1225 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1226 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 1227 | * @retval 0 Element popped from stack. |
| 1228 | * @retval -EBUSY Returned without waiting. |
| 1229 | * @retval -EAGAIN Waiting period timed out. |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1230 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1231 | extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout); |
| 1232 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1233 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1234 | * @brief Statically define and initialize a stack |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1235 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1236 | * The stack can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1237 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1238 | * @code extern struct k_stack <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1239 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1240 | * @param name Name of the stack. |
| 1241 | * @param stack_num_entries Maximum number of values that can be stacked. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1242 | */ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 1243 | #define K_STACK_DEFINE(name, stack_num_entries) \ |
| 1244 | uint32_t __noinit \ |
| 1245 | _k_stack_buf_##name[stack_num_entries]; \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1246 | struct k_stack name \ |
| 1247 | __in_section(_k_stack, static, name) = \ |
Peter Mitsis | 602e6a8 | 2016-10-17 11:48:43 -0400 | [diff] [blame] | 1248 | K_STACK_INITIALIZER(name, _k_stack_buf_##name, \ |
| 1249 | stack_num_entries) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1250 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1251 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1252 | * @} end defgroup stack_apis |
| 1253 | */ |
| 1254 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1255 | struct k_work; |
| 1256 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1257 | /** |
| 1258 | * @defgroup workqueue_apis Workqueue Thread APIs |
| 1259 | * @ingroup kernel_apis |
| 1260 | * @{ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1261 | */ |
| 1262 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1263 | /** |
| 1264 | * @typedef k_work_handler_t |
| 1265 | * @brief Work item handler function type. |
| 1266 | * |
| 1267 | * A work item's handler function is executed by a workqueue's thread |
| 1268 | * when the work item is processed by the workqueue. |
| 1269 | * |
| 1270 | * @param work Address of the work item. |
| 1271 | * |
| 1272 | * @return N/A |
| 1273 | */ |
| 1274 | typedef void (*k_work_handler_t)(struct k_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1275 | |
| 1276 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1277 | * @cond INTERNAL_HIDDEN |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1278 | */ |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1279 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1280 | struct k_work_q { |
| 1281 | struct k_fifo fifo; |
| 1282 | }; |
| 1283 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1284 | enum { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 1285 | K_WORK_STATE_PENDING, /* Work item pending state */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1286 | }; |
| 1287 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1288 | struct k_work { |
| 1289 | void *_reserved; /* Used by k_fifo implementation. */ |
| 1290 | k_work_handler_t handler; |
| 1291 | atomic_t flags[1]; |
| 1292 | }; |
| 1293 | |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1294 | struct k_delayed_work { |
| 1295 | struct k_work work; |
| 1296 | struct _timeout timeout; |
| 1297 | struct k_work_q *work_q; |
| 1298 | }; |
| 1299 | |
| 1300 | extern struct k_work_q k_sys_work_q; |
| 1301 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1302 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1303 | * INTERNAL_HIDDEN @endcond |
| 1304 | */ |
| 1305 | |
| 1306 | /** |
| 1307 | * @brief Initialize a statically-defined work item. |
| 1308 | * |
| 1309 | * This macro can be used to initialize a statically-defined workqueue work |
| 1310 | * item, prior to its first use. For example, |
| 1311 | * |
| 1312 | * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode |
| 1313 | * |
| 1314 | * @param work_handler Function to invoke each time work item is processed. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1315 | */ |
| 1316 | #define K_WORK_INITIALIZER(work_handler) \ |
| 1317 | { \ |
| 1318 | ._reserved = NULL, \ |
| 1319 | .handler = work_handler, \ |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1320 | .flags = { 0 } \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1324 | * @brief Initialize a work item. |
| 1325 | * |
| 1326 | * This routine initializes a workqueue work item, prior to its first use. |
| 1327 | * |
| 1328 | * @param work Address of work item. |
| 1329 | * @param handler Function to invoke each time work item is processed. |
| 1330 | * |
| 1331 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1332 | */ |
| 1333 | static inline void k_work_init(struct k_work *work, k_work_handler_t handler) |
| 1334 | { |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1335 | atomic_clear_bit(work->flags, K_WORK_STATE_PENDING); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1336 | work->handler = handler; |
| 1337 | } |
| 1338 | |
| 1339 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1340 | * @brief Submit a work item. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 1341 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1342 | * This routine submits work item @a work to be processed by workqueue |
| 1343 | * @a work_q. If the work item is already pending in the workqueue's queue |
| 1344 | * as a result of an earlier submission, this routine has no effect on the |
| 1345 | * work item. If the work item has already been processed, or is currently |
| 1346 | * being processed, its work is considered complete and the work item can be |
| 1347 | * resubmitted. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 1348 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1349 | * @warning |
| 1350 | * A submitted work item must not be modified until it has been processed |
| 1351 | * by the workqueue. |
| 1352 | * |
| 1353 | * @note Can be called by ISRs. |
| 1354 | * |
| 1355 | * @param work_q Address of workqueue. |
| 1356 | * @param work Address of work item. |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 1357 | * |
| 1358 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1359 | */ |
| 1360 | static inline void k_work_submit_to_queue(struct k_work_q *work_q, |
| 1361 | struct k_work *work) |
| 1362 | { |
Luiz Augusto von Dentz | 4ab9d32 | 2016-09-26 09:39:27 +0300 | [diff] [blame] | 1363 | if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1364 | k_fifo_put(&work_q->fifo, work); |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1369 | * @brief Check if a work item is pending. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1370 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1371 | * This routine indicates if work item @a work is pending in a workqueue's |
| 1372 | * queue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1373 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1374 | * @note Can be called by ISRs. |
| 1375 | * |
| 1376 | * @param work Address of work item. |
| 1377 | * |
| 1378 | * @return 1 if work item is pending, or 0 if it is not pending. |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1379 | */ |
| 1380 | static inline int k_work_pending(struct k_work *work) |
| 1381 | { |
Iván Briano | 9c7b5ea | 2016-10-04 18:11:05 -0300 | [diff] [blame] | 1382 | return atomic_test_bit(work->flags, K_WORK_STATE_PENDING); |
Luiz Augusto von Dentz | ee1e99b | 2016-09-26 09:36:49 +0300 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1386 | * @brief Start a workqueue. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1387 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1388 | * This routine starts workqueue @a work_q. The workqueue spawns its work |
| 1389 | * processing thread, which runs forever. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1390 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1391 | * @param work_q Address of workqueue. |
| 1392 | * @param stack Pointer to work queue thread's stack space. |
| 1393 | * @param stack_size Size of the work queue thread's stack (in bytes). |
| 1394 | * @param prio Priority of the work queue's thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1395 | * |
| 1396 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1397 | */ |
Allan Stephens | 904cf97 | 2016-10-07 13:59:23 -0500 | [diff] [blame] | 1398 | extern void k_work_q_start(struct k_work_q *work_q, char *stack, |
Benjamin Walsh | 669360d | 2016-11-14 16:46:14 -0500 | [diff] [blame] | 1399 | size_t stack_size, int prio); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1400 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1401 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1402 | * @brief Initialize a delayed work item. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1403 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1404 | * This routine initializes a workqueue delayed work item, prior to |
| 1405 | * its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1406 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1407 | * @param work Address of delayed work item. |
| 1408 | * @param handler Function to invoke each time work item is processed. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1409 | * |
| 1410 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1411 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1412 | extern void k_delayed_work_init(struct k_delayed_work *work, |
| 1413 | k_work_handler_t handler); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1414 | |
| 1415 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1416 | * @brief Submit a delayed work item. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1417 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1418 | * This routine schedules work item @a work to be processed by workqueue |
| 1419 | * @a work_q after a delay of @a delay milliseconds. The routine initiates |
| 1420 | * an asychronous countdown for the work item and then returns to the caller. |
| 1421 | * Only when the countdown completes is the work item actually submitted to |
| 1422 | * the workqueue and becomes pending. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1423 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1424 | * Submitting a previously submitted delayed work item that is still |
| 1425 | * counting down cancels the existing submission and restarts the countdown |
| 1426 | * using the new delay. If the work item is currently pending on the |
| 1427 | * workqueue's queue because the countdown has completed it is too late to |
| 1428 | * resubmit the item, and resubmission fails without impacting the work item. |
| 1429 | * If the work item has already been processed, or is currently being processed, |
| 1430 | * its work is considered complete and the work item can be resubmitted. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1431 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1432 | * @warning |
| 1433 | * A delayed work item must not be modified until it has been processed |
| 1434 | * by the workqueue. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1435 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1436 | * @note Can be called by ISRs. |
| 1437 | * |
| 1438 | * @param work_q Address of workqueue. |
| 1439 | * @param work Address of delayed work item. |
| 1440 | * @param delay Delay before submitting the work item (in milliseconds). |
| 1441 | * |
| 1442 | * @retval 0 Work item countdown started. |
| 1443 | * @retval -EINPROGRESS Work item is already pending. |
| 1444 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 1445 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1446 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1447 | extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q, |
| 1448 | struct k_delayed_work *work, |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1449 | int32_t delay); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1450 | |
| 1451 | /** |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1452 | * @brief Cancel a delayed work item. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1453 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1454 | * This routine cancels the submission of delayed work item @a work. |
| 1455 | * A delayed work item can only be cancelled while its countdown is still |
| 1456 | * underway. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1457 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1458 | * @note Can be called by ISRs. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1459 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1460 | * @param work Address of delayed work item. |
| 1461 | * |
| 1462 | * @retval 0 Work item countdown cancelled. |
| 1463 | * @retval -EINPROGRESS Work item is already pending. |
| 1464 | * @retval -EINVAL Work item is being processed or has completed its work. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1465 | */ |
Benjamin Walsh | 72e5a39 | 2016-09-30 11:32:33 -0400 | [diff] [blame] | 1466 | extern int k_delayed_work_cancel(struct k_delayed_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1467 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1468 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1469 | * @brief Submit a work item to the system workqueue. |
| 1470 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1471 | * This routine submits work item @a work to be processed by the system |
| 1472 | * workqueue. If the work item is already pending in the workqueue's queue |
| 1473 | * as a result of an earlier submission, this routine has no effect on the |
| 1474 | * work item. If the work item has already been processed, or is currently |
| 1475 | * being processed, its work is considered complete and the work item can be |
| 1476 | * resubmitted. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1477 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1478 | * @warning |
| 1479 | * Work items submitted to the system workqueue should avoid using handlers |
| 1480 | * that block or yield since this may prevent the system workqueue from |
| 1481 | * processing other work items in a timely manner. |
| 1482 | * |
| 1483 | * @note Can be called by ISRs. |
| 1484 | * |
| 1485 | * @param work Address of work item. |
| 1486 | * |
| 1487 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1488 | */ |
| 1489 | static inline void k_work_submit(struct k_work *work) |
| 1490 | { |
| 1491 | k_work_submit_to_queue(&k_sys_work_q, work); |
| 1492 | } |
| 1493 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1494 | /** |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1495 | * @brief Submit a delayed work item to the system workqueue. |
| 1496 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1497 | * This routine schedules work item @a work to be processed by the system |
| 1498 | * workqueue after a delay of @a delay milliseconds. The routine initiates |
| 1499 | * an asychronous countdown for the work item and then returns to the caller. |
| 1500 | * Only when the countdown completes is the work item actually submitted to |
| 1501 | * the workqueue and becomes pending. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1502 | * |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1503 | * Submitting a previously submitted delayed work item that is still |
| 1504 | * counting down cancels the existing submission and restarts the countdown |
| 1505 | * using the new delay. If the work item is currently pending on the |
| 1506 | * workqueue's queue because the countdown has completed it is too late to |
| 1507 | * resubmit the item, and resubmission fails without impacting the work item. |
| 1508 | * If the work item has already been processed, or is currently being processed, |
| 1509 | * its work is considered complete and the work item can be resubmitted. |
| 1510 | * |
| 1511 | * @warning |
| 1512 | * Work items submitted to the system workqueue should avoid using handlers |
| 1513 | * that block or yield since this may prevent the system workqueue from |
| 1514 | * processing other work items in a timely manner. |
| 1515 | * |
| 1516 | * @note Can be called by ISRs. |
| 1517 | * |
| 1518 | * @param work Address of delayed work item. |
| 1519 | * @param delay Delay before submitting the work item (in milliseconds). |
| 1520 | * |
| 1521 | * @retval 0 Work item countdown started. |
| 1522 | * @retval -EINPROGRESS Work item is already pending. |
| 1523 | * @retval -EINVAL Work item is being processed or has completed its work. |
| 1524 | * @retval -EADDRINUSE Work item is pending on a different workqueue. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1525 | */ |
| 1526 | static inline int k_delayed_work_submit(struct k_delayed_work *work, |
Allan Stephens | 6bba9b0 | 2016-11-16 14:56:54 -0500 | [diff] [blame] | 1527 | int32_t delay) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1528 | { |
Allan Stephens | 6c98c4d | 2016-10-17 14:34:53 -0500 | [diff] [blame] | 1529 | 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] | 1530 | } |
| 1531 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1532 | /** |
Johan Hedberg | c8201b2 | 2016-12-09 10:42:22 +0200 | [diff] [blame^] | 1533 | * @brief Get time remaining before a delayed work gets scheduled. |
| 1534 | * |
| 1535 | * This routine computes the (approximate) time remaining before a |
| 1536 | * delayed work gets executed. If the delayed work is not waiting to be |
| 1537 | * schedules, it returns zero. |
| 1538 | * |
| 1539 | * @param work Delayed work item. |
| 1540 | * |
| 1541 | * @return Remaining time (in milliseconds). |
| 1542 | */ |
| 1543 | static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work) |
| 1544 | { |
| 1545 | return _timeout_remaining_get(&work->timeout); |
| 1546 | } |
| 1547 | |
| 1548 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1549 | * @} end defgroup workqueue_apis |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1550 | */ |
| 1551 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1552 | /** |
| 1553 | * @cond INTERNAL_HIDDEN |
| 1554 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1555 | |
| 1556 | struct k_mutex { |
| 1557 | _wait_q_t wait_q; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 1558 | struct k_thread *owner; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1559 | uint32_t lock_count; |
| 1560 | int owner_orig_prio; |
| 1561 | #ifdef CONFIG_OBJECT_MONITOR |
| 1562 | int num_lock_state_changes; |
| 1563 | int num_conflicts; |
| 1564 | #endif |
| 1565 | |
| 1566 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mutex); |
| 1567 | }; |
| 1568 | |
| 1569 | #ifdef CONFIG_OBJECT_MONITOR |
| 1570 | #define _MUTEX_INIT_OBJECT_MONITOR \ |
| 1571 | .num_lock_state_changes = 0, .num_conflicts = 0, |
| 1572 | #else |
| 1573 | #define _MUTEX_INIT_OBJECT_MONITOR |
| 1574 | #endif |
| 1575 | |
| 1576 | #define K_MUTEX_INITIALIZER(obj) \ |
| 1577 | { \ |
| 1578 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1579 | .owner = NULL, \ |
| 1580 | .lock_count = 0, \ |
| 1581 | .owner_orig_prio = K_LOWEST_THREAD_PRIO, \ |
| 1582 | _MUTEX_INIT_OBJECT_MONITOR \ |
| 1583 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1584 | } |
| 1585 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1586 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1587 | * INTERNAL_HIDDEN @endcond |
| 1588 | */ |
| 1589 | |
| 1590 | /** |
| 1591 | * @defgroup mutex_apis Mutex APIs |
| 1592 | * @ingroup kernel_apis |
| 1593 | * @{ |
| 1594 | */ |
| 1595 | |
| 1596 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1597 | * @brief Statically define and initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1598 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1599 | * The mutex can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1600 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1601 | * @code extern struct k_mutex <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1602 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1603 | * @param name Name of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1604 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1605 | #define K_MUTEX_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1606 | struct k_mutex name \ |
| 1607 | __in_section(_k_mutex, static, name) = \ |
| 1608 | K_MUTEX_INITIALIZER(name) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1609 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1610 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1611 | * @brief Initialize a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1612 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1613 | * This routine initializes a mutex object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1614 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1615 | * Upon completion, the mutex is available and does not have an owner. |
| 1616 | * |
| 1617 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1618 | * |
| 1619 | * @return N/A |
| 1620 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1621 | extern void k_mutex_init(struct k_mutex *mutex); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1622 | |
| 1623 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1624 | * @brief Lock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1625 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1626 | * This routine locks @a mutex. If the mutex is locked by another thread, |
| 1627 | * the calling thread waits until the mutex becomes available or until |
| 1628 | * a timeout occurs. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1629 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1630 | * A thread is permitted to lock a mutex it has already locked. The operation |
| 1631 | * completes immediately and the lock count is increased by 1. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1632 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1633 | * @param mutex Address of the mutex. |
| 1634 | * @param timeout Waiting period to lock the mutex (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1635 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1636 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 1637 | * @retval 0 Mutex locked. |
| 1638 | * @retval -EBUSY Returned without waiting. |
| 1639 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1640 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1641 | extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1642 | |
| 1643 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1644 | * @brief Unlock a mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1645 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1646 | * This routine unlocks @a mutex. The mutex must already be locked by the |
| 1647 | * calling thread. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1648 | * |
| 1649 | * The mutex cannot be claimed by another thread until it has been unlocked by |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1650 | * the calling thread as many times as it was previously locked by that |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1651 | * thread. |
| 1652 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1653 | * @param mutex Address of the mutex. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1654 | * |
| 1655 | * @return N/A |
| 1656 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1657 | extern void k_mutex_unlock(struct k_mutex *mutex); |
| 1658 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1659 | /** |
| 1660 | * @} end defgroup mutex_apis |
| 1661 | */ |
| 1662 | |
| 1663 | /** |
| 1664 | * @cond INTERNAL_HIDDEN |
| 1665 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1666 | |
| 1667 | struct k_sem { |
| 1668 | _wait_q_t wait_q; |
| 1669 | unsigned int count; |
| 1670 | unsigned int limit; |
| 1671 | |
| 1672 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_sem); |
| 1673 | }; |
| 1674 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1675 | #define K_SEM_INITIALIZER(obj, initial_count, count_limit) \ |
| 1676 | { \ |
| 1677 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
| 1678 | .count = initial_count, \ |
| 1679 | .limit = count_limit, \ |
| 1680 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1681 | } |
| 1682 | |
| 1683 | /** |
| 1684 | * INTERNAL_HIDDEN @endcond |
| 1685 | */ |
| 1686 | |
| 1687 | /** |
| 1688 | * @defgroup semaphore_apis Semaphore APIs |
| 1689 | * @ingroup kernel_apis |
| 1690 | * @{ |
| 1691 | */ |
| 1692 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1693 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1694 | * @brief Initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1695 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1696 | * This routine initializes a semaphore object, prior to its first use. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1697 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1698 | * @param sem Address of the semaphore. |
| 1699 | * @param initial_count Initial semaphore count. |
| 1700 | * @param limit Maximum permitted semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1701 | * |
| 1702 | * @return N/A |
| 1703 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1704 | extern void k_sem_init(struct k_sem *sem, unsigned int initial_count, |
| 1705 | unsigned int limit); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1706 | |
| 1707 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1708 | * @brief Take a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1709 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1710 | * This routine takes @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1711 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1712 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1713 | * |
| 1714 | * @param sem Address of the semaphore. |
| 1715 | * @param timeout Waiting period to take the semaphore (in milliseconds), |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1716 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1717 | * |
Benjamin Walsh | 91f834c | 2016-12-01 11:39:49 -0500 | [diff] [blame] | 1718 | * @note When porting code from the nanokernel legacy API to the new API, be |
| 1719 | * careful with the return value of this function. The return value is the |
| 1720 | * reverse of the one of nano_sem_take family of APIs: 0 means success, and |
| 1721 | * non-zero means failure, while the nano_sem_take family returns 1 for success |
| 1722 | * and 0 for failure. |
| 1723 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 1724 | * @retval 0 Semaphore taken. |
| 1725 | * @retval -EBUSY Returned without waiting. |
| 1726 | * @retval -EAGAIN Waiting period timed out. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1727 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1728 | extern int k_sem_take(struct k_sem *sem, int32_t timeout); |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1729 | |
| 1730 | /** |
| 1731 | * @brief Give a semaphore. |
| 1732 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1733 | * This routine gives @a sem, unless the semaphore is already at its maximum |
| 1734 | * permitted count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1735 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1736 | * @note Can be called by ISRs. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1737 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1738 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1739 | * |
| 1740 | * @return N/A |
| 1741 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1742 | extern void k_sem_give(struct k_sem *sem); |
| 1743 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1744 | /** |
| 1745 | * @brief Reset a semaphore's count to zero. |
| 1746 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1747 | * This routine sets the count of @a sem to zero. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1748 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1749 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1750 | * |
| 1751 | * @return N/A |
| 1752 | */ |
Benjamin Walsh | 70c68b9 | 2016-09-21 10:37:34 -0400 | [diff] [blame] | 1753 | static inline void k_sem_reset(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1754 | { |
| 1755 | sem->count = 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1756 | } |
| 1757 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1758 | /** |
| 1759 | * @brief Get a semaphore's count. |
| 1760 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1761 | * This routine returns the current count of @a sem. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1762 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1763 | * @param sem Address of the semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1764 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1765 | * @return Current semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1766 | */ |
Tomasz Bursztyka | 276086d | 2016-09-21 16:03:21 +0200 | [diff] [blame] | 1767 | static inline unsigned int k_sem_count_get(struct k_sem *sem) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1768 | { |
| 1769 | return sem->count; |
| 1770 | } |
| 1771 | |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1772 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1773 | * @brief Statically define and initialize a semaphore. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1774 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1775 | * The semaphore can be accessed outside the module where it is defined using: |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1776 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1777 | * @code extern struct k_sem <name>; @endcode |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1778 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1779 | * @param name Name of the semaphore. |
| 1780 | * @param initial_count Initial semaphore count. |
| 1781 | * @param count_limit Maximum permitted semaphore count. |
Benjamin Walsh | b9c1a06 | 2016-10-15 17:12:35 -0400 | [diff] [blame] | 1782 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1783 | #define K_SEM_DEFINE(name, initial_count, count_limit) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1784 | struct k_sem name \ |
| 1785 | __in_section(_k_sem, static, name) = \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1786 | K_SEM_INITIALIZER(name, initial_count, count_limit) |
| 1787 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1788 | /** |
| 1789 | * @} end defgroup semaphore_apis |
| 1790 | */ |
| 1791 | |
| 1792 | /** |
| 1793 | * @defgroup alert_apis Alert APIs |
| 1794 | * @ingroup kernel_apis |
| 1795 | * @{ |
| 1796 | */ |
| 1797 | |
Allan Stephens | 5eceb85 | 2016-11-16 10:16:30 -0500 | [diff] [blame] | 1798 | /** |
| 1799 | * @typedef k_alert_handler_t |
| 1800 | * @brief Alert handler function type. |
| 1801 | * |
| 1802 | * An alert's alert handler function is invoked by the system workqueue |
| 1803 | * when the alert is signalled. The alert handler function is optional, |
| 1804 | * and is only invoked if the alert has been initialized with one. |
| 1805 | * |
| 1806 | * @param alert Address of the alert. |
| 1807 | * |
| 1808 | * @return 0 if alert has been consumed; non-zero if alert should pend. |
| 1809 | */ |
| 1810 | typedef int (*k_alert_handler_t)(struct k_alert *alert); |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1811 | |
| 1812 | /** |
| 1813 | * @} end defgroup alert_apis |
| 1814 | */ |
| 1815 | |
| 1816 | /** |
| 1817 | * @cond INTERNAL_HIDDEN |
| 1818 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1819 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1820 | #define K_ALERT_DEFAULT NULL |
| 1821 | #define K_ALERT_IGNORE ((void *)(-1)) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1822 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1823 | struct k_alert { |
| 1824 | k_alert_handler_t handler; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1825 | atomic_t send_count; |
| 1826 | struct k_work work_item; |
| 1827 | struct k_sem sem; |
| 1828 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1829 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_alert); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1830 | }; |
| 1831 | |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1832 | extern void _alert_deliver(struct k_work *work); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1833 | |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1834 | #define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1835 | { \ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1836 | .handler = (k_alert_handler_t)alert_handler, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1837 | .send_count = ATOMIC_INIT(0), \ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1838 | .work_item = K_WORK_INITIALIZER(_alert_deliver), \ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1839 | .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1840 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1841 | } |
| 1842 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1843 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1844 | * INTERNAL_HIDDEN @endcond |
| 1845 | */ |
| 1846 | |
| 1847 | /** |
| 1848 | * @addtogroup alert_apis |
| 1849 | * @{ |
| 1850 | */ |
| 1851 | |
| 1852 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1853 | * @brief Statically define and initialize an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1854 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1855 | * The alert can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1856 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1857 | * @code extern struct k_alert <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1858 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1859 | * @param name Name of the alert. |
| 1860 | * @param alert_handler Action to take when alert is sent. Specify either |
| 1861 | * the address of a function to be invoked by the system workqueue |
| 1862 | * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or |
| 1863 | * K_ALERT_DEFAULT (which causes the alert to pend). |
| 1864 | * @param max_num_pending_alerts Maximum number of pending alerts. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1865 | */ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1866 | #define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1867 | struct k_alert name \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1868 | __in_section(_k_alert, static, name) = \ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1869 | K_ALERT_INITIALIZER(name, alert_handler, \ |
| 1870 | max_num_pending_alerts) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1871 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1872 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1873 | * @brief Initialize an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1874 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1875 | * This routine initializes an alert object, prior to its first use. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1876 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1877 | * @param alert Address of the alert. |
| 1878 | * @param handler Action to take when alert is sent. Specify either the address |
| 1879 | * of a function to be invoked by the system workqueue thread, |
| 1880 | * K_ALERT_IGNORE (which causes the alert to be ignored), or |
| 1881 | * K_ALERT_DEFAULT (which causes the alert to pend). |
| 1882 | * @param max_num_pending_alerts Maximum number of pending alerts. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1883 | * |
| 1884 | * @return N/A |
| 1885 | */ |
Peter Mitsis | 058fa4e | 2016-10-25 14:42:30 -0400 | [diff] [blame] | 1886 | extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler, |
| 1887 | unsigned int max_num_pending_alerts); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1888 | |
| 1889 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1890 | * @brief Receive an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1891 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1892 | * This routine receives a pending alert for @a alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1893 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1894 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
| 1895 | * |
| 1896 | * @param alert Address of the alert. |
| 1897 | * @param timeout Waiting period to receive the alert (in milliseconds), |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1898 | * or one of the special values K_NO_WAIT and K_FOREVER. |
| 1899 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 1900 | * @retval 0 Alert received. |
| 1901 | * @retval -EBUSY Returned without waiting. |
| 1902 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1903 | */ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1904 | extern int k_alert_recv(struct k_alert *alert, int32_t timeout); |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1905 | |
| 1906 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1907 | * @brief Signal an alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1908 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1909 | * This routine signals @a alert. The action specified for @a alert will |
| 1910 | * be taken, which may trigger the execution of an alert handler function |
| 1911 | * and/or cause the alert to pend (assuming the alert has not reached its |
| 1912 | * maximum number of pending alerts). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1913 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1914 | * @note Can be called by ISRs. |
| 1915 | * |
| 1916 | * @param alert Address of the alert. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1917 | * |
| 1918 | * @return N/A |
| 1919 | */ |
Benjamin Walsh | 31a3f6a | 2016-10-25 13:28:35 -0400 | [diff] [blame] | 1920 | extern void k_alert_send(struct k_alert *alert); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1921 | |
| 1922 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1923 | * @} end addtogroup alert_apis |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1924 | */ |
| 1925 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1926 | /** |
| 1927 | * @cond INTERNAL_HIDDEN |
| 1928 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1929 | |
| 1930 | struct k_msgq { |
| 1931 | _wait_q_t wait_q; |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 1932 | size_t msg_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1933 | uint32_t max_msgs; |
| 1934 | char *buffer_start; |
| 1935 | char *buffer_end; |
| 1936 | char *read_ptr; |
| 1937 | char *write_ptr; |
| 1938 | uint32_t used_msgs; |
| 1939 | |
| 1940 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_msgq); |
| 1941 | }; |
| 1942 | |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1943 | #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] | 1944 | { \ |
| 1945 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1946 | .max_msgs = q_max_msgs, \ |
| 1947 | .msg_size = q_msg_size, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1948 | .buffer_start = q_buffer, \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1949 | .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1950 | .read_ptr = q_buffer, \ |
| 1951 | .write_ptr = q_buffer, \ |
| 1952 | .used_msgs = 0, \ |
| 1953 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 1954 | } |
| 1955 | |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1956 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 1957 | * INTERNAL_HIDDEN @endcond |
| 1958 | */ |
| 1959 | |
| 1960 | /** |
| 1961 | * @defgroup msgq_apis Message Queue APIs |
| 1962 | * @ingroup kernel_apis |
| 1963 | * @{ |
| 1964 | */ |
| 1965 | |
| 1966 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1967 | * @brief Statically define and initialize a message queue. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1968 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1969 | * The message queue's ring buffer contains space for @a q_max_msgs messages, |
| 1970 | * each of which is @a q_msg_size bytes long. The buffer is aligned to a |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 1971 | * @a q_align -byte boundary, which must be a power of 2. To ensure that each |
| 1972 | * message is similarly aligned to this boundary, @a q_msg_size must also be |
| 1973 | * a multiple of @a q_align. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1974 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1975 | * The message queue can be accessed outside the module where it is defined |
| 1976 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1977 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 1978 | * @code extern struct k_msgq <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 1979 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1980 | * @param q_name Name of the message queue. |
| 1981 | * @param q_msg_size Message size (in bytes). |
| 1982 | * @param q_max_msgs Maximum number of messages that can be queued. |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 1983 | * @param q_align Alignment of the message queue's ring buffer. |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1984 | */ |
| 1985 | #define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \ |
| 1986 | static char __noinit __aligned(q_align) \ |
| 1987 | _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 1988 | struct k_msgq q_name \ |
| 1989 | __in_section(_k_msgq, static, q_name) = \ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 1990 | K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \ |
| 1991 | q_msg_size, q_max_msgs) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1992 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 1993 | /** |
| 1994 | * @brief Initialize a message queue. |
| 1995 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 1996 | * This routine initializes a message queue object, prior to its first use. |
| 1997 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 1998 | * The message queue's ring buffer must contain space for @a max_msgs messages, |
| 1999 | * each of which is @a msg_size bytes long. The buffer must be aligned to an |
| 2000 | * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure |
| 2001 | * that each message is similarly aligned to this boundary, @a q_msg_size |
| 2002 | * must also be a multiple of N. |
| 2003 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2004 | * @param q Address of the message queue. |
| 2005 | * @param buffer Pointer to ring buffer that holds queued messages. |
| 2006 | * @param msg_size Message size (in bytes). |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2007 | * @param max_msgs Maximum number of messages that can be queued. |
| 2008 | * |
| 2009 | * @return N/A |
| 2010 | */ |
Peter Mitsis | 1da807e | 2016-10-06 11:36:59 -0400 | [diff] [blame] | 2011 | extern void k_msgq_init(struct k_msgq *q, char *buffer, |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 2012 | size_t msg_size, uint32_t max_msgs); |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2013 | |
| 2014 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2015 | * @brief Send a message to a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2016 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2017 | * This routine sends a message to message queue @a q. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2018 | * |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 2019 | * @note Can be called by ISRs. |
| 2020 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2021 | * @param q Address of the message queue. |
| 2022 | * @param data Pointer to the message. |
| 2023 | * @param timeout Waiting period to add the message (in milliseconds), |
| 2024 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2025 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2026 | * @retval 0 Message sent. |
| 2027 | * @retval -ENOMSG Returned without waiting or queue purged. |
| 2028 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2029 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2030 | 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] | 2031 | |
| 2032 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2033 | * @brief Receive a message from a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2034 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2035 | * This routine receives a message from message queue @a q in a "first in, |
| 2036 | * first out" manner. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2037 | * |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2038 | * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT. |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 2039 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2040 | * @param q Address of the message queue. |
| 2041 | * @param data Address of area to hold the received message. |
| 2042 | * @param timeout Waiting period to receive the message (in milliseconds), |
| 2043 | * or one of the special values K_NO_WAIT and K_FOREVER. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2044 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2045 | * @retval 0 Message received. |
| 2046 | * @retval -ENOMSG Returned without waiting. |
| 2047 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2048 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2049 | 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] | 2050 | |
| 2051 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2052 | * @brief Purge a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2053 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2054 | * This routine discards all unreceived messages in a message queue's ring |
| 2055 | * buffer. Any threads that are blocked waiting to send a message to the |
| 2056 | * message queue are unblocked and see an -ENOMSG error code. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2057 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2058 | * @param q Address of the message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2059 | * |
| 2060 | * @return N/A |
| 2061 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2062 | extern void k_msgq_purge(struct k_msgq *q); |
| 2063 | |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 2064 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2065 | * @brief Get the amount of free space in a message queue. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 2066 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2067 | * This routine returns the number of unused entries in a message queue's |
| 2068 | * ring buffer. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 2069 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2070 | * @param q Address of the message queue. |
| 2071 | * |
| 2072 | * @return Number of unused ring buffer entries. |
Peter Mitsis | 67be249 | 2016-10-07 11:44:34 -0400 | [diff] [blame] | 2073 | */ |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 2074 | 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] | 2075 | { |
| 2076 | return q->max_msgs - q->used_msgs; |
| 2077 | } |
| 2078 | |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2079 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2080 | * @brief Get the number of messages in a message queue. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2081 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2082 | * This routine returns the number of messages in a message queue's ring buffer. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2083 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2084 | * @param q Address of the message queue. |
| 2085 | * |
| 2086 | * @return Number of messages. |
Peter Mitsis | d7a3750 | 2016-10-13 11:37:40 -0400 | [diff] [blame] | 2087 | */ |
Peter Mitsis | 026b4ed | 2016-10-13 11:41:45 -0400 | [diff] [blame] | 2088 | 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] | 2089 | { |
| 2090 | return q->used_msgs; |
| 2091 | } |
| 2092 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2093 | /** |
| 2094 | * @} end defgroup msgq_apis |
| 2095 | */ |
| 2096 | |
| 2097 | /** |
| 2098 | * @defgroup mem_pool_apis Memory Pool APIs |
| 2099 | * @ingroup kernel_apis |
| 2100 | * @{ |
| 2101 | */ |
| 2102 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2103 | struct k_mem_block { |
Peter Mitsis | 0cb65c3 | 2016-09-29 14:07:36 -0400 | [diff] [blame] | 2104 | struct k_mem_pool *pool_id; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2105 | void *addr_in_pool; |
| 2106 | void *data; |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2107 | size_t req_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2108 | }; |
| 2109 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2110 | /** |
| 2111 | * @} end defgroup mem_pool_apis |
| 2112 | */ |
| 2113 | |
| 2114 | /** |
| 2115 | * @defgroup mailbox_apis Mailbox APIs |
| 2116 | * @ingroup kernel_apis |
| 2117 | * @{ |
| 2118 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2119 | |
| 2120 | struct k_mbox_msg { |
| 2121 | /** internal use only - needed for legacy API support */ |
| 2122 | uint32_t _mailbox; |
| 2123 | /** size of message (in bytes) */ |
Peter Mitsis | d93078c | 2016-10-14 12:59:37 -0400 | [diff] [blame] | 2124 | size_t size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2125 | /** application-defined information value */ |
| 2126 | uint32_t info; |
| 2127 | /** sender's message data buffer */ |
| 2128 | void *tx_data; |
| 2129 | /** internal use only - needed for legacy API support */ |
| 2130 | void *_rx_data; |
| 2131 | /** message data block descriptor */ |
| 2132 | struct k_mem_block tx_block; |
| 2133 | /** source thread id */ |
| 2134 | k_tid_t rx_source_thread; |
| 2135 | /** target thread id */ |
| 2136 | k_tid_t tx_target_thread; |
| 2137 | /** internal use only - thread waiting on send (may be a dummy) */ |
| 2138 | k_tid_t _syncing_thread; |
| 2139 | #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0) |
| 2140 | /** internal use only - semaphore used during asynchronous send */ |
| 2141 | struct k_sem *_async_sem; |
| 2142 | #endif |
| 2143 | }; |
| 2144 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2145 | /** |
| 2146 | * @cond INTERNAL_HIDDEN |
| 2147 | */ |
| 2148 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2149 | struct k_mbox { |
| 2150 | _wait_q_t tx_msg_queue; |
| 2151 | _wait_q_t rx_msg_queue; |
| 2152 | |
| 2153 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mbox); |
| 2154 | }; |
| 2155 | |
| 2156 | #define K_MBOX_INITIALIZER(obj) \ |
| 2157 | { \ |
| 2158 | .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \ |
| 2159 | .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \ |
| 2160 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 2161 | } |
| 2162 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2163 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2164 | * INTERNAL_HIDDEN @endcond |
| 2165 | */ |
| 2166 | |
| 2167 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2168 | * @brief Statically define and initialize a mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2169 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2170 | * The mailbox is to be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2171 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2172 | * @code extern struct k_mbox <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2173 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2174 | * @param name Name of the mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2175 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2176 | #define K_MBOX_DEFINE(name) \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 2177 | struct k_mbox name \ |
| 2178 | __in_section(_k_mbox, static, name) = \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2179 | K_MBOX_INITIALIZER(name) \ |
| 2180 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2181 | /** |
| 2182 | * @brief Initialize a mailbox. |
| 2183 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2184 | * This routine initializes a mailbox object, prior to its first use. |
| 2185 | * |
| 2186 | * @param mbox Address of the mailbox. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2187 | * |
| 2188 | * @return N/A |
| 2189 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2190 | extern void k_mbox_init(struct k_mbox *mbox); |
| 2191 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2192 | /** |
| 2193 | * @brief Send a mailbox message in a synchronous manner. |
| 2194 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2195 | * This routine sends a message to @a mbox and waits for a receiver to both |
| 2196 | * receive and process it. The message data may be in a buffer, in a memory |
| 2197 | * pool block, or non-existent (i.e. an empty message). |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2198 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2199 | * @param mbox Address of the mailbox. |
| 2200 | * @param tx_msg Address of the transmit message descriptor. |
| 2201 | * @param timeout Waiting period for the message to be received (in |
| 2202 | * milliseconds), or one of the special values K_NO_WAIT |
| 2203 | * and K_FOREVER. Once the message has been received, |
| 2204 | * this routine waits as long as necessary for the message |
| 2205 | * to be completely processed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2206 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2207 | * @retval 0 Message sent. |
| 2208 | * @retval -ENOMSG Returned without waiting. |
| 2209 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2210 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 2211 | 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] | 2212 | int32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2213 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2214 | /** |
| 2215 | * @brief Send a mailbox message in an asynchronous manner. |
| 2216 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2217 | * This routine sends a message to @a mbox without waiting for a receiver |
| 2218 | * to process it. The message data may be in a buffer, in a memory pool block, |
| 2219 | * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem |
| 2220 | * will be given when the message has been both received and completely |
| 2221 | * processed by the receiver. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2222 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2223 | * @param mbox Address of the mailbox. |
| 2224 | * @param tx_msg Address of the transmit message descriptor. |
| 2225 | * @param sem Address of a semaphore, or NULL if none is needed. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2226 | * |
| 2227 | * @return N/A |
| 2228 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 2229 | 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] | 2230 | struct k_sem *sem); |
| 2231 | |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2232 | /** |
| 2233 | * @brief Receive a mailbox message. |
| 2234 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2235 | * This routine receives a message from @a mbox, then optionally retrieves |
| 2236 | * its data and disposes of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2237 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2238 | * @param mbox Address of the mailbox. |
| 2239 | * @param rx_msg Address of the receive message descriptor. |
| 2240 | * @param buffer Address of the buffer to receive data, or NULL to defer data |
| 2241 | * retrieval and message disposal until later. |
| 2242 | * @param timeout Waiting period for a message to be received (in |
| 2243 | * milliseconds), or one of the special values K_NO_WAIT |
| 2244 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2245 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2246 | * @retval 0 Message received. |
| 2247 | * @retval -ENOMSG Returned without waiting. |
| 2248 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2249 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 2250 | 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] | 2251 | void *buffer, int32_t timeout); |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2252 | |
| 2253 | /** |
| 2254 | * @brief Retrieve mailbox message data into a buffer. |
| 2255 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2256 | * This routine completes the processing of a received message by retrieving |
| 2257 | * its data into a buffer, then disposing of the message. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2258 | * |
| 2259 | * Alternatively, this routine can be used to dispose of a received message |
| 2260 | * without retrieving its data. |
| 2261 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2262 | * @param rx_msg Address of the receive message descriptor. |
| 2263 | * @param buffer Address of the buffer to receive data, or NULL to discard |
| 2264 | * the data. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2265 | * |
| 2266 | * @return N/A |
| 2267 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 2268 | 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] | 2269 | |
| 2270 | /** |
| 2271 | * @brief Retrieve mailbox message data into a memory pool block. |
| 2272 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2273 | * This routine completes the processing of a received message by retrieving |
| 2274 | * its data into a memory pool block, then disposing of the message. |
| 2275 | * The memory pool block that results from successful retrieval must be |
| 2276 | * returned to the pool once the data has been processed, even in cases |
| 2277 | * where zero bytes of data are retrieved. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2278 | * |
| 2279 | * Alternatively, this routine can be used to dispose of a received message |
| 2280 | * without retrieving its data. In this case there is no need to return a |
| 2281 | * memory pool block to the pool. |
| 2282 | * |
| 2283 | * This routine allocates a new memory pool block for the data only if the |
| 2284 | * data is not already in one. If a new block cannot be allocated, the routine |
| 2285 | * returns a failure code and the received message is left unchanged. This |
| 2286 | * permits the caller to reattempt data retrieval at a later time or to dispose |
| 2287 | * of the received message without retrieving its data. |
| 2288 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2289 | * @param rx_msg Address of a receive message descriptor. |
| 2290 | * @param pool Address of memory pool, or NULL to discard data. |
| 2291 | * @param block Address of the area to hold memory pool block info. |
| 2292 | * @param timeout Waiting period to wait for a memory pool block (in |
| 2293 | * milliseconds), or one of the special values K_NO_WAIT |
| 2294 | * and K_FOREVER. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2295 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2296 | * @retval 0 Data retrieved. |
| 2297 | * @retval -ENOMEM Returned without waiting. |
| 2298 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 1209270 | 2016-10-14 12:57:23 -0400 | [diff] [blame] | 2299 | */ |
Peter Mitsis | 40680f6 | 2016-10-14 10:04:55 -0400 | [diff] [blame] | 2300 | 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] | 2301 | struct k_mem_pool *pool, |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2302 | struct k_mem_block *block, int32_t timeout); |
| 2303 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2304 | /** |
| 2305 | * @} end defgroup mailbox_apis |
| 2306 | */ |
| 2307 | |
| 2308 | /** |
| 2309 | * @cond INTERNAL_HIDDEN |
| 2310 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2311 | |
| 2312 | struct k_pipe { |
| 2313 | unsigned char *buffer; /* Pipe buffer: may be NULL */ |
| 2314 | size_t size; /* Buffer size */ |
| 2315 | size_t bytes_used; /* # bytes used in buffer */ |
| 2316 | size_t read_index; /* Where in buffer to read from */ |
| 2317 | size_t write_index; /* Where in buffer to write */ |
| 2318 | |
| 2319 | struct { |
| 2320 | _wait_q_t readers; /* Reader wait queue */ |
| 2321 | _wait_q_t writers; /* Writer wait queue */ |
| 2322 | } wait_q; |
| 2323 | |
| 2324 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_pipe); |
| 2325 | }; |
| 2326 | |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2327 | #define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2328 | { \ |
| 2329 | .buffer = pipe_buffer, \ |
| 2330 | .size = pipe_buffer_size, \ |
| 2331 | .bytes_used = 0, \ |
| 2332 | .read_index = 0, \ |
| 2333 | .write_index = 0, \ |
| 2334 | .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \ |
| 2335 | .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \ |
| 2336 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 2337 | } |
| 2338 | |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2339 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2340 | * INTERNAL_HIDDEN @endcond |
| 2341 | */ |
| 2342 | |
| 2343 | /** |
| 2344 | * @defgroup pipe_apis Pipe APIs |
| 2345 | * @ingroup kernel_apis |
| 2346 | * @{ |
| 2347 | */ |
| 2348 | |
| 2349 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2350 | * @brief Statically define and initialize a pipe. |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2351 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2352 | * The pipe can be accessed outside the module where it is defined using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2353 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2354 | * @code extern struct k_pipe <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2355 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2356 | * @param name Name of the pipe. |
| 2357 | * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes), |
| 2358 | * or zero if no ring buffer is used. |
| 2359 | * @param pipe_align Alignment of the pipe's ring buffer (power of 2). |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2360 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2361 | #define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \ |
| 2362 | static unsigned char __noinit __aligned(pipe_align) \ |
| 2363 | _k_pipe_buf_##name[pipe_buffer_size]; \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 2364 | struct k_pipe name \ |
| 2365 | __in_section(_k_pipe, static, name) = \ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2366 | K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2367 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2368 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2369 | * @brief Initialize a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2370 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2371 | * This routine initializes a pipe object, prior to its first use. |
| 2372 | * |
| 2373 | * @param pipe Address of the pipe. |
| 2374 | * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer |
| 2375 | * is used. |
| 2376 | * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring |
| 2377 | * buffer is used. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2378 | * |
| 2379 | * @return N/A |
| 2380 | */ |
| 2381 | extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, |
| 2382 | size_t size); |
| 2383 | |
| 2384 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2385 | * @brief Write data to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2386 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2387 | * This routine writes up to @a bytes_to_write bytes of data to @a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2388 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2389 | * @param pipe Address of the pipe. |
| 2390 | * @param data Address of data to write. |
| 2391 | * @param bytes_to_write Size of data (in bytes). |
| 2392 | * @param bytes_written Address of area to hold the number of bytes written. |
| 2393 | * @param min_xfer Minimum number of bytes to write. |
| 2394 | * @param timeout Waiting period to wait for the data to be written (in |
| 2395 | * milliseconds), or one of the special values K_NO_WAIT |
| 2396 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2397 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2398 | * @retval 0 At least @a min_xfer bytes of data were written. |
| 2399 | * @retval -EIO Returned without waiting; zero data bytes were written. |
| 2400 | * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2401 | * minus one data bytes were written. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2402 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2403 | extern int k_pipe_put(struct k_pipe *pipe, void *data, |
| 2404 | size_t bytes_to_write, size_t *bytes_written, |
| 2405 | size_t min_xfer, int32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2406 | |
| 2407 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2408 | * @brief Read data from a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2409 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2410 | * This routine reads up to @a bytes_to_read bytes of data from @a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2411 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2412 | * @param pipe Address of the pipe. |
| 2413 | * @param data Address to place the data read from pipe. |
| 2414 | * @param bytes_to_read Maximum number of data bytes to read. |
| 2415 | * @param bytes_read Address of area to hold the number of bytes read. |
| 2416 | * @param min_xfer Minimum number of data bytes to read. |
| 2417 | * @param timeout Waiting period to wait for the data to be read (in |
| 2418 | * milliseconds), or one of the special values K_NO_WAIT |
| 2419 | * and K_FOREVER. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2420 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2421 | * @retval 0 At least @a min_xfer bytes of data were read. |
| 2422 | * @retval -EIO Returned without waiting; zero data bytes were read. |
| 2423 | * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2424 | * minus one data bytes were read. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2425 | */ |
Peter Mitsis | e5d9c58 | 2016-10-14 14:44:57 -0400 | [diff] [blame] | 2426 | extern int k_pipe_get(struct k_pipe *pipe, void *data, |
| 2427 | size_t bytes_to_read, size_t *bytes_read, |
| 2428 | size_t min_xfer, int32_t timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2429 | |
| 2430 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2431 | * @brief Write memory block to a pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2432 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2433 | * This routine writes the data contained in a memory block to @a pipe. |
| 2434 | * Once all of the data in the block has been written to the pipe, it will |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2435 | * free the memory block @a block and give the semaphore @a sem (if specified). |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2436 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2437 | * @param pipe Address of the pipe. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2438 | * @param block Memory block containing data to send |
| 2439 | * @param size Number of data bytes in memory block to send |
| 2440 | * @param sem Semaphore to signal upon completion (else NULL) |
| 2441 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2442 | * @return N/A |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2443 | */ |
| 2444 | extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block, |
| 2445 | size_t size, struct k_sem *sem); |
| 2446 | |
| 2447 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2448 | * @} end defgroup pipe_apis |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2449 | */ |
| 2450 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2451 | /** |
| 2452 | * @cond INTERNAL_HIDDEN |
| 2453 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2454 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2455 | struct k_mem_slab { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2456 | _wait_q_t wait_q; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 2457 | uint32_t num_blocks; |
| 2458 | size_t block_size; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2459 | char *buffer; |
| 2460 | char *free_list; |
Peter Mitsis | fb02d57 | 2016-10-13 16:55:45 -0400 | [diff] [blame] | 2461 | uint32_t num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2462 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2463 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_slab); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2464 | }; |
| 2465 | |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2466 | #define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \ |
| 2467 | slab_num_blocks) \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2468 | { \ |
| 2469 | .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2470 | .num_blocks = slab_num_blocks, \ |
| 2471 | .block_size = slab_block_size, \ |
| 2472 | .buffer = slab_buffer, \ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2473 | .free_list = NULL, \ |
| 2474 | .num_used = 0, \ |
| 2475 | _DEBUG_TRACING_KERNEL_OBJECTS_INIT \ |
| 2476 | } |
| 2477 | |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 2478 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2479 | * INTERNAL_HIDDEN @endcond |
| 2480 | */ |
| 2481 | |
| 2482 | /** |
| 2483 | * @defgroup mem_slab_apis Memory Slab APIs |
| 2484 | * @ingroup kernel_apis |
| 2485 | * @{ |
| 2486 | */ |
| 2487 | |
| 2488 | /** |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2489 | * @brief Statically define and initialize a memory slab. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 2490 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2491 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2492 | * that are @a slab_block_size bytes long. The buffer is aligned to a |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2493 | * @a slab_align -byte boundary. To ensure that each memory block is similarly |
| 2494 | * aligned to this boundary, @a slab_block_size must also be a multiple of |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2495 | * @a slab_align. |
Peter Mitsis | 578f911 | 2016-10-07 13:50:31 -0400 | [diff] [blame] | 2496 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2497 | * The memory slab can be accessed outside the module where it is defined |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2498 | * using: |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2499 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2500 | * @code extern struct k_mem_slab <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2501 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2502 | * @param name Name of the memory slab. |
| 2503 | * @param slab_block_size Size of each memory block (in bytes). |
| 2504 | * @param slab_num_blocks Number memory blocks. |
| 2505 | * @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] | 2506 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2507 | #define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \ |
| 2508 | char __noinit __aligned(slab_align) \ |
| 2509 | _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \ |
| 2510 | struct k_mem_slab name \ |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 2511 | __in_section(_k_mem_slab, static, name) = \ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2512 | K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \ |
| 2513 | slab_block_size, slab_num_blocks) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2514 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2515 | /** |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2516 | * @brief Initialize a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2517 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2518 | * Initializes a memory slab, prior to its first use. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2519 | * |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2520 | * The memory slab's buffer contains @a slab_num_blocks memory blocks |
| 2521 | * that are @a slab_block_size bytes long. The buffer must be aligned to an |
| 2522 | * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...). |
| 2523 | * To ensure that each memory block is similarly aligned to this boundary, |
| 2524 | * @a slab_block_size must also be a multiple of N. |
| 2525 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2526 | * @param slab Address of the memory slab. |
| 2527 | * @param buffer Pointer to buffer used for the memory blocks. |
| 2528 | * @param block_size Size of each memory block (in bytes). |
| 2529 | * @param num_blocks Number of memory blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2530 | * |
| 2531 | * @return N/A |
| 2532 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2533 | 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] | 2534 | size_t block_size, uint32_t num_blocks); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2535 | |
| 2536 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2537 | * @brief Allocate memory from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2538 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2539 | * This routine allocates a memory block from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2540 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2541 | * @param slab Address of the memory slab. |
| 2542 | * @param mem Pointer to block address area. |
| 2543 | * @param timeout Maximum time to wait for operation to complete |
| 2544 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 2545 | * or K_FOREVER to wait as long as necessary. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2546 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2547 | * @retval 0 Memory allocated. The block address area pointed at by @a mem |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2548 | * is set to the starting address of the memory block. |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2549 | * @retval -ENOMEM Returned without waiting. |
| 2550 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2551 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2552 | extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem, |
| 2553 | int32_t timeout); |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2554 | |
| 2555 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2556 | * @brief Free memory allocated from a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2557 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2558 | * This routine releases a previously allocated memory block back to its |
| 2559 | * associated memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2560 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2561 | * @param slab Address of the memory slab. |
| 2562 | * @param mem Pointer to block address area (as set by k_mem_slab_alloc()). |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2563 | * |
| 2564 | * @return N/A |
| 2565 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2566 | 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] | 2567 | |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2568 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2569 | * @brief Get the number of used blocks in a memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2570 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2571 | * This routine gets the number of memory blocks that are currently |
| 2572 | * allocated in @a slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2573 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2574 | * @param slab Address of the memory slab. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2575 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2576 | * @return Number of allocated memory blocks. |
Peter Mitsis | 4a5d62f | 2016-10-13 16:53:30 -0400 | [diff] [blame] | 2577 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2578 | 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] | 2579 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2580 | return slab->num_used; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2581 | } |
| 2582 | |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2583 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2584 | * @brief Get the number of unused blocks in a memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2585 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2586 | * This routine gets the number of memory blocks that are currently |
| 2587 | * unallocated in @a slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2588 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2589 | * @param slab Address of the memory slab. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2590 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2591 | * @return Number of unallocated memory blocks. |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2592 | */ |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2593 | 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] | 2594 | { |
Benjamin Walsh | 7ef0f62 | 2016-10-24 17:04:43 -0400 | [diff] [blame] | 2595 | return slab->num_blocks - slab->num_used; |
Peter Mitsis | c001aa8 | 2016-10-13 13:53:37 -0400 | [diff] [blame] | 2596 | } |
| 2597 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2598 | /** |
| 2599 | * @} end defgroup mem_slab_apis |
| 2600 | */ |
| 2601 | |
| 2602 | /** |
| 2603 | * @cond INTERNAL_HIDDEN |
| 2604 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2605 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2606 | /* |
| 2607 | * Memory pool requires a buffer and two arrays of structures for the |
| 2608 | * memory block accounting: |
| 2609 | * A set of arrays of k_mem_pool_quad_block structures where each keeps a |
| 2610 | * status of four blocks of memory. |
| 2611 | */ |
| 2612 | struct k_mem_pool_quad_block { |
| 2613 | char *mem_blocks; /* pointer to the first of four memory blocks */ |
| 2614 | uint32_t mem_status; /* four bits. If bit is set, memory block is |
| 2615 | allocated */ |
| 2616 | }; |
| 2617 | /* |
| 2618 | * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting |
| 2619 | * blocks of one size. Block sizes go from maximal to minimal. Next memory |
| 2620 | * block size is 4 times less than the previous one and thus requires 4 times |
| 2621 | * bigger array of k_mem_pool_quad_block structures to keep track of the |
| 2622 | * memory blocks. |
| 2623 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2624 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2625 | /* |
| 2626 | * The array of k_mem_pool_block_set keeps the information of each array of |
| 2627 | * k_mem_pool_quad_block structures |
| 2628 | */ |
| 2629 | struct k_mem_pool_block_set { |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2630 | size_t block_size; /* memory block size */ |
| 2631 | 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] | 2632 | struct k_mem_pool_quad_block *quad_block; |
| 2633 | int count; |
| 2634 | }; |
| 2635 | |
| 2636 | /* Memory pool descriptor */ |
| 2637 | struct k_mem_pool { |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2638 | size_t max_block_size; |
| 2639 | size_t min_block_size; |
| 2640 | uint32_t nr_of_maxblocks; |
| 2641 | uint32_t nr_of_block_sets; |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2642 | struct k_mem_pool_block_set *block_set; |
| 2643 | char *bufblock; |
| 2644 | _wait_q_t wait_q; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2645 | _DEBUG_TRACING_KERNEL_OBJECTS_NEXT_PTR(k_mem_pool); |
| 2646 | }; |
| 2647 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2648 | #ifdef CONFIG_ARM |
| 2649 | #define _SECTION_TYPE_SIGN "%" |
| 2650 | #else |
| 2651 | #define _SECTION_TYPE_SIGN "@" |
| 2652 | #endif |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2653 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2654 | /* |
| 2655 | * Static memory pool initialization |
| 2656 | */ |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2657 | |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2658 | /* |
| 2659 | * Use .altmacro to be able to recalculate values and pass them as string |
| 2660 | * arguments when calling assembler macros resursively |
| 2661 | */ |
| 2662 | __asm__(".altmacro\n\t"); |
| 2663 | |
| 2664 | /* |
| 2665 | * Recursively calls a macro |
| 2666 | * The followig global symbols need to be initialized: |
| 2667 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2668 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2669 | * Notes: |
| 2670 | * Global symbols are used due the fact that assembler macro allows only |
| 2671 | * one argument be passed with the % conversion |
| 2672 | * Some assemblers do not get division operation ("/"). To avoid it >> 2 |
| 2673 | * is used instead of / 4. |
| 2674 | * n_max argument needs to go first in the invoked macro, as some |
| 2675 | * assemblers concatenate \name and %(\n_max * 4) arguments |
| 2676 | * if \name goes first |
| 2677 | */ |
| 2678 | __asm__(".macro __do_recurse macro_name, name, n_max\n\t" |
| 2679 | ".ifge __memory_pool_max_block_size >> 2 -" |
| 2680 | " __memory_pool_min_block_size\n\t\t" |
| 2681 | "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t" |
| 2682 | "\\macro_name %(\\n_max * 4) \\name\n\t" |
| 2683 | ".endif\n\t" |
| 2684 | ".endm\n"); |
| 2685 | |
| 2686 | /* |
| 2687 | * Build quad blocks |
| 2688 | * Macro allocates space in memory for the array of k_mem_pool_quad_block |
| 2689 | * structures and recursively calls itself for the next array, 4 times |
| 2690 | * larger. |
| 2691 | * The followig global symbols need to be initialized: |
| 2692 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2693 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2694 | * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block) |
| 2695 | */ |
| 2696 | __asm__(".macro _build_quad_blocks n_max, name\n\t" |
Dmitriy Korovkin | 3c90651 | 2016-10-06 15:50:40 -0400 | [diff] [blame] | 2697 | ".balign 4\n\t" |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2698 | "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t" |
| 2699 | ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t" |
| 2700 | ".if \\n_max % 4\n\t\t" |
| 2701 | ".skip __memory_pool_quad_block_size\n\t" |
| 2702 | ".endif\n\t" |
| 2703 | "__do_recurse _build_quad_blocks \\name \\n_max\n\t" |
| 2704 | ".endm\n"); |
| 2705 | |
| 2706 | /* |
| 2707 | * Build block sets and initialize them |
| 2708 | * Macro initializes the k_mem_pool_block_set structure and |
| 2709 | * recursively calls itself for the next one. |
| 2710 | * The followig global symbols need to be initialized: |
| 2711 | * __memory_pool_max_block_size - maximal size of the memory block |
| 2712 | * __memory_pool_min_block_size - minimal size of the memory block |
| 2713 | * __memory_pool_block_set_count, the number of the elements in the |
| 2714 | * block set array must be set to 0. Macro calculates it's real |
| 2715 | * value. |
| 2716 | * Since the macro initializes pointers to an array of k_mem_pool_quad_block |
| 2717 | * structures, _build_quad_blocks must be called prior it. |
| 2718 | */ |
| 2719 | __asm__(".macro _build_block_set n_max, name\n\t" |
| 2720 | ".int __memory_pool_max_block_size\n\t" /* block_size */ |
| 2721 | ".if \\n_max % 4\n\t\t" |
| 2722 | ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */ |
| 2723 | ".else\n\t\t" |
| 2724 | ".int \\n_max >> 2\n\t" |
| 2725 | ".endif\n\t" |
| 2726 | ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */ |
| 2727 | ".int 0\n\t" /* count */ |
| 2728 | "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t" |
| 2729 | "__do_recurse _build_block_set \\name \\n_max\n\t" |
| 2730 | ".endm\n"); |
| 2731 | |
| 2732 | /* |
| 2733 | * Build a memory pool structure and initialize it |
| 2734 | * Macro uses __memory_pool_block_set_count global symbol, |
| 2735 | * block set addresses and buffer address, it may be called only after |
| 2736 | * _build_block_set |
| 2737 | */ |
| 2738 | __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] | 2739 | ".pushsection ._k_mem_pool.static.\\name,\"aw\"," |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2740 | _SECTION_TYPE_SIGN "progbits\n\t" |
| 2741 | ".globl \\name\n\t" |
| 2742 | "\\name:\n\t" |
| 2743 | ".int \\max_size\n\t" /* max_block_size */ |
| 2744 | ".int \\min_size\n\t" /* min_block_size */ |
| 2745 | ".int \\n_max\n\t" /* nr_of_maxblocks */ |
| 2746 | ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */ |
| 2747 | ".int _mem_pool_block_sets_\\name\n\t" /* block_set */ |
| 2748 | ".int _mem_pool_buffer_\\name\n\t" /* bufblock */ |
| 2749 | ".int 0\n\t" /* wait_q->head */ |
| 2750 | ".int 0\n\t" /* wait_q->next */ |
| 2751 | ".popsection\n\t" |
| 2752 | ".endm\n"); |
| 2753 | |
| 2754 | #define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \ |
| 2755 | __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \ |
| 2756 | _SECTION_TYPE_SIGN "progbits\n\t"); \ |
| 2757 | __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \ |
| 2758 | __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \ |
| 2759 | __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \ |
| 2760 | STRINGIFY(name) "\n\t"); \ |
| 2761 | __asm__(".popsection\n\t") |
| 2762 | |
| 2763 | #define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \ |
| 2764 | __asm__("__memory_pool_block_set_count = 0\n\t"); \ |
| 2765 | __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \ |
| 2766 | __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \ |
| 2767 | _SECTION_TYPE_SIGN "progbits\n\t"); \ |
Dmitriy Korovkin | 3c90651 | 2016-10-06 15:50:40 -0400 | [diff] [blame] | 2768 | __asm__(".balign 4\n\t"); \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2769 | __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \ |
| 2770 | __asm__("_build_block_set " STRINGIFY(n_max) " " \ |
| 2771 | STRINGIFY(name) "\n\t"); \ |
| 2772 | __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \ |
| 2773 | __asm__(".int __memory_pool_block_set_count\n\t"); \ |
| 2774 | __asm__(".popsection\n\t"); \ |
| 2775 | extern uint32_t _mem_pool_block_set_count_##name; \ |
| 2776 | extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[] |
| 2777 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2778 | #define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \ |
| 2779 | char __noinit __aligned(align) \ |
| 2780 | _mem_pool_buffer_##name[(max_size) * (n_max)] |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2781 | |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 2782 | /* |
| 2783 | * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block) |
| 2784 | * to __memory_pool_quad_block_size absolute symbol. |
| 2785 | * This function does not get called, but compiler calculates the value and |
| 2786 | * assigns it to the absolute symbol, that, in turn is used by assembler macros. |
| 2787 | */ |
| 2788 | static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void) |
| 2789 | { |
| 2790 | __asm__(".globl __memory_pool_quad_block_size\n\t" |
| 2791 | #ifdef CONFIG_NIOS2 |
| 2792 | "__memory_pool_quad_block_size = %0\n\t" |
| 2793 | #else |
| 2794 | "__memory_pool_quad_block_size = %c0\n\t" |
| 2795 | #endif |
| 2796 | : |
| 2797 | : "n"(sizeof(struct k_mem_pool_quad_block))); |
| 2798 | } |
| 2799 | |
| 2800 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2801 | * INTERNAL_HIDDEN @endcond |
Dmitriy Korovkin | 0741467 | 2016-11-03 13:35:42 -0400 | [diff] [blame] | 2802 | */ |
| 2803 | |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2804 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2805 | * @addtogroup mem_pool_apis |
| 2806 | * @{ |
| 2807 | */ |
| 2808 | |
| 2809 | /** |
| 2810 | * @brief Statically define and initialize a memory pool. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2811 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2812 | * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes |
| 2813 | * long. The memory pool allows blocks to be repeatedly partitioned into |
| 2814 | * quarters, down to blocks of @a min_size bytes long. The buffer is aligned |
| 2815 | * to a @a align -byte boundary. To ensure that the minimum sized blocks are |
Allan Stephens | da82722 | 2016-11-09 14:23:58 -0600 | [diff] [blame] | 2816 | * similarly aligned to this boundary, @a min_size must also be a multiple of |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2817 | * @a align. |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2818 | * |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2819 | * If the pool is to be accessed outside the module where it is defined, it |
| 2820 | * can be declared via |
| 2821 | * |
Allan Stephens | 82d4c3a | 2016-11-17 09:23:46 -0500 | [diff] [blame] | 2822 | * @code extern struct k_mem_pool <name>; @endcode |
Peter Mitsis | 348eb4c | 2016-10-26 11:22:14 -0400 | [diff] [blame] | 2823 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2824 | * @param name Name of the memory pool. |
| 2825 | * @param min_size Size of the smallest blocks in the pool (in bytes). |
| 2826 | * @param max_size Size of the largest blocks in the pool (in bytes). |
| 2827 | * @param n_max Number of maximum sized blocks in the pool. |
| 2828 | * @param align Alignment of the pool's buffer (power of 2). |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2829 | */ |
| 2830 | #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] | 2831 | _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \ |
| 2832 | _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \ |
Peter Mitsis | 2a2b075 | 2016-10-06 16:27:01 -0400 | [diff] [blame] | 2833 | _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2834 | __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \ |
| 2835 | STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \ |
| 2836 | extern struct k_mem_pool name |
| 2837 | |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2838 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2839 | * @brief Allocate memory from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2840 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2841 | * This routine allocates a memory block from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2842 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2843 | * @param pool Address of the memory pool. |
| 2844 | * @param block Pointer to block descriptor for the allocated memory. |
| 2845 | * @param size Amount of memory to allocate (in bytes). |
| 2846 | * @param timeout Maximum time to wait for operation to complete |
| 2847 | * (in milliseconds). Use K_NO_WAIT to return without waiting, |
| 2848 | * or K_FOREVER to wait as long as necessary. |
| 2849 | * |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2850 | * @retval 0 Memory allocated. The @a data field of the block descriptor |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2851 | * is set to the starting address of the memory block. |
Allan Stephens | 9ef50f4 | 2016-11-16 15:33:31 -0500 | [diff] [blame] | 2852 | * @retval -ENOMEM Returned without waiting. |
| 2853 | * @retval -EAGAIN Waiting period timed out. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2854 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2855 | 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] | 2856 | size_t size, int32_t timeout); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2857 | |
| 2858 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2859 | * @brief Free memory allocated from a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2860 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2861 | * This routine releases a previously allocated memory block back to its |
| 2862 | * memory pool. |
| 2863 | * |
| 2864 | * @param block Pointer to block descriptor for the allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2865 | * |
| 2866 | * @return N/A |
| 2867 | */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2868 | extern void k_mem_pool_free(struct k_mem_block *block); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2869 | |
| 2870 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2871 | * @brief Defragment a memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2872 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2873 | * This routine instructs a memory pool to concatenate unused memory blocks |
| 2874 | * into larger blocks wherever possible. Manually defragmenting the memory |
| 2875 | * pool may speed up future allocations of memory blocks by eliminating the |
| 2876 | * need for the memory pool to perform an automatic partial defragmentation. |
| 2877 | * |
| 2878 | * @param pool Address of the memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2879 | * |
| 2880 | * @return N/A |
| 2881 | */ |
Dmitriy Korovkin | 3c42688 | 2016-09-01 18:14:17 -0400 | [diff] [blame] | 2882 | extern void k_mem_pool_defrag(struct k_mem_pool *pool); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2883 | |
| 2884 | /** |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2885 | * @} end addtogroup mem_pool_apis |
| 2886 | */ |
| 2887 | |
| 2888 | /** |
| 2889 | * @defgroup heap_apis Heap Memory Pool APIs |
| 2890 | * @ingroup kernel_apis |
| 2891 | * @{ |
| 2892 | */ |
| 2893 | |
| 2894 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2895 | * @brief Allocate memory from heap. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2896 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2897 | * This routine provides traditional malloc() semantics. Memory is |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 2898 | * allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2899 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2900 | * @param size Amount of memory requested (in bytes). |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2901 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2902 | * @return Address of the allocated memory if successful; otherwise NULL. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2903 | */ |
Peter Mitsis | 5f39924 | 2016-10-13 13:26:25 -0400 | [diff] [blame] | 2904 | extern void *k_malloc(size_t size); |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2905 | |
| 2906 | /** |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2907 | * @brief Free memory allocated from heap. |
Allan Stephens | 480a131 | 2016-10-13 15:44:48 -0500 | [diff] [blame] | 2908 | * |
| 2909 | * This routine provides traditional free() semantics. The memory being |
| 2910 | * returned must have been allocated from the heap memory pool. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2911 | * |
Allan Stephens | 5a7a86c | 2016-11-04 13:53:19 -0500 | [diff] [blame] | 2912 | * @param ptr Pointer to previously allocated memory. |
Peter Mitsis | 937042c | 2016-10-13 13:18:26 -0400 | [diff] [blame] | 2913 | * |
| 2914 | * @return N/A |
| 2915 | */ |
| 2916 | extern void k_free(void *ptr); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2917 | |
Allan Stephens | c98da84 | 2016-11-11 15:45:03 -0500 | [diff] [blame] | 2918 | /** |
| 2919 | * @} end defgroup heap_apis |
| 2920 | */ |
| 2921 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2922 | /* |
| 2923 | * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to |
| 2924 | * hook into the device subsystem, which itself uses nanokernel semaphores, |
| 2925 | * and thus currently requires the definition of nano_sem. |
| 2926 | */ |
| 2927 | #include <legacy.h> |
| 2928 | #include <arch/cpu.h> |
| 2929 | |
| 2930 | /* |
| 2931 | * private APIs that are utilized by one or more public APIs |
| 2932 | */ |
| 2933 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2934 | extern int _is_thread_essential(void); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2935 | extern void _init_static_threads(void); |
Allan Stephens | 1342adb | 2016-11-03 13:54:53 -0500 | [diff] [blame] | 2936 | extern void _timer_expiration_handler(struct _timeout *t); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2937 | |
| 2938 | #ifdef __cplusplus |
| 2939 | } |
| 2940 | #endif |
| 2941 | |
Andrew Boie | e004dec | 2016-11-07 09:01:19 -0800 | [diff] [blame] | 2942 | #if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) |
| 2943 | /* |
| 2944 | * Define new and delete operators. |
| 2945 | * At this moment, the operators do nothing since objects are supposed |
| 2946 | * to be statically allocated. |
| 2947 | */ |
| 2948 | inline void operator delete(void *ptr) |
| 2949 | { |
| 2950 | (void)ptr; |
| 2951 | } |
| 2952 | |
| 2953 | inline void operator delete[](void *ptr) |
| 2954 | { |
| 2955 | (void)ptr; |
| 2956 | } |
| 2957 | |
| 2958 | inline void *operator new(size_t size) |
| 2959 | { |
| 2960 | (void)size; |
| 2961 | return NULL; |
| 2962 | } |
| 2963 | |
| 2964 | inline void *operator new[](size_t size) |
| 2965 | { |
| 2966 | (void)size; |
| 2967 | return NULL; |
| 2968 | } |
| 2969 | |
| 2970 | /* Placement versions of operator new and delete */ |
| 2971 | inline void operator delete(void *ptr1, void *ptr2) |
| 2972 | { |
| 2973 | (void)ptr1; |
| 2974 | (void)ptr2; |
| 2975 | } |
| 2976 | |
| 2977 | inline void operator delete[](void *ptr1, void *ptr2) |
| 2978 | { |
| 2979 | (void)ptr1; |
| 2980 | (void)ptr2; |
| 2981 | } |
| 2982 | |
| 2983 | inline void *operator new(size_t size, void *ptr) |
| 2984 | { |
| 2985 | (void)size; |
| 2986 | return ptr; |
| 2987 | } |
| 2988 | |
| 2989 | inline void *operator new[](size_t size, void *ptr) |
| 2990 | { |
| 2991 | (void)size; |
| 2992 | return ptr; |
| 2993 | } |
| 2994 | |
| 2995 | #endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */ |
| 2996 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 2997 | #endif /* _kernel__h_ */ |