home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / internet / tcpipsrc / h / if / NetMisc / c / TIMER < prev    next >
Encoding:
Text File  |  1994-06-24  |  3.0 KB  |  96 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "global.h"
  4. #include "timer.h"
  5. #include "arc.h"
  6. #include "misc.h"
  7.  
  8. /* Head of running timer chain */
  9. struct timer *timers = NULL;
  10.  
  11.  
  12. void tick(void)
  13. {
  14.         register struct timer *t,*tp;
  15.         register struct timer *expired = NULLTIMER;
  16.  
  17.         /* Run through the list of running timers, decrementing each one.
  18.          * If one has expired, take it off the running list and put it
  19.          * on a singly linked list of expired timers
  20.          */
  21.         for (t = timers;t != NULLTIMER; t = tp)
  22.         {
  23.                 tp = t->next;
  24.                 if(tp == t)
  25.                 {
  26.                         panic("PANIC: Timer loop at %lx\n",(long)tp);
  27.                         iostop();
  28.                         exit(1);
  29.                 }
  30.                 if(t->state == TIMER_RUN && --(t->count) == 0)
  31.                 {
  32.                         stop_timer(t);
  33.                         t->state = TIMER_EXPIRE;
  34.                         /* Put on head of expired timer list */
  35.                         t->next = expired;
  36.                         expired = t;
  37.                 }
  38.         }
  39.         /* Now go through the list of expired timers, removing each
  40.          * one and kicking the notify function, if there is one
  41.          */
  42.         /* Note: the check for TIMER_EXPIRE below has a specific */
  43.         /* purpose.  It prevents wasted timer calls to the NET/ROM */
  44.         /* transport protocol timeout routine.  This routine does */
  45.         /* not know which timer expired, so it scans and processes */
  46.         /* the whole window.  If multiple timers expired, it handles */
  47.         /* them all and resets their states to something other than */
  48.         /* TIMER_EXPIRE.  So, we oblige here by not re-processing */
  49.         /* them under those circumstances. */
  50.         
  51.         while((t = expired), t != NULLTIMER)
  52.         {
  53.                 expired = t->next;
  54.                 if (t->state == TIMER_EXPIRE && t->func != NULL)
  55.                 {
  56.                         (*t->func)(t->arg);
  57.                 }
  58.         }
  59. }
  60. /* Start a timer */
  61. void start_timer(register struct timer *t)
  62. {
  63.  
  64.         if (t == NULLTIMER || t->start == 0)
  65.                 return;
  66.         t->count = t->start;
  67.         if (t->state != TIMER_RUN)
  68.         {
  69.                 t->state = TIMER_RUN;
  70.                 /* Put on head of active timer list */
  71.                 t->prev = NULLTIMER;
  72.                 t->next = timers;
  73.                 if(t->next != NULLTIMER)
  74.                         t->next->prev = t;
  75.                 timers = t;
  76.         }
  77. }
  78. /* Stop a timer */
  79. void stop_timer(register struct timer *t)
  80. {
  81.  
  82.         if(t == NULLTIMER)
  83.                 return;
  84.         if(t->state == TIMER_RUN)
  85.         {
  86.                 /* Delete from active timer list */
  87.                 if(timers == t)
  88.                         timers = t->next;
  89.                 if(t->next != NULLTIMER)
  90.                         t->next->prev = t->prev;
  91.                 if(t->prev != NULLTIMER)
  92.                         t->prev->next = t->next;
  93.         }
  94.         t->state = TIMER_STOP;
  95. }
  96.