Skip to content
Snippets Groups Projects
Commit 0ddd6ec1 authored by Oleg Hahm's avatar Oleg Hahm
Browse files

xtimer: implement xtimer_msg_receive_timeout

parent 567d0efe
Branches
No related tags found
No related merge requests found
...@@ -159,22 +159,44 @@ void xtimer_now_timex(timex_t *out) ...@@ -159,22 +159,44 @@ void xtimer_now_timex(timex_t *out)
out->microseconds = now - (out->seconds * SEC_IN_USEC); out->microseconds = now - (out->seconds * SEC_IN_USEC);
} }
int xtimer_msg_receive_timeout64(msg_t *m, uint64_t timeout) { /* Prepares the message to trigger the timeout.
msg_t tmsg; * Additionally, the xtimer_t struct gets initialized.
tmsg.type = MSG_XTIMER; */
tmsg.content.ptr = (char *) &tmsg; static void _setup_timer_msg(msg_t *m, xtimer_t *t)
{
m->type = MSG_XTIMER;
m->content.ptr = (char *) m;
xtimer_t t; t->target = t->long_target = 0;
t.target = t.long_target = 0; }
xtimer_set_msg64(&t, timeout, &tmsg, sched_active_pid);
/* Waits for incoming message or timeout. */
static int _msg_wait(msg_t *m, msg_t *tmsg, xtimer_t *t)
{
msg_receive(m); msg_receive(m);
if (m->type == MSG_XTIMER && m->content.ptr == (char *) &tmsg) { if (m->type == MSG_XTIMER && m->content.ptr == (char *) tmsg) {
/* we hit the timeout */ /* we hit the timeout */
return -1; return -1;
} }
else { else {
xtimer_remove(&t); xtimer_remove(t);
return 1; return 1;
} }
} }
int xtimer_msg_receive_timeout64(msg_t *m, uint64_t timeout) {
msg_t tmsg;
xtimer_t t;
_setup_timer_msg(&tmsg, &t);
xtimer_set_msg64(&t, timeout, &tmsg, sched_active_pid);
return _msg_wait(m, &tmsg, &t);
}
int xtimer_msg_receive_timeout(msg_t *msg, uint32_t us)
{
msg_t tmsg;
xtimer_t t;
_setup_timer_msg(&tmsg, &t);
xtimer_set_msg(&t, us, &tmsg, sched_active_pid);
return _msg_wait(msg, &tmsg, &t);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment