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