Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a948e49

Browse files
committed
Use nanosleep() to implement pg_usleep().
The previous coding based on select() had commentary about historical portability concerns. Use POSIX nanosleep() instead. This has independently been suggested a couple of times before, but never managed to stick. Since recent and proposed work removes other uses of select(), and associated code and comments relating to its non-portable interaction with signals, it seems like a good time to tidy up this case, too. Also modernize the explanation of why WaitLatch() is a better way to wait. Reviewed-by: Nathan Bossart <nathandbossart@gmail.com> Suggested-by: Paul Guo <paulguo@gmail.com> Suggested-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/CAAKRu_b-q0hXCBUCAATh0Z4Zi6UkiC0k2DFgoD3nC-r3SkR3tg%40mail.gmail.com Discussion: https://postgr.es/m/CABQrizfxpBLZT5mZeE0js5oCh1tqEWvcGF3vMRCv5P-RwUY5dQ@mail.gmail.com Discussion: https://postgr.es/m/4902.1552349020@sss.pgh.pa.us
1 parent e4da2a4 commit a948e49

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/port/pgsleep.c

+10-15
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
*/
1313
#include "c.h"
1414

15-
#include <unistd.h>
16-
#include <sys/select.h>
17-
#include <sys/time.h>
15+
#include <time.h>
1816

1917
/*
2018
* In a Windows backend, we don't use this implementation, but rather
@@ -32,27 +30,24 @@
3230
*
3331
* On machines where "long" is 32 bits, the maximum delay is ~2000 seconds.
3432
*
35-
* CAUTION: the behavior when a signal arrives during the sleep is platform
36-
* dependent. On most Unix-ish platforms, a signal does not terminate the
37-
* sleep; but on some, it will (the Windows implementation also allows signals
38-
* to terminate pg_usleep). And there are platforms where not only does a
39-
* signal not terminate the sleep, but it actually resets the timeout counter
40-
* so that the sleep effectively starts over! It is therefore rather hazardous
41-
* to use this for long sleeps; a continuing stream of signal events could
42-
* prevent the sleep from ever terminating. Better practice for long sleeps
43-
* is to use WaitLatch() with a timeout.
33+
* CAUTION: It's not a good idea to use long sleeps in the backend. They will
34+
* silently return early if a signal is caught, but that doesn't include
35+
* latches being set on most OSes, and even signal handlers that set MyLatch
36+
* might happen to run before the sleep begins, allowing the full delay.
37+
* Better practice is to use WaitLatch() with a timeout, so that backends
38+
* respond to latches and signals promptly.
4439
*/
4540
void
4641
pg_usleep(long microsec)
4742
{
4843
if (microsec > 0)
4944
{
5045
#ifndef WIN32
51-
struct timeval delay;
46+
struct timespec delay;
5247

5348
delay.tv_sec = microsec / 1000000L;
54-
delay.tv_usec = microsec % 1000000L;
55-
(void) select(0, NULL, NULL, NULL, &delay);
49+
delay.tv_nsec = (microsec % 1000000L) * 1000;
50+
(void) nanosleep(&delay, NULL);
5651
#else
5752
SleepEx((microsec < 500 ? 1 : (microsec + 500) / 1000), FALSE);
5853
#endif

0 commit comments

Comments
 (0)