2010-09-22 15:10:42 +02:00
|
|
|
/**
|
|
|
|
* kernel messaging implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Freie Universität Berlin
|
|
|
|
*
|
|
|
|
* This file subject to the terms and conditions of the GNU General Public
|
|
|
|
* License. See the file LICENSE in the top level directory for more details.
|
|
|
|
*
|
|
|
|
* @ingroup kernel_msg
|
|
|
|
* @{
|
|
|
|
* @file
|
|
|
|
* @author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de>
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel.h"
|
2010-10-28 11:22:57 +02:00
|
|
|
#include "sched.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
#include "msg.h"
|
|
|
|
#include "queue.h"
|
|
|
|
#include "tcb.h"
|
|
|
|
#include <stddef.h>
|
2010-09-24 16:24:13 +02:00
|
|
|
#include <irq.h>
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
#include "flags.h"
|
|
|
|
|
|
|
|
//#define ENABLE_DEBUG
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
int msg_send(msg* m, unsigned int target_pid, bool block) {
|
|
|
|
if (inISR()) {
|
|
|
|
return msg_send_int(m, target_pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
int result = 1;
|
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
tcb *target = (tcb*)sched_threads[target_pid];
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
m->sender_pid = thread_pid;
|
2010-09-22 15:10:42 +02:00
|
|
|
if (m->sender_pid == target_pid) return -1;
|
|
|
|
|
|
|
|
dINT();
|
|
|
|
|
|
|
|
if (target == NULL) {
|
|
|
|
eINT();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target->status != STATUS_RECEIVE_BLOCKED) {
|
|
|
|
if (! block ) {
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: receiver not waiting. block=%u\n", active_thread->name, block);
|
2010-09-22 15:10:42 +02:00
|
|
|
eINT();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: send_blocked.\n", active_thread->name);
|
2010-09-22 15:10:42 +02:00
|
|
|
queue_node_t n;
|
2010-10-28 11:22:57 +02:00
|
|
|
n.priority = active_thread->priority;
|
|
|
|
n.data = (unsigned int) active_thread;
|
|
|
|
DEBUG("%s: Adding node to msg_queue:\n", active_thread->name);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
queue_priority_add(&(target->msg_queue), &n);
|
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
active_thread->wait_data = (void*) m;
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
int newstatus;
|
2010-10-28 11:22:57 +02:00
|
|
|
if (active_thread->status == STATUS_REPLY_BLOCKED) {
|
2010-09-22 15:10:42 +02:00
|
|
|
newstatus = STATUS_REPLY_BLOCKED;
|
|
|
|
} else {
|
|
|
|
newstatus = STATUS_SEND_BLOCKED;
|
|
|
|
}
|
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
sched_set_status((tcb*)active_thread, newstatus);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: back from send block.\n", active_thread->name);
|
2010-09-22 15:10:42 +02:00
|
|
|
} else {
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: direct msg copy.\n", active_thread->name);
|
2010-09-22 15:10:42 +02:00
|
|
|
/* copy msg to target */
|
|
|
|
msg* target_message = (msg*)target->wait_data;
|
|
|
|
*target_message = *m;
|
|
|
|
sched_set_status(target, STATUS_PENDING);
|
|
|
|
}
|
|
|
|
|
|
|
|
eINT();
|
2010-10-28 11:22:57 +02:00
|
|
|
thread_yield();
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int msg_send_int(msg* m, unsigned int target_pid) {
|
2010-10-28 11:22:57 +02:00
|
|
|
tcb *target = (tcb*)sched_threads[target_pid];
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
if (target->status == STATUS_RECEIVE_BLOCKED) {
|
|
|
|
DEBUG("msg_send_int: direct msg copy.\n");
|
|
|
|
|
2010-09-24 16:24:13 +02:00
|
|
|
m->sender_pid = target_pid;
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
/* copy msg to target */
|
|
|
|
msg* target_message = (msg*)target->wait_data;
|
|
|
|
*target_message = *m;
|
|
|
|
sched_set_status(target, STATUS_PENDING);
|
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
sched_context_switch_request = 1;
|
2010-09-22 15:10:42 +02:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
DEBUG("msg_send_int: receiver not waiting.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int msg_send_receive(msg *m, msg *reply, unsigned int target_pid) {
|
|
|
|
dINT();
|
2010-10-28 11:22:57 +02:00
|
|
|
tcb *me = (tcb*) sched_threads[thread_pid];
|
2010-09-22 15:10:42 +02:00
|
|
|
sched_set_status(me, STATUS_REPLY_BLOCKED);
|
|
|
|
me->wait_data = (void*) reply;
|
|
|
|
msg_send(m, target_pid, true);
|
|
|
|
|
|
|
|
/* msg_send blocks until reply received */
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int msg_reply(msg *m, msg *reply) {
|
2010-09-24 16:24:13 +02:00
|
|
|
int state = disableIRQ();
|
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
tcb *target = (tcb*)sched_threads[m->sender_pid];
|
2010-09-22 15:10:42 +02:00
|
|
|
if (target->status != STATUS_REPLY_BLOCKED) {
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: msg_reply(): target \"%s\" not waiting for reply.", active_thread->name, target->name);
|
2010-09-24 16:24:13 +02:00
|
|
|
restoreIRQ(state);
|
2010-09-22 15:10:42 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: msg_reply(): direct msg copy.\n", active_thread->name);
|
2010-09-22 15:10:42 +02:00
|
|
|
/* copy msg to target */
|
|
|
|
msg* target_message = (msg*)target->wait_data;
|
2010-10-28 10:12:45 +02:00
|
|
|
*target_message = *reply;
|
2010-09-22 15:10:42 +02:00
|
|
|
sched_set_status(target, STATUS_PENDING);
|
2010-09-24 16:24:13 +02:00
|
|
|
restoreIRQ(state);
|
2010-10-28 11:22:57 +02:00
|
|
|
thread_yield();
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-09-24 16:24:13 +02:00
|
|
|
int msg_reply_int(msg *m, msg *reply) {
|
2010-10-28 11:22:57 +02:00
|
|
|
tcb *target = (tcb*)sched_threads[m->sender_pid];
|
2010-09-24 16:24:13 +02:00
|
|
|
if (target->status != STATUS_REPLY_BLOCKED) {
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: msg_reply_int(): target \"%s\" not waiting for reply.", active_thread->name, target->name);
|
2010-09-24 16:24:13 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
msg* target_message = (msg*)target->wait_data;
|
|
|
|
*target_message = *m;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
int msg_receive(msg* m) {
|
|
|
|
dINT();
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: msg_receive.\n", active_thread->name);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2010-10-28 11:22:57 +02:00
|
|
|
tcb *me = (tcb*) sched_threads[thread_pid];
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
me->wait_data = (void*) m;
|
|
|
|
|
|
|
|
queue_node_t *n = queue_remove_head(&(me->msg_queue));
|
|
|
|
|
|
|
|
if (n == NULL) {
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: msg_receive blocked\n", active_thread->name);
|
2010-09-22 15:10:42 +02:00
|
|
|
sched_set_status(me, STATUS_RECEIVE_BLOCKED);
|
|
|
|
|
|
|
|
eINT();
|
2010-10-28 11:22:57 +02:00
|
|
|
thread_yield();
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
/* sender copied message */
|
|
|
|
return 1;
|
|
|
|
} else {
|
2010-10-28 11:22:57 +02:00
|
|
|
DEBUG("%s: msg_receive direct copy.\n", active_thread->name);
|
2010-09-22 15:10:42 +02:00
|
|
|
tcb *sender = (tcb*)n->data;
|
|
|
|
|
|
|
|
/* copy msg */
|
|
|
|
msg* sender_msg = (msg*)sender->wait_data;
|
|
|
|
*m = *sender_msg;
|
|
|
|
|
|
|
|
/* remove sender from queue */
|
|
|
|
sender->wait_data = NULL;
|
|
|
|
sched_set_status(sender, STATUS_PENDING);
|
|
|
|
|
|
|
|
eINT();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|