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