home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 5.ddi / C / UTSLEEP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-04-13  |  1.8 KB  |  67 lines

  1. /**
  2. *
  3. * Name        utsleep -- Suspend processing
  4. *
  5. * Synopsis    rdur = utsleep(period);
  6. *
  7. *        unsigned rdur      The number of timer ticks the process
  8. *                  was actually suspended.
  9. *        unsigned period   The number of timer ticks the process
  10. *                  is to be suspended.
  11. *
  12. * Description    UTSLEEP suspends processing for at least the number of
  13. *        ticks requested, and returns the actual number as the
  14. *        functional value.  Because the function checks the
  15. *        system clock to determine if the period has transpired,
  16. *        it is possible that the actual duration is longer than
  17. *        requested.
  18. *
  19. *        On standard IBM PCs, timer ticks occur 1193180/65536
  20. *        (about 18.2) times per second.
  21. *
  22. *        This function temporarily enables hardware interrupts
  23. *        but restores the state of the interrupt flag before it
  24. *        returns.
  25. *
  26. * Returns    rdur          The number of timer ticks the
  27. *                  process actually slept.
  28. *
  29. * Version    3.0  (C)Copyright Blaise Computing Inc.  1984, 1986
  30. *
  31. * Version    3.02 March 20, 1987
  32. *        Corrected handling of midnight rollover.
  33. *
  34. **/
  35.  
  36. #include <butility.h>
  37.  
  38. unsigned utsleep(period)
  39. unsigned period;
  40. {
  41.     long     initclk;              /* Initial clock count          */
  42.     long     nowclk;              /* Moving clock count          */
  43.     unsigned elpticks;              /* Elapsed tick count          */
  44.     int      ints_were_on;          /* Whether interrupts were      */
  45.                       /* already on              */
  46.  
  47.     /* Find out the current clock count and wait until period counts  */
  48.     /* have passed.                              */
  49.  
  50.     ints_were_on = utinton();
  51.     utgetclk(&initclk);
  52.  
  53.     for (elpticks = 0;
  54.      elpticks < period;
  55.      elpticks = (unsigned) (nowclk - initclk))
  56.     {
  57.     utgetclk(&nowclk);
  58.     if (nowclk < initclk)           /* Must have wrapped past  */
  59.         nowclk += 0x1800b0L;       /* midnight.           */
  60.     }
  61.  
  62.     if (!ints_were_on)
  63.     utintoff();
  64.  
  65.     return(elpticks);
  66. }
  67.