00001 // EPOS-- Alarm Abstraction Declarations 00002 00003 // This work is licensed under the Creative Commons 00004 // Attribution-NonCommercial-NoDerivs License. To view a copy of this license, 00005 // visit http://creativecommons.org/licenses/by-nc-nd/2.0/ or send a letter to 00006 // Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. 00007 00008 #ifndef __alarm_h 00009 #define __alarm_h 00010 00011 #include <system/config.h> 00012 #include <utility/queue.h> 00013 #include <tsc.h> 00014 #include <rtc.h> 00015 #include <timer.h> 00016 #include <semaphore.h> 00017 00018 __BEGIN_SYS 00019 00024 class Alarm { 00025 public: 00029 typedef void (*Handler)(); 00030 private: 00031 static const Type_Id TYPE = Type<Alarm>::TYPE; 00032 00036 static const int FREQUENCY = __SYS(Traits)<Timer>::FREQUENCY; 00037 00041 static const bool ATOMIC = true; 00042 00043 typedef Relative_Queue<Alarm, List_Element_Rank, ATOMIC> AlarmQueue; 00044 00048 typedef Queue<Handler, ATOMIC> HandlerQueue; 00049 00050 public: 00051 // Infinite times (for alarms) 00052 enum { INFINITE = 0 }; 00053 00054 public: 00058 static const int SINGLE_TIME = 1; 00059 00066 Alarm(const RTC::Microseconds & time, int times, Handler handler); 00067 00071 Alarm(const RTC::Microseconds& time, int times); 00072 00076 ~Alarm(); 00077 00082 void wait(); 00083 00087 static TSC::Hertz frequency() { return _timer.frequency(); } 00088 00093 static void delay(const RTC::Microseconds& time); 00094 00099 static int init(System_Info* si); 00100 00104 static int alarmLoop(); 00105 00109 static RTC::Microseconds period() { return 1000000 / frequency(); } 00110 00111 private: 00112 friend class Scheduler; 00113 00119 static void master(const RTC::Microseconds& time, const Handler& handler); 00120 00124 static void timerHandler(void); 00125 00126 private: 00130 Timer::Tick _ticks; 00134 int _times; 00138 AlarmQueue::Element _link; 00139 00143 Semaphore _sem; 00144 00148 Handler _handler; 00152 HandlerQueue::Element _handlerLink; 00153 00157 static Timer _timer; 00158 00162 static volatile Timer::Tick _elapsed; 00163 00167 static Handler _master; 00168 00172 static Timer::Tick _masterTicks; 00173 00177 static AlarmQueue _requests; 00178 00182 static HandlerQueue _handlerQueue; 00183 00187 static Thread *_alarmThread; 00188 }; 00189 00190 __END_SYS 00191 00192 #endif