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