home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / program / gempp15b / gemt.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-23  |  1.8 KB  |  88 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  This file is Copyright 1992,1993 by Warwick W. Allison.
  4. //  This file is part of the gem++ library.
  5. //  You are free to copy and modify these sources, provided you acknowledge
  6. //  the origin by retaining this notice, and adhere to the conditions
  7. //  described in the file COPYING.LIB.
  8. //
  9. /////////////////////////////////////////////////////////////////////////////
  10.  
  11. #include "gemt.h"
  12. #include "gema.h"
  13.  
  14. GEMtimer::GEMtimer(GEMactivity& in, int millisec) :
  15.     interval(millisec), mytime(now+interval)
  16. {
  17.     if (!act) (act=&in)->SetTimer(this);
  18.     InsertInto(head);
  19. }
  20.  
  21. GEMtimer::~GEMtimer()
  22. {
  23.     if (head==this && !next) {
  24.         act->SetTimer(0);
  25.         act=0;
  26.     } else {
  27.         DeleteFrom(head);
  28.     }
  29. }
  30.  
  31. // Below for service provider
  32. int GEMtimer::NextInterval()
  33. {
  34.     if (head)
  35.         return head->mytime-now;
  36.     else
  37.         // WAIT A SEC!  THIS SHOULDN'T HAPPEN!
  38.         return 1000;
  39. }
  40.  
  41. GEMfeedback GEMtimer::ExpireNext(const GEMevent& e)
  42. // Expire() the next GEMtimer and any equal to it.
  43. {
  44.     GEMfeedback result=ContinueInteraction;
  45.     now=head->mytime;
  46.     while (head->mytime <= now && result!=EndInteraction) {
  47.         result=head->Expire(e);
  48.         head->mytime+=head->interval+1; // So interval==0 doesn't lock or starve the scheduler.
  49.         GEMtimer* toinsert=head;
  50.         head=head->next;
  51.         toinsert->InsertInto(head);
  52.     }
  53.     return result;
  54. }
  55.  
  56.  
  57. GEMactivity* GEMtimer::act=0;
  58. GEMtimer* GEMtimer::head=0;
  59. int GEMtimer::now=0;
  60.  
  61. void GEMtimer::InsertInto(GEMtimer*& list)
  62. // Insert self into the given list in order of increasing mytime.
  63. {
  64.     if (list) {
  65.         if (list->mytime > mytime) {
  66.             next=list;
  67.             list=this;
  68.         } else {
  69.             InsertInto(list->next);
  70.         }
  71.     } else {
  72.         next=list;
  73.         list=this;
  74.     }
  75. }
  76.  
  77. void GEMtimer::DeleteFrom(GEMtimer*& list)
  78. // Delete self from the given list
  79. {
  80.     if (list) {
  81.         if (list==this) {
  82.             list=next;
  83.         } else {
  84.             DeleteFrom(list->next);
  85.         }
  86.     }
  87. }
  88.