2001-10-06 14:13:28 +00:00
|
|
|
#include "CThread.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#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"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "CStopwatch.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// CThread
|
|
|
|
//
|
|
|
|
|
|
|
|
CThread::CThread(IJob* job, void* userData)
|
|
|
|
{
|
|
|
|
m_rep = new CThreadRep(job, userData);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CThread::CThread(const CThread& thread) :
|
|
|
|
m_rep(thread.m_rep)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
m_rep->ref();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CThread::init()
|
2001-10-14 18:29:43 +00:00
|
|
|
{
|
|
|
|
CThreadRep::initThreads();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
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());
|
2002-04-27 18:49:03 +00:00
|
|
|
log((CLOG_DEBUG1 "throw exit on thread %p", currentRep.operator->()));
|
2001-10-06 14:13:28 +00:00
|
|
|
throw XThreadExit(result);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CThread::cancel()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
m_rep->cancel();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CThread
|
|
|
|
CThread::getCurrentThread()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
return CThread(CThreadRep::getCurrentThreadRep());
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2002-06-19 11:23:49 +00:00
|
|
|
#if WINDOWS_LIKE
|
2002-06-14 18:08:20 +00:00
|
|
|
bool
|
2002-06-17 13:31:21 +00:00
|
|
|
CThread::waitForEvent(double timeout)
|
2002-06-14 18:08:20 +00:00
|
|
|
{
|
|
|
|
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
|
|
|
|
return currentRep->waitForEvent(timeout);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CThread::testCancel()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
|
|
|
|
currentRep->testCancel();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void*
|
|
|
|
CThread::getResult() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
if (wait())
|
|
|
|
return m_rep->getResult();
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void*
|
|
|
|
CThread::getUserData()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
CThreadPtr currentRep(CThreadRep::getCurrentThreadRep());
|
|
|
|
return currentRep->getUserData();
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
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);
|
|
|
|
}
|
2002-05-22 16:55:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// CThreadMaskCancel
|
|
|
|
//
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CThreadMaskCancel::CThreadMaskCancel() :
|
|
|
|
m_old(CThread::enableCancel(false))
|
2002-05-22 16:55:05 +00:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
CThreadMaskCancel::~CThreadMaskCancel()
|
|
|
|
{
|
|
|
|
CThread::enableCancel(m_old);
|
|
|
|
}
|