2013-11-27 16:28:31 +01:00
|
|
|
/*
|
2014-04-02 17:48:09 +02:00
|
|
|
* Copyright (C) 2014 Freie Universität Berlin
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2013-11-27 16:28:31 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup core_msg
|
2010-09-22 15:10:42 +02:00
|
|
|
* @{
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2010-09-22 15:10:42 +02:00
|
|
|
* @file
|
2013-11-27 16:28:31 +01:00
|
|
|
* @brief Kernel messaging implementation
|
|
|
|
*
|
2014-01-28 11:50:12 +01:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2013-11-27 16:28:31 +01:00
|
|
|
* @author Oliver Hahm <oliver.hahm@inria.fr>
|
2014-04-02 17:48:09 +02:00
|
|
|
* @author Kévin Roussel <Kevin.Roussel@inria.fr>
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
2010-09-22 15:10:42 +02:00
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2013-12-16 17:54:58 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <inttypes.h>
|
2015-08-23 20:31:25 +02:00
|
|
|
#include <assert.h>
|
2010-10-28 11:22:57 +02:00
|
|
|
#include "sched.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
#include "msg.h"
|
2020-04-24 15:55:48 +02:00
|
|
|
#include "msg_bus.h"
|
2016-04-10 00:16:48 +02:00
|
|
|
#include "list.h"
|
2016-01-04 22:29:34 +01:00
|
|
|
#include "thread.h"
|
2017-08-29 13:56:50 +02:00
|
|
|
#if MODULE_CORE_THREAD_FLAGS
|
|
|
|
#include "thread_flags.h"
|
|
|
|
#endif
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "irq.h"
|
|
|
|
#include "cib.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2020-10-22 11:32:06 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
2010-09-22 15:10:42 +02:00
|
|
|
#include "debug.h"
|
|
|
|
|
2013-07-05 23:34:11 +02:00
|
|
|
static int _msg_receive(msg_t *m, int block);
|
2020-03-30 17:02:08 +02:00
|
|
|
static int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block,
|
|
|
|
unsigned state);
|
2013-07-05 23:34:11 +02:00
|
|
|
|
2016-01-04 22:29:34 +01:00
|
|
|
static int queue_msg(thread_t *target, const msg_t *m)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2014-11-07 12:32:15 +01:00
|
|
|
int n = cib_put(&(target->msg_queue));
|
2020-03-30 17:02:08 +02:00
|
|
|
|
2014-11-07 12:32:15 +01:00
|
|
|
if (n < 0) {
|
2023-10-19 17:56:40 +02:00
|
|
|
DEBUG("queue_msg(): message queue of thread %" PRIkernel_pid
|
|
|
|
" is full (or there is none)\n", target->pid);
|
2014-11-07 12:32:15 +01:00
|
|
|
return 0;
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
2010-11-26 14:21:48 +01:00
|
|
|
|
2014-11-07 12:32:15 +01:00
|
|
|
DEBUG("queue_msg(): queuing message\n");
|
|
|
|
msg_t *dest = &target->msg_array[n];
|
2021-01-19 17:47:23 +01:00
|
|
|
|
2014-11-07 12:32:15 +01:00
|
|
|
*dest = *m;
|
2017-08-29 13:56:50 +02:00
|
|
|
#if MODULE_CORE_THREAD_FLAGS
|
|
|
|
target->flags |= THREAD_FLAG_MSG_WAITING;
|
|
|
|
thread_flags_wake(target);
|
|
|
|
#endif
|
2014-11-07 12:32:15 +01:00
|
|
|
return 1;
|
2010-11-26 14:21:48 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 17:21:35 +01:00
|
|
|
int msg_send(msg_t *m, kernel_pid_t target_pid)
|
|
|
|
{
|
2016-03-19 09:25:47 +01:00
|
|
|
if (irq_is_in()) {
|
2014-10-28 09:16:55 +01:00
|
|
|
return msg_send_int(m, target_pid);
|
|
|
|
}
|
2020-08-06 10:46:17 +02:00
|
|
|
if (thread_getpid() == target_pid) {
|
2014-10-28 09:16:55 +01:00
|
|
|
return msg_send_to_self(m);
|
|
|
|
}
|
2016-03-19 09:25:47 +01:00
|
|
|
return _msg_send(m, target_pid, true, irq_disable());
|
2014-09-02 18:32:37 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 17:21:35 +01:00
|
|
|
int msg_try_send(msg_t *m, kernel_pid_t target_pid)
|
|
|
|
{
|
2016-03-19 09:25:47 +01:00
|
|
|
if (irq_is_in()) {
|
2010-09-22 15:10:42 +02:00
|
|
|
return msg_send_int(m, target_pid);
|
|
|
|
}
|
2020-08-06 10:46:17 +02:00
|
|
|
if (thread_getpid() == target_pid) {
|
2014-06-05 17:25:53 +02:00
|
|
|
return msg_send_to_self(m);
|
|
|
|
}
|
2016-03-19 09:25:47 +01:00
|
|
|
return _msg_send(m, target_pid, false, irq_disable());
|
2014-10-28 09:16:55 +01:00
|
|
|
}
|
2014-06-05 17:25:53 +02:00
|
|
|
|
2020-03-30 17:02:08 +02:00
|
|
|
static int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block,
|
|
|
|
unsigned state)
|
2014-10-28 09:16:55 +01:00
|
|
|
{
|
2015-09-12 12:43:15 +02:00
|
|
|
#ifdef DEVELHELP
|
2014-10-10 16:20:56 +02:00
|
|
|
if (!pid_is_valid(target_pid)) {
|
|
|
|
DEBUG("msg_send(): target_pid is invalid, continuing anyways\n");
|
|
|
|
}
|
|
|
|
#endif /* DEVELHELP */
|
|
|
|
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_t *target = thread_get_unchecked(target_pid);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2020-08-06 10:46:17 +02:00
|
|
|
m->sender_pid = thread_getpid();
|
2014-07-08 18:00:54 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (target == NULL) {
|
2020-04-24 12:59:01 +02:00
|
|
|
DEBUG("msg_send(): target thread %d does not exist\n", target_pid);
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2010-09-22 15:10:42 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_t *me = thread_get_active();
|
2016-04-10 00:16:48 +02:00
|
|
|
|
2015-04-10 09:10:16 +02:00
|
|
|
DEBUG("msg_send() %s:%i: Sending from %" PRIkernel_pid " to %" PRIkernel_pid
|
2022-11-19 01:42:51 +01:00
|
|
|
". block=%i src->state=%i target->state=%i\n", __FILE__,
|
2020-08-06 10:46:17 +02:00
|
|
|
__LINE__, thread_getpid(), target_pid,
|
2021-03-23 09:37:52 +01:00
|
|
|
block, (int)me->status, (int)target->status);
|
2014-02-12 19:09:50 +01:00
|
|
|
|
2013-12-02 16:38:39 +01:00
|
|
|
if (target->status != STATUS_RECEIVE_BLOCKED) {
|
2020-03-30 17:02:08 +02:00
|
|
|
DEBUG(
|
|
|
|
"msg_send() %s:%i: Target %" PRIkernel_pid " is not RECEIVE_BLOCKED.\n",
|
2022-11-19 01:42:51 +01:00
|
|
|
__FILE__, __LINE__, target_pid);
|
2015-04-10 09:10:16 +02:00
|
|
|
|
2014-11-07 12:32:15 +01:00
|
|
|
if (queue_msg(target, m)) {
|
2015-04-10 09:10:16 +02:00
|
|
|
DEBUG("msg_send() %s:%i: Target %" PRIkernel_pid
|
2022-11-19 01:42:51 +01:00
|
|
|
" has a msg_queue. Queueing message.\n", __FILE__,
|
2015-04-10 09:10:16 +02:00
|
|
|
__LINE__, target_pid);
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2021-09-27 20:35:00 +02:00
|
|
|
if (me->status == STATUS_REPLY_BLOCKED
|
|
|
|
|| (IS_USED(MODULE_CORE_THREAD_FLAGS) &&
|
|
|
|
sched_context_switch_request)
|
|
|
|
) {
|
2014-10-18 01:24:49 +02:00
|
|
|
thread_yield_higher();
|
2014-02-12 19:12:58 +01:00
|
|
|
}
|
2010-11-26 14:21:48 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (!block) {
|
2020-08-06 10:46:17 +02:00
|
|
|
DEBUG("msg_send: %" PRIkernel_pid ": Receiver not waiting, "
|
2021-03-23 09:45:03 +01:00
|
|
|
"block=%d\n", me->pid, block);
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2010-09-22 15:10:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-10 00:16:48 +02:00
|
|
|
DEBUG("msg_send: %" PRIkernel_pid ": going send blocked.\n",
|
|
|
|
me->pid);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2020-08-06 10:46:17 +02:00
|
|
|
me->wait_data = m;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
int newstatus;
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2016-04-10 00:16:48 +02:00
|
|
|
if (me->status == STATUS_REPLY_BLOCKED) {
|
2010-09-22 15:10:42 +02:00
|
|
|
newstatus = STATUS_REPLY_BLOCKED;
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2010-09-22 15:10:42 +02:00
|
|
|
newstatus = STATUS_SEND_BLOCKED;
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:46:17 +02:00
|
|
|
sched_set_status(me, newstatus);
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2016-04-10 00:16:48 +02:00
|
|
|
thread_add_to_list(&(target->msg_waiters), me);
|
2014-10-18 01:24:49 +02:00
|
|
|
|
2017-08-29 13:56:50 +02:00
|
|
|
#if MODULE_CORE_THREAD_FLAGS
|
|
|
|
target->flags |= THREAD_FLAG_MSG_WAITING;
|
|
|
|
thread_flags_wake(target);
|
|
|
|
#endif
|
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2014-10-18 01:24:49 +02:00
|
|
|
thread_yield_higher();
|
2016-04-10 00:16:48 +02:00
|
|
|
|
|
|
|
DEBUG("msg_send: %" PRIkernel_pid ": Back from send block.\n",
|
|
|
|
me->pid);
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-04-10 09:10:16 +02:00
|
|
|
DEBUG("msg_send: %" PRIkernel_pid ": Direct msg copy from %"
|
|
|
|
PRIkernel_pid " to %" PRIkernel_pid ".\n",
|
2016-04-10 00:16:48 +02:00
|
|
|
me->pid, thread_getpid(), target_pid);
|
2010-09-22 15:10:42 +02:00
|
|
|
/* copy msg to target */
|
2020-08-06 10:46:17 +02:00
|
|
|
msg_t *target_message = target->wait_data;
|
2010-09-22 15:10:42 +02:00
|
|
|
*target_message = *m;
|
2013-12-04 20:36:21 +01:00
|
|
|
sched_set_status(target, STATUS_PENDING);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2014-11-04 20:19:13 +01:00
|
|
|
thread_yield_higher();
|
2014-10-18 01:24:49 +02:00
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2010-11-26 14:21:48 +01:00
|
|
|
return 1;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2014-04-02 17:48:09 +02:00
|
|
|
int msg_send_to_self(msg_t *m)
|
|
|
|
{
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned state = irq_disable();
|
2014-04-02 17:48:09 +02:00
|
|
|
|
2020-08-06 10:46:17 +02:00
|
|
|
m->sender_pid = thread_getpid();
|
|
|
|
int res = queue_msg(thread_get_active(), m);
|
2014-04-02 17:48:09 +02:00
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2014-04-02 17:48:09 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-04-24 12:59:01 +02:00
|
|
|
static int _msg_send_oneway(msg_t *m, kernel_pid_t target_pid)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2015-09-12 12:43:15 +02:00
|
|
|
#ifdef DEVELHELP
|
2014-10-10 16:20:56 +02:00
|
|
|
if (!pid_is_valid(target_pid)) {
|
2020-04-24 12:59:01 +02:00
|
|
|
DEBUG("%s: target_pid is invalid, continuing anyways\n", __func__);
|
2014-10-10 16:20:56 +02:00
|
|
|
}
|
|
|
|
#endif /* DEVELHELP */
|
|
|
|
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_t *target = thread_get_unchecked(target_pid);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-06-05 17:25:53 +02:00
|
|
|
if (target == NULL) {
|
2020-04-24 12:59:01 +02:00
|
|
|
DEBUG("%s: target thread %d does not exist\n", __func__, target_pid);
|
2014-06-05 17:25:53 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-12-02 16:38:39 +01:00
|
|
|
if (target->status == STATUS_RECEIVE_BLOCKED) {
|
2020-04-24 12:59:01 +02:00
|
|
|
DEBUG("%s: Direct msg copy from %" PRIkernel_pid " to %"
|
|
|
|
PRIkernel_pid ".\n", __func__, thread_getpid(), target_pid);
|
2010-09-24 16:24:13 +02:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
/* copy msg to target */
|
2020-03-30 17:02:08 +02:00
|
|
|
msg_t *target_message = (msg_t *)target->wait_data;
|
2010-09-22 15:10:42 +02:00
|
|
|
*target_message = *m;
|
|
|
|
|
2020-04-24 12:59:01 +02:00
|
|
|
sched_set_status(target, STATUS_PENDING);
|
2020-04-24 15:55:48 +02:00
|
|
|
|
2023-10-16 12:17:48 +02:00
|
|
|
/* Interrupts are disabled here, we can set / reuse
|
2020-04-24 15:55:48 +02:00
|
|
|
sched_context_switch_request. */
|
2010-10-28 11:22:57 +02:00
|
|
|
sched_context_switch_request = 1;
|
2020-04-24 12:59:01 +02:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
return 1;
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-04-24 12:59:01 +02:00
|
|
|
DEBUG("%s: Receiver not waiting.\n", __func__);
|
2010-11-26 14:21:48 +01:00
|
|
|
return (queue_msg(target, m));
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 12:59:01 +02:00
|
|
|
int msg_send_int(msg_t *m, kernel_pid_t target_pid)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
m->sender_pid = KERNEL_PID_ISR;
|
|
|
|
|
|
|
|
res = _msg_send_oneway(m, target_pid);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:55:48 +02:00
|
|
|
int msg_send_bus(msg_t *m, msg_bus_t *bus)
|
|
|
|
{
|
|
|
|
const bool in_irq = irq_is_in();
|
core/msg_bus: fix shift on 8-bit platforms
The previous shift would wrap if the compiler defaults to 16 bit words.
Use explicit `unsigned long` integer constants to mitigate that.
before:
2020-07-22 15:25:17,063 # THREAD 1 start
2020-07-22 15:25:17,063 # THREAD 2 start
2020-07-22 15:25:17,065 # THREAD 3 start
2020-07-22 15:25:17,066 # THREADS CREATED
2020-07-22 15:25:17,068 # Posted event 22 to 0 threads
2020-07-22 15:25:17,071 # Posted event 23 to 0 threads
2020-07-22 15:25:17,076 # Posted event 24 to 0 threads
2020-07-22 15:25:17,076 # SUCCESS
2020-07-22 15:26:00,188 # Exiting Pyterm
after:
2020-07-22 15:26:10,374 # THREAD 1 start
2020-07-22 15:26:10,374 # THREAD 2 start
2020-07-22 15:26:10,377 # THREAD 3 start
2020-07-22 15:26:10,377 # THREADS CREATED
2020-07-22 15:26:10,380 # Posted event 22 to 0 threads
2020-07-22 15:26:10,383 # T1 recv: Hello Threads! (type=23)
2020-07-22 15:26:10,386 # T3 recv: Hello Threads! (type=23)
2020-07-22 15:26:10,388 # Posted event 23 to 2 threads
2020-07-22 15:26:10,391 # T2 recv: Hello Threads! (type=24)
2020-07-22 15:26:10,394 # Posted event 24 to 1 threads
2020-07-22 15:26:10,396 # SUCCESS
2020-07-22 15:27:31 +02:00
|
|
|
const uint32_t event_mask = (1UL << (m->type & 0x1F));
|
2020-04-24 15:55:48 +02:00
|
|
|
int count = 0;
|
|
|
|
|
2020-05-05 13:39:48 +02:00
|
|
|
m->sender_pid = (in_irq ? KERNEL_PID_ISR : thread_getpid())
|
2021-01-19 17:47:23 +01:00
|
|
|
| MSB_BUS_PID_FLAG;
|
2020-04-24 15:55:48 +02:00
|
|
|
|
|
|
|
unsigned state = irq_disable();
|
|
|
|
|
|
|
|
for (list_node_t *e = bus->subs.next; e; e = e->next) {
|
|
|
|
msg_bus_entry_t *subscriber = container_of(e, msg_bus_entry_t, next);
|
|
|
|
|
|
|
|
if ((subscriber->event_mask & event_mask) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_msg_send_oneway(m, subscriber->pid) > 0) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
irq_restore(state);
|
|
|
|
|
|
|
|
if (sched_context_switch_request && !in_irq) {
|
|
|
|
thread_yield_higher();
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2014-07-06 22:57:56 +02:00
|
|
|
int msg_send_receive(msg_t *m, msg_t *reply, kernel_pid_t target_pid)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2020-08-06 10:46:17 +02:00
|
|
|
assert(thread_getpid() != target_pid);
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned state = irq_disable();
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_t *me = thread_get_active();
|
2021-01-19 17:47:23 +01:00
|
|
|
|
2013-12-04 20:36:21 +01:00
|
|
|
sched_set_status(me, STATUS_REPLY_BLOCKED);
|
2020-08-06 10:46:17 +02:00
|
|
|
me->wait_data = reply;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2023-10-16 12:17:48 +02:00
|
|
|
/* we reuse (abuse) reply for sending, because wait_data might be
|
2016-05-16 23:55:13 +02:00
|
|
|
* overwritten if the target is not in RECEIVE_BLOCKED */
|
|
|
|
*reply = *m;
|
2010-09-22 15:10:42 +02:00
|
|
|
/* msg_send blocks until reply received */
|
2016-05-16 23:55:13 +02:00
|
|
|
return _msg_send(reply, target_pid, true, state);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
int msg_reply(msg_t *m, msg_t *reply)
|
|
|
|
{
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned state = irq_disable();
|
2010-09-24 16:24:13 +02:00
|
|
|
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_t *target = thread_get_unchecked(m->sender_pid);
|
2020-03-30 17:02:08 +02:00
|
|
|
|
2015-10-12 20:20:27 +02:00
|
|
|
assert(target != NULL);
|
2013-09-11 19:39:34 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (target->status != STATUS_REPLY_BLOCKED) {
|
2015-04-10 09:10:16 +02:00
|
|
|
DEBUG("msg_reply(): %" PRIkernel_pid ": Target \"%" PRIkernel_pid
|
2020-08-06 10:46:17 +02:00
|
|
|
"\" not waiting for reply.", thread_getpid(),
|
2020-03-30 17:02:08 +02:00
|
|
|
target->pid);
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2010-09-22 15:10:42 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2015-04-10 09:10:16 +02:00
|
|
|
DEBUG("msg_reply(): %" PRIkernel_pid ": Direct msg copy.\n",
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_getpid());
|
2010-09-22 15:10:42 +02:00
|
|
|
/* copy msg to target */
|
2020-03-30 17:02:08 +02:00
|
|
|
msg_t *target_message = (msg_t *)target->wait_data;
|
2021-01-19 17:47:23 +01:00
|
|
|
|
2010-10-28 10:12:45 +02:00
|
|
|
*target_message = *reply;
|
2013-12-04 20:36:21 +01:00
|
|
|
sched_set_status(target, STATUS_PENDING);
|
2014-10-18 01:24:49 +02:00
|
|
|
uint16_t target_prio = target->priority;
|
2021-01-19 17:47:23 +01:00
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2014-10-18 01:24:49 +02:00
|
|
|
sched_switch(target_prio);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
int msg_reply_int(msg_t *m, msg_t *reply)
|
|
|
|
{
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_t *target = thread_get_unchecked(m->sender_pid);
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2023-06-22 21:24:05 +02:00
|
|
|
/* msg_reply_int() can only be used to reply to existing threads */
|
|
|
|
assert(target != NULL);
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (target->status != STATUS_REPLY_BLOCKED) {
|
2015-04-10 09:10:16 +02:00
|
|
|
DEBUG("msg_reply_int(): %" PRIkernel_pid ": Target \"%" PRIkernel_pid
|
2020-08-06 10:46:17 +02:00
|
|
|
"\" not waiting for reply.", thread_getpid(),
|
2020-03-30 17:02:08 +02:00
|
|
|
target->pid);
|
2010-09-24 16:24:13 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2020-03-30 17:02:08 +02:00
|
|
|
msg_t *target_message = (msg_t *)target->wait_data;
|
2021-01-19 17:47:23 +01:00
|
|
|
|
2010-11-03 11:37:20 +01:00
|
|
|
*target_message = *reply;
|
2013-12-04 20:36:21 +01:00
|
|
|
sched_set_status(target, STATUS_PENDING);
|
2010-10-28 11:22:57 +02:00
|
|
|
sched_context_switch_request = 1;
|
2010-09-24 16:24:13 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-05 19:22:29 +02:00
|
|
|
int msg_try_receive(msg_t *m)
|
|
|
|
{
|
2013-07-09 13:41:08 +02:00
|
|
|
return _msg_receive(m, 0);
|
2013-07-05 19:22:29 +02:00
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
int msg_receive(msg_t *m)
|
2013-07-05 19:22:29 +02:00
|
|
|
{
|
2013-07-09 13:41:08 +02:00
|
|
|
return _msg_receive(m, 1);
|
2013-07-05 19:22:29 +02:00
|
|
|
}
|
|
|
|
|
2013-07-09 13:41:08 +02:00
|
|
|
static int _msg_receive(msg_t *m, int block)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned state = irq_disable();
|
2020-03-30 17:02:08 +02:00
|
|
|
|
2015-04-10 09:10:16 +02:00
|
|
|
DEBUG("_msg_receive: %" PRIkernel_pid ": _msg_receive.\n",
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_getpid());
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_t *me = thread_get_active();
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-12-02 16:56:43 +01:00
|
|
|
int queue_index = -1;
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2018-11-15 14:04:28 +01:00
|
|
|
if (thread_has_msg_queue(me)) {
|
2013-12-02 16:56:43 +01:00
|
|
|
queue_index = cib_get(&(me->msg_queue));
|
2010-11-26 15:02:15 +01:00
|
|
|
}
|
|
|
|
|
2013-07-05 19:22:29 +02:00
|
|
|
/* no message, fail */
|
2016-04-10 00:16:48 +02:00
|
|
|
if ((!block) && ((!me->msg_waiters.next) && (queue_index == -1))) {
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2013-07-05 19:22:29 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-12-02 16:56:43 +01:00
|
|
|
if (queue_index >= 0) {
|
2020-08-06 10:46:17 +02:00
|
|
|
DEBUG("_msg_receive: %" PRIkernel_pid ": _msg_receive(): We've got a "
|
|
|
|
"queued message.\n", thread_getpid());
|
2013-12-02 16:56:43 +01:00
|
|
|
*m = me->msg_array[queue_index];
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-03-30 17:02:08 +02:00
|
|
|
me->wait_data = (void *)m;
|
2010-11-26 14:21:48 +01:00
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2016-04-10 00:16:48 +02:00
|
|
|
list_node_t *next = list_remove_head(&me->msg_waiters);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2016-04-10 00:16:48 +02:00
|
|
|
if (next == NULL) {
|
2020-08-06 10:46:17 +02:00
|
|
|
DEBUG("_msg_receive: %" PRIkernel_pid ": _msg_receive(): No thread in "
|
|
|
|
"waiting list.\n", thread_getpid());
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2013-12-02 16:56:43 +01:00
|
|
|
if (queue_index < 0) {
|
2020-08-06 10:46:17 +02:00
|
|
|
DEBUG("_msg_receive(): %" PRIkernel_pid ": No msg in queue. Going "
|
|
|
|
"blocked.\n", thread_getpid());
|
2013-12-04 20:36:21 +01:00
|
|
|
sched_set_status(me, STATUS_RECEIVE_BLOCKED);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2014-10-18 01:24:49 +02:00
|
|
|
thread_yield_higher();
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2010-11-26 14:21:48 +01:00
|
|
|
/* sender copied message */
|
2020-08-06 10:46:17 +02:00
|
|
|
assert(thread_get_active()->status != STATUS_RECEIVE_BLOCKED);
|
2010-11-26 14:21:48 +01:00
|
|
|
}
|
2014-06-05 17:25:53 +02:00
|
|
|
else {
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2014-06-05 17:25:53 +02:00
|
|
|
}
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
return 1;
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-08-06 10:46:17 +02:00
|
|
|
DEBUG("_msg_receive: %" PRIkernel_pid ": _msg_receive(): Waking up "
|
2021-01-19 17:47:23 +01:00
|
|
|
"waiting thread.\n", thread_getpid());
|
2016-04-10 00:16:48 +02:00
|
|
|
|
2020-03-30 17:02:08 +02:00
|
|
|
thread_t *sender =
|
|
|
|
container_of((clist_node_t *)next, thread_t, rq_entry);
|
2010-11-26 14:21:48 +01:00
|
|
|
|
2013-12-02 16:56:43 +01:00
|
|
|
if (queue_index >= 0) {
|
2013-12-04 20:36:21 +01:00
|
|
|
/* We've already got a message from the queue. As there is a
|
2010-11-26 14:21:48 +01:00
|
|
|
* waiter, take it's message into the just freed queue space.
|
|
|
|
*/
|
|
|
|
m = &(me->msg_array[cib_put(&(me->msg_queue))]);
|
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
/* copy msg */
|
2020-03-30 17:02:08 +02:00
|
|
|
msg_t *sender_msg = (msg_t *)sender->wait_data;
|
2010-09-22 15:10:42 +02:00
|
|
|
*m = *sender_msg;
|
|
|
|
|
|
|
|
/* remove sender from queue */
|
2015-04-28 20:02:05 +02:00
|
|
|
uint16_t sender_prio = THREAD_PRIORITY_IDLE;
|
2014-01-25 15:42:13 +01:00
|
|
|
if (sender->status != STATUS_REPLY_BLOCKED) {
|
|
|
|
sender->wait_data = NULL;
|
|
|
|
sched_set_status(sender, STATUS_PENDING);
|
2014-10-17 23:32:40 +02:00
|
|
|
sender_prio = sender->priority;
|
2014-01-25 15:42:13 +01:00
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2015-04-28 20:02:05 +02:00
|
|
|
if (sender_prio < THREAD_PRIORITY_IDLE) {
|
2014-10-17 23:32:40 +02:00
|
|
|
sched_switch(sender_prio);
|
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2014-06-05 17:25:53 +02:00
|
|
|
|
|
|
|
DEBUG("This should have never been reached!\n");
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
2010-11-26 14:21:48 +01:00
|
|
|
|
2021-03-09 13:46:03 +01:00
|
|
|
static unsigned _msg_avail(thread_t *thread)
|
2015-09-30 11:57:20 +02:00
|
|
|
{
|
|
|
|
DEBUG("msg_available: %" PRIkernel_pid ": msg_available.\n",
|
2021-03-09 13:46:03 +01:00
|
|
|
thread->pid);
|
2015-09-30 11:57:20 +02:00
|
|
|
|
2021-11-24 13:35:20 +01:00
|
|
|
unsigned queue_count = 0;
|
2015-09-30 11:57:20 +02:00
|
|
|
|
2021-03-09 13:46:03 +01:00
|
|
|
if (thread_has_msg_queue(thread)) {
|
|
|
|
queue_count = cib_avail(&(thread->msg_queue));
|
2015-09-30 11:57:20 +02:00
|
|
|
}
|
|
|
|
|
2021-11-24 13:35:20 +01:00
|
|
|
return queue_count;
|
2015-09-30 11:57:20 +02:00
|
|
|
}
|
|
|
|
|
2021-03-09 13:46:03 +01:00
|
|
|
unsigned msg_avail_thread(kernel_pid_t pid)
|
|
|
|
{
|
|
|
|
return _msg_avail(thread_get(pid));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned msg_avail(void)
|
|
|
|
{
|
|
|
|
return _msg_avail(thread_get_active());
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned msg_queue_capacity(kernel_pid_t pid)
|
|
|
|
{
|
|
|
|
DEBUG("msg_queue_capacity: %" PRIkernel_pid ": msg_queue_capacity.\n",
|
|
|
|
pid);
|
|
|
|
|
|
|
|
thread_t *thread = thread_get(pid);
|
2023-06-22 21:24:05 +02:00
|
|
|
|
|
|
|
assert(thread != NULL);
|
|
|
|
|
2021-03-09 13:46:03 +01:00
|
|
|
int queue_cap = 0;
|
|
|
|
|
|
|
|
if (thread_has_msg_queue(thread)) {
|
|
|
|
queue_cap = cib_size(&(thread->msg_queue));
|
|
|
|
}
|
|
|
|
|
|
|
|
return queue_cap;
|
|
|
|
}
|
|
|
|
|
2016-04-11 21:55:28 +02:00
|
|
|
void msg_init_queue(msg_t *array, int num)
|
2013-06-20 18:18:29 +02:00
|
|
|
{
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_t *me = thread_get_active();
|
2020-03-30 17:02:08 +02:00
|
|
|
|
2016-04-11 21:55:28 +02:00
|
|
|
me->msg_array = array;
|
|
|
|
cib_init(&(me->msg_queue), num);
|
2010-11-26 14:21:48 +01:00
|
|
|
}
|
2016-02-26 22:19:31 +01:00
|
|
|
|
|
|
|
void msg_queue_print(void)
|
|
|
|
{
|
|
|
|
unsigned state = irq_disable();
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_t *thread = thread_get_active();
|
2020-10-06 09:21:40 +02:00
|
|
|
|
2021-11-24 13:35:20 +01:00
|
|
|
unsigned msg_counter = msg_avail();
|
2020-10-06 09:21:40 +02:00
|
|
|
|
2021-11-24 13:35:20 +01:00
|
|
|
if (msg_counter < 1) {
|
2020-10-06 09:21:40 +02:00
|
|
|
/* no msg queue */
|
2022-09-16 15:56:17 +02:00
|
|
|
irq_restore(state);
|
2021-11-24 13:35:20 +01:00
|
|
|
printf("No messages or no message queue\n");
|
2020-10-06 09:21:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-02-26 22:19:31 +01:00
|
|
|
cib_t *msg_queue = &thread->msg_queue;
|
|
|
|
msg_t *msg_array = thread->msg_array;
|
2020-10-06 09:21:40 +02:00
|
|
|
int first_msg = cib_peek(msg_queue);
|
2016-02-26 22:19:31 +01:00
|
|
|
|
|
|
|
printf("Message queue of thread %" PRIkernel_pid "\n", thread->pid);
|
2021-11-24 13:35:20 +01:00
|
|
|
printf(" size: %u (avail: %u)\n", msg_queue->mask + 1, msg_counter);
|
2016-02-26 22:19:31 +01:00
|
|
|
|
2021-11-24 13:35:20 +01:00
|
|
|
for (unsigned i = 0; i < msg_counter; i++) {
|
2020-10-06 09:21:40 +02:00
|
|
|
msg_t *m = &msg_array[(first_msg + i) & msg_queue->mask];
|
2016-02-26 22:19:31 +01:00
|
|
|
printf(" * %u: sender: %" PRIkernel_pid ", type: 0x%04" PRIu16
|
|
|
|
", content: %" PRIu32 " (%p)\n", i, m->sender_pid, m->type,
|
2016-06-02 20:40:15 +02:00
|
|
|
m->content.value, m->content.ptr);
|
2016-02-26 22:19:31 +01:00
|
|
|
}
|
|
|
|
irq_restore(state);
|
|
|
|
}
|