![]() |
Shadowrun: Awakened 29 September 2011 - Build 871
|
#include <TimedEventManager.h>
Inheritance diagram for Support::TimedEventManager:Classes | |
| class | TimedTask |
| class | TimerManager |
| struct | TimerTracker |
Public Member Functions | |
| void | addTimedEvent (double periodInSeconds, ThreadTaskBase *task, bool recurring=false) |
| TimedEventManager (size_t threadCount) | |
| ~TimedEventManager () | |
Protected Member Functions | |
| void | checkTimers () |
Protected Attributes | |
| ObjectPool< TimedTask > | _taskPool |
| boost::mutex | _timerMutex |
| std::list< TimerTracker > | _timers |
| friend | TimedTask |
| friend | TimerManager |
This class allows for many timed events to be fired concurrently Underneath, this utilizes a ThreadPool, so many timed events may be called at the same time
Definition at line 22 of file TimedEventManager.h.
| Support::TimedEventManager::TimedEventManager | ( | size_t | threadCount | ) |
Constructor
Definition at line 108 of file TimedEventManager.cpp.
References Support::ThreadPool::createWorkerThread(), Support::ThreadPool::queueTask(), and TimerManager.
: ThreadPool(threadCount), _taskPool((unsigned int)threadCount) { //we reserve one worker to carry out timer management createWorkerThread(); queueTask(new TimerManager(this)); }
| Support::TimedEventManager::~TimedEventManager | ( | ) |
Destructor
Definition at line 118 of file TimedEventManager.cpp.
References Support::ThreadPool::stop().
{
stop();
}
| void Support::TimedEventManager::addTimedEvent | ( | double | periodInSeconds, |
| ThreadTaskBase * | task, | ||
| bool | recurring = false |
||
| ) |
Sets a time
Definition at line 126 of file TimedEventManager.cpp.
References _timerMutex, _timers, and Support::TimedEventManager::TimerTracker::setRecurring().
{
TimerTracker tracker(periodInSeconds, task);
tracker.setRecurring(recurring);
//lock when adding timer
boost::lock_guard<boost::mutex> guard(_timerMutex);
_timers.push_back(tracker);
}
| void Support::TimedEventManager::checkTimers | ( | ) | [protected] |
Checks the timer contained within the event manager
Definition at line 78 of file TimedEventManager.cpp.
References Support::TimedEventManager::TimedTask::_manager, _taskPool, _timerMutex, _timers, Support::TimedEventManager::TimedTask::_tracker, ObjectPool< ClassType >::getInstance(), and Support::ThreadPool::queueTask().
{
//perform this check while locking on the timer mutex
boost::lock_guard<boost::mutex> guard(_timerMutex);
//iterate through the list of timers, checking if they have elapsed
std::list<TimerTracker>::iterator it=_timers.begin();
while(it != _timers.end())
{
//act on the current item only, save the next item in the loop iter
std::list<TimerTracker>::iterator currentItem = it++;
//only act if this is elapsed
if(currentItem->isTimerElapsed())
{
//get a task from the pool, set it, and queue it
TimedTask* task = _taskPool.getInstance();
task->_tracker = *currentItem;
task->_manager = this;
queueTask(task);
//the queued item will carry things out, from here we either renew it or let it go
if(currentItem->isRecurring())
currentItem->reset();
else
_timers.remove(*currentItem);
}
}
}
ObjectPool<TimedTask> Support::TimedEventManager::_taskPool [protected] |
A pool of tasks that are sent into the thread pool, then reclaimed This is to improve performance
Definition at line 120 of file TimedEventManager.h.
Referenced by checkTimers().
boost::mutex Support::TimedEventManager::_timerMutex [mutable, protected] |
Provides locking for functions in instances of this class Use: boost::lock_guard<boost::mutex> guard(_timerMutex);
Definition at line 126 of file TimedEventManager.h.
Referenced by addTimedEvent(), and checkTimers().
std::list<TimerTracker> Support::TimedEventManager::_timers [protected] |
This list of timed events held in this managed
Definition at line 114 of file TimedEventManager.h.
Referenced by addTimedEvent(), and checkTimers().
friend Support::TimedEventManager::TimedTask [protected] |
Definition at line 93 of file TimedEventManager.h.
friend Support::TimedEventManager::TimerManager [protected] |
Definition at line 109 of file TimedEventManager.h.
Referenced by TimedEventManager().
Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.