Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / librols / usleep.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 /* @(#)usleep.c 1.17 03/06/15 Copyright 1995-2003 J. Schilling */
14 /*
15  *      Copyright (c) 1995-2003 J. Schilling
16  */
17 /*
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License version 2
20  * as published by the Free Software Foundation.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License along with
28  * this program; see the file COPYING.  If not, write to the Free Software
29  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */
31
32 #include <mconfig.h>
33 #define usleep  __nothing_    /* prototype in unistd.h may be different */
34 #include <standard.h>
35 #include <stdxlib.h>
36 #include <timedefs.h>
37 #ifdef  HAVE_POLL_H
38 #       include <poll.h>
39 #else
40 #       ifdef   HAVE_SYS_POLL_H
41 #       include <sys/poll.h>
42 #       endif
43 #endif
44 #ifdef  HAVE_SYS_SYSTEMINFO_H
45 #include <sys/systeminfo.h>
46 #endif
47 #include <libport.h>
48 #undef  usleep
49
50 #ifndef HAVE_USLEEP
51 EXPORT  int     usleep          __PR((int usec));
52 #endif
53
54 #ifdef  OPENSERVER
55 /*
56  * Don't use the usleep() from libc on SCO's OPENSERVER.
57  * It will kill our processes with SIGALRM.
58  * SCO has a usleep() prototype in unistd.h, for this reason we
59  * #define usleep to __nothing__ before including unistd.h
60  */
61 #undef  HAVE_USLEEP
62 #endif
63
64 #ifdef apollo
65 /*
66  * Apollo sys5.3 usleep is broken.  Define a version based on time_$wait.
67  */
68 #include <apollo/base.h>
69 #include <apollo/time.h>
70 #undef HAVE_USLEEP
71 #endif
72
73 #if     !defined(HAVE_USLEEP)
74
75 EXPORT int
76 usleep(usec)
77         int     usec;
78 {
79 #if defined(apollo)
80         /*
81          * Need to check apollo before HAVE_SELECT, because Apollo has select,
82          * but it's time wait feature is also broken :-(
83          */
84 #define HAVE_USLEEP
85         /*
86          * XXX Do these vars need to be static on Domain/OS ???
87          */
88         static time_$clock_t    DomainDelay;
89         static status_$t        DomainStatus;
90
91         /*
92          * DomainDelay is a 48 bit value that defines how many 4uS periods to
93          * delay.  Since the input value range is 32 bits, the upper 16 bits of
94          * DomainDelay must be zero.  So we just divide the input value by 4 to
95          * determine how many "ticks" to wait
96          */
97         DomainDelay.c2.high16 = 0;
98         DomainDelay.c2.low32 = usec / 4;
99         time_$wait(time_$relative, DomainDelay, &DomainStatus);
100 #endif  /* Apollo */
101
102 #if     defined(HAVE_SELECT) && !defined(HAVE_USLEEP)
103 #define HAVE_USLEEP
104
105         struct timeval tv;
106         tv.tv_sec = usec / 1000000;
107         tv.tv_usec = usec % 1000000;
108         select(0, 0, 0, 0, &tv);
109 #endif
110
111 #if     defined(HAVE_POLL) && !defined(HAVE_USLEEP)
112 #define HAVE_USLEEP
113
114         if (poll(0, 0, usec/1000) < 0)
115                 comerr("poll delay failed.\n");
116
117 #endif
118
119 #if     defined(HAVE_NANOSLEEP) && !defined(HAVE_USLEEP)
120 #define HAVE_USLEEP
121
122         struct timespec ts;
123
124         ts.tv_sec = usec / 1000000;
125         ts.tv_nsec = (usec % 1000000) * 1000;
126
127         nanosleep(&ts, 0);
128 #endif
129
130
131 #if     !defined(HAVE_USLEEP)
132 #define HAVE_USLEEP
133
134         sleep((usec+500000)/1000000);
135 #endif
136
137         return (0);
138 }
139 #endif