home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / s / s001 / 1.ddi / TS / SRC / TIMERS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-22  |  2.5 KB  |  110 lines

  1.                 /******************************************
  2.            *            TIMERS.C             *
  3.                * Copyright TimeSlice, Inc. 1985, 86, 87. *
  4.                ******************************************/
  5.  
  6. #include <ts.h>
  7.  
  8. extern TIMER *_tpfrnt;            /*head of TIMER structs list*/
  9.  
  10. void time_isr();
  11.  
  12. /***
  13. * PTIMER( TUNITS, PROC )
  14. * This function attaches an isr to interrupt # 8. Evry Tunits, the process
  15. * proc is awakened. A pointer to that allocated TIMER is returned.
  16. ***/
  17. TIMER *ptimer( tunits, proc )
  18. unsigned long  tunits;
  19. PROCESS *proc;
  20. {
  21.   return ftimer( tunits, wakeproc, (char *)proc );
  22. }
  23.  
  24. /***
  25. * FTIMER( TUNITS, FUNC, ARG )
  26. * This function attaches an isr to interrupt # 8. Evry Tunits, the function
  27. * func is called. A pointer to that allocated TIMER is returned.
  28. ***/
  29. TIMER *ftimer( tunits, func, arg )
  30. unsigned long  tunits;
  31. void (*func)();
  32. char *arg;
  33. {
  34.   TIMER    *tp;
  35.   char *malloc();
  36.   int cpuflags;
  37.   static installed;
  38.  
  39.   critstart( DOS_CRCLASS );
  40.  
  41.   /*if called for the first time, install time_isr*/
  42.   if( !installed ) {
  43.     intatt( 0x08, 0x300, time_isr, BEFORE ) ;
  44.     installed = 1;
  45.   }
  46.  
  47.   /*allocate a new TIMER and put it in front of _tpfrnt list*/
  48.   tp = (TIMER *)malloc( sizeof( TIMER ) );
  49.   cpuflags = cli();
  50.   tp->nxt = _tpfrnt;
  51.   _tpfrnt = tp;
  52.   putf( cpuflags );
  53.   tp->cntr = tp->reset = tunits;
  54.   tp->func = func;
  55.   tp->arg = arg;
  56.   critend( DOS_CRCLASS );
  57.  
  58.   return tp;
  59. }
  60.  
  61. /*****
  62. * TIME_ISR() 
  63. * This isr is attached to the primary hardware timer interrupt.
  64. * It then processes each WPROC in the list and calls each function as needed.
  65. *****/
  66. void time_isr() {                                    
  67.   TIMER *tp;                /* Pointer to TIMER structure */
  68.  
  69.   /*process all wprocs*/
  70.   for( tp = _tpfrnt ; tp ; tp = tp->nxt )
  71.     if( !--tp->cntr ) {            /* if counter reaches 0, call func*/
  72.       tp->cntr = tp->reset;
  73.       (*tp->func)( tp->arg );
  74.     }
  75. }
  76.  
  77. /***
  78. * RMVTIMER( TP )
  79. * Remove TIMER structure *TP from TIMER list.
  80. ***/
  81. void rmvtimer( tp )
  82. TIMER *tp;
  83. {
  84.   TIMER *tp2;
  85.   int cpuflags;
  86.  
  87.   cpuflags = cli();
  88.   for( tp2 = (TIMER *)&_tpfrnt ; tp2->nxt ; tp2 = tp2->nxt )
  89.     if( tp2->nxt == tp ) {
  90.       tp2->nxt = tp->nxt;
  91.       critstart( DOS_CRCLASS );
  92.       free( tp );
  93.       critend( DOS_CRCLASS );
  94.       break;
  95.     }
  96.   putf( cpuflags );
  97. }
  98.  
  99.  
  100. /***
  101. * RESET_TIMER( TIMER, TUNITS )
  102. * This function resets the timer to a new value.
  103. ***/
  104.  
  105. void reset_timer(TIMER *tp, unsigned long tunits)
  106. {
  107.   if (tunits == 0xffffffffL) tp->cntr = tp->reset;
  108.      else tp->cntr = tp->reset = tunits;
  109. }
  110.