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"
|
2003-01-04 22:01:32 +00:00
|
|
|
#include "XMT.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
#include "XThread.h"
|
2001-10-14 19:16:54 +00:00
|
|
|
#include "CLog.h"
|
2003-01-04 22:01:32 +00:00
|
|
|
#include "IJob.h"
|
|
|
|
#include "CArch.h"
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// CThread
|
|
|
|
//
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
CThread::CThread(IJob* job)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
m_thread = ARCH->newThread(&CThread::threadFunc, job);
|
|
|
|
if (m_thread == NULL) {
|
|
|
|
// couldn't create thread
|
|
|
|
delete job;
|
|
|
|
throw XMTThreadUnavailable();
|
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
CThread::CThread(const CThread& thread)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
m_thread = ARCH->copyThread(thread.m_thread);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
CThread::CThread(CArchThread adoptedThread)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
m_thread = adoptedThread;
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CThread::~CThread()
|
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->closeThread(m_thread);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
// copy given thread and release ours
|
|
|
|
CArchThread copy = ARCH->copyThread(thread.m_thread);
|
|
|
|
ARCH->closeThread(m_thread);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
// cut over
|
|
|
|
m_thread = copy;
|
2001-10-14 18:29:43 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
return *this;
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
throw XThreadExit(result);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
|
|
|
CThread::cancel()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->cancelThread(m_thread);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->setPriorityOfThread(m_thread, n);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2004-02-28 12:19:49 +00:00
|
|
|
void
|
|
|
|
CThread::unblockPollSocket()
|
|
|
|
{
|
|
|
|
ARCH->unblockPollSocket(m_thread);
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CThread
|
|
|
|
CThread::getCurrentThread()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
return CThread(ARCH->newCurrentThread());
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
void
|
|
|
|
CThread::testCancel()
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
ARCH->testCancelThread();
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-14 18:08:20 +00:00
|
|
|
bool
|
2003-01-04 22:01:32 +00:00
|
|
|
CThread::wait(double timeout) const
|
2002-06-14 18:08:20 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
return ARCH->wait(m_thread, timeout);
|
2002-06-14 18:08:20 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void*
|
|
|
|
CThread::getResult() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
if (wait())
|
2003-01-04 22:01:32 +00:00
|
|
|
return ARCH->getResultOfThread(m_thread);
|
2001-10-06 14:13:28 +00:00
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
IArchMultithread::ThreadID
|
|
|
|
CThread::getID() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
return ARCH->getIDOfThread(m_thread);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
return ARCH->isSameThread(m_thread, thread.m_thread);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2003-01-04 22:01:32 +00:00
|
|
|
return !ARCH->isSameThread(m_thread, thread.m_thread);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
2002-05-22 16:55:05 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
void*
|
|
|
|
CThread::threadFunc(void* vjob)
|
|
|
|
{
|
|
|
|
// get this thread's id for logging
|
|
|
|
IArchMultithread::ThreadID id;
|
|
|
|
{
|
|
|
|
CArchThread thread = ARCH->newCurrentThread();
|
|
|
|
id = ARCH->getIDOfThread(thread);
|
|
|
|
ARCH->closeThread(thread);
|
|
|
|
}
|
2002-05-22 16:55:05 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
// get job
|
|
|
|
IJob* job = reinterpret_cast<IJob*>(vjob);
|
2002-05-22 16:55:05 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
// run job
|
|
|
|
void* result = NULL;
|
|
|
|
try {
|
|
|
|
// go
|
|
|
|
LOG((CLOG_DEBUG1 "thread 0x%08x entry", id));
|
|
|
|
job->run();
|
|
|
|
LOG((CLOG_DEBUG1 "thread 0x%08x exit", id));
|
|
|
|
}
|
2002-05-22 16:55:05 +00:00
|
|
|
|
2003-01-04 22:01:32 +00:00
|
|
|
catch (XThreadCancel&) {
|
|
|
|
// client called cancel()
|
|
|
|
LOG((CLOG_DEBUG1 "caught cancel on thread 0x%08x", id));
|
|
|
|
delete job;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (XThreadExit& e) {
|
|
|
|
// client called exit()
|
|
|
|
result = e.m_result;
|
|
|
|
LOG((CLOG_DEBUG1 "caught exit on thread 0x%08x, result %p", id, result));
|
|
|
|
}
|
2003-09-02 21:41:00 +00:00
|
|
|
catch (XBase& e) {
|
|
|
|
LOG((CLOG_ERR "exception on thread 0x%08x: %s", id, e.what()));
|
|
|
|
delete job;
|
|
|
|
throw;
|
|
|
|
}
|
2003-01-04 22:01:32 +00:00
|
|
|
catch (...) {
|
2003-09-02 21:41:00 +00:00
|
|
|
LOG((CLOG_ERR "exception on thread 0x%08x: <unknown>", id));
|
2003-01-04 22:01:32 +00:00
|
|
|
delete job;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
// done with job
|
|
|
|
delete job;
|
|
|
|
|
|
|
|
// return exit result
|
|
|
|
return result;
|
2002-05-22 16:55:05 +00:00
|
|
|
}
|