Initial Commit
[packages] / xemacs-packages / oo-browser / tree-x / usleep.c
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   <mike@sherlock.med.ge.com>
25  */
26
27 #ifndef HAVE_USLEEP
28
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <errno.h>
33 #include <time.h>
34 #include <sys/time.h>
35 #include <sys/param.h>
36 #include <sys/types.h>
37
38 int
39 usleep (unsigned long microSeconds)
40 {
41   unsigned int     Seconds, uSec;
42   int              nfds = 0;
43   fd_set           readfds, writefds, exceptfds;
44   struct  timeval  Timer;
45
46   if( (microSeconds == (unsigned long) 0) ||
47       microSeconds  >  (unsigned long) 4000000 )
48     {
49       errno = ERANGE;         /* value out of range */
50       perror( "usleep time out of range ( 0 -> 4000000 ) " );
51       return -1;
52     }
53
54   Seconds = microSeconds / (unsigned long) 1000000;
55   uSec    = microSeconds % (unsigned long) 1000000;
56
57   Timer.tv_sec  = Seconds;
58   Timer.tv_usec = uSec;
59
60   if( select( nfds, &readfds, &writefds, &exceptfds, &Timer ) < 0 )
61     {
62       perror( "usleep (select) failed" );
63       return -1;
64     }
65
66   return 0;
67 }
68
69 #endif /* not HAVE_USLEEP */