home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 February / PCO_0299.ISO / filesbbs / linux / mikmod-3.000 / mikmod-3 / mikmod-3.1.2 / extra / usleep.c < prev   
Encoding:
C/C++ Source or Header  |  1998-12-07  |  2.5 KB  |  86 lines

  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.  *      Modified for Solaris-specific bits by SAM 24/10/96
  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. #ifdef HAVE_CONFIG_H
  32. #include "config.h"
  33. #endif
  34.  
  35. #ifdef HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif
  38. #include <errno.h>
  39. #include <time.h>
  40. #ifdef HAVE_SYS_TIME_H
  41. #include <sys/time.h>
  42. #endif
  43. #include <sys/param.h>
  44. #include <sys/types.h>
  45.  
  46. int usleep(unsigned long microSeconds)
  47. {
  48.     unsigned int    Seconds, uSec;
  49.     int             nfds;
  50.     struct  timeval Timer;
  51.  
  52. #if (defined(SOLARIS) || (defined(SGI)))
  53.     fd_set          readfds, writefds, exceptfds;
  54.  
  55.     nfds = 0;
  56.     FD_ZERO(&readfds);
  57.     FD_ZERO(&writefds);
  58.     FD_ZERO(&exceptfds);
  59. #else
  60.     int            readfds, writefds, exceptfds;
  61.     nfds = readfds = writefds = exceptfds = 0;
  62. #endif
  63.  
  64.     if( (microSeconds == (unsigned long) 0)
  65.         || microSeconds > (unsigned long) 4000000 )
  66.     {
  67.         errno = ERANGE;     /* value out of range */
  68.         perror( "usleep time out of range ( 0 -> 4000000 ) " );
  69.         return -1;
  70.     }
  71.  
  72.     Seconds = microSeconds / (unsigned long) 1000000;
  73.     uSec    = microSeconds % (unsigned long) 1000000;
  74.  
  75.     Timer.tv_sec        = Seconds;
  76.     Timer.tv_usec       = uSec;
  77.  
  78.     if( select( nfds, &readfds, &writefds, &exceptfds, &Timer ) < 0 )
  79.     {
  80.         perror( "usleep (select) failed" );
  81.         return -1;
  82.     }
  83.  
  84.     return 0;
  85. }
  86.