Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Wind River Systems, Inc. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @file |
| 9 | * @brief Message queues. |
| 10 | */ |
| 11 | |
| 12 | |
| 13 | #include <kernel.h> |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 14 | #include <kernel_structs.h> |
Anas Nashif | 569f0b4 | 2016-12-17 13:18:45 -0500 | [diff] [blame] | 15 | #include <debug/object_tracing_common.h> |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 16 | #include <toolchain.h> |
Anas Nashif | 397d29d | 2017-06-17 11:30:47 -0400 | [diff] [blame] | 17 | #include <linker/sections.h> |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 18 | #include <string.h> |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 19 | #include <ksched.h> |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 20 | #include <wait_q.h> |
Anas Nashif | ee9dd1a | 2019-06-26 10:33:41 -0400 | [diff] [blame] | 21 | #include <sys/dlist.h> |
Anas Nashif | 6ecadb0 | 2019-06-26 10:33:45 -0400 | [diff] [blame] | 22 | #include <sys/math_extras.h> |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 23 | #include <init.h> |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 24 | #include <syscall_handler.h> |
Andy Ross | 4f911e1 | 2018-09-05 10:13:38 -0700 | [diff] [blame] | 25 | #include <kernel_internal.h> |
Anas Nashif | 11b9365 | 2019-06-16 08:43:48 -0400 | [diff] [blame] | 26 | #include <sys/check.h> |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 27 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 28 | #ifdef CONFIG_OBJECT_TRACING |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 29 | |
Maciek Borzecki | 059544d | 2017-05-18 12:16:45 +0200 | [diff] [blame] | 30 | struct k_msgq *_trace_list_k_msgq; |
| 31 | |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 32 | /* |
| 33 | * Complete initialization of statically defined message queues. |
| 34 | */ |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 35 | static int init_msgq_module(const struct device *dev) |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 36 | { |
| 37 | ARG_UNUSED(dev); |
| 38 | |
Nicolas Pitre | aa922885 | 2019-06-03 13:01:43 -0400 | [diff] [blame] | 39 | Z_STRUCT_SECTION_FOREACH(k_msgq, msgq) { |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 40 | SYS_TRACING_OBJ_INIT(k_msgq, msgq); |
| 41 | } |
| 42 | return 0; |
| 43 | } |
| 44 | |
Andrew Boie | 0b474ee | 2016-11-08 11:06:55 -0800 | [diff] [blame] | 45 | SYS_INIT(init_msgq_module, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS); |
Allan Stephens | e7d2cc2 | 2016-10-19 16:10:46 -0500 | [diff] [blame] | 46 | |
Anas Nashif | 2f203c2 | 2016-12-18 06:57:45 -0500 | [diff] [blame] | 47 | #endif /* CONFIG_OBJECT_TRACING */ |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 48 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 49 | void k_msgq_init(struct k_msgq *msgq, char *buffer, size_t msg_size, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 50 | uint32_t max_msgs) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 51 | { |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 52 | msgq->msg_size = msg_size; |
| 53 | msgq->max_msgs = max_msgs; |
| 54 | msgq->buffer_start = buffer; |
| 55 | msgq->buffer_end = buffer + (max_msgs * msg_size); |
| 56 | msgq->read_ptr = buffer; |
| 57 | msgq->write_ptr = buffer; |
| 58 | msgq->used_msgs = 0; |
| 59 | msgq->flags = 0; |
| 60 | z_waitq_init(&msgq->wait_q); |
| 61 | msgq->lock = (struct k_spinlock) {}; |
Andrew Boie | 945af95 | 2017-08-22 13:15:23 -0700 | [diff] [blame] | 62 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 63 | SYS_TRACING_OBJ_INIT(k_msgq, msgq); |
| 64 | |
| 65 | z_object_init(msgq); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 68 | int z_impl_k_msgq_alloc_init(struct k_msgq *msgq, size_t msg_size, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 69 | uint32_t max_msgs) |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 70 | { |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 71 | void *buffer; |
| 72 | int ret; |
| 73 | size_t total_size; |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 74 | |
Jakob Olesen | c8708d9 | 2019-05-07 10:17:35 -0700 | [diff] [blame] | 75 | if (size_mul_overflow(msg_size, max_msgs, &total_size)) { |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 76 | ret = -EINVAL; |
| 77 | } else { |
| 78 | buffer = z_thread_malloc(total_size); |
Flavio Ceolin | ea716bf | 2018-09-20 16:30:45 -0700 | [diff] [blame] | 79 | if (buffer != NULL) { |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 80 | k_msgq_init(msgq, buffer, msg_size, max_msgs); |
| 81 | msgq->flags = K_MSGQ_FLAG_ALLOC; |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 82 | ret = 0; |
| 83 | } else { |
| 84 | ret = -ENOMEM; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 92 | int z_vrfy_k_msgq_alloc_init(struct k_msgq *q, size_t msg_size, |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 93 | uint32_t max_msgs) |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 94 | { |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 95 | Z_OOPS(Z_SYSCALL_OBJ_NEVER_INIT(q, K_OBJ_MSGQ)); |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 96 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 97 | return z_impl_k_msgq_alloc_init(q, msg_size, max_msgs); |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 98 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 99 | #include <syscalls/k_msgq_alloc_init_mrsh.c> |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 100 | #endif |
| 101 | |
Anas Nashif | 11b9365 | 2019-06-16 08:43:48 -0400 | [diff] [blame] | 102 | int k_msgq_cleanup(struct k_msgq *msgq) |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 103 | { |
Anas Nashif | 11b9365 | 2019-06-16 08:43:48 -0400 | [diff] [blame] | 104 | CHECKIF(z_waitq_head(&msgq->wait_q) != NULL) { |
| 105 | return -EBUSY; |
| 106 | } |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 107 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 108 | if ((msgq->flags & K_MSGQ_FLAG_ALLOC) != 0) { |
| 109 | k_free(msgq->buffer_start); |
| 110 | msgq->flags &= ~K_MSGQ_FLAG_ALLOC; |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 111 | } |
Anas Nashif | 11b9365 | 2019-06-16 08:43:48 -0400 | [diff] [blame] | 112 | return 0; |
Andrew Boie | 0fe789f | 2018-04-12 18:35:56 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 116 | int z_impl_k_msgq_put(struct k_msgq *msgq, void *data, k_timeout_t timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 117 | { |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 118 | __ASSERT(!arch_is_in_isr() || K_TIMEOUT_EQ(timeout, K_NO_WAIT), ""); |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 119 | |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 120 | struct k_thread *pending_thread; |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 121 | k_spinlock_key_t key; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 122 | int result; |
| 123 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 124 | key = k_spin_lock(&msgq->lock); |
| 125 | |
| 126 | if (msgq->used_msgs < msgq->max_msgs) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 127 | /* message queue isn't full */ |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 128 | pending_thread = z_unpend_first_thread(&msgq->wait_q); |
Flavio Ceolin | ea716bf | 2018-09-20 16:30:45 -0700 | [diff] [blame] | 129 | if (pending_thread != NULL) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 130 | /* give message to waiting thread */ |
Flavio Ceolin | 6699423 | 2018-08-13 15:17:04 -0700 | [diff] [blame] | 131 | (void)memcpy(pending_thread->base.swap_data, data, |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 132 | msgq->msg_size); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 133 | /* wake up waiting thread */ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 134 | arch_thread_return_value_set(pending_thread, 0); |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 135 | z_ready_thread(pending_thread); |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 136 | z_reschedule(&msgq->lock, key); |
Andy Ross | 8606fab | 2018-03-26 10:54:40 -0700 | [diff] [blame] | 137 | return 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 138 | } else { |
| 139 | /* put message in queue */ |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 140 | (void)memcpy(msgq->write_ptr, data, msgq->msg_size); |
| 141 | msgq->write_ptr += msgq->msg_size; |
| 142 | if (msgq->write_ptr == msgq->buffer_end) { |
| 143 | msgq->write_ptr = msgq->buffer_start; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 144 | } |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 145 | msgq->used_msgs++; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 146 | } |
| 147 | result = 0; |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 148 | } else if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 149 | /* don't wait for message space to become available */ |
| 150 | result = -ENOMSG; |
| 151 | } else { |
| 152 | /* wait for put message success, failure, or timeout */ |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 153 | _current->base.swap_data = data; |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 154 | return z_pend_curr(&msgq->lock, key, &msgq->wait_q, timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 157 | k_spin_unlock(&msgq->lock, key); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 158 | |
| 159 | return result; |
| 160 | } |
| 161 | |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 162 | #ifdef CONFIG_USERSPACE |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 163 | static inline int z_vrfy_k_msgq_put(struct k_msgq *q, void *data, |
| 164 | k_timeout_t timeout) |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 165 | { |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 166 | Z_OOPS(Z_SYSCALL_OBJ(q, K_OBJ_MSGQ)); |
| 167 | Z_OOPS(Z_SYSCALL_MEMORY_READ(data, q->msg_size)); |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 168 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 169 | return z_impl_k_msgq_put(q, data, timeout); |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 170 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 171 | #include <syscalls/k_msgq_put_mrsh.c> |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 172 | #endif |
| 173 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 174 | void z_impl_k_msgq_get_attrs(struct k_msgq *msgq, struct k_msgq_attrs *attrs) |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 175 | { |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 176 | attrs->msg_size = msgq->msg_size; |
| 177 | attrs->max_msgs = msgq->max_msgs; |
| 178 | attrs->used_msgs = msgq->used_msgs; |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | #ifdef CONFIG_USERSPACE |
Andy Ross | 643701a | 2019-08-13 12:58:38 -0700 | [diff] [blame] | 182 | static inline void z_vrfy_k_msgq_get_attrs(struct k_msgq *q, |
| 183 | struct k_msgq_attrs *attrs) |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 184 | { |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 185 | Z_OOPS(Z_SYSCALL_OBJ(q, K_OBJ_MSGQ)); |
| 186 | Z_OOPS(Z_SYSCALL_MEMORY_WRITE(attrs, sizeof(struct k_msgq_attrs))); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 187 | z_impl_k_msgq_get_attrs(q, attrs); |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 188 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 189 | #include <syscalls/k_msgq_get_attrs_mrsh.c> |
Youvedeep Singh | 188c1ab | 2018-03-19 20:02:40 +0530 | [diff] [blame] | 190 | #endif |
| 191 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 192 | int z_impl_k_msgq_get(struct k_msgq *msgq, void *data, k_timeout_t timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 193 | { |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 194 | __ASSERT(!arch_is_in_isr() || K_TIMEOUT_EQ(timeout, K_NO_WAIT), ""); |
Benjamin Walsh | 8215ce1 | 2016-11-09 19:45:19 -0500 | [diff] [blame] | 195 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 196 | k_spinlock_key_t key; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 197 | struct k_thread *pending_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 198 | int result; |
| 199 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 200 | key = k_spin_lock(&msgq->lock); |
| 201 | |
| 202 | if (msgq->used_msgs > 0) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 203 | /* take first available message from queue */ |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 204 | (void)memcpy(data, msgq->read_ptr, msgq->msg_size); |
| 205 | msgq->read_ptr += msgq->msg_size; |
| 206 | if (msgq->read_ptr == msgq->buffer_end) { |
| 207 | msgq->read_ptr = msgq->buffer_start; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 208 | } |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 209 | msgq->used_msgs--; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 210 | |
| 211 | /* handle first thread waiting to write (if any) */ |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 212 | pending_thread = z_unpend_first_thread(&msgq->wait_q); |
Flavio Ceolin | 4218d5f | 2018-09-17 09:39:51 -0700 | [diff] [blame] | 213 | if (pending_thread != NULL) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 214 | /* add thread's message to queue */ |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 215 | (void)memcpy(msgq->write_ptr, pending_thread->base.swap_data, |
| 216 | msgq->msg_size); |
| 217 | msgq->write_ptr += msgq->msg_size; |
| 218 | if (msgq->write_ptr == msgq->buffer_end) { |
| 219 | msgq->write_ptr = msgq->buffer_start; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 220 | } |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 221 | msgq->used_msgs++; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 222 | |
| 223 | /* wake up waiting thread */ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 224 | arch_thread_return_value_set(pending_thread, 0); |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 225 | z_ready_thread(pending_thread); |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 226 | z_reschedule(&msgq->lock, key); |
Andy Ross | 8606fab | 2018-03-26 10:54:40 -0700 | [diff] [blame] | 227 | return 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 228 | } |
| 229 | result = 0; |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 230 | } else if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 231 | /* don't wait for a message to become available */ |
| 232 | result = -ENOMSG; |
| 233 | } else { |
| 234 | /* wait for get message success or timeout */ |
Benjamin Walsh | f6ca7de | 2016-11-08 10:36:50 -0500 | [diff] [blame] | 235 | _current->base.swap_data = data; |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 236 | return z_pend_curr(&msgq->lock, key, &msgq->wait_q, timeout); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 239 | k_spin_unlock(&msgq->lock, key); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 240 | |
| 241 | return result; |
| 242 | } |
| 243 | |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 244 | #ifdef CONFIG_USERSPACE |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 245 | static inline int z_vrfy_k_msgq_get(struct k_msgq *q, void *data, |
| 246 | k_timeout_t timeout) |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 247 | { |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 248 | Z_OOPS(Z_SYSCALL_OBJ(q, K_OBJ_MSGQ)); |
| 249 | Z_OOPS(Z_SYSCALL_MEMORY_WRITE(data, q->msg_size)); |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 250 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 251 | return z_impl_k_msgq_get(q, data, timeout); |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 252 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 253 | #include <syscalls/k_msgq_get_mrsh.c> |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 254 | #endif |
| 255 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 256 | int z_impl_k_msgq_peek(struct k_msgq *msgq, void *data) |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 257 | { |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 258 | k_spinlock_key_t key; |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 259 | int result; |
| 260 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 261 | key = k_spin_lock(&msgq->lock); |
| 262 | |
| 263 | if (msgq->used_msgs > 0) { |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 264 | /* take first available message from queue */ |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 265 | (void)memcpy(data, msgq->read_ptr, msgq->msg_size); |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 266 | result = 0; |
| 267 | } else { |
| 268 | /* don't wait for a message to become available */ |
| 269 | result = -ENOMSG; |
| 270 | } |
| 271 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 272 | k_spin_unlock(&msgq->lock, key); |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 273 | |
| 274 | return result; |
| 275 | } |
| 276 | |
| 277 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 278 | static inline int z_vrfy_k_msgq_peek(struct k_msgq *q, void *data) |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 279 | { |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 280 | Z_OOPS(Z_SYSCALL_OBJ(q, K_OBJ_MSGQ)); |
| 281 | Z_OOPS(Z_SYSCALL_MEMORY_WRITE(data, q->msg_size)); |
| 282 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 283 | return z_impl_k_msgq_peek(q, data); |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 284 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 285 | #include <syscalls/k_msgq_peek_mrsh.c> |
Sathish Kuttan | a8aa235 | 2018-11-09 21:04:36 -0800 | [diff] [blame] | 286 | #endif |
| 287 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 288 | void z_impl_k_msgq_purge(struct k_msgq *msgq) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 289 | { |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 290 | k_spinlock_key_t key; |
Benjamin Walsh | b7ef0cb | 2016-10-05 17:32:01 -0400 | [diff] [blame] | 291 | struct k_thread *pending_thread; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 292 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 293 | key = k_spin_lock(&msgq->lock); |
| 294 | |
Peter Mitsis | 340d00a | 2016-09-22 13:59:00 -0400 | [diff] [blame] | 295 | /* wake up any threads that are waiting to write */ |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 296 | while ((pending_thread = z_unpend_first_thread(&msgq->wait_q)) != NULL) { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 297 | arch_thread_return_value_set(pending_thread, -ENOMSG); |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 298 | z_ready_thread(pending_thread); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 299 | } |
| 300 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 301 | msgq->used_msgs = 0; |
| 302 | msgq->read_ptr = msgq->write_ptr; |
Peter Mitsis | 340d00a | 2016-09-22 13:59:00 -0400 | [diff] [blame] | 303 | |
Anas Nashif | 7bde81f | 2019-06-19 07:30:50 -0400 | [diff] [blame] | 304 | z_reschedule(&msgq->lock, key); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 305 | } |
Andrew Boie | 82edb6e | 2017-10-02 10:53:06 -0700 | [diff] [blame] | 306 | |
| 307 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 308 | static inline void z_vrfy_k_msgq_purge(struct k_msgq *q) |
| 309 | { |
| 310 | Z_OOPS(Z_SYSCALL_OBJ(q, K_OBJ_MSGQ)); |
| 311 | z_impl_k_msgq_purge(q); |
| 312 | } |
| 313 | #include <syscalls/k_msgq_purge_mrsh.c> |
| 314 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 315 | static inline uint32_t z_vrfy_k_msgq_num_free_get(struct k_msgq *q) |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 316 | { |
| 317 | Z_OOPS(Z_SYSCALL_OBJ(q, K_OBJ_MSGQ)); |
| 318 | return z_impl_k_msgq_num_free_get(q); |
| 319 | } |
| 320 | #include <syscalls/k_msgq_num_free_get_mrsh.c> |
| 321 | |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 322 | static inline uint32_t z_vrfy_k_msgq_num_used_get(struct k_msgq *q) |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 323 | { |
| 324 | Z_OOPS(Z_SYSCALL_OBJ(q, K_OBJ_MSGQ)); |
| 325 | return z_impl_k_msgq_num_used_get(q); |
| 326 | } |
| 327 | #include <syscalls/k_msgq_num_used_get_mrsh.c> |
| 328 | |
Andrew Boie | 225e4c0 | 2017-10-12 09:54:26 -0700 | [diff] [blame] | 329 | #endif |