home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BTMTSRC3.ZIP / TIMER.C < prev    next >
C/C++ Source or Header  |  1991-09-01  |  8KB  |  256 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software, Co.                       */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          Freely Available<tm> Software.                 */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  (C) Copyright 1987-90, Bit Bucket Software Co., a Delaware Corporation. */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*                 This module was written by Bob Hartman                   */
  14. /*               with some hacking done by Vince Perriello                  */
  15. /*                                                                          */
  16. /*                                                                          */
  17. /*                       BinkleyTerm timer routines                         */
  18. /*                                                                          */
  19. /*                                                                          */
  20. /*    For complete  details  of the licensing restrictions, please refer    */
  21. /*    to the License  agreement,  which  is published in its entirety in    */
  22. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.240.    */
  23. /*                                                                          */
  24. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  25. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  26. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  27. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  28. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  29. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  30. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  31. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  32. /*                                                                          */
  33. /*                                                                          */
  34. /* You can contact Bit Bucket Software Co. at any one of the following      */
  35. /* addresses:                                                               */
  36. /*                                                                          */
  37. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:132/491, 1:141/491  */
  38. /* P.O. Box 460398                AlterNet 7:491/0                          */
  39. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  40. /*                                Internet f491.n132.z1.fidonet.org         */
  41. /*                                                                          */
  42. /* Please feel free to contact us at any time to share your comments about  */
  43. /* our software and/or licensing policies.                                  */
  44. /*                                                                          */
  45. /*--------------------------------------------------------------------------*/
  46.  
  47. /*
  48.   
  49.     This file contains routines to implement a simple multiple
  50.     alarm system.  The routines allow setting any number of alarms,
  51.     and then checking if any one of them has expired.  It also allows
  52.     adding time to an alarm.
  53. */
  54.  
  55.  
  56. /*
  57.  * $Log:   H:/binkley/vcs/timer.c_v  $
  58. ** 
  59. **    Rev 1.0   01 Sep 1991 14:34:30   cml
  60. ** Changes made on Binkley:
  61. ** 
  62. **  - Bent the timing rules slightly for Wazoo initiation.
  63. **  - Changed ALL async routines.
  64. **  - Removed ESC as a shell.
  65.  */
  66.  
  67.  
  68. #include <stdio.h>
  69. #ifdef OS_2
  70.     #define INCL_DOSDATETIME
  71.     #define INCL_DOSPROCESS
  72.     #include <os2.h>
  73.     #include <process.h>
  74. #endif
  75. #include "com.h"
  76. #include "xfer.h"
  77. #include "zmodem.h"
  78. #include "keybd.h"
  79. #include "sbuf.h"
  80. #include "sched.h"
  81. #include "externs.h"
  82. #include "prototyp.h"
  83. #include "timer.h"
  84.  
  85.  
  86.  
  87. #ifdef OS_2
  88.  
  89.  
  90. /* CML saving excess cpu cycles calling the OS!  Start a separate thread! */
  91.  
  92. #define TIMER_STACKSIZE            2048
  93.  
  94. TID timer_tid;
  95. int timer_stack[TIMER_STACKSIZE / sizeof(int)];
  96. long update_sem = 0L;
  97. long timer_sem = 0L;
  98. DATETIME curtime;            /* CML */
  99.  
  100.  
  101.  
  102. void far timer_thread(void *dummy)
  103.     {
  104.     DosSetPrty(PRTYS_THREAD,PRTYC_FOREGROUNDSERVER,PRTYD_MAXIMUM,timer_tid);
  105.     while (1)
  106.         {
  107.         DosSemRequest(&update_sem,SEM_INDEFINITE_WAIT);
  108.         DosGetDateTime(&curtime);
  109.         DosSemClear(&update_sem);
  110.         DosSleep(5L);                /* sleep */
  111.         }
  112.     }
  113.  
  114.  
  115.  
  116. int start_timer(void)
  117.     {
  118.     DosSemClear(&timer_sem);
  119.     if ((timer_tid = _beginthread(timer_thread,timer_stack,TIMER_STACKSIZE,NULL)) == -1)
  120.         return 0;
  121.     return 1;
  122.     }
  123.  
  124.  
  125.  
  126. void hold_timer(void)
  127.     {
  128.     DosSemRequest(&timer_sem,SEM_INDEFINITE_WAIT);
  129.     }
  130.  
  131.  
  132.  
  133. void restart_timer(void)
  134.     {
  135.     DosSemClear(&timer_sem);
  136.     }
  137.  
  138.  
  139. /* End of code by CML */
  140.  
  141.  
  142. #endif
  143.  
  144.  
  145. /*
  146.     long timerset (t)
  147.     unsigned int t;
  148.   
  149.     This routine returns a timer variable based on the MS-DOS
  150.     time.  The variable returned is a long which corresponds to
  151.     the MS-DOS time at which the timer will expire.  The variable
  152.     need never be used once set, but to find if the timer has in
  153.     fact expired, it will be necessary to call the timeup function.
  154.     The expire time 't' is in hundredths of a second.
  155.   
  156.     Note: This routine as coded had a granularity of one week. I
  157.     have put the code that implements that inside the flag 
  158.     "HIGH_OVERHEAD". If you want it, go ahead and use it. For
  159.     BT's purposes, (minute) granularity should suffice.
  160.  
  161. */
  162. long pascal timerset (t)
  163. unsigned int t;
  164.     {
  165.     long l;
  166. #ifdef HIGH_OVERHEAD
  167.     int l2;
  168. #endif
  169.     int hours, mins, secs, ths;
  170. #ifdef HIGH_OVERHEAD
  171.     extern int week_day ();
  172. #endif
  173.  
  174. #ifndef OS_2
  175.     /* Get the DOS time and day of week */
  176.     dostime (&hours, &mins, &secs, &ths);
  177. #else
  178.     DosSemRequest(&update_sem,SEM_INDEFINITE_WAIT);
  179.     hours = curtime.hours;         /* CML added this time to save repeated calls to OS */
  180.     mins = curtime.minutes;
  181.     secs = curtime.seconds;
  182.     ths = curtime.hundredths;
  183.     DosSemClear(&update_sem);
  184. #endif
  185.  
  186. #ifdef HIGH_OVERHEAD
  187.     l2 = week_day ();
  188. #endif
  189.  
  190.    /* Figure out the hundredths of a second so far this week */
  191.     l =
  192. #ifdef HIGH_OVERHEAD
  193.         l2 * PER_DAY +
  194.         (hours % 24) * PER_HOUR +
  195. #endif
  196.         (mins % 60) * PER_MINUTE +
  197.         (secs % 60) * PER_SECOND +
  198.         ths;
  199.  
  200.     /* Add in the timer value */
  201.     l += t;
  202.  
  203.     /* Return the alarm off time */
  204.     return (l);
  205.     }
  206.  
  207. /*
  208.     int timeup (t)
  209.     long t;
  210.   
  211.     This routine returns a 1 if the passed timer variable corresponds
  212.     to a timer which has expired, or 0 otherwise.
  213. */
  214.  
  215.  
  216. int pascal timeup (t)
  217. long t;
  218. {
  219.    long l;
  220.  
  221.    /* Get current time in hundredths */
  222.    l = timerset (0);
  223.  
  224.    /* If current is less then set by more than max int, then adjust */
  225.    if (l < (t - 65536L))
  226.       {
  227. #ifdef HIGH_OVERHEAD
  228.       l += PER_WEEK;
  229. #else
  230.       l += PER_HOUR;
  231. #endif
  232.       }
  233.  
  234.    /* Return whether the current is greater than the timer variable */
  235.    return ((l - t) >= 0L);
  236. }
  237.  
  238.  
  239. /*
  240.     long addtime (t1, t2)
  241.     long t1;
  242.     unsigned int t2;
  243.   
  244.     This routine adds t2 hundredths of a second to the timer variable
  245.     in t1.  It returns a new timer variable.
  246.  
  247.         Not needed in BinkleyTerm, commented out.
  248.  
  249. long addtime (t1, t2)
  250. long t1;
  251. unsigned int t2;
  252. {
  253.    return (t1 + t2);
  254. }
  255. */
  256.