1 /*-------------------------------------------------------------------------
4 * Portable delay handling.
7 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
11 *-------------------------------------------------------------------------
16 #include <sys/select.h>
20 * In a Windows backend, we don't use this implementation, but rather
21 * the signal-aware version in src/backend/port/win32/signal.c.
23 #if defined(FRONTEND) || !defined(WIN32)
26 * pg_usleep --- delay the specified number of microseconds.
28 * NOTE: although the delay is specified in microseconds, the effective
29 * resolution is only 1/HZ, or 10 milliseconds, on most Unixen. Expect
30 * the requested delay to be rounded up to the next resolution boundary.
32 * On machines where "long" is 32 bits, the maximum delay is ~2000 seconds.
34 * CAUTION: the behavior when a signal arrives during the sleep is platform
35 * dependent. On most Unix-ish platforms, a signal does not terminate the
36 * sleep; but on some, it will (the Windows implementation also allows signals
37 * to terminate pg_usleep). And there are platforms where not only does a
38 * signal not terminate the sleep, but it actually resets the timeout counter
39 * so that the sleep effectively starts over! It is therefore rather hazardous
40 * to use this for long sleeps; a continuing stream of signal events could
41 * prevent the sleep from ever terminating. Better practice for long sleeps
42 * is to use WaitLatch() with a timeout.
45 pg_usleep(long microsec
)
52 delay
.tv_sec
= microsec
/ 1000000L;
53 delay
.tv_usec
= microsec
% 1000000L;
54 (void) select(0, NULL
, NULL
, NULL
, &delay
);
56 SleepEx((microsec
< 500 ? 1 : (microsec
+ 500) / 1000), FALSE
);
61 #endif /* defined(FRONTEND) || !defined(WIN32) */