]> granicus.if.org Git - postgresql/blob - src/port/pgsleep.c
6cd43cade67c1bea7a0c7fbfd6a044e6d4299600
[postgresql] / src / port / pgsleep.c
1 /*-------------------------------------------------------------------------
2  *
3  * pgsleep.c
4  *         Portable delay handling.
5  *
6  *
7  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8  *
9  * $PostgreSQL: pgsql/src/port/pgsleep.c,v 1.11 2008/01/01 19:46:00 momjian Exp $
10  *
11  *-------------------------------------------------------------------------
12  */
13 #include "c.h"
14
15 #include <unistd.h>
16 #include <sys/time.h>
17
18 /*
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.
21  */
22 #if defined(FRONTEND) || !defined(WIN32)
23
24 /*
25  * pg_usleep --- delay the specified number of microseconds.
26  *
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.
30  *
31  * On machines where "long" is 32 bits, the maximum delay is ~2000 seconds.
32  */
33 void
34 pg_usleep(long microsec)
35 {
36         if (microsec > 0)
37         {
38 #ifndef WIN32
39                 struct timeval delay;
40
41                 delay.tv_sec = microsec / 1000000L;
42                 delay.tv_usec = microsec % 1000000L;
43                 (void) select(0, NULL, NULL, NULL, &delay);
44 #else
45                 SleepEx((microsec < 500 ? 1 : (microsec + 500) / 1000), FALSE);
46 #endif
47         }
48 }
49
50 #endif   /* defined(FRONTEND) || !defined(WIN32) */