|
12 | 12 | */
|
13 | 13 | #include "c.h"
|
14 | 14 |
|
15 |
| -#include <unistd.h> |
16 |
| -#include <sys/select.h> |
17 |
| -#include <sys/time.h> |
| 15 | +#include <time.h> |
18 | 16 |
|
19 | 17 | /*
|
20 | 18 | * In a Windows backend, we don't use this implementation, but rather
|
|
32 | 30 | *
|
33 | 31 | * On machines where "long" is 32 bits, the maximum delay is ~2000 seconds.
|
34 | 32 | *
|
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. |
44 | 39 | */
|
45 | 40 | void
|
46 | 41 | pg_usleep(long microsec)
|
47 | 42 | {
|
48 | 43 | if (microsec > 0)
|
49 | 44 | {
|
50 | 45 | #ifndef WIN32
|
51 |
| - struct timeval delay; |
| 46 | + struct timespec delay; |
52 | 47 |
|
53 | 48 | 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); |
56 | 51 | #else
|
57 | 52 | SleepEx((microsec < 500 ? 1 : (microsec + 500) / 1000), FALSE);
|
58 | 53 | #endif
|
|
0 commit comments