1 /*
2  *  NAME:
3  *      usleep     -- This is the precision timer for Test Set
4  *                    Automation. It uses the select(2) system
5  *                    call to delay for the desired number of
6  *                    micro-seconds. This call returns ZERO
7  *                    (which is usually ignored) on successful
8  *                    completion, -1 otherwise.
9  *
10  *  ALGORITHM:
11  *      1) We range check the passed in microseconds and log a
12  *         warning message if appropriate. We then return without
13  *         delay, flagging an error.
14  *      2) Load the Seconds and micro-seconds portion of the
15  *         interval timer structure.
16  *      3) Call select(2) with no file descriptors set, just the
17  *         timer, this results in either delaying the proper
18  *         ammount of time or being interupted early by a signal.
19  *
20  *  HISTORY:
21  *      Added when the need for a subsecond timer was evident.
22  *
23  *  AUTHOR:
24  *      Michael J. Dyer                   Telephone:   AT&T 414.647.4044
25  *      General Electric Medical Systems        GE DialComm  8 *767.4044
26  *      P.O. Box 414  Mail Stop 12-27         Sect'y   AT&T 414.647.4584
27  *      Milwaukee, Wisconsin  USA 53201                      8 *767.4584
28  *      internet:  mike@sherlock.med.ge.com     GEMS WIZARD e-mail: DYER
29  */
30 
31 #include "vice.h"
32 
33 #ifndef HAVE_USLEEP
34 
35 #if defined(HAVE_UNISTD_H) && !defined(AMIGA_MORPHOS)
36 #include <unistd.h>
37 #endif
38 
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <errno.h>
42 #include <time.h>
43 #include <sys/time.h>
44 #include <sys/types.h>
45 
46 #ifdef AMIGA_MORPHOS
47 #include <proto/socket.h>
48 # define select(nfds, read_fds, write_fds, except_fds, timeout) \
49     WaitSelect(nfds, read_fds, write_fds, except_fds, timeout, NULL)
50 #endif
51 
52 #ifdef __OS2__
53 #include <sys/select.h>
54 #endif
55 
56 /* FIXME: this is a replacement function for a standardfunction that should come
57           with your c-library in the first place. it is only used in a few
58           sound-drivers and some hw-sid drivers - and it probably shouldnt be
59           used even there, instead a target specific vice_usleep should be
60           implemented, which really waits the requested delay (and no more, and
61           no less) */
usleep(unsigned long int microSeconds)62 int usleep(unsigned long int microSeconds)
63 {
64     unsigned int Seconds, uSec;
65 #ifdef __OS2__
66     int nfds;
67     fd_set readfds, writefds, exceptfds;
68 #else
69     int nfds, readfds, writefds, exceptfds;
70 #endif
71 
72     struct  timeval Timer;
73 
74 #ifdef __OS2__
75     nfds = 0;
76 #else
77     nfds = readfds = writefds = exceptfds = 0;
78 #endif
79 
80     if ((microSeconds == (unsigned long) 0) || microSeconds > (unsigned long) 4000000) {
81         errno = ERANGE;                 /* value out of range */
82         return -1;
83     }
84 
85     Seconds = microSeconds / (unsigned long) 1000000;
86     uSec = microSeconds % (unsigned long) 1000000;
87 
88     Timer.tv_sec = Seconds;
89     Timer.tv_usec = uSec;
90 
91     if (select( nfds, &readfds, &writefds, &exceptfds, &Timer ) < 0) {
92         return -1;
93     }
94 
95     return 0;
96 }
97 #endif
98