2001-10-06 18:13:28 +04:00
|
|
|
#include "CThread.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "CLock.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
#include "CThreadRep.h"
|
|
|
|
#include "XThread.h"
|
2001-10-14 23:16:54 +04:00
|
|
|
#include "CLog.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include "CStopwatch.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// CThread
|
|
|
|
//
|
|
|
|
|
|
|
|
CThread::CThread(IJob* job, void* userData)
|
|
|
|
{
|
|
|
|
m_rep = new CThreadRep(job, userData);
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CThread::CThread(const CThread& thread) :
|
|
|
|
m_rep(thread.m_rep)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
m_rep->ref();
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CThread::CThread(CThreadRep* rep) :
|
|
|
|
m_rep(rep)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
// do nothing. rep should have already been Ref()'d.
|
|
|
|
}
|
|
|
|
|
|
|
|
CThread::~CThread()
|
|
|
|
{
|
|
|
|
m_rep->unref();
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CThread&
|
|
|
|
CThread::operator=(
|
|
|
|
const CThread& thread)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
if (thread.m_rep != m_rep) {
|
|
|
|
m_rep->unref();
|
|
|
|
m_rep = thread.m_rep;
|
|
|
|
m_rep->ref();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CThread::init()
|
2001-10-14 22:29:43 +04:00
|
|
|
{
|
|
|
|
CThreadRep::initThreads();
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CThread::sleep(
|
|
|
|
double timeout)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
|
|
|
|
if (timeout >= 0.0) {
|
|
|
|
currentRep->testCancel();
|
|
|
|
currentRep->sleep(timeout);
|
|
|
|
}
|
|
|
|
currentRep->testCancel();
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CThread::exit(
|
|
|
|
void* result)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2001-10-14 23:16:54 +04:00
|
|
|
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
|
2002-04-27 22:49:03 +04:00
|
|
|
log((CLOG_DEBUG1 "throw exit on thread %p", currentRep.operator->()));
|
2001-10-06 18:13:28 +04:00
|
|
|
throw XThreadExit(result);
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
|
|
|
CThread::enableCancel(
|
|
|
|
bool enable)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
|
|
|
|
return currentRep->enableCancel(enable);
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CThread::cancel()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
m_rep->cancel();
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CThread::setPriority(
|
|
|
|
int n)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
m_rep->setPriority(n);
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CThread
|
|
|
|
CThread::getCurrentThread()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
return CThread(CThreadRep::getCurrentThreadRep());
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
|
|
|
CThread::wait(
|
|
|
|
double timeout) const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
|
|
|
|
return currentRep->wait(m_rep, timeout);
|
|
|
|
}
|
|
|
|
|
2002-06-14 22:08:20 +04:00
|
|
|
#if defined(CONFIG_PLATFORM_WIN32)
|
|
|
|
bool
|
|
|
|
CThread::waitForEvent(
|
|
|
|
double timeout)
|
|
|
|
{
|
|
|
|
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
|
|
|
|
return currentRep->waitForEvent(timeout);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CThread::testCancel()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
|
|
|
|
currentRep->testCancel();
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void*
|
|
|
|
CThread::getResult() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
if (wait())
|
|
|
|
return m_rep->getResult();
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void*
|
|
|
|
CThread::getUserData()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
|
|
|
|
return currentRep->getUserData();
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
|
|
|
CThread::operator==(
|
|
|
|
const CThread& thread) const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
return (m_rep == thread.m_rep);
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
|
|
|
CThread::operator!=(
|
|
|
|
const CThread& thread) const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
return (m_rep != thread.m_rep);
|
|
|
|
}
|
2002-05-22 20:55:05 +04:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// CThreadMaskCancel
|
|
|
|
//
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CThreadMaskCancel::CThreadMaskCancel() :
|
|
|
|
m_old(CThread::enableCancel(false))
|
2002-05-22 20:55:05 +04:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
CThreadMaskCancel::~CThreadMaskCancel()
|
|
|
|
{
|
|
|
|
CThread::enableCancel(m_old);
|
|
|
|
}
|