Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h3Utb-0004DG-CQ for pgsql-hackers@arkaria.postgresql.org; Tue, 12 Mar 2019 00:03:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h3UtZ-0004lF-Vv for pgsql-hackers@arkaria.postgresql.org; Tue, 12 Mar 2019 00:03:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h3UtZ-0004l7-Kg for pgsql-hackers@lists.postgresql.org; Tue, 12 Mar 2019 00:03:49 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h3UtS-0005wv-V4 for pgsql-hackers@lists.postgresql.org; Tue, 12 Mar 2019 00:03:48 +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 x2C03eHG004903 for ; Mon, 11 Mar 2019 20:03:40 -0400 From: Tom Lane To: pgsql-hackers@lists.postgresql.org Subject: Use nanosleep(2) in pg_usleep, if available? MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4901.1552349020.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 11 Mar 2019 20:03:40 -0400 Message-ID: <4902.1552349020@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk In the thread about vacuum_cost_delay vs vacuum_cost_limit, I wondered whether nanosleep(2) would provide any better timing resolution than select(2). Some experimentation suggests that it doesn't, but nonetheless I see a good reason why we should consider making pg_usleep use nanosleep() if possible: it's got much better-defined semantics for what happens if a signal arrives. The comments for pg_usleep lay out the problems with relying on select(): * CAUTION: the behavior when a signal arrives during the sleep is platfor= m * dependent. On most Unix-ish platforms, a signal does not terminate the * sleep; but on some, it will (the Windows implementation also allows sig= nals * to terminate pg_usleep). And there are platforms where not only does a * signal not terminate the sleep, but it actually resets the timeout coun= ter * so that the sleep effectively starts over! It is therefore rather haza= rdous * to use this for long sleeps; a continuing stream of signal events could * prevent the sleep from ever terminating. Better practice for long slee= ps * is to use WaitLatch() with a timeout. While the WaitLatch alternative avoids the problem, I doubt we're ever going to remove pg_usleep entirely, so it'd be good if it had fewer sharp edges. nanosleep() has the same behavior as Windows, ie, the sleep is guaranteed to be terminated by a signal. So if we used nanosleep() where available we'd have that behavior on just about every interesting platform. nanosleep() does exist pretty far back: it's in SUSv2, though that version of the standard allows it to fail with ENOSYS. Not sure if we'd need to teach configure to check for that possibility. Thoughts? regards, tom lane