2002-08-02 23:57:46 +04: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 18:13:28 +04:00
|
|
|
#include "CThread.h"
|
2003-01-05 01:01:32 +03:00
|
|
|
#include "XMT.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
#include "XThread.h"
|
2001-10-14 23:16:54 +04:00
|
|
|
#include "CLog.h"
|
2003-01-05 01:01:32 +03:00
|
|
|
#include "IJob.h"
|
|
|
|
#include "CArch.h"
|
2001-10-06 18:13:28 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// CThread
|
|
|
|
//
|
|
|
|
|
2003-01-05 01:01:32 +03:00
|
|
|
CThread::CThread(IJob* job)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
m_thread = ARCH->newThread(&CThread::threadFunc, job);
|
|
|
|
if (m_thread == NULL) {
|
|
|
|
// couldn't create thread
|
|
|
|
delete job;
|
|
|
|
throw XMTThreadUnavailable();
|
|
|
|
}
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2003-01-05 01:01:32 +03:00
|
|
|
CThread::CThread(const CThread& thread)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
m_thread = ARCH->copyThread(thread.m_thread);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2003-01-05 01:01:32 +03:00
|
|
|
CThread::CThread(CArchThread adoptedThread)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
m_thread = adoptedThread;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CThread::~CThread()
|
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
ARCH->closeThread(m_thread);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CThread&
|
2002-06-17 17:31:21 +04:00
|
|
|
CThread::operator=(const CThread& thread)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
// copy given thread and release ours
|
|
|
|
CArchThread copy = ARCH->copyThread(thread.m_thread);
|
|
|
|
ARCH->closeThread(m_thread);
|
2001-10-06 18:13:28 +04:00
|
|
|
|
2003-01-05 01:01:32 +03:00
|
|
|
// cut over
|
|
|
|
m_thread = copy;
|
2001-10-14 22:29:43 +04:00
|
|
|
|
2003-01-05 01:01:32 +03:00
|
|
|
return *this;
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CThread::exit(void* result)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
throw XThreadExit(result);
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CThread::cancel()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
ARCH->cancelThread(m_thread);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CThread::setPriority(int n)
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
ARCH->setPriorityOfThread(m_thread, n);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CThread
|
|
|
|
CThread::getCurrentThread()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
return CThread(ARCH->newCurrentThread());
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2003-01-05 01:01:32 +03:00
|
|
|
void
|
|
|
|
CThread::testCancel()
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
ARCH->testCancelThread();
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-14 22:08:20 +04:00
|
|
|
bool
|
2003-01-05 01:01:32 +03:00
|
|
|
CThread::wait(double timeout) const
|
2002-06-14 22:08:20 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
return ARCH->wait(m_thread, timeout);
|
2002-06-14 22:08:20 +04:00
|
|
|
}
|
|
|
|
|
2003-03-13 01:34:07 +03:00
|
|
|
CThread::EWaitResult
|
|
|
|
CThread::waitForEvent(double timeout) const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-03-13 01:34:07 +03:00
|
|
|
// IArchMultithread EWaitResults map directly to our EWaitResults
|
|
|
|
static const EWaitResult s_map[] = {
|
|
|
|
kEvent,
|
|
|
|
kExit,
|
|
|
|
kTimeout
|
|
|
|
};
|
|
|
|
return s_map[ARCH->waitForEvent(m_thread, timeout)];
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void*
|
|
|
|
CThread::getResult() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
|
|
|
if (wait())
|
2003-01-05 01:01:32 +03:00
|
|
|
return ARCH->getResultOfThread(m_thread);
|
2001-10-06 18:13:28 +04:00
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-01-05 01:01:32 +03:00
|
|
|
IArchMultithread::ThreadID
|
|
|
|
CThread::getID() const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
return ARCH->getIDOfThread(m_thread);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
2002-06-17 17:31:21 +04:00
|
|
|
CThread::operator==(const CThread& thread) const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
return ARCH->isSameThread(m_thread, thread.m_thread);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
2002-06-17 17:31:21 +04:00
|
|
|
CThread::operator!=(const CThread& thread) const
|
2001-10-06 18:13:28 +04:00
|
|
|
{
|
2003-01-05 01:01:32 +03:00
|
|
|
return !ARCH->isSameThread(m_thread, thread.m_thread);
|
2001-10-06 18:13:28 +04:00
|
|
|
}
|
2002-05-22 20:55:05 +04:00
|
|
|
|
2003-01-05 01:01:32 +03: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 20:55:05 +04:00
|
|
|
|
2003-01-05 01:01:32 +03:00
|
|
|
// get job
|
|
|
|
IJob* job = reinterpret_cast<IJob*>(vjob);
|
2002-05-22 20:55:05 +04:00
|
|
|
|
2003-01-05 01:01:32 +03: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 20:55:05 +04:00
|
|
|
|
2003-01-05 01:01:32 +03: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));
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
LOG((CLOG_DEBUG1 "exception on thread 0x%08x", id));
|
|
|
|
delete job;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
// done with job
|
|
|
|
delete job;
|
|
|
|
|
|
|
|
// return exit result
|
|
|
|
return result;
|
2002-05-22 20:55:05 +04:00
|
|
|
}
|