1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 05:52:44 +01:00

core/msg: fix, optimize and improve

fixes:
fix race conditions by reordering dINTs
prevent null pointer dereference by adding forgotten target check
add forgotten eINTs
replace printf with DEBUG
fix debug messages

optimizations:
optimize pid access
reorder msg_send switches

improvements:
add debug statements
add missing return value to msg_send_to_self documentation
This commit is contained in:
Ludwig Ortmann 2014-06-05 17:25:53 +02:00
parent 5b70271b3f
commit 5c9a975afb
2 changed files with 20 additions and 6 deletions

View File

@ -105,6 +105,7 @@ int msg_send_to_self(msg_t *m);
*
* @return 1, if sending was successful
* @return 0, if receiver is not waiting and ``block == 0``
* @return -1, on error (invalid PID)
*/
int msg_send_int(msg_t *m, unsigned int target_pid);

View File

@ -58,20 +58,22 @@ int msg_send(msg_t *m, unsigned int target_pid, bool block)
return msg_send_int(m, target_pid);
}
if (sched_active_pid == target_pid) {
return msg_send_to_self(m);
}
dINT();
tcb_t *target = (tcb_t*) sched_threads[target_pid];
m->sender_pid = sched_active_pid;
if (m->sender_pid == target_pid) {
return msg_send_to_self(m);
}
if (target == NULL) {
DEBUG("msg_send(): target thread does not exist\n");
eINT();
return -1;
}
dINT();
DEBUG("msg_send() %s:%i: Sending from %i to %i. block=%i src->state=%i target->state=%i\n", __FILE__, __LINE__, sched_active_pid, target_pid, block, sched_active_thread->status, target->status);
if (target->status != STATUS_RECEIVE_BLOCKED) {
@ -143,6 +145,11 @@ int msg_send_int(msg_t *m, unsigned int target_pid)
{
tcb_t *target = (tcb_t *) sched_threads[target_pid];
if (target == NULL) {
DEBUG("msg_send_int(): target thread does not exist\n");
return -1;
}
if (target->status == STATUS_RECEIVE_BLOCKED) {
DEBUG("msg_send_int: Direct msg copy from %i to %i.\n", thread_getpid(), target_pid);
@ -243,6 +250,7 @@ static int _msg_receive(msg_t *m, int block)
/* no message, fail */
if ((!block) && (queue_index == -1)) {
eINT();
return -1;
}
@ -268,6 +276,9 @@ static int _msg_receive(msg_t *m, int block)
/* sender copied message */
}
else {
eINT();
}
return 1;
}
@ -295,6 +306,8 @@ static int _msg_receive(msg_t *m, int block)
eINT();
return 1;
}
DEBUG("This should have never been reached!\n");
}
int msg_init_queue(msg_t *array, int num)