home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / sleep.c < prev    next >
C/C++ Source or Header  |  1993-02-26  |  2KB  |  105 lines

  1. /* sleep -- sleep for a specified number of seconds */
  2. /* usleep -- sleep for a specified number of microSecond */
  3. /* written by Eric R. Smith and placed in the public domain */
  4. /* extensively rehacked by entropy for mint >= 0.95, still public domain */
  5.  
  6. #include <time.h>
  7. #include <mintbind.h>
  8. #include <signal.h>
  9. #include <unistd.h>
  10.  
  11. /* _clock() has a rez of CLOCKS_PER_SEC ticks/sec */
  12.  
  13. #define USEC_PER_TICK (1000000L / ((unsigned long)CLOCKS_PER_SEC))
  14. #define    USEC_TO_CLOCK_TICKS(us)    ((us) / USEC_PER_TICK )
  15.  
  16. static void __CDECL alarm_catch __PROTO((long signum));
  17.  
  18. static void __CDECL
  19. alarm_catch(signum)
  20.     long signum;
  21. {
  22.     return;
  23. }
  24.  
  25. unsigned int
  26. sleep(n)
  27.     unsigned int n;
  28. {
  29.     unsigned long stop;
  30.     long old_alarm_func;
  31.     long old_sigmask;
  32.     long alarm_sec;
  33.     long remain;
  34.     extern int __mint;
  35.  
  36.     if (__mint) {
  37.         if (__mint < 95) {
  38.             while (n > 32) {
  39.                 (void)Fselect(32000, 0L, 0L, 0L);
  40.                 n -= 32;
  41.             }
  42.             (void)Fselect(1000*n, 0L, 0L, 0L);
  43.             return 0;
  44.         }
  45.         if (n == 0)
  46.             return 0;
  47.         alarm_sec = (long) alarm(0);
  48.         old_sigmask = Psigblock(~0L);
  49.         old_alarm_func = Psignal(SIGALRM, (long)alarm_catch);
  50.         (void) Psigblock(~0L);
  51.         stop = _clock() + n * CLOCKS_PER_SEC;
  52.         if (alarm_sec && (alarm_sec < n))
  53.             (void) alarm((unsigned) alarm_sec);
  54.         else
  55.             (void) alarm(n);
  56.         Psigpause(old_sigmask);
  57.         (void) alarm(0);
  58.         (void) Psignal(SIGALRM, (long) alarm_catch);
  59.         (void) Syield();
  60.         (void) Psigblock(~0L);
  61.         remain = ((long) stop - (long) _clock()) 
  62.               / (long) CLOCKS_PER_SEC;
  63.         if (alarm_sec) {
  64.             alarm_sec -= ((long) n - remain);
  65.             if (alarm_sec <= 0)
  66.                 (void) Pkill(Pgetpid(), SIGALRM);
  67.             else
  68.                 (void) alarm((unsigned) alarm_sec);
  69.         }
  70.         (void) Psignal(SIGALRM, (long) old_alarm_func);
  71.         (void) Psigsetmask(old_sigmask);
  72.         (void) Syield();
  73.         return ((remain > 0) ? (unsigned) remain : 0);
  74.     }
  75.     else {
  76.         stop = _clock() + n * CLOCKS_PER_SEC;
  77.         while (_clock() < stop)
  78.             ;
  79.     }
  80.     return 0;
  81. }
  82.  
  83. /*
  84.  * Sleep for usec microSeconds 
  85.  * the actual suspension time can be arbitrarily longer
  86.  *
  87.  */
  88. void
  89. usleep(usec)
  90.     unsigned long usec;
  91. {
  92.     unsigned long    stop;
  93.     extern int __mint;
  94.  
  95.     if (__mint) {
  96.         if (usec >= 1000)
  97.             (void)Fselect((unsigned)(usec/1000), 0L, 0L, 0L);
  98.     }
  99.     else {
  100.         stop = _clock() + USEC_TO_CLOCK_TICKS(usec);
  101.         while (_clock() < stop)
  102.             ;
  103.     }
  104. }
  105.