Spurious wake-up and safety property

30 views Asked by At

In mutual exclusion, we should satisfy the safety and progress properties. However, if we have a spurious wake-ups, is the safety property still satisfied?

1

There are 1 answers

0
Nate Eldredge On

If your system has lock semantics that allow for spurious wakeups, then the programmer has to handle them accordingly. At each wakeup, check whether it was real or spurious. This information could be returned by the wait function, or the program could just check whether the desired event has occurred (e.g. do we hold the mutex we were waiting on). If yes, it's safe to enter the critical section. If no, then go back to sleep, or maybe do some other work.

So pseudo-code to protect a critical section could look like:

Mutex m;

void do_critical_stuff() {
    while (true) {
        m.wait();
        if (m.held()) {
            critical_code();
            m.unlock();
            return;
        } else {
            // No critical code allowed here, but you could do something else.
            print("You have waked me too soon, I must slumber again.");
        }
    }
}