Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xtimer_msg_receive_timeout: only send timeout msg if thread still waits for it #13500

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MichelRottleuthner
Copy link
Contributor

@MichelRottleuthner MichelRottleuthner commented Feb 27, 2020

Contribution description

This PR fixes #13345 without requiring core_thread_flags.
The idea came from @JulianHolzwarth #13345 (comment)

Testing procedure

In master the following snipped crashes, with this PR it should work.
See #13345 for further details.

#include <stdio.h>
#include <string.h>

#include "xtimer.h"
#include "msg.h"
#include "thread.h"

#define DELAY            (100UL * US_PER_MS)
#define CORRECT_MSG_TYPE (42)
xtimer_t timer;

static void timer_event(void *arg) {
    kernel_pid_t *pid = (kernel_pid_t*)arg;
    msg_t msg = {
        .content.value = 1,
        .type = CORRECT_MSG_TYPE
    };
    puts("Interrupt");
    msg_send_int(&msg, *pid);
    puts("msg send");

    xtimer_set(&timer, DELAY-20);
}

int main(void)
{
    static msg_t rcv_queue[2];
    static msg_t recv;

    printf("You are running RIOT on a(n) %s board.\n", RIOT_BOARD);
    printf("This board features a(n) %s MCU.\n", RIOT_MCU);

    msg_init_queue(rcv_queue, 2);
    timer.callback = timer_event;
    kernel_pid_t pid = thread_getpid();
    timer.arg = (void*)&pid;

    xtimer_set(&timer, DELAY-20);

    while (1) {
        puts("wait");
    	xtimer_msg_receive_timeout(&recv, DELAY);
        puts("received");

        if (recv.type != CORRECT_MSG_TYPE) {
            printf("ERROR received wrong message: %d != %d\n", recv.type,
                   CORRECT_MSG_TYPE);
        }

    	xtimer_sleep(1);
    }
    return 0;
}

Issues/PRs references

Alternative to #13429.

@MichelRottleuthner MichelRottleuthner added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Area: timers Area: timer subsystems labels Feb 27, 2020
xtimer_t t = { .callback = _callback_timeout_msg,
.arg = &tmsg };

_xtimer_set64(&t, timeout_ticks, timeout_ticks >> 32);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now racey.

If the thread gets scheduled away after this set(), the timer triggers, then the thread gets scheduled again, it'll sit in msg_receive() forever.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Ok how about something like the idea I sketched in #13429 comment then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: timers Area: timer subsystems CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sys/xtimer: segmentation fault: in function xtimer_msg_received_timeout
3 participants