barrier/lib/mt/CThread.cpp

174 lines
2.9 KiB
C++
Raw Normal View History

2002-08-02 19:57:46 +00:00
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2002 Chris Schoeneman
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file COPYING that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
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
{
return m_rep->getUserData();
2001-10-06 14:13:28 +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);
}
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);
}