0

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?

Ann
  • 11
  • 2
  • Could you explain the definition of "safety property" you are using? – Nate Eldredge Dec 04 '22 at 04:23
  • Normally a spurious wakeup is not a safety problem, if the application programmer handles it correctly. There must be some way to detect that whatever you were waiting for didn't happen, you don't hold the relevant mutex, etc. In which case you go back to sleep, or perhaps do some other unrelated work. – Nate Eldredge Dec 04 '22 at 04:25
  • safety property according to my understanding is : at most one thread can exist at the critical section at a given point of time. – Ann Dec 04 '22 at 09:56
  • Okay, so then what I said applies: the program just has to be coded to check whether the wakeup was "real" or "spurious", and in the second case, don't enter the critical section. – Nate Eldredge Dec 04 '22 at 16:37

1 Answers1

0

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.");
        }
    }
}
Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82