home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Texteditors / Origami / Sources / src / lib / usleep.c < prev   
Encoding:
C/C++ Source or Header  |  1996-09-27  |  1.4 KB  |  68 lines

  1. /*{{{}}}*/
  2. /*{{{  includes*/
  3. #ifdef _INCLUDE_HPUX_SOURCE
  4. #   undef _POSIX_SOURCE
  5. #endif
  6.  
  7. #ifdef CONFIG_H
  8. #   define USLEEP_C
  9. #   include "config.h"
  10. #endif
  11.  
  12. #ifdef SELECT_USLEEP
  13. #   include <sys/time.h>
  14. #else
  15. #   ifdef POLL_USLEEP
  16. #      include <poll.h>
  17. #   else
  18. #      ifdef BUSY_USLEEP
  19. #         include <sys/time.h>
  20. #      endif
  21. #   endif
  22. #endif
  23. /*}}}  */
  24.  
  25. /*{{{  usleep*/
  26. #define sec ((unsigned long)1000000)
  27.  
  28. int usleep(unsigned long t)
  29. { if (t>sec) { sleep((int)(t/sec));t%=sec; }
  30.   if (t)
  31. #     ifdef SELECT_USLEEP
  32.       { struct timeval timeout;
  33.  
  34.         timeout.tv_sec=0;
  35.         timeout.tv_usec=t;
  36.         select((size_t)0,(void*)0,(void*)0,(void*)0,&timeout);
  37.       }
  38. #     else
  39. #        ifdef POLL_USLEEP
  40.            poll((struct pollfd*)0,(size_t)0,(int)(t/1000));
  41. #        else
  42. #           ifdef BUSY_USLEEP
  43.             /*{{{  wait busy*/
  44.             { struct timezone zone;
  45.               struct timeval start;
  46.  
  47.               gettimeofday(&start,&zone);
  48.               do
  49.                { struct timeval end;
  50.  
  51.                  gettimeofday(&end,&zone);
  52.                  if (start.tv_usec>end.tv_usec)
  53.                   { end.tv_usec+=sec;end.tv_usec--; }
  54.                  if (  ((end.tv_sec-start.tv_sec)>0)
  55.                      ||((end.tv_usec-start.tv_usec)>t)) t=0;
  56.                }
  57.               while (t);
  58.             }
  59.             /*}}}  */
  60. #           else
  61.               sleep(1);
  62. #           endif
  63. #        endif
  64. #     endif
  65.   return(0);
  66. }
  67. /*}}}  */
  68.