diff options
author | Max Horn | 2003-09-10 12:43:54 +0000 |
---|---|---|
committer | Max Horn | 2003-09-10 12:43:54 +0000 |
commit | afe56a4aebd46d77b4c4cfbfda0bdb8533f0e318 (patch) | |
tree | 3f8e44fe4c4e3c24256bd626612fdf779b850fb8 /common | |
parent | c0e2d2be66c783a7a3c149b8accc941da894d567 (diff) | |
download | scummvm-rg350-afe56a4aebd46d77b4c4cfbfda0bdb8533f0e318.tar.gz scummvm-rg350-afe56a4aebd46d77b4c4cfbfda0bdb8533f0e318.tar.bz2 scummvm-rg350-afe56a4aebd46d77b4c4cfbfda0bdb8533f0e318.zip |
added refCon parameter to timer class
svn-id: r10156
Diffstat (limited to 'common')
-rw-r--r-- | common/engine.cpp | 2 | ||||
-rw-r--r-- | common/timer.cpp | 52 | ||||
-rw-r--r-- | common/timer.h | 11 |
3 files changed, 33 insertions, 32 deletions
diff --git a/common/engine.cpp b/common/engine.cpp index 32bb73a9ac..b26338ed0f 100644 --- a/common/engine.cpp +++ b/common/engine.cpp @@ -41,7 +41,7 @@ Engine::Engine(GameDetector *detector, OSystem *syst) g_system = _system; // FIXME - BIG HACK for MidiEmu - _timer = new Timer(this); + _timer = new Timer(_system); } Engine::~Engine() { diff --git a/common/timer.cpp b/common/timer.cpp index 8ce5b809b2..243eba2872 100644 --- a/common/timer.cpp +++ b/common/timer.cpp @@ -23,16 +23,17 @@ #include "stdafx.h" #include "common/scummsys.h" #include "common/timer.h" +#include "common/util.h" static Timer *g_timer = NULL; -Timer::Timer(Engine * engine) : - _engine(engine), +Timer::Timer(OSystem *system) : + _system(system), _mutex(0), _timerHandler(0), _lastTime(0) { - _mutex = _engine->_system->create_mutex(); + _mutex = _system->create_mutex(); g_timer = this; @@ -42,23 +43,25 @@ Timer::Timer(Engine * engine) : _timerSlots[i].counter = 0; } - _thisTime = _engine->_system->get_msecs(); + _thisTime = _system->get_msecs(); // Set the timer last, after everything has been initialised - _engine->_system->set_timer(10, &timer_handler); + _system->set_timer(10, &timer_handler); } Timer::~Timer() { - _engine->_system->set_timer(0, NULL); - - _engine->_system->lock_mutex(_mutex); - for (int i = 0; i < MAX_TIMERS; i++) { - _timerSlots[i].procedure = NULL; - _timerSlots[i].interval = 0; - _timerSlots[i].counter = 0; + _system->set_timer(0, NULL); + + { + StackLock lock(_mutex); + for (int i = 0; i < MAX_TIMERS; i++) { + _timerSlots[i].procedure = NULL; + _timerSlots[i].interval = 0; + _timerSlots[i].counter = 0; + } } - _engine->_system->unlock_mutex(_mutex); + // FIXME: There is still a potential race condition here, depending on how // the system backend implements set_timer: If timers are done using @@ -68,7 +71,7 @@ Timer::~Timer() { // it is still waiting for the _mutex. So, again depending on the backend, // we might end up unlocking the mutex then immediately deleting it, while // the timer thread is about to lock it. - _engine->_system->delete_mutex(_mutex); + _system->delete_mutex(_mutex); } int Timer::timer_handler(int t) { @@ -78,12 +81,11 @@ int Timer::timer_handler(int t) { } int Timer::handler(int t) { + StackLock lock(_mutex); uint32 interval, l; - _engine->_system->lock_mutex(_mutex); - _lastTime = _thisTime; - _thisTime = _engine->_system->get_msecs(); + _thisTime = _system->get_msecs(); interval = 1000 * (_thisTime - _lastTime); for (l = 0; l < MAX_TIMERS; l++) { @@ -91,31 +93,29 @@ int Timer::handler(int t) { _timerSlots[l].counter -= interval; if (_timerSlots[l].counter <= 0) { _timerSlots[l].counter += _timerSlots[l].interval; - _timerSlots[l].procedure(_engine); + _timerSlots[l].procedure(_timerSlots[l].refCon); } } } - _engine->_system->unlock_mutex(_mutex); - return t; } -bool Timer::installProcedure (TimerProc procedure, int32 interval) { +bool Timer::installProcedure(TimerProc procedure, int32 interval, void *refCon) { + StackLock lock(_mutex); int32 l; bool found = false; - _engine->_system->lock_mutex(_mutex); for (l = 0; l < MAX_TIMERS; l++) { if (!_timerSlots[l].procedure) { _timerSlots[l].procedure = procedure; _timerSlots[l].interval = interval; _timerSlots[l].counter = interval; + _timerSlots[l].refCon = refCon; found = true; break; } } - _engine->_system->unlock_mutex(_mutex); if (!found) warning("Couldn't find free timer slot!"); @@ -123,18 +123,18 @@ bool Timer::installProcedure (TimerProc procedure, int32 interval) { return found; } -void Timer::releaseProcedure (TimerProc procedure) { +void Timer::releaseProcedure(TimerProc procedure) { + StackLock lock(_mutex); int32 l; - _engine->_system->lock_mutex(_mutex); for (l = 0; l < MAX_TIMERS; l++) { if (_timerSlots[l].procedure == procedure) { _timerSlots[l].procedure = 0; _timerSlots[l].interval = 0; _timerSlots[l].counter = 0; + _timerSlots[l].refCon = 0; } } - _engine->_system->unlock_mutex(_mutex); } #endif diff --git a/common/timer.h b/common/timer.h index 7d194e47d9..8299b193f7 100644 --- a/common/timer.h +++ b/common/timer.h @@ -22,11 +22,11 @@ #define COMMON_TIMER_H #include "common/scummsys.h" -#include "common/engine.h" +#include "common/system.h" #define MAX_TIMERS 5 -typedef void (*TimerProc)(void *); +typedef void (*TimerProc)(void *refCon); #ifdef __MORPHOS__ #include "morphos_timer.h" @@ -35,7 +35,7 @@ typedef void (*TimerProc)(void *); class Timer { private: - Engine *_engine; + OSystem *_system; OSystem::MutexRef _mutex; void *_timerHandler; int32 _thisTime; @@ -45,13 +45,14 @@ private: TimerProc procedure; int32 interval; int32 counter; + void *refCon; } _timerSlots[MAX_TIMERS]; public: - Timer(Engine *engine); + Timer(OSystem *system); ~Timer(); - bool installProcedure(TimerProc procedure, int32 interval); + bool installProcedure(TimerProc procedure, int32 interval, void *refCon); void releaseProcedure(TimerProc procedure); protected: |