- 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

@ -56,9 +56,9 @@ void ConnectionSlotThread::signalUpdate(ConnectionSlotEvent *event) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
if(event != NULL) {
triggerIdMutex.p();
MutexSafeWrapper safeMutex(&triggerIdMutex);
this->event = event;
triggerIdMutex.v();
safeMutex.ReleaseLock();
}
semTaskSignalled.signal();
@ -69,9 +69,9 @@ void ConnectionSlotThread::setTaskCompleted(ConnectionSlotEvent *event) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
if(event != NULL) {
triggerIdMutex.p();
MutexSafeWrapper safeMutex(&triggerIdMutex);
event->eventCompleted = true;
triggerIdMutex.v();
safeMutex.ReleaseLock();
}
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
@ -80,9 +80,9 @@ void ConnectionSlotThread::setTaskCompleted(ConnectionSlotEvent *event) {
bool ConnectionSlotThread::isSignalCompleted() {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
triggerIdMutex.p();
bool result = this->event->eventCompleted;
triggerIdMutex.v();
MutexSafeWrapper safeMutex(&triggerIdMutex);
bool result = (this->event != NULL ? this->event->eventCompleted : true);
safeMutex.ReleaseLock();
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);

View File

@ -645,8 +645,6 @@ bool ServerInterface::launchGame(const GameSettings* gameSettings){
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
//serverSynchAccessor.p();
for(int i= 0; i<GameConstants::maxPlayers; ++i)
{
ConnectionSlot *connectionSlot= slots[i];
@ -662,8 +660,6 @@ bool ServerInterface::launchGame(const GameSettings* gameSettings){
}
}
//serverSynchAccessor.v();
if(bOkToStart == true)
{
serverSocket.stopBroadCastThread();
@ -688,7 +684,6 @@ void ServerInterface::broadcastGameSetup(const GameSettings* gameSettings) {
void ServerInterface::broadcastMessage(const NetworkMessage* networkMessage, int excludeSlot){
//serverSynchAccessor.p();
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
try {
@ -724,8 +719,6 @@ void ServerInterface::broadcastMessage(const NetworkMessage* networkMessage, int
}
void ServerInterface::broadcastMessageToConnectedClients(const NetworkMessage* networkMessage, int excludeSlot){
//serverSynchAccessor.p();
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
for(int i= 0; i<GameConstants::maxPlayers; ++i) {
@ -741,7 +734,6 @@ void ServerInterface::broadcastMessageToConnectedClients(const NetworkMessage* n
}
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
//serverSynchAccessor.v();
}
void ServerInterface::updateListen() {
@ -751,27 +743,23 @@ void ServerInterface::updateListen() {
int openSlotCount= 0;
for(int i= 0; i<GameConstants::maxPlayers; ++i) {
serverSynchAccessor.p();
bool isSlotOpen = (slots[i] != NULL && slots[i]->isConnected() == false);
serverSynchAccessor.v();
if(isSlotOpen == true) {
++openSlotCount;
}
}
serverSynchAccessor.p();
MutexSafeWrapper safeMutex(&serverSynchAccessor);
serverSocket.listen(openSlotCount);
serverSynchAccessor.v();
safeMutex.ReleaseLock();
}
int ServerInterface::getOpenSlotCount() {
int openSlotCount= 0;
for(int i= 0; i<GameConstants::maxPlayers; ++i) {
serverSynchAccessor.p();
bool isSlotOpen = (slots[i] != NULL && slots[i]->isConnected() == false);
serverSynchAccessor.v();
if(isSlotOpen == true) {
++openSlotCount;

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
// =====================================================

View File

@ -45,9 +45,9 @@ void BaseThread::signalQuit() {
void BaseThread::setQuitStatus(bool value) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
mutexQuit.p();
MutexSafeWrapper safeMutex(&mutexQuit);
quit = value;
mutexQuit.v();
safeMutex.ReleaseLock();
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
@ -56,9 +56,9 @@ bool BaseThread::getQuitStatus() {
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
bool retval = false;
mutexQuit.p();
MutexSafeWrapper safeMutex(&mutexQuit);
retval = quit;
mutexQuit.v();
safeMutex.ReleaseLock();
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
@ -69,9 +69,9 @@ bool BaseThread::getRunningStatus() {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
bool retval = false;
mutexRunning.p();
MutexSafeWrapper safeMutex(&mutexRunning);
retval = running;
mutexRunning.v();
safeMutex.ReleaseLock();
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] running = %d\n",__FILE__,__FUNCTION__,__LINE__,retval);
@ -81,9 +81,9 @@ bool BaseThread::getRunningStatus() {
void BaseThread::setRunningStatus(bool value) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] value = %d\n",__FILE__,__FUNCTION__,__LINE__,value);
mutexRunning.p();
MutexSafeWrapper safeMutex(&mutexRunning);
running = value;
mutexRunning.v();
safeMutex.ReleaseLock();
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] running = %d\n",__FILE__,__FUNCTION__,__LINE__,value);
}

View File

@ -135,9 +135,9 @@ void SimpleTaskThread::execute() {
void SimpleTaskThread::setTaskSignalled(bool value) {
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
mutexTaskSignaller.p();
MutexSafeWrapper safeMutex(&mutexTaskSignaller);
taskSignalled = value;
mutexTaskSignaller.v();
safeMutex.ReleaseLock();
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
@ -146,9 +146,9 @@ bool SimpleTaskThread::getTaskSignalled() {
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
bool retval = false;
mutexTaskSignaller.p();
MutexSafeWrapper safeMutex(&mutexTaskSignaller);
retval = taskSignalled;
mutexTaskSignaller.v();
safeMutex.ReleaseLock();
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);