diff --git a/core/include/msg.h b/core/include/msg.h index e1c390afb3..da9fe81b77 100644 --- a/core/include/msg.h +++ b/core/include/msg.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Freie Universität Berlin + * Copyright (C) 2014 Freie Universität Berlin * * This file subject to the terms and conditions of the GNU Lesser General * Public License. See the file LICENSE in the top level directory for more @@ -26,6 +26,7 @@ * * @author Freie Universität Berlin, Computer Systems & Telematics * @author Kaspar Schleiser + * @author Kévin Roussel */ #ifndef __MSG_H @@ -76,6 +77,21 @@ typedef struct msg { int msg_send(msg_t *m, unsigned int target_pid, bool block); +/** + * @brief Send a message to the current thread. + * @details Will work only if the thread has a message queue. + * + * Will be automatically chosen instead of @c msg_send + * if @c target_pid == @c thread_pid. + * This function never blocks. + * + * @param m pointer to message structure + * + * @return 1 if sending was successful + * @return 0 if the thread's message queue is full (or inexistent) + */ +int msg_send_to_self(msg_t *m); + /** * @brief Send message from interrupt. * diff --git a/core/msg.c b/core/msg.c index f40d1e7198..c22fd66188 100644 --- a/core/msg.c +++ b/core/msg.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Freie Universität Berlin + * Copyright (C) 2014 Freie Universität Berlin * * This file is subject to the terms and conditions of the GNU Lesser General * Public License. See the file LICENSE in the top level directory for more @@ -16,6 +16,7 @@ * @author Freie Universität Berlin, Computer Systems & Telematics, FeuerWhere project * @author Kaspar Schleiser * @author Oliver Hahm + * @author Kévin Roussel * * @} */ @@ -62,7 +63,7 @@ int msg_send(msg_t *m, unsigned int target_pid, bool block) m->sender_pid = thread_pid; if (m->sender_pid == target_pid) { - return -1; + return msg_send_to_self(m); } if (target == NULL) { @@ -128,6 +129,16 @@ int msg_send(msg_t *m, unsigned int target_pid, bool block) return 1; } +int msg_send_to_self(msg_t *m) +{ + unsigned int state = disableIRQ(); + + int res = queue_msg(active_thread, m); + + restoreIRQ(state); + return res; +} + int msg_send_int(msg_t *m, unsigned int target_pid) { tcb_t *target = (tcb_t *) sched_threads[target_pid];