[IOMultiplexerV2] dev snapshot
[NextIRCd.git] / src / compat / utime.c
1 #ifndef HAVE_USLEEP
2
3 #ifdef HAVE_SELECT
4
5 #ifdef HAVE_SYS_SELECT_H
6 # include <sys/select.h>
7 #else
8 # include <sys/time.h>
9 # include <sys/types.h>
10 # include <unistd.h>
11 #endif
12
13 void usleep(long usec)
14 {
15         struct timeval tv;
16
17         tv.tv_sec = usec / 1000000;
18         tv.tv_usec = usec % 1000000;
19         select(0, NULL, NULL, NULL, &tv);
20 }
21
22 #elif defined WIN32
23
24 /* usleep implementation from FreeSCI */
25
26 #include <windows.h>
27
28 void usleep (long usec)
29 {
30         LARGE_INTEGER lFrequency;
31         LARGE_INTEGER lEndTime;
32         LARGE_INTEGER lCurTime;
33
34         QueryPerformanceFrequency (&lFrequency);
35         if (lFrequency.QuadPart) {
36                 QueryPerformanceCounter (&lEndTime);
37                 lEndTime.QuadPart += (LONGLONG) usec *
38                                         lFrequency.QuadPart / 1000000;
39                 do {
40                         QueryPerformanceCounter (&lCurTime);
41                         Sleep(0);
42                 } while (lCurTime.QuadPart < lEndTime.QuadPart);
43         }
44 }
45
46 #endif
47
48 #endif /* !HAVE_USLEEP */