PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pgsleep.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pgsleep.c
4  * Portable delay handling.
5  *
6  *
7  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
8  *
9  * src/port/pgsleep.c
10  *
11  *-------------------------------------------------------------------------
12  */
13 #include "c.h"
14 
15 #include <unistd.h>
16 #include <sys/time.h>
17 #ifdef HAVE_SYS_SELECT_H
18 #include <sys/select.h>
19 #endif
20 
21 /*
22  * In a Windows backend, we don't use this implementation, but rather
23  * the signal-aware version in src/backend/port/win32/signal.c.
24  */
25 #if defined(FRONTEND) || !defined(WIN32)
26 
27 /*
28  * pg_usleep --- delay the specified number of microseconds.
29  *
30  * NOTE: although the delay is specified in microseconds, the effective
31  * resolution is only 1/HZ, or 10 milliseconds, on most Unixen. Expect
32  * the requested delay to be rounded up to the next resolution boundary.
33  *
34  * On machines where "long" is 32 bits, the maximum delay is ~2000 seconds.
35  *
36  * CAUTION: the behavior when a signal arrives during the sleep is platform
37  * dependent. On most Unix-ish platforms, a signal does not terminate the
38  * sleep; but on some, it will (the Windows implementation also allows signals
39  * to terminate pg_usleep). And there are platforms where not only does a
40  * signal not terminate the sleep, but it actually resets the timeout counter
41  * so that the sleep effectively starts over! It is therefore rather hazardous
42  * to use this for long sleeps; a continuing stream of signal events could
43  * prevent the sleep from ever terminating. Better practice for long sleeps
44  * is to use WaitLatch() with a timeout.
45  */
46 void
47 pg_usleep(long microsec)
48 {
49  if (microsec > 0)
50  {
51 #ifndef WIN32
52  struct timeval delay;
53 
54  delay.tv_sec = microsec / 1000000L;
55  delay.tv_usec = microsec % 1000000L;
56  (void) select(0, NULL, NULL, NULL, &delay);
57 #else
58  SleepEx((microsec < 500 ? 1 : (microsec + 500) / 1000), FALSE);
59 #endif
60  }
61 }
62 
63 #endif /* defined(FRONTEND) || !defined(WIN32) */
#define select(n, r, w, e, timeout)
Definition: win32.h:384
void pg_usleep(long microsec)
Definition: pgsleep.c:47
#define FALSE
Definition: c.h:221
#define NULL
Definition: c.h:229