Header And Logo

PostgreSQL
| The world's most advanced open source database.

pgsleep.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * pgsleep.c
00004  *     Portable delay handling.
00005  *
00006  *
00007  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00008  *
00009  * src/port/pgsleep.c
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 #include "c.h"
00014 
00015 #include <unistd.h>
00016 #include <sys/time.h>
00017 
00018 /*
00019  * In a Windows backend, we don't use this implementation, but rather
00020  * the signal-aware version in src/backend/port/win32/signal.c.
00021  */
00022 #if defined(FRONTEND) || !defined(WIN32)
00023 
00024 /*
00025  * pg_usleep --- delay the specified number of microseconds.
00026  *
00027  * NOTE: although the delay is specified in microseconds, the effective
00028  * resolution is only 1/HZ, or 10 milliseconds, on most Unixen.  Expect
00029  * the requested delay to be rounded up to the next resolution boundary.
00030  *
00031  * On machines where "long" is 32 bits, the maximum delay is ~2000 seconds.
00032  */
00033 void
00034 pg_usleep(long microsec)
00035 {
00036     if (microsec > 0)
00037     {
00038 #ifndef WIN32
00039         struct timeval delay;
00040 
00041         delay.tv_sec = microsec / 1000000L;
00042         delay.tv_usec = microsec % 1000000L;
00043         (void) select(0, NULL, NULL, NULL, &delay);
00044 #else
00045         SleepEx((microsec < 500 ? 1 : (microsec + 500) / 1000), FALSE);
00046 #endif
00047     }
00048 }
00049 
00050 #endif   /* defined(FRONTEND) || !defined(WIN32) */