barrier/mt/CThread.cpp

170 lines
2.5 KiB
C++
Raw Normal View History

2001-10-06 18:13:28 +04:00
#include "CThread.h"
#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"
#include "CStopwatch.h"
2001-10-06 18:13:28 +04:00
//
// CThread
//
CThread::CThread(IJob* job, void* userData)
{
m_rep = new CThreadRep(job, userData);
}
CThread::CThread(const CThread& thread) :
m_rep(thread.m_rep)
2001-10-06 18:13:28 +04:00
{
m_rep->ref();
}
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();
}
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;
}
void
CThread::init()
{
CThreadRep::initThreads();
}
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();
}
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());
log((CLOG_DEBUG1 "throw exit on thread %p", currentRep.operator->()));
2001-10-06 18:13:28 +04:00
throw XThreadExit(result);
}
bool
CThread::enableCancel(
bool enable)
2001-10-06 18:13:28 +04:00
{
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
return currentRep->enableCancel(enable);
}
void
CThread::cancel()
2001-10-06 18:13:28 +04:00
{
m_rep->cancel();
}
void
CThread::setPriority(
int n)
2001-10-06 18:13:28 +04:00
{
m_rep->setPriority(n);
}
CThread
CThread::getCurrentThread()
2001-10-06 18:13:28 +04:00
{
return CThread(CThreadRep::getCurrentThreadRep());
}
bool
CThread::wait(
double timeout) const
2001-10-06 18:13:28 +04:00
{
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
return currentRep->wait(m_rep, timeout);
}
#if defined(CONFIG_PLATFORM_WIN32)
bool
CThread::waitForEvent(
double timeout)
{
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
return currentRep->waitForEvent(timeout);
}
#endif
void
CThread::testCancel()
2001-10-06 18:13:28 +04:00
{
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
currentRep->testCancel();
}
void*
CThread::getResult() const
2001-10-06 18:13:28 +04:00
{
if (wait())
return m_rep->getResult();
else
return NULL;
}
void*
CThread::getUserData()
2001-10-06 18:13:28 +04:00
{
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
return currentRep->getUserData();
}
bool
CThread::operator==(
const CThread& thread) const
2001-10-06 18:13:28 +04:00
{
return (m_rep == thread.m_rep);
}
bool
CThread::operator!=(
const CThread& thread) const
2001-10-06 18:13:28 +04:00
{
return (m_rep != thread.m_rep);
}
//
// CThreadMaskCancel
//
CThreadMaskCancel::CThreadMaskCancel() :
m_old(CThread::enableCancel(false))
{
// do nothing
}
CThreadMaskCancel::~CThreadMaskCancel()
{
CThread::enableCancel(m_old);
}