@@ -124,8 +124,8 @@ int k_msgq_cleanup(struct k_msgq *msgq)
124
124
return 0 ;
125
125
}
126
126
127
-
128
- int z_impl_k_msgq_put ( struct k_msgq * msgq , const void * data , k_timeout_t timeout )
127
+ static inline int put_msg_in_queue ( struct k_msgq * msgq , const void * data ,
128
+ k_timeout_t timeout , bool put_at_back )
129
129
{
130
130
__ASSERT (!arch_is_in_isr () || K_TIMEOUT_EQ (timeout , K_NO_WAIT ), "" );
131
131
@@ -150,13 +150,30 @@ int z_impl_k_msgq_put(struct k_msgq *msgq, const void *data, k_timeout_t timeout
150
150
arch_thread_return_value_set (pending_thread , 0 );
151
151
z_ready_thread (pending_thread );
152
152
} else {
153
- /* put message in queue */
154
153
__ASSERT_NO_MSG (msgq -> write_ptr >= msgq -> buffer_start &&
155
154
msgq -> write_ptr < msgq -> buffer_end );
156
- (void )memcpy (msgq -> write_ptr , (char * )data , msgq -> msg_size );
157
- msgq -> write_ptr += msgq -> msg_size ;
158
- if (msgq -> write_ptr == msgq -> buffer_end ) {
159
- msgq -> write_ptr = msgq -> buffer_start ;
155
+ if (put_at_back ) {
156
+ /*
157
+ * to write a message to the back of the queue,
158
+ * copy the message and increment write_ptr
159
+ */
160
+ (void )memcpy (msgq -> write_ptr , (char * )data , msgq -> msg_size );
161
+ msgq -> write_ptr += msgq -> msg_size ;
162
+ if (msgq -> write_ptr == msgq -> buffer_end ) {
163
+ msgq -> write_ptr = msgq -> buffer_start ;
164
+ }
165
+ } else {
166
+ /*
167
+ * to write a message to the head of the queue,
168
+ * first decrement the read pointer (to open
169
+ * space at the front of the queue) then copy
170
+ * the message to the newly created space.
171
+ */
172
+ if (msgq -> read_ptr == msgq -> buffer_start ) {
173
+ msgq -> read_ptr = msgq -> buffer_end ;
174
+ }
175
+ msgq -> read_ptr -= msgq -> msg_size ;
176
+ (void )memcpy (msgq -> read_ptr , (char * )data , msgq -> msg_size );
160
177
}
161
178
msgq -> used_msgs ++ ;
162
179
resched = handle_poll_events (msgq );
@@ -187,6 +204,17 @@ int z_impl_k_msgq_put(struct k_msgq *msgq, const void *data, k_timeout_t timeout
187
204
return result ;
188
205
}
189
206
207
+
208
+ int z_impl_k_msgq_put (struct k_msgq * msgq , const void * data , k_timeout_t timeout )
209
+ {
210
+ return put_msg_in_queue (msgq , data , timeout , true);
211
+ }
212
+
213
+ int z_impl_k_msgq_put_front (struct k_msgq * msgq , const void * data , k_timeout_t timeout )
214
+ {
215
+ return put_msg_in_queue (msgq , data , timeout , false);
216
+ }
217
+
190
218
#ifdef CONFIG_USERSPACE
191
219
static inline int z_vrfy_k_msgq_put (struct k_msgq * msgq , const void * data ,
192
220
k_timeout_t timeout )
@@ -197,6 +225,16 @@ static inline int z_vrfy_k_msgq_put(struct k_msgq *msgq, const void *data,
197
225
return z_impl_k_msgq_put (msgq , data , timeout );
198
226
}
199
227
#include <zephyr/syscalls/k_msgq_put_mrsh.c>
228
+
229
+ static inline int z_vrfy_k_msgq_put_front (struct k_msgq * msgq , const void * data ,
230
+ k_timeout_t timeout )
231
+ {
232
+ K_OOPS (K_SYSCALL_OBJ (msgq , K_OBJ_MSGQ ));
233
+ K_OOPS (K_SYSCALL_MEMORY_READ (data , msgq -> msg_size ));
234
+
235
+ return z_impl_k_msgq_put_front (msgq , data , timeout );
236
+ }
237
+ #include <zephyr/syscalls/k_msgq_put_front_mrsh.c>
200
238
#endif /* CONFIG_USERSPACE */
201
239
202
240
void z_impl_k_msgq_get_attrs (struct k_msgq * msgq , struct k_msgq_attrs * attrs )
0 commit comments