- added safe mutex wrapper class

This commit is contained in:
Mark Vejvoda
2010-06-02 01:37:45 +00:00
parent bab0e87141
commit ace1cef8a8
6 changed files with 48 additions and 40 deletions

View File

@@ -44,25 +44,23 @@ protected:
if(accessor == cacheItemSet) {
if(value == NULL) {
try {
mutexCache.p();
MutexSafeWrapper safeMutex(&mutexCache);
if(itemCache.find(cacheKey) != itemCache.end()) {
itemCache.erase(cacheKey);
}
mutexCache.v();
safeMutex.ReleaseLock();
}
catch(const std::exception &ex) {
mutexCache.v();
throw runtime_error(ex.what());
}
}
try {
mutexCache.p();
MutexSafeWrapper safeMutex(&mutexCache);
itemCache[cacheKey] = *value;
mutexCache.v();
safeMutex.ReleaseLock();
}
catch(const std::exception &ex) {
mutexCache.v();
throw runtime_error(ex.what());
}
}

View File

@@ -51,7 +51,7 @@ private:
// class Mutex
// =====================================================
class Mutex{
class Mutex {
private:
SDL_mutex* mutex;
@@ -62,6 +62,28 @@ public:
void v();
};
class MutexSafeWrapper {
protected:
Mutex *mutex;
public:
MutexSafeWrapper(Mutex *mutex) {
this->mutex = mutex;
if(this->mutex != NULL) {
this->mutex->p();
}
}
~MutexSafeWrapper() {
ReleaseLock();
}
void ReleaseLock() {
if(this->mutex != NULL) {
this->mutex->v();
this->mutex = NULL;
}
}
};
// =====================================================
// class Semaphore
// =====================================================