Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eYKWd-0005J3-C5 for pgsql-hackers@arkaria.postgresql.org; Sun, 07 Jan 2018 23:38:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eYKWc-0001jv-Su for pgsql-hackers@arkaria.postgresql.org; Sun, 07 Jan 2018 23:38:46 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eYKWc-0001jl-JG for pgsql-hackers@lists.postgresql.org; Sun, 07 Jan 2018 23:38:46 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eYKWW-00086e-4t for pgsql-hackers@postgresql.org; Sun, 07 Jan 2018 23:38:45 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w07NcaCd002438; Sun, 7 Jan 2018 18:38:36 -0500 From: Tom Lane To: Robert Haas cc: Thomas Munro , Andres Freund , Pg Hackers Subject: Re: Condition variable live lock In-reply-to: <28942.1515275038@sss.pgh.pa.us> References: <20171229193843.tsbob2xsjfmxkq2g@alap3.anarazel.de> <20180104205447.mdaub47aunp3h3mq@alap3.anarazel.de> <17054.1515126429@sss.pgh.pa.us> <21761.1515132657@sss.pgh.pa.us> <23234.1515134575@sss.pgh.pa.us> <15204.1515166090@sss.pgh.pa.us> <14191.1515173593@sss.pgh.pa.us> <9342.1515181117@sss.pgh.pa.us> <10713.1515182599@sss.pgh.pa.us> <26266.1515272424@sss.pgh.pa.us> <28942.1515275038@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Sat, 06 Jan 2018 16:43:58 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <2412.1515368280.0@sss.pgh.pa.us> Date: Sun, 07 Jan 2018 18:38:36 -0500 Message-ID: <2437.1515368316@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2412.1515368280.1@sss.pgh.pa.us> I wrote: > Actually ... perhaps a better design would be to have > ConditionVariable[PrepareTo]Sleep auto-cancel any prepared sleep for > a different condition variable, analogously to what we just did in > ConditionVariableBroadcast, on the same theory that whenever control > returns to the other CV wait loop it can re-establish the relevant > state easily enough. I have to think that if the use of CVs grows > much, the existing restriction is going to become untenable anyway, > so why not just get rid of it? Concretely, as per attached. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="allow-interleaved-cv-sleeps.patch"; charset="us-ascii" Content-ID: <2412.1515368280.2@sss.pgh.pa.us> Content-Description: allow-interleaved-cv-sleeps.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/storage/lmgr/condition_variable.c b/src/backend/s= torage/lmgr/condition_variable.c index 0b9d676..b01864c 100644 --- a/src/backend/storage/lmgr/condition_variable.c +++ b/src/backend/storage/lmgr/condition_variable.c @@ -50,10 +50,6 @@ ConditionVariableInit(ConditionVariable *cv) * However, if the first test of the exit condition is likely to succeed, * it's more efficient to omit the ConditionVariablePrepareToSleep call. * See comments in ConditionVariableSleep for more detail. - * - * Only one condition variable can be used at a time, ie, - * ConditionVariableCancelSleep must be called before any attempt is made - * to sleep on a different condition variable. */ void ConditionVariablePrepareToSleep(ConditionVariable *cv) @@ -76,10 +72,14 @@ ConditionVariablePrepareToSleep(ConditionVariable *cv) } = /* - * It's not legal to prepare a sleep until the previous sleep has been - * completed or canceled. + * If some other sleep is already prepared, cancel it; this is necessary + * because we have just one static variable tracking the prepared sleep. + * It's okay to do this because whenever control does return to the othe= r + * test-and-sleep loop, its ConditionVariableSleep call will just + * re-establish that sleep as the prepared one. */ - Assert(cv_sleep_target =3D=3D NULL); + if (cv_sleep_target !=3D NULL) + ConditionVariableCancelSleep(); = /* Record the condition variable on which we will sleep. */ cv_sleep_target =3D cv; @@ -128,16 +128,16 @@ ConditionVariableSleep(ConditionVariable *cv, uint32= wait_event_info) * recommended because it avoids manipulations of the wait list, or not * met initially, in which case preparing first is better because it * avoids one extra test of the exit condition. + * + * If we are currently prepared to sleep on some other CV, we just cance= l + * that and prepare this one; see ConditionVariablePrepareToSleep. */ - if (cv_sleep_target =3D=3D NULL) + if (cv_sleep_target !=3D cv) { ConditionVariablePrepareToSleep(cv); return; } = - /* Any earlier condition variable sleep must have been canceled. */ - Assert(cv_sleep_target =3D=3D cv); - do { CHECK_FOR_INTERRUPTS(); diff --git a/src/include/storage/condition_variable.h b/src/include/storag= e/condition_variable.h index 7dac477..32e645c 100644 --- a/src/include/storage/condition_variable.h +++ b/src/include/storage/condition_variable.h @@ -40,9 +40,7 @@ extern void ConditionVariableInit(ConditionVariable *cv)= ; * ConditionVariableSleep. Spurious wakeups are possible, but should be * infrequent. After exiting the loop, ConditionVariableCancelSleep must * be called to ensure that the process is no longer in the wait list for - * the condition variable. Only one condition variable can be used at a - * time, ie, ConditionVariableCancelSleep must be called before any attem= pt - * is made to sleep on a different condition variable. + * the condition variable. */ extern void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_eve= nt_info); extern void ConditionVariableCancelSleep(void); ------- =_aaaaaaaaaa0--