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 
53 /* FIXME: this is a replacement function for a standardfunction that should come
54           with your c-library in the first place. it is only used in a few
55           sound-drivers and some hw-sid drivers - and it probably shouldnt be
56           used even there, instead a target specific vice_usleep should be
57           implemented, which really waits the requested delay (and no more, and
58           no less) */
usleep(unsigned long int microSeconds)59 int usleep(unsigned long int microSeconds)
60 {
61     unsigned int Seconds, uSec;
62     int nfds, readfds, writefds, exceptfds;
63     struct timeval Timer;
64 
65     fprintf(stderr,
66             "%s:%d:%s(): I'm surprised anyone actually compiled VICE for an OS"
67             " that doesn't supply usleep(). -- compyx 2020-07-05\n",
68             __FILE__, __LINE__, __func__);
69     abort();
70     nfds = readfds = writefds = exceptfds = 0;
71 
72     if ((microSeconds == (unsigned long) 0) || microSeconds > (unsigned long) 4000000) {
73         errno = ERANGE;                 /* value out of range */
74         return -1;
75     }
76 
77     Seconds = microSeconds / (unsigned long) 1000000;
78     uSec = microSeconds % (unsigned long) 1000000;
79 
80     Timer.tv_sec = Seconds;
81     Timer.tv_usec = uSec;
82 
83     if (select(nfds, &readfds, &writefds, &exceptfds, &Timer) < 0) {
84         return -1;
85     }
86 
87     return 0;
88 }
89 #endif
90