repositories
/
deb_libcec.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
cec: don't init the default handler before the physical address is known
[deb_libcec.git]
/
src
/
lib
/
platform
/
threads.cpp
diff --git
a/src/lib/platform/threads.cpp
b/src/lib/platform/threads.cpp
index 08a4c3b8884c39e9ce775212e8d5665a78175316..8ba391920feb2695326b52ca6e7467f033771dd1 100644
(file)
--- a/
src/lib/platform/threads.cpp
+++ b/
src/lib/platform/threads.cpp
@@
-60,11
+60,11
@@
void CMutex::Unlock(void)
pthread_mutex_unlock(&m_mutex);
}
pthread_mutex_unlock(&m_mutex);
}
-CLockObject::CLockObject(CMutex *mutex) :
+CLockObject::CLockObject(CMutex *mutex
, bool bTryLock /* = false */
) :
m_mutex(mutex)
{
if (m_mutex)
m_mutex(mutex)
{
if (m_mutex)
- m_mutex->Lock();
+ m_
bLocked = bTryLock ? m_mutex->TryLock() : m_
mutex->Lock();
}
CLockObject::~CLockObject(void)
}
CLockObject::~CLockObject(void)
@@
-75,14
+75,17
@@
CLockObject::~CLockObject(void)
void CLockObject::Leave(void)
{
void CLockObject::Leave(void)
{
- if (m_mutex)
+ if (m_mutex && m_bLocked)
+ {
+ m_bLocked = false;
m_mutex->Unlock();
m_mutex->Unlock();
+ }
}
void CLockObject::Lock(void)
{
if (m_mutex)
}
void CLockObject::Lock(void)
{
if (m_mutex)
- m_mutex->Lock();
+ m_
bLocked = m_
mutex->Lock();
}
CCondition::CCondition(void)
}
CCondition::CCondition(void)
@@
-106,27
+109,32
@@
void CCondition::Signal(void)
pthread_cond_signal(&m_cond);
}
pthread_cond_signal(&m_cond);
}
-bool CCondition::Wait(CMutex *mutex,
int64_t iTimeout
)
+bool CCondition::Wait(CMutex *mutex,
uint32_t iTimeout /* = 0 */
)
{
bool bReturn(false);
sched_yield();
if (mutex)
{
{
bool bReturn(false);
sched_yield();
if (mutex)
{
- struct timespec abstime;
- struct timeval now;
- if (gettimeofday(&now, NULL) == 0)
+ if (iTimeout > 0)
{
{
+ struct timespec abstime;
+ struct timeval now;
+ gettimeofday(&now, NULL);
iTimeout += now.tv_usec / 1000;
abstime.tv_sec = now.tv_sec + (time_t)(iTimeout / 1000);
iTimeout += now.tv_usec / 1000;
abstime.tv_sec = now.tv_sec + (time_t)(iTimeout / 1000);
- abstime.tv_nsec = (
long)((iTimeout % (unsigned long)1000) * (unsigned long
)1000000);
+ abstime.tv_nsec = (
int32_t)((iTimeout % (uint32_t)1000) * (uint32_t
)1000000);
bReturn = (pthread_cond_timedwait(&m_cond, &mutex->m_mutex, &abstime) == 0);
}
bReturn = (pthread_cond_timedwait(&m_cond, &mutex->m_mutex, &abstime) == 0);
}
+ else
+ {
+ bReturn = (pthread_cond_wait(&m_cond, &mutex->m_mutex) == 0);
+ }
}
return bReturn;
}
}
return bReturn;
}
-void CCondition::Sleep(
int64
_t iTimeout)
+void CCondition::Sleep(
uint32
_t iTimeout)
{
CCondition w;
CMutex m;
{
CCondition w;
CMutex m;
@@
-135,19
+143,17
@@
void CCondition::Sleep(int64_t iTimeout)
}
CThread::CThread(void) :
}
CThread::CThread(void) :
- m_b
Running
(false),
- m_b
Stop
(false)
+ m_b
Stop
(false),
+ m_b
Running
(false)
{
}
CThread::~CThread(void)
{
{
}
CThread::~CThread(void)
{
- m_bStop = true;
- m_threadCondition.Broadcast();
- pthread_join(m_thread, NULL);
+ StopThread();
}
}
-bool CThread::CreateThread(
void
)
+bool CThread::CreateThread(
bool bWait /* = true */
)
{
bool bReturn(false);
{
bool bReturn(false);
@@
-155,7
+161,8
@@
bool CThread::CreateThread(void)
m_bStop = false;
if (!m_bRunning && pthread_create(&m_thread, NULL, (void *(*) (void *))&CThread::ThreadHandler, (void *)this) == 0)
{
m_bStop = false;
if (!m_bRunning && pthread_create(&m_thread, NULL, (void *(*) (void *))&CThread::ThreadHandler, (void *)this) == 0)
{
- m_bRunning = true;
+ if (bWait)
+ m_threadCondition.Wait(&m_threadMutex);
bReturn = true;
}
bReturn = true;
}
@@
-167,27
+174,39
@@
void *CThread::ThreadHandler(CThread *thread)
void *retVal = NULL;
if (thread)
void *retVal = NULL;
if (thread)
+ {
+ CLockObject lock(&thread->m_threadMutex);
+ thread->m_bRunning = true;
+ lock.Leave();
+ thread->m_threadCondition.Broadcast();
+
retVal = thread->Process();
retVal = thread->Process();
- thread->m_bRunning = false;
+
+ lock.Lock();
+ thread->m_bRunning = false;
+ lock.Leave();
+ thread->m_threadCondition.Broadcast();
+ }
return retVal;
}
bool CThread::StopThread(bool bWaitForExit /* = true */)
{
return retVal;
}
bool CThread::StopThread(bool bWaitForExit /* = true */)
{
- bool bReturn(
fals
e);
+ bool bReturn(
tru
e);
m_bStop = true;
m_threadCondition.Broadcast();
m_bStop = true;
m_threadCondition.Broadcast();
+
void *retVal;
void *retVal;
- if (bWaitForExit)
+ if (bWaitForExit
&& m_bRunning
)
bReturn = (pthread_join(m_thread, &retVal) == 0);
return bReturn;
}
bReturn = (pthread_join(m_thread, &retVal) == 0);
return bReturn;
}
-bool CThread::Sleep(uint
64
_t iTimeout)
+bool CThread::Sleep(uint
32
_t iTimeout)
{
CLockObject lock(&m_threadMutex);
{
CLockObject lock(&m_threadMutex);
- return m_bStop ? false :m_threadCondition.Wait(&m_threadMutex, iTimeout);
+ return m_bStop ? false :
m_threadCondition.Wait(&m_threadMutex, iTimeout);
}
}