using namespace CEC;
-CMutex::CMutex(void)
+CMutex::CMutex(bool bRecursive /* = true */)
{
- pthread_mutex_init(&m_mutex, NULL);
+ pthread_mutex_init(&m_mutex, bRecursive ? GetMutexAttribute() : NULL);
}
CMutex::~CMutex(void)
pthread_mutex_unlock(&m_mutex);
}
+static pthread_mutexattr_t g_mutexAttr;
+pthread_mutexattr_t *CMutex::GetMutexAttribute()
+{
+ static bool bAttributeInitialised = false;
+ if (!bAttributeInitialised)
+ {
+ pthread_mutexattr_init(&g_mutexAttr);
+ pthread_mutexattr_settype(&g_mutexAttr, PTHREAD_MUTEX_RECURSIVE);
+ bAttributeInitialised = true;
+ }
+ return &g_mutexAttr;
+}
+
CLockObject::CLockObject(CMutex *mutex, bool bTryLock /* = false */) :
m_mutex(mutex)
{
if (thread)
{
+ CLockObject lock(&thread->m_threadMutex);
thread->m_bRunning = true;
+ lock.Leave();
thread->m_threadCondition.Broadcast();
+
retVal = thread->Process();
+
+ lock.Lock();
thread->m_bRunning = false;
+ lock.Leave();
+ thread->m_threadCondition.Broadcast();
}
return retVal;