Changed: CSynchronized and CMutex are now, by default, Fair on Windows and Unfair on GNU/Linux and Mac OS X

This commit is contained in:
vl 2010-07-08 17:36:41 +02:00
parent 63ddba1cfe
commit a649c77979
9 changed files with 35 additions and 34 deletions

View file

@ -56,7 +56,7 @@ using NLMISC::CSmartPtr;
using NLMISC::CRGBA; using NLMISC::CRGBA;
using NLMISC::CVector; using NLMISC::CVector;
using NLMISC::CMatrix; using NLMISC::CMatrix;
using NLMISC::CUnfairSynchronized; using NLMISC::CSynchronized;
class CMaterial; class CMaterial;
@ -144,7 +144,7 @@ public:
protected: protected:
CUnfairSynchronized<TTexDrvInfoPtrMap> _SyncTexDrvInfos; CSynchronized<TTexDrvInfoPtrMap> _SyncTexDrvInfos;
TTexDrvSharePtrList _TexDrvShares; TTexDrvSharePtrList _TexDrvShares;
TMatDrvInfoPtrList _MatDrvInfos; TMatDrvInfoPtrList _MatDrvInfos;

View file

@ -48,9 +48,16 @@ namespace NLMISC {
*/ */
#define STORE_MUTEX_NAME #define STORE_MUTEX_NAME
#ifdef NL_OS_WINDOWS
// By default on Windows, all mutex/synchronization use the CFair* class to avoid freeze problem.
# define CMutex CFairMutex
# define CSynchronized CFairSynchronized
#else
// On GNU/Linux and Mac, we use CUnfair* everwise it creates some strange deadlock during loading and after
# define CMutex CUnfairMutex
# define CSynchronized CUnfairSynchronized
#endif
// By default, all mutex use the CFairMutex class to avoid freeze problem.
#define CMutex CFairMutex
/** /**
* Classic mutex implementation (not necessarly fair) * Classic mutex implementation (not necessarly fair)
@ -572,12 +579,6 @@ extern uint32 NbMutexes;
#endif // MUTEX_DEBUG #endif // MUTEX_DEBUG
// By default, all synchronization use the CFairSynchronized class to avoid freeze problem.
#define CSynchronized CFairSynchronized
/** /**
* This class ensure that the Value is accessed by only one thread. First you have to create a CSynchronized class with you type. * This class ensure that the Value is accessed by only one thread. First you have to create a CSynchronized class with you type.
* Then, if a thread want to modify or do anything on it, you create a CAccessor in a \b sub \b scope. You can modify the value * Then, if a thread want to modify or do anything on it, you create a CAccessor in a \b sub \b scope. You can modify the value
@ -618,13 +619,13 @@ public:
CAccessor(CUnfairSynchronized<T> *cs) CAccessor(CUnfairSynchronized<T> *cs)
{ {
Synchronized = cs; Synchronized = cs;
const_cast<CUnfairMutex&>(Synchronized->_Mutex).enter(); const_cast<CMutex&>(Synchronized->_Mutex).enter();
} }
/// release the mutex /// release the mutex
~CAccessor() ~CAccessor()
{ {
const_cast<CUnfairMutex&>(Synchronized->_Mutex).leave(); const_cast<CMutex&>(Synchronized->_Mutex).leave();
} }
/// access to the Value /// access to the Value

View file

@ -140,7 +140,7 @@ protected:
/// queue of tasks, using list container instead of queue for DeleteTask methode /// queue of tasks, using list container instead of queue for DeleteTask methode
CSynchronized<std::string> _RunningTask; CSynchronized<std::string> _RunningTask;
CUnfairSynchronized<std::list<CWaitingTask> > _TaskQueue; CSynchronized<std::list<CWaitingTask> > _TaskQueue;
CSynchronized<std::deque<std::string> > _DoneTaskQueue; CSynchronized<std::deque<std::string> > _DoneTaskQueue;
/// thread pointer /// thread pointer

View file

@ -50,7 +50,7 @@ IDriver::~IDriver()
// Must clean up everything before closing driver. // Must clean up everything before closing driver.
// Must doing this in release(), so assert here if not done... // Must doing this in release(), so assert here if not done...
{ {
CUnfairSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos); CSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos);
TTexDrvInfoPtrMap &rTexDrvInfos = access.value(); TTexDrvInfoPtrMap &rTexDrvInfos = access.value();
nlassert( rTexDrvInfos.size() == 0 ); nlassert( rTexDrvInfos.size() == 0 );
} }
@ -81,7 +81,7 @@ bool IDriver::release(void)
// Release refptr of TextureDrvInfos. Should be all null (because of precedent pass). // Release refptr of TextureDrvInfos. Should be all null (because of precedent pass).
{ {
CUnfairSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos); CSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos);
TTexDrvInfoPtrMap &rTexDrvInfos = access.value(); TTexDrvInfoPtrMap &rTexDrvInfos = access.value();
// must be empty, because precedent pass should have deleted all. // must be empty, because precedent pass should have deleted all.
@ -234,7 +234,7 @@ void IDriver::removeIBDrvInfoPtr(ItIBDrvInfoPtrList ibDrvInfoIt)
// *************************************************************************** // ***************************************************************************
void IDriver::removeTextureDrvInfoPtr(ItTexDrvInfoPtrMap texDrvInfoIt) void IDriver::removeTextureDrvInfoPtr(ItTexDrvInfoPtrMap texDrvInfoIt)
{ {
CUnfairSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos); CSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos);
TTexDrvInfoPtrMap &rTexDrvInfos = access.value(); TTexDrvInfoPtrMap &rTexDrvInfos = access.value();
rTexDrvInfos.erase(texDrvInfoIt); rTexDrvInfos.erase(texDrvInfoIt);
@ -268,7 +268,7 @@ bool IDriver::invalidateShareTexture (ITexture &texture)
getTextureShareName (texture, name); getTextureShareName (texture, name);
// Look for the driver info for this share name // Look for the driver info for this share name
CUnfairSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos); CSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos);
TTexDrvInfoPtrMap &rTexDrvInfos = access.value(); TTexDrvInfoPtrMap &rTexDrvInfos = access.value();
TTexDrvInfoPtrMap::iterator iteDrvInfo = rTexDrvInfos.find (name); TTexDrvInfoPtrMap::iterator iteDrvInfo = rTexDrvInfos.find (name);
if (iteDrvInfo != rTexDrvInfos.end()) if (iteDrvInfo != rTexDrvInfos.end())

View file

@ -549,7 +549,7 @@ bool CDriverD3D::setupTextureEx (ITexture& tex, bool bUpload, bool &bAllUploaded
// insert or get the texture. // insert or get the texture.
{ {
CUnfairSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos); CSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos);
TTexDrvInfoPtrMap &rTexDrvInfos = access.value(); TTexDrvInfoPtrMap &rTexDrvInfos = access.value();
ItTexDrvInfoPtrMap itTex; ItTexDrvInfoPtrMap itTex;
@ -1013,7 +1013,7 @@ bool CDriverD3D::isTextureExist(const ITexture&tex)
getTextureShareName (tex, name); getTextureShareName (tex, name);
{ {
CUnfairSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos); CSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos);
TTexDrvInfoPtrMap &rTexDrvInfos = access.value(); TTexDrvInfoPtrMap &rTexDrvInfos = access.value();
result = (rTexDrvInfos.find(name) != rTexDrvInfos.end()); result = (rTexDrvInfos.find(name) != rTexDrvInfos.end());
} }

View file

@ -727,7 +727,7 @@ bool CDriverGL::isTextureExist(const ITexture&tex)
getTextureShareName (tex, name); getTextureShareName (tex, name);
{ {
CUnfairSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos); CSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos);
TTexDrvInfoPtrMap &rTexDrvInfos = access.value(); TTexDrvInfoPtrMap &rTexDrvInfos = access.value();
result = (rTexDrvInfos.find(name) != rTexDrvInfos.end()); result = (rTexDrvInfos.find(name) != rTexDrvInfos.end());
} }

View file

@ -653,7 +653,7 @@ bool CDriverGL::setupTextureEx (ITexture& tex, bool bUpload, bool &bAllUploaded,
// insert or get the texture. // insert or get the texture.
{ {
CUnfairSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos); CSynchronized<TTexDrvInfoPtrMap>::CAccessor access(&_SyncTexDrvInfos);
TTexDrvInfoPtrMap &rTexDrvInfos = access.value(); TTexDrvInfoPtrMap &rTexDrvInfos = access.value();
ItTexDrvInfoPtrMap itTex; ItTexDrvInfoPtrMap itTex;

View file

@ -67,7 +67,7 @@ void CAsyncFileManager::addLoadTask(IRunnable *ploadTask)
bool CAsyncFileManager::cancelLoadTask(const CAsyncFileManager::ICancelCallback &callback) bool CAsyncFileManager::cancelLoadTask(const CAsyncFileManager::ICancelCallback &callback)
{ {
CUnfairSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue); CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
list<CWaitingTask> &rTaskQueue = acces.value (); list<CWaitingTask> &rTaskQueue = acces.value ();
list<CWaitingTask>::iterator it = rTaskQueue.begin(); list<CWaitingTask>::iterator it = rTaskQueue.begin();
@ -87,8 +87,8 @@ bool CAsyncFileManager::cancelLoadTask(const CAsyncFileManager::ICancelCallback
} }
// If not found, the current running task may be the one we want to cancel. Must wait it. // If not found, the current running task may be the one we want to cancel. Must wait it.
// Beware that this code works because of the CUnfairSynchronized access we made above (ensure that the // Beware that this code works because of the CSynchronized access we made above (ensure that the
// taskmanager will end just the current task async (if any) and won't start another one. // taskmanager will end just the current task async (if any) and won't start an other one.
waitCurrentTaskToComplete (); waitCurrentTaskToComplete ();
return false; return false;
@ -105,7 +105,7 @@ void CAsyncFileManager::loadMesh(const std::string& meshName, IShape **ppShp, ID
/* /*
bool CAsyncFileManager::cancelLoadMesh(const std::string& sMeshName) bool CAsyncFileManager::cancelLoadMesh(const std::string& sMeshName)
{ {
CUnfairSynchronized<list<IRunnable *> >::CAccessor acces(&_TaskQueue); CSynchronized<list<IRunnable *> >::CAccessor acces(&_TaskQueue);
list<IRunnable*> &rTaskQueue = acces.value (); list<IRunnable*> &rTaskQueue = acces.value ();
list<IRunnable*>::iterator it = rTaskQueue.begin(); list<IRunnable*>::iterator it = rTaskQueue.begin();
@ -167,7 +167,7 @@ void CAsyncFileManager::signal (bool *pSgn)
void CAsyncFileManager::cancelSignal (bool *pSgn) void CAsyncFileManager::cancelSignal (bool *pSgn)
{ {
CUnfairSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue); CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
list<CWaitingTask> &rTaskQueue = acces.value (); list<CWaitingTask> &rTaskQueue = acces.value ();
list<CWaitingTask>::iterator it = rTaskQueue.begin(); list<CWaitingTask>::iterator it = rTaskQueue.begin();

View file

@ -49,7 +49,7 @@ CTaskManager::~CTaskManager()
nlSleep(10); nlSleep(10);
// There should be no remaining Tasks // There should be no remaining Tasks
CUnfairSynchronized<std::list<CWaitingTask> >::CAccessor acces(&_TaskQueue); CSynchronized<std::list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
nlassert(acces.value().empty()); nlassert(acces.value().empty());
_Thread->wait(); _Thread->wait();
delete _Thread; delete _Thread;
@ -65,7 +65,7 @@ void CTaskManager::run(void)
while(_ThreadRunning) while(_ThreadRunning)
{ {
{ {
CUnfairSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue); CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
if(acces.value().empty()) if(acces.value().empty())
{ {
runnableTask = NULL; runnableTask = NULL;
@ -126,14 +126,14 @@ void CTaskManager::run(void)
// Add a task to TaskManager // Add a task to TaskManager
void CTaskManager::addTask(IRunnable *r, float priority) void CTaskManager::addTask(IRunnable *r, float priority)
{ {
CUnfairSynchronized<std::list<CWaitingTask> >::CAccessor acces(&_TaskQueue); CSynchronized<std::list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
acces.value().push_back(CWaitingTask(r, priority)); acces.value().push_back(CWaitingTask(r, priority));
} }
/// Delete a task, only if task is not running, return true if found and deleted /// Delete a task, only if task is not running, return true if found and deleted
bool CTaskManager::deleteTask(IRunnable *r) bool CTaskManager::deleteTask(IRunnable *r)
{ {
CUnfairSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue); CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
for(list<CWaitingTask>::iterator it = acces.value().begin(); it != acces.value().end(); it++) for(list<CWaitingTask>::iterator it = acces.value().begin(); it != acces.value().end(); it++)
{ {
if(it->Task == r) if(it->Task == r)
@ -148,7 +148,7 @@ bool CTaskManager::deleteTask(IRunnable *r)
/// Task list size /// Task list size
uint CTaskManager::taskListSize(void) uint CTaskManager::taskListSize(void)
{ {
CUnfairSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue); CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
return (uint)acces.value().size(); return (uint)acces.value().size();
} }
@ -164,7 +164,7 @@ void CTaskManager::waitCurrentTaskToComplete ()
void CTaskManager::dump (std::vector<std::string> &result) void CTaskManager::dump (std::vector<std::string> &result)
{ {
CSynchronized<string>::CAccessor accesCurrent(&_RunningTask); CSynchronized<string>::CAccessor accesCurrent(&_RunningTask);
CUnfairSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue); CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
CSynchronized<deque<string> >::CAccessor accesDone(&_DoneTaskQueue); CSynchronized<deque<string> >::CAccessor accesDone(&_DoneTaskQueue);
const list<CWaitingTask> &taskList = acces.value(); const list<CWaitingTask> &taskList = acces.value();
@ -215,7 +215,7 @@ void CTaskManager::clearDump()
uint CTaskManager::getNumWaitingTasks() uint CTaskManager::getNumWaitingTasks()
{ {
CUnfairSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue); CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
return (uint)acces.value().size(); return (uint)acces.value().size();
} }
@ -225,7 +225,7 @@ void CTaskManager::changeTaskPriority ()
{ {
if (_ChangePriorityCallback) if (_ChangePriorityCallback)
{ {
CUnfairSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue); CSynchronized<list<CWaitingTask> >::CAccessor acces(&_TaskQueue);
list<CWaitingTask> &taskList = acces.value(); list<CWaitingTask> &taskList = acces.value();
list<CWaitingTask>::iterator ite = taskList.begin(); list<CWaitingTask>::iterator ite = taskList.begin();