- another attempt to guard socket thread access so avoid menu hang problems

This commit is contained in:
Mark Vejvoda
2010-12-18 19:52:47 +00:00
parent 7305391b6d
commit 6a99e2db8e
4 changed files with 125 additions and 50 deletions

View File

@@ -3,9 +3,9 @@
//
// Copyright (C) 2005 Matthias Braun <matze@braunis.de>
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
@@ -14,12 +14,17 @@
#include <SDL_thread.h>
#include <SDL_mutex.h>
#include <string>
#include "leak_dumper.h"
//#define DEBUG_MUTEXES
// =====================================================
// class Thread
// =====================================================
using namespace std;
namespace Shared{ namespace Platform{
class Thread{
@@ -31,17 +36,17 @@ public:
pHigh = 3,
pRealTime = 4
};
private:
SDL_Thread* thread;
public:
Thread();
virtual ~Thread();
void start();
virtual void execute()=0;
void setPriority(Thread::Priority threadPriority);
void setPriority(Thread::Priority threadPriority);
void suspend();
void resume();
@@ -56,21 +61,25 @@ private:
class Mutex {
private:
SDL_mutex* mutex;
int refCount;
public:
Mutex();
~Mutex();
void p();
void v();
int getRefCount() const { return refCount; }
};
class MutexSafeWrapper {
protected:
Mutex *mutex;
string ownerId;
public:
MutexSafeWrapper(Mutex *mutex) {
MutexSafeWrapper(Mutex *mutex,string ownerId="") {
this->mutex = mutex;
this->ownerId = ownerId;
Lock();
}
~MutexSafeWrapper() {
@@ -79,12 +88,37 @@ public:
void Lock() {
if(this->mutex != NULL) {
#ifdef DEBUG_MUTEXES
if(ownerId != "") {
printf("Locking Mutex [%s] refCount: %d\n",ownerId.c_str(),this->mutex->getRefCount());
}
#endif
this->mutex->p();
#ifdef DEBUG_MUTEXES
if(ownerId != "") {
printf("Locked Mutex [%s] refCount: %d\n",ownerId.c_str(),this->mutex->getRefCount());
}
#endif
}
}
void ReleaseLock(bool keepMutex=false) {
if(this->mutex != NULL) {
#ifdef DEBUG_MUTEXES
if(ownerId != "") {
printf("UnLocking Mutex [%s] refCount: %d\n",ownerId.c_str(),this->mutex->getRefCount());
}
#endif
this->mutex->v();
#ifdef DEBUG_MUTEXES
if(ownerId != "") {
printf("UnLocked Mutex [%s] refCount: %d\n",ownerId.c_str(),this->mutex->getRefCount());
}
#endif
if(keepMutex == false) {
this->mutex = NULL;
}

View File

@@ -1,9 +1,9 @@
//This file is part of Glest Shared Library (www.glest.org)
//Copyright (C) 2005 Matthias Braun <matze@braunis.de>
//You can redistribute this code and/or modify it under
//the terms of the GNU General Public License as published by the Free Software
//Foundation; either version 2 of the License, or (at your option) any later
//You can redistribute this code and/or modify it under
//the terms of the GNU General Public License as published by the Free Software
//Foundation; either version 2 of the License, or (at your option) any later
//version.
#include "thread.h"
@@ -23,10 +23,10 @@ using namespace std;
#endif
namespace Shared{ namespace Platform{
namespace Shared{ namespace Platform{
// =====================================
// Threads
// Threads
// =====================================
Thread::Thread() {
thread = NULL;
@@ -74,10 +74,11 @@ void Thread::resume() {
}
// =====================================
// Mutex
// Mutex
// =====================================
Mutex::Mutex() {
refCount=0;
mutex = SDL_CreateMutex();
assert(mutex != NULL);
if(mutex == NULL) {
@@ -104,6 +105,7 @@ void Mutex::p() {
throw runtime_error(szBuf);
}
SDL_mutexP(mutex);
refCount++;
}
void Mutex::v() {
@@ -112,6 +114,7 @@ void Mutex::v() {
snprintf(szBuf,1023,"In [%s::%s Line: %d] mutex == NULL",__FILE__,__FUNCTION__,__LINE__);
throw runtime_error(szBuf);
}
refCount--;
SDL_mutexV(mutex);
}