home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / internet / netlite / NET / c / TIMER < prev    next >
Text File  |  1993-03-15  |  2KB  |  82 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "werr.h"
  4. #include "global.h"
  5. #include "timer.h"
  6. #include "arc.h"
  7. #include "misc.h"
  8.  
  9. /* Head of running timer chain */
  10. struct timer *timers;
  11.  
  12.  
  13. void tick(void)
  14. {
  15.         register struct timer *t,*tp;
  16.         register struct timer *expired = NULLTIMER;
  17.  
  18.         /* Run through the list of running timers, decrementing each one.
  19.          * If one has expired, take it off the running list and put it
  20.          * on a singly linked list of expired timers
  21.          */
  22.         for(t = timers;t != NULLTIMER; t = tp){
  23.                 tp = t->next;
  24.                 if(tp == t){
  25.                         werr(1,"Timer loop at %lx\n",(long)tp);
  26.                         iostop();
  27.                         exit(1);
  28.                 }
  29.                 if(t->state == TIMER_RUN && --(t->count) == 0){
  30.                         stop_timer(t);
  31.                         t->state = TIMER_EXPIRE;
  32.                         /* Put on head of expired timer list */
  33.                         t->next = expired;
  34.                         expired = t;
  35.                 }
  36.         }
  37.         /* Now go through the list of expired timers, removing each
  38.          * one and kicking the notify function, if there is one
  39.          */
  40.  
  41.         while((t = expired) != NULLTIMER){
  42.                 expired = t->next;
  43.                 if(t->state == TIMER_EXPIRE && t->func){
  44.                         (*t->func)(t->arg);
  45.                 }
  46.         }
  47. }
  48. /* Start a timer */
  49. void start_timer(register struct timer *t)
  50. {
  51.  
  52.         if(t == NULLTIMER || t->start == 0)
  53.                 return;
  54.         t->count = t->start;
  55.         if(t->state != TIMER_RUN){
  56.                 t->state = TIMER_RUN;
  57.                 /* Put on head of active timer list */
  58.                 t->prev = NULLTIMER;
  59.                 t->next = timers;
  60.                 if(t->next != NULLTIMER)
  61.                         t->next->prev = t;
  62.                 timers = t;
  63.         }
  64. }
  65. /* Stop a timer */
  66. void stop_timer(register struct timer *t)
  67. {
  68.  
  69.         if(t == NULLTIMER)
  70.                 return;
  71.         if(t->state == TIMER_RUN){
  72.                 /* Delete from active timer list */
  73.                 if(timers == t)
  74.                         timers = t->next;
  75.                 if(t->next != NULLTIMER)
  76.                         t->next->prev = t->prev;
  77.                 if(t->prev != NULLTIMER)
  78.                         t->prev->next = t->next;
  79.         }
  80.         t->state = TIMER_STOP;
  81. }
  82.