vacuumlazy.c: Tweak local variable name.
[pgsql.git] / src / port / pgsleep.c
blob8a709cd01dff095faeeee1bcd47afa340ef62fe0
1 /*-------------------------------------------------------------------------
3 * pgsleep.c
4 * Portable delay handling.
7 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
9 * src/port/pgsleep.c
11 *-------------------------------------------------------------------------
13 #include "c.h"
15 #include <unistd.h>
16 #include <sys/select.h>
17 #include <sys/time.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.
44 void
45 pg_usleep(long microsec)
47 if (microsec > 0)
49 #ifndef WIN32
50 struct timeval delay;
52 delay.tv_sec = microsec / 1000000L;
53 delay.tv_usec = microsec % 1000000L;
54 (void) select(0, NULL, NULL, NULL, &delay);
55 #else
56 SleepEx((microsec < 500 ? 1 : (microsec + 500) / 1000), FALSE);
57 #endif
61 #endif /* defined(FRONTEND) || !defined(WIN32) */