Forward received condition variable signals on cancel.
authorThomas Munro <tmunro@postgresql.org>
Sat, 13 Jul 2019 01:55:10 +0000 (13:55 +1200)
committerThomas Munro <tmunro@postgresql.org>
Sat, 13 Jul 2019 02:50:18 +0000 (14:50 +1200)
After a process decides not to wait for a condition variable, it can
still consume a signal before it reaches ConditionVariableCancelSleep().
In that case, pass the signal on to another waiter if possible, so that
a signal doesn't go missing when there is another process ready to
receive it.

Author: Thomas Munro
Reviewed-by: Shawn Debnath
Discussion: https://postgr.es/m/CA%2BhUKGLQ_RW%2BXs8znDn36e-%2Bmq2--zrPemBqTQ8eKT-VO1OF4Q%40mail.gmail.com

src/backend/storage/lmgr/condition_variable.c

index bc8607efe457b980c4d125e84dabd92eb978ca95..e08507f0cc59a81c9798f865fdc2b91345d8ce99 100644 (file)
@@ -245,6 +245,7 @@ void
 ConditionVariableCancelSleep(void)
 {
    ConditionVariable *cv = cv_sleep_target;
+   bool        signaled = false;
 
    if (cv == NULL)
        return;
@@ -252,8 +253,18 @@ ConditionVariableCancelSleep(void)
    SpinLockAcquire(&cv->mutex);
    if (proclist_contains(&cv->wakeup, MyProc->pgprocno, cvWaitLink))
        proclist_delete(&cv->wakeup, MyProc->pgprocno, cvWaitLink);
+   else
+       signaled = true;
    SpinLockRelease(&cv->mutex);
 
+   /*
+    * If we've received a signal, pass it on to another waiting process, if
+    * there is one.  Otherwise a call to ConditionVariableSignal() might get
+    * lost, despite there being another process ready to handle it.
+    */
+   if (signaled)
+       ConditionVariableSignal(cv);
+
    cv_sleep_target = NULL;
 }