Main Page | Alphabetical List | Class List | File List | Class Members | File Members

timer.cc

Go to the documentation of this file.
00001 // timer.cc 00002 // Routines to emulate a hardware timer device. 00003 // 00004 // A hardware timer generates a CPU interrupt every X milliseconds. 00005 // This means it can be used for implementing time-slicing. 00006 // 00007 // We emulate a hardware timer by scheduling an interrupt to occur 00008 // every time stats->totalTicks has increased by TimerTicks. 00009 // 00010 // In order to introduce some randomness into time-slicing, if "doRandom" 00011 // is set, then the interrupt is comes after a random number of ticks. 00012 // 00013 // Remember -- nothing in here is part of Nachos. It is just 00014 // an emulation for the hardware that Nachos is running on top of. 00015 // 00016 // DO NOT CHANGE -- part of the machine emulation 00017 // 00018 // Copyright (c) 1992-1993 The Regents of the University of California. 00019 // All rights reserved. See copyright.h for copyright notice and limitation 00020 // of liability and disclaimer of warranty provisions. 00021 00022 #include "copyright.h" 00023 #include "timer.h" 00024 #include "system.h" 00025 00026 // dummy function because C++ does not allow pointers to member functions 00027 static void TimerHandler(int arg) 00028 { Timer *p = (Timer *)arg; p->TimerExpired(); } 00029 00030 //---------------------------------------------------------------------- 00031 // Timer::Timer 00032 // Initialize a hardware timer device. Save the place to call 00033 // on each interrupt, and then arrange for the timer to start 00034 // generating interrupts. 00035 // 00036 // "timerHandler" is the interrupt handler for the timer device. 00037 // It is called with interrupts disabled every time the 00038 // the timer expires. 00039 // "callArg" is the parameter to be passed to the interrupt handler. 00040 // "doRandom" -- if true, arrange for the interrupts to occur 00041 // at random, instead of fixed, intervals. 00042 //---------------------------------------------------------------------- 00043 00044 Timer::Timer(VoidFunctionPtr timerHandler, int callArg, bool doRandom) 00045 { 00046 randomize = doRandom; 00047 handler = timerHandler; 00048 arg = callArg; 00049 00050 // schedule the first interrupt from the timer device 00051 interrupt->Schedule(TimerHandler, (int) this, TimeOfNextInterrupt(), 00052 TimerInt); 00053 } 00054 00055 //---------------------------------------------------------------------- 00056 // Timer::TimerExpired 00057 // Routine to simulate the interrupt generated by the hardware 00058 // timer device. Schedule the next interrupt, and invoke the 00059 // interrupt handler. 00060 //---------------------------------------------------------------------- 00061 void 00062 Timer::TimerExpired() 00063 { 00064 // schedule the next timer device interrupt 00065 interrupt->Schedule(TimerHandler, (int) this, TimeOfNextInterrupt(), 00066 TimerInt); 00067 00068 // invoke the Nachos interrupt handler for this device 00069 (*handler)(arg); 00070 } 00071 00072 //---------------------------------------------------------------------- 00073 // Timer::TimeOfNextInterrupt 00074 // Return when the hardware timer device will next cause an interrupt. 00075 // If randomize is turned on, make it a (pseudo-)random delay. 00076 //---------------------------------------------------------------------- 00077 00078 int 00079 Timer::TimeOfNextInterrupt() 00080 { 00081 if (randomize) 00082 return 1 + (Random() % (TimerTicks * 2)); 00083 else 00084 return TimerTicks; 00085 }

Generated on Thu Sep 16 12:33:46 2004 for NachOS by doxygen 1.3.8