2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2016-09-07 14:24:00 +00:00
|
|
|
* Copyright (C) 2012-2016 Symless Ltd.
|
2012-09-04 02:09:56 +00:00
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "mt/Thread.h"
|
|
|
|
|
|
|
|
|
|
#include "arch/Arch.h"
|
|
|
|
|
#include "base/IJob.h"
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "base/Log.h"
|
|
|
|
|
#include "mt/XMT.h"
|
|
|
|
|
#include "mt/XThread.h"
|
2023-09-27 13:01:30 +00:00
|
|
|
#include <exception>
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// Thread
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
Thread::Thread(IJob *job) {
|
|
|
|
|
m_thread = ARCH->newThread(&Thread::threadFunc, job);
|
|
|
|
|
if (m_thread == NULL) {
|
|
|
|
|
// couldn't create thread
|
|
|
|
|
delete job;
|
|
|
|
|
throw XMTThreadUnavailable();
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
Thread::Thread(const Thread &thread) {
|
|
|
|
|
m_thread = ARCH->copyThread(thread.m_thread);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
Thread::Thread(ArchThread adoptedThread) { m_thread = adoptedThread; }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
Thread::~Thread() { ARCH->closeThread(m_thread); }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
Thread &Thread::operator=(const Thread &thread) {
|
|
|
|
|
// copy given thread and release ours
|
|
|
|
|
ArchThread copy = ARCH->copyThread(thread.m_thread);
|
|
|
|
|
ARCH->closeThread(m_thread);
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// cut over
|
|
|
|
|
m_thread = copy;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return *this;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void Thread::exit(void *result) { throw XThreadExit(result); }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void Thread::cancel() { ARCH->cancelThread(m_thread); }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void Thread::setPriority(int n) { ARCH->setPriorityOfThread(m_thread, n); }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void Thread::unblockPollSocket() { ARCH->unblockPollSocket(m_thread); }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
Thread Thread::getCurrentThread() { return Thread(ARCH->newCurrentThread()); }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void Thread::testCancel() { ARCH->testCancelThread(); }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool Thread::wait(double timeout) const {
|
|
|
|
|
return ARCH->wait(m_thread, timeout);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void *Thread::getResult() const {
|
|
|
|
|
if (wait())
|
|
|
|
|
return ARCH->getResultOfThread(m_thread);
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
IArchMultithread::ThreadID Thread::getID() const {
|
|
|
|
|
return ARCH->getIDOfThread(m_thread);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool Thread::operator==(const Thread &thread) const {
|
|
|
|
|
return ARCH->isSameThread(m_thread, thread.m_thread);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool Thread::operator!=(const Thread &thread) const {
|
|
|
|
|
return !ARCH->isSameThread(m_thread, thread.m_thread);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void *Thread::threadFunc(void *vjob) {
|
|
|
|
|
// get this thread's id for logging
|
|
|
|
|
IArchMultithread::ThreadID id;
|
|
|
|
|
{
|
|
|
|
|
ArchThread thread = ARCH->newCurrentThread();
|
|
|
|
|
id = ARCH->getIDOfThread(thread);
|
|
|
|
|
ARCH->closeThread(thread);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get job
|
|
|
|
|
IJob *job = static_cast<IJob *>(vjob);
|
|
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
} 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 (XBase &e) {
|
2024-09-17 19:00:25 +00:00
|
|
|
LOG((CLOG_ERR "deskflow exception on thread 0x%08x: %s", id, e.what()));
|
2024-07-02 19:07:06 +00:00
|
|
|
delete job;
|
|
|
|
|
throw;
|
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
|
LOG((CLOG_ERR "standard exception on thread 0x%08x: %s", id, e.what()));
|
2016-12-28 11:50:32 +00:00
|
|
|
delete job;
|
2024-07-02 19:07:06 +00:00
|
|
|
throw;
|
|
|
|
|
} catch (...) {
|
|
|
|
|
LOG((CLOG_ERR "non-exception throw on thread 0x%08x: <unknown>", id));
|
|
|
|
|
delete job;
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// done with job
|
|
|
|
|
delete job;
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// return exit result
|
|
|
|
|
return result;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|