Shadowrun: Awakened 29 September 2011 - Build 871
TimedEventManager.h
Go to the documentation of this file.
00001 #ifndef __TIMEDEVENTMANAGER_H
00002 #define __TIMEDEVENTMANAGER_H
00003 
00004 #include <boost/timer.hpp>
00005 #include <boost/thread/thread.hpp>
00006 #include <boost/pool/object_pool.hpp>
00007 
00008 #include "CodeGenMacros.h"
00009 #include "ObjectPool.h"
00010 #include "ThreadPool.h"
00011 #include "Timer.h"
00012 
00013 namespace Support
00014 {
00015     //sets the amount of sleep timer the checking threading spends
00016     const boost::xtime::xtime_sec_t TimedEventManagerGranularityInSeconds = (boost::xtime::xtime_sec_t)0.01f;
00017 
00022     class TimedEventManager : ThreadPool
00023     {
00024     protected:
00029         struct TimerTracker
00030         {
00031         protected:
00032             ThreadTaskBase* _task;
00033             Timer _timer;
00034             bool _recurring;
00035 
00036         public:
00037             TimerTracker();
00038             TimerTracker(double periodInSeconds, ThreadTaskBase* task);
00039 
00040             bool operator==(const TimerTracker& rhs);
00041 
00045             INLINE bool isTimerElapsed()
00046             {
00047                 return _timer.isTargetTimeElapsed();
00048             }
00049 
00050             INLINE GET(isRecurring, bool, _recurring)
00051             INLINE SET(setRecurring, bool, _recurring)
00052 
00056             INLINE void reset()
00057             {
00058                 _timer.restart();
00059             }
00060 
00065             INLINE bool execute()
00066             {
00067                 _task->execute();
00068                 if(_recurring)
00069                 {
00070                     _timer.restart();
00071                     return false;
00072                 }
00073                 else
00074                     return true;
00075             }
00076         };
00077 
00082         class TimedTask : public ThreadPoolTaskBase
00083         {
00084         public:
00085             TimerTracker _tracker;
00086             TimedEventManager* _manager;
00087 
00088             TimedTask();
00089 
00090             virtual void execute();
00091         };
00092 
00093         friend TimedTask;
00094 
00099         class TimerManager : public ThreadPoolTaskBase
00100         {
00101             TimedEventManager* _manager;
00102             boost::xtime _timerGrain;
00103 
00104         public:
00105             TimerManager(TimedEventManager* manager);
00106             virtual void execute();
00107         };
00108 
00109         friend TimerManager;
00110 
00114         std::list<TimerTracker> _timers;
00115 
00120         ObjectPool<TimedTask> _taskPool;
00121 
00126         mutable boost::mutex _timerMutex;
00127 
00128         void checkTimers();
00129 
00130     public:
00131         TimedEventManager(size_t threadCount);
00132         ~TimedEventManager();
00133 
00134         void addTimedEvent(double periodInSeconds, ThreadTaskBase* task, bool recurring = false);
00135     };
00136 }
00137 
00138 #endif

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net