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