2015-12-30 15:39:43 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup core_mbox
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief mailbox implementation
|
|
|
|
*
|
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "mbox.h"
|
|
|
|
#include "irq.h"
|
|
|
|
#include "sched.h"
|
|
|
|
#include "thread.h"
|
|
|
|
|
2020-10-22 11:32:06 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
2015-12-30 15:39:43 +01:00
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
static void _wake_waiter(thread_t *thread, unsigned irqstate)
|
|
|
|
{
|
|
|
|
sched_set_status(thread, STATUS_PENDING);
|
|
|
|
|
2020-03-30 17:02:08 +02:00
|
|
|
DEBUG("mbox: Thread %" PRIkernel_pid ": _wake_waiter(): waking up "
|
2020-08-06 10:46:17 +02:00
|
|
|
"%" PRIkernel_pid ".\n", thread_getpid(), thread->pid);
|
2015-12-30 15:39:43 +01:00
|
|
|
|
|
|
|
uint16_t process_priority = thread->priority;
|
2021-01-19 17:47:23 +01:00
|
|
|
|
2015-12-30 15:39:43 +01:00
|
|
|
irq_restore(irqstate);
|
|
|
|
sched_switch(process_priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _wait(list_node_t *wait_list, unsigned irqstate)
|
|
|
|
{
|
2020-03-30 17:02:08 +02:00
|
|
|
DEBUG("mbox: Thread %" PRIkernel_pid " _wait(): going blocked.\n",
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_getpid());
|
2015-12-30 15:39:43 +01:00
|
|
|
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_t *me = thread_get_active();
|
2021-01-19 17:47:23 +01:00
|
|
|
|
2015-12-30 15:39:43 +01:00
|
|
|
sched_set_status(me, STATUS_MBOX_BLOCKED);
|
|
|
|
thread_add_to_list(wait_list, me);
|
|
|
|
irq_restore(irqstate);
|
|
|
|
thread_yield();
|
|
|
|
|
2020-03-30 17:02:08 +02:00
|
|
|
DEBUG("mbox: Thread %" PRIkernel_pid " _wait(): woke up.\n",
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_getpid());
|
2015-12-30 15:39:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int _mbox_put(mbox_t *mbox, msg_t *msg, int blocking)
|
|
|
|
{
|
|
|
|
unsigned irqstate = irq_disable();
|
|
|
|
|
2019-01-07 20:58:35 +01:00
|
|
|
list_node_t *next = list_remove_head(&mbox->readers);
|
2020-03-30 17:02:08 +02:00
|
|
|
|
2015-12-30 15:39:43 +01:00
|
|
|
if (next) {
|
2020-03-30 17:02:08 +02:00
|
|
|
DEBUG("mbox: Thread %" PRIkernel_pid " mbox 0x%08x: _tryput(): "
|
2020-08-06 10:46:17 +02:00
|
|
|
"there's a waiter.\n", thread_getpid(), (unsigned)mbox);
|
2020-03-30 17:02:08 +02:00
|
|
|
thread_t *thread =
|
|
|
|
container_of((clist_node_t *)next, thread_t, rq_entry);
|
2015-12-30 15:39:43 +01:00
|
|
|
*(msg_t *)thread->wait_data = *msg;
|
|
|
|
_wake_waiter(thread, irqstate);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
2022-11-22 22:35:13 +01:00
|
|
|
while (cib_full(&mbox->cib)) {
|
2015-12-30 15:39:43 +01:00
|
|
|
if (blocking) {
|
|
|
|
_wait(&mbox->writers, irqstate);
|
|
|
|
irqstate = irq_disable();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
irq_restore(irqstate);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 17:02:08 +02:00
|
|
|
DEBUG("mbox: Thread %" PRIkernel_pid " mbox 0x%08x: _tryput(): "
|
2020-08-06 10:46:17 +02:00
|
|
|
"queued message.\n", thread_getpid(), (unsigned)mbox);
|
|
|
|
msg->sender_pid = thread_getpid();
|
2015-12-30 15:39:43 +01:00
|
|
|
/* copy msg into queue */
|
|
|
|
mbox->msg_array[cib_put_unsafe(&mbox->cib)] = *msg;
|
|
|
|
irq_restore(irqstate);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int _mbox_get(mbox_t *mbox, msg_t *msg, int blocking)
|
|
|
|
{
|
|
|
|
unsigned irqstate = irq_disable();
|
|
|
|
|
|
|
|
if (cib_avail(&mbox->cib)) {
|
2020-03-30 17:02:08 +02:00
|
|
|
DEBUG("mbox: Thread %" PRIkernel_pid " mbox 0x%08x: _tryget(): "
|
2020-08-06 10:46:17 +02:00
|
|
|
"got queued message.\n", thread_getpid(), (unsigned)mbox);
|
2015-12-30 15:39:43 +01:00
|
|
|
/* copy msg from queue */
|
|
|
|
*msg = mbox->msg_array[cib_get_unsafe(&mbox->cib)];
|
2019-01-07 20:58:35 +01:00
|
|
|
list_node_t *next = list_remove_head(&mbox->writers);
|
2015-12-30 15:39:43 +01:00
|
|
|
if (next) {
|
2020-03-30 17:02:08 +02:00
|
|
|
thread_t *thread = container_of((clist_node_t *)next, thread_t,
|
|
|
|
rq_entry);
|
2015-12-30 15:39:43 +01:00
|
|
|
_wake_waiter(thread, irqstate);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
irq_restore(irqstate);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (blocking) {
|
2020-08-06 10:46:17 +02:00
|
|
|
thread_get_active()->wait_data = msg;
|
2015-12-30 15:39:43 +01:00
|
|
|
_wait(&mbox->readers, irqstate);
|
|
|
|
/* sender has copied message */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
irq_restore(irqstate);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|