diff --git a/core/include/msg.h b/core/include/msg.h index c8770b7a8d..78db899b43 100644 --- a/core/include/msg.h +++ b/core/include/msg.h @@ -90,6 +90,16 @@ int msg_send_int(msg_t *m, unsigned int target_pid); */ int msg_receive(msg_t *m); +/** + * @brief Try to receive a message. + * + * This function does not block if no message can be received. + * @param m pointer to preallocated msg + * + * @return 1 if a message was received, -1 otherwise. + */ +int msg_try_receive(msg_t *m); + /** * @brief Send a message, block until reply received. * diff --git a/core/msg.c b/core/msg.c index 9e621027f9..52e6e1dc30 100644 --- a/core/msg.c +++ b/core/msg.c @@ -28,6 +28,9 @@ //#define ENABLE_DEBUG #include "debug.h" +static int _msg_receive(msg_t *m, int block); + + static int queue_msg(tcb_t *target, msg_t *m) { int n = cib_put(&(target->msg_queue)); @@ -184,7 +187,17 @@ int msg_reply_int(msg_t *m, msg_t *reply) return 1; } +int msg_try_receive(msg_t *m) +{ + return _msg_receive(m, 0); +} + int msg_receive(msg_t *m) +{ + return _msg_receive(m, 1); +} + +static int _msg_receive(msg_t *m, int block) { dINT(); DEBUG("%s: msg_receive.\n", active_thread->name); @@ -197,6 +210,11 @@ int msg_receive(msg_t *m) n = cib_get(&(me->msg_queue)); } + /* no message, fail */ + if ((!block) && (n == -1)) { + return -1; + } + if (n >= 0) { DEBUG("%s: msg_receive(): We've got a queued message.\n", active_thread->name); *m = me->msg_array[n];