home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / UNXSLEEP.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  65 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /* microsleep.c */
  4.  
  5. /* Author:
  6.  * Pieter de Jong
  7.  * Released in Public Domain 7-1996
  8.  */
  9.  
  10.  
  11. /* This module contains the unix-specific versions the microsleep function.
  12.  * There are actually two versions of microsleep defined here, because
  13.  * BSD, SysV, and V7 all need quite different implementations.
  14.  *
  15.  * At this time, the select() call is not (yet) in the POSIX standard, but
  16.  * it is in BSD4.2+ , and SVR4.
  17.  * poll() is SYSV only. BSD has a usleep() function that looks & feels
  18.  * like microsleep(), but implements it using setitimer(). It has
  19.  * (usually) unwanted side effects when multiple timers are active.
  20.  *
  21.  * This version should interact correctly with other timers set
  22.  * by the calling process, and is not interrupted if a signal is caught
  23.  */
  24. /* #include "config.h"*/
  25.  
  26. # ifdef BSD
  27. /* For BSD, we use select() */
  28.  
  29. #include<sys/types.h>
  30. #include<sys/time.h>
  31. #include<stddef.h>
  32.  
  33. void
  34. microsleep(unsigned int microsecs)
  35. {
  36.     struct timeval tval;
  37.  
  38.     tval.tv_sec = microsecs/ 1000000;
  39.     tval.tv_usec= microsecs% 1000000;
  40.     select(0, NULL, NULL, NULL, &tval);
  41. }
  42. #endif
  43.  
  44. # ifdef SYSV
  45. /* For System-V, we use poll to implement the timeout. */
  46. /* R4 has select(), but implements it using poll() */
  47. /* older versions may only have poll() */
  48.  
  49. #include<sys/types.h>
  50. #include<sys/poll.h>
  51. #include<stropts.h>
  52.  
  53. void
  54. microsleep(unsigned int microsecs)
  55. {
  56.     struct pollfd dummy;
  57.     int timeout;
  58.  
  59.     if ((timeout = microsecs / 1000) <= 0)
  60.         timeout = 1;
  61.     poll(&dummy, 0, timeout);
  62. }
  63.  
  64. # endif /* !BSD */
  65.