diff --git a/common/system.cpp b/common/system.cpp index ef41f7c234e..ce9ba67c3cd 100644 --- a/common/system.cpp +++ b/common/system.cpp @@ -83,3 +83,37 @@ bool OSystem::setGraphicsMode(const char *name) { return false; } + +#pragma mark - + + +namespace Common { + +StackLock::StackLock(OSystem::MutexRef mutex, OSystem *syst, const char *mutexName) + : _mutex(mutex), _syst(syst), _mutexName(mutexName) { + if (syst == 0) + _syst = g_system; + lock(); +} + +StackLock::~StackLock() { + unlock(); +} + +void StackLock::lock() { + assert(_syst); + if (_mutexName != NULL) + debug(6, "Locking mutex %s", _mutexName); + + _syst->lockMutex(_mutex); +} + +void StackLock::unlock() { + assert(_syst); + if (_mutexName != NULL) + debug(6, "Unlocking mutex %s", _mutexName); + + _syst->unlockMutex(_mutex); +} + +} // End of namespace Common diff --git a/common/system.h b/common/system.h index 3afe5196173..3ddf4982d70 100644 --- a/common/system.h +++ b/common/system.h @@ -25,6 +25,8 @@ #include "common/scummsys.h" #include "common/savefile.h" +#include "common/util.h" +#include "common/rect.h" /** @@ -479,7 +481,19 @@ public: - /** @name Mutex handling */ + /** + * @name Mutex handling + * Historically, the OSystem API used to have a method which allowed + * creating threads. Hence mutex support was needed for thread syncing. + * To ease portability, though, we decided to remove the threading API. + * Instead, we now use timers (see setTimerCallback() and Common::Timer). + * But since those may be implemented using threads (and in fact, that's + * how our primary backend, the SDL one, does it on many systems), we + * still have to do mutex syncing in our timer callbacks. + * + * Hence backends which do not use threads to implement the timers simply + * can use dummy implementations for these methods. + */ //@{ typedef struct Mutex *MutexRef; @@ -570,5 +584,24 @@ public: /** The global OSystem instance. Inited in main(). */ #define g_system (OSystem::instance()) +namespace Common { + +/** + * Auxillary class to (un)lock a mutex on the stack. + */ +class StackLock { + OSystem::MutexRef _mutex; + OSystem *_syst; + const char *_mutexName; + + void lock(); + void unlock(); +public: + StackLock(OSystem::MutexRef mutex, OSystem *syst = 0, const char *mutexName = NULL); + ~StackLock(); +}; + +} // End of namespace Common + #endif diff --git a/common/util.cpp b/common/util.cpp index 811140618ef..bb7c4ed9bab 100644 --- a/common/util.cpp +++ b/common/util.cpp @@ -100,36 +100,6 @@ uint RandomSource::getRandomNumberRng(uint min, uint max) { return getRandomNumber(max - min) + min; } -#pragma mark - - - -StackLock::StackLock(OSystem::MutexRef mutex, OSystem *syst, const char *mutexName) - : _mutex(mutex), _syst(syst), _mutexName(mutexName) { - if (syst == 0) - _syst = g_system; - lock(); -} - -StackLock::~StackLock() { - unlock(); -} - -void StackLock::lock() { - assert(_syst); - if (_mutexName != NULL) - debug(6, "Locking mutex %s", _mutexName); - - _syst->lockMutex(_mutex); -} - -void StackLock::unlock() { - assert(_syst); - if (_mutexName != NULL) - debug(6, "Unlocking mutex %s", _mutexName); - - _syst->unlockMutex(_mutex); -} - #pragma mark - diff --git a/common/util.h b/common/util.h index b602f3ef852..68c47a1fb50 100644 --- a/common/util.h +++ b/common/util.h @@ -22,7 +22,6 @@ #define COMMON_UTIL_H #include "common/scummsys.h" -#include "common/system.h" template inline T ABS (T x) { return (x>=0) ? x : -x; } template inline T MIN (T a, T b) { return (a