1 /* @(#)usleep.c 1.24 14/03/03 Copyright 1995-2014 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static UConst char sccsid[] =
5 "@(#)usleep.c 1.24 14/03/03 Copyright 1995-20014 J. Schilling";
6 #endif
7 /*
8 * Copyright (c) 1995-2014 J. Schilling
9 */
10 /*
11 * The contents of this file are subject to the terms of the
12 * Common Development and Distribution License, Version 1.0 only
13 * (the "License"). You may not use this file except in compliance
14 * with the License.
15 *
16 * See the file CDDL.Schily.txt in this distribution for details.
17 * A copy of the CDDL is also available via the Internet at
18 * http://www.opensource.org/licenses/cddl1.txt
19 *
20 * When distributing Covered Code, include this CDDL HEADER in each
21 * file and include the License file CDDL.Schily.txt from this distribution.
22 */
23
24 #define usleep __nothing_ /* prototype in unistd.h may be different */
25 #include <schily/standard.h>
26 #include <schily/stdlib.h>
27 #include <schily/time.h>
28 #ifdef HAVE_POLL_H
29 # include <poll.h>
30 #else
31 # ifdef HAVE_SYS_POLL_H
32 # include <sys/poll.h>
33 # endif
34 #endif
35 #include <schily/systeminfo.h>
36 #include <schily/libport.h>
37 #undef usleep
38
39 #ifndef HAVE_USLEEP
40 EXPORT int usleep __PR((int usec));
41 #endif
42
43 #ifdef OPENSERVER
44 /*
45 * Don't use the usleep() from libc on SCO's OPENSERVER.
46 * It will kill our processes with SIGALRM.
47 * SCO has a usleep() prototype in unistd.h, for this reason we
48 * #define usleep to __nothing__ before including unistd.h
49 */
50 #undef HAVE_USLEEP
51 #endif
52
53 #ifdef apollo
54 /*
55 * Apollo sys5.3 usleep is broken. Define a version based on time_$wait.
56 */
57 #include <apollo/base.h>
58 #include <apollo/time.h>
59 #undef HAVE_USLEEP
60 #endif
61
62 #if !defined(HAVE_USLEEP)
63
64 EXPORT int
usleep(usec)65 usleep(usec)
66 int usec;
67 {
68 #if defined(apollo)
69 /*
70 * Need to check apollo before HAVE_SELECT, because Apollo has select,
71 * but it's time wait feature is also broken :-(
72 */
73 #define HAVE_USLEEP
74 /*
75 * XXX Do these vars need to be static on Domain/OS ???
76 */
77 static time_$clock_t DomainDelay;
78 static status_$t DomainStatus;
79
80 /*
81 * DomainDelay is a 48 bit value that defines how many 4uS periods to
82 * delay. Since the input value range is 32 bits, the upper 16 bits of
83 * DomainDelay must be zero. So we just divide the input value by 4 to
84 * determine how many "ticks" to wait
85 */
86 DomainDelay.c2.high16 = 0;
87 DomainDelay.c2.low32 = usec / 4;
88 time_$wait(time_$relative, DomainDelay, &DomainStatus);
89 #endif /* Apollo */
90
91 #if defined(HAVE_SELECT) && !defined(HAVE_USLEEP)
92 #define HAVE_USLEEP
93
94 struct timeval tv;
95 tv.tv_sec = usec / 1000000;
96 tv.tv_usec = usec % 1000000;
97 select(0, 0, 0, 0, &tv);
98 #endif
99
100 #if defined(HAVE_POLL) && !defined(HAVE_USLEEP)
101 #define HAVE_USLEEP
102
103 if (poll(0, 0, usec/1000) < 0)
104 comerr("poll delay failed.\n");
105
106 #endif
107
108 /*
109 * We cannot use nanosleep() until we found a way to check
110 * separately for nanosleep() in libc and for nanosleep in librt.
111 */
112 #if defined(xxxHAVE_NANOSLEEP) && !defined(HAVE_USLEEP)
113 #define HAVE_USLEEP
114
115 struct timespec ts;
116
117 ts.tv_sec = usec / 1000000;
118 ts.tv_nsec = (usec % 1000000) * 1000;
119
120 nanosleep(&ts, 0);
121 #endif
122
123 #if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(HAVE_USLEEP)
124 #define HAVE_USLEEP
125 Sleep(usec/1000);
126 #endif
127
128 #if !defined(HAVE_USLEEP)
129 #define HAVE_USLEEP
130
131 sleep((usec+500000)/1000000);
132 #endif
133
134 return (0);
135 }
136 #endif
137