Skip to content

Commit

Permalink
core/msg: use disable/restoreIRQ
Browse files Browse the repository at this point in the history
* needed to change internal `msg_send` to allow external disabling of interrupts
  • Loading branch information
LudwigKnuepfer committed Oct 28, 2014
1 parent 6b4ac47 commit cc84edf
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions core/msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
#include "thread.h"

static int _msg_receive(msg_t *m, int block);
static int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block);
int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block);
static int __msg_send(msg_t *m, kernel_pid_t target_pid, bool block, unsigned *statep);


static int queue_msg(tcb_t *target, msg_t *m)
Expand All @@ -60,7 +61,12 @@ int msg_try_send(msg_t *m, kernel_pid_t target_pid) {
return _msg_send(m, target_pid, false);
}

static int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block)
int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block)
{
return __msg_send(m, target_pid, block, NULL);
}

static int __msg_send(msg_t *m, kernel_pid_t target_pid, bool block, unsigned *statep)
{
if (inISR()) {
return msg_send_int(m, target_pid);
Expand All @@ -76,15 +82,21 @@ static int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block)
}
#endif /* DEVELHELP */

dINT();
unsigned state;
if (statep == NULL) {
state = disableIRQ();
}
else {
state = *statep;
}

tcb_t *target = (tcb_t*) sched_threads[target_pid];

m->sender_pid = sched_active_pid;

if (target == NULL) {
DEBUG("msg_send(): target thread does not exist\n");
eINT();
restoreIRQ(state);
return -1;
}

Expand All @@ -94,7 +106,7 @@ static int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block)
DEBUG("msg_send() %s:%i: Target %" PRIkernel_pid " is not RECEIVE_BLOCKED.\n", __FILE__, __LINE__, target_pid);
if (target->msg_array && queue_msg(target, m)) {
DEBUG("msg_send() %s:%i: Target %" PRIkernel_pid " has a msg_queue. Queueing message.\n", __FILE__, __LINE__, target_pid);
eINT();
restoreIRQ(state);
if (sched_active_thread->status == STATUS_REPLY_BLOCKED) {
thread_yield_higher();
}
Expand All @@ -103,7 +115,7 @@ static int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block)

if (!block) {
DEBUG("msg_send: %s: Receiver not waiting, block=%u\n", sched_active_thread->name, block);
eINT();
restoreIRQ(state);
return 0;
}

Expand Down Expand Up @@ -131,7 +143,7 @@ static int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block)

DEBUG("msg_send: %s: Back from send block.\n", sched_active_thread->name);

eINT();
restoreIRQ(state);
thread_yield_higher();
}
else {
Expand All @@ -142,7 +154,7 @@ static int _msg_send(msg_t *m, kernel_pid_t target_pid, bool block)
sched_set_status(target, STATUS_PENDING);

uint16_t target_prio = target->priority;
eINT();
restoreIRQ(state);
sched_switch(target_prio);
}

Expand Down Expand Up @@ -196,14 +208,13 @@ int msg_send_int(msg_t *m, kernel_pid_t target_pid)

int msg_send_receive(msg_t *m, msg_t *reply, kernel_pid_t target_pid)
{
dINT();
unsigned state = disableIRQ();
tcb_t *me = (tcb_t*) sched_threads[sched_active_pid];
sched_set_status(me, STATUS_REPLY_BLOCKED);
me->wait_data = (void*) reply;

/* msg_send blocks until reply received */

return msg_send(m, target_pid);
return __msg_send(m, target_pid, true, &state);
}

int msg_reply(msg_t *m, msg_t *reply)
Expand Down Expand Up @@ -263,7 +274,7 @@ int msg_receive(msg_t *m)

static int _msg_receive(msg_t *m, int block)
{
dINT();
unsigned state = disableIRQ();
DEBUG("_msg_receive: %s: _msg_receive.\n", sched_active_thread->name);

tcb_t *me = (tcb_t*) sched_threads[sched_active_pid];
Expand All @@ -276,7 +287,7 @@ static int _msg_receive(msg_t *m, int block)

/* no message, fail */
if ((!block) && (queue_index == -1)) {
eINT();
restoreIRQ(state);
return -1;
}

Expand All @@ -297,13 +308,13 @@ static int _msg_receive(msg_t *m, int block)
DEBUG("_msg_receive(): %s: No msg in queue. Going blocked.\n", sched_active_thread->name);
sched_set_status(me, STATUS_RECEIVE_BLOCKED);

eINT();
restoreIRQ(state);
thread_yield_higher();

/* sender copied message */
}
else {
eINT();
restoreIRQ(state);
}

return 1;
Expand All @@ -329,7 +340,7 @@ static int _msg_receive(msg_t *m, int block)
sched_set_status(sender, STATUS_PENDING);
}

eINT();
restoreIRQ(state);
return 1;
}

Expand Down

0 comments on commit cc84edf

Please sign in to comment.