mirror of
https://github.com/glest/glest-source.git
synced 2025-08-15 21:04:00 +02:00
- added safe mutex wrapper class
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
// =====================================================
|
||||
|
Reference in New Issue
Block a user