barrier/mt/CThread.cpp

161 lines
2.4 KiB
C++
Raw Normal View History

2001-10-06 14:13:28 +00:00
#include "CThread.h"
#include "CLock.h"
2001-10-06 14:13:28 +00:00
#include "CThreadRep.h"
#include "XThread.h"
2001-10-14 19:16:54 +00:00
#include "CLog.h"
#include "CStopwatch.h"
2001-10-06 14:13:28 +00: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 14:13:28 +00:00
{
m_rep->ref();
}
CThread::CThread(CThreadRep* rep) :
m_rep(rep)
2001-10-06 14:13:28 +00:00
{
// do nothing. rep should have already been Ref()'d.
}
CThread::~CThread()
{
m_rep->unref();
}
CThread&
2002-06-17 13:31:21 +00:00
CThread::operator=(const CThread& thread)
2001-10-06 14:13:28 +00: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
2002-06-17 13:31:21 +00:00
CThread::sleep(double timeout)
2001-10-06 14:13:28 +00:00
{
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
if (timeout >= 0.0) {
currentRep->testCancel();
currentRep->sleep(timeout);
}
currentRep->testCancel();
}
void
2002-06-17 13:31:21 +00:00
CThread::exit(void* result)
2001-10-06 14:13:28 +00:00
{
2001-10-14 19:16:54 +00:00
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
log((CLOG_DEBUG1 "throw exit on thread %p", currentRep.operator->()));
2001-10-06 14:13:28 +00:00
throw XThreadExit(result);
}
bool
2002-06-17 13:31:21 +00:00
CThread::enableCancel(bool enable)
2001-10-06 14:13:28 +00:00
{
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
return currentRep->enableCancel(enable);
}
void
CThread::cancel()
2001-10-06 14:13:28 +00:00
{
m_rep->cancel();
}
void
2002-06-17 13:31:21 +00:00
CThread::setPriority(int n)
2001-10-06 14:13:28 +00:00
{
m_rep->setPriority(n);
}
CThread
CThread::getCurrentThread()
2001-10-06 14:13:28 +00:00
{
return CThread(CThreadRep::getCurrentThreadRep());
}
bool
2002-06-17 13:31:21 +00:00
CThread::wait(double timeout) const
2001-10-06 14:13:28 +00:00
{
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
return currentRep->wait(m_rep, timeout);
}
#if WINDOWS_LIKE
bool
2002-06-17 13:31:21 +00:00
CThread::waitForEvent(double timeout)
{
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
return currentRep->waitForEvent(timeout);
}
#endif
void
CThread::testCancel()
2001-10-06 14:13:28 +00:00
{
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
currentRep->testCancel();
}
void*
CThread::getResult() const
2001-10-06 14:13:28 +00:00
{
if (wait())
return m_rep->getResult();
else
return NULL;
}
void*
CThread::getUserData()
2001-10-06 14:13:28 +00:00
{
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
return currentRep->getUserData();
}
bool
2002-06-17 13:31:21 +00:00
CThread::operator==(const CThread& thread) const
2001-10-06 14:13:28 +00:00
{
return (m_rep == thread.m_rep);
}
bool
2002-06-17 13:31:21 +00:00
CThread::operator!=(const CThread& thread) const
2001-10-06 14:13:28 +00:00
{
return (m_rep != thread.m_rep);
}
//
// CThreadMaskCancel
//
CThreadMaskCancel::CThreadMaskCancel() :
m_old(CThread::enableCancel(false))
{
// do nothing
}
CThreadMaskCancel::~CThreadMaskCancel()
{
CThread::enableCancel(m_old);
}