2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
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"
|
2025-08-07 20:57:23 +00:00
|
|
|
#include "mt/MTException.h"
|
2025-08-07 21:00:57 +00:00
|
|
|
#include "mt/ThreadException.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-10-17 18:04:35 +00:00
|
|
|
Thread::Thread(IJob *job)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
m_thread = ARCH->newThread(&Thread::threadFunc, job);
|
2025-04-27 19:39:23 +00:00
|
|
|
if (m_thread == nullptr) {
|
2024-07-02 19:07:06 +00:00
|
|
|
// couldn't create thread
|
|
|
|
|
delete job;
|
2025-08-07 20:57:23 +00:00
|
|
|
throw MTThreadUnavailableException();
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-22 22:22:20 +00:00
|
|
|
Thread::Thread(const Thread &thread) : m_thread{ARCH->copyThread(thread.m_thread)}
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-05-22 22:22:20 +00:00
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-22 22:22:20 +00:00
|
|
|
Thread::Thread(ArchThread adoptedThread) : m_thread{adoptedThread}
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-05-22 22:22:20 +00:00
|
|
|
// do nothing
|
2024-10-17 18:04:35 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
Thread::~Thread()
|
|
|
|
|
{
|
|
|
|
|
ARCH->closeThread(m_thread);
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
Thread &Thread::operator=(const Thread &thread)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
2025-07-03 23:26:13 +00:00
|
|
|
[[noreturn]] void Thread::exit(void *result)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-08-07 21:00:57 +00:00
|
|
|
throw ThreadExitException(result);
|
2024-10-17 18:04:35 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void Thread::cancel()
|
|
|
|
|
{
|
|
|
|
|
ARCH->cancelThread(m_thread);
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void Thread::setPriority(int n)
|
|
|
|
|
{
|
|
|
|
|
ARCH->setPriorityOfThread(m_thread, n);
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void Thread::unblockPollSocket()
|
|
|
|
|
{
|
|
|
|
|
ARCH->unblockPollSocket(m_thread);
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
Thread Thread::getCurrentThread()
|
|
|
|
|
{
|
|
|
|
|
return Thread(ARCH->newCurrentThread());
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void Thread::testCancel()
|
|
|
|
|
{
|
|
|
|
|
ARCH->testCancelThread();
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool Thread::wait(double timeout) const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
return ARCH->wait(m_thread, timeout);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void *Thread::getResult() const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
if (wait())
|
|
|
|
|
return ARCH->getResultOfThread(m_thread);
|
|
|
|
|
else
|
2025-04-27 19:39:23 +00:00
|
|
|
return nullptr;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
IArchMultithread::ThreadID Thread::getID() const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
return ARCH->getIDOfThread(m_thread);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool Thread::operator==(const Thread &thread) const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
return ARCH->isSameThread(m_thread, thread.m_thread);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void *Thread::threadFunc(void *vjob)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// get this thread's id for logging
|
|
|
|
|
IArchMultithread::ThreadID id;
|
|
|
|
|
{
|
|
|
|
|
ArchThread thread = ARCH->newCurrentThread();
|
|
|
|
|
id = ARCH->getIDOfThread(thread);
|
|
|
|
|
ARCH->closeThread(thread);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get job
|
2025-04-27 16:54:31 +00:00
|
|
|
auto *job = static_cast<IJob *>(vjob);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
// run job
|
2025-04-27 19:39:23 +00:00
|
|
|
void *result = nullptr;
|
2024-07-02 19:07:06 +00:00
|
|
|
try {
|
|
|
|
|
// go
|
2025-08-06 11:54:59 +00:00
|
|
|
LOG_DEBUG1("thread 0x%08x entry", id);
|
2024-07-02 19:07:06 +00:00
|
|
|
job->run();
|
2025-08-06 11:54:59 +00:00
|
|
|
LOG_DEBUG1("thread 0x%08x exit", id);
|
2024-07-02 19:07:06 +00:00
|
|
|
} catch (XThreadCancel &) {
|
|
|
|
|
// client called cancel()
|
2025-08-06 11:54:59 +00:00
|
|
|
LOG_DEBUG1("caught cancel on thread 0x%08x", id);
|
2024-07-02 19:07:06 +00:00
|
|
|
delete job;
|
|
|
|
|
throw;
|
2025-08-07 21:00:57 +00:00
|
|
|
} catch (ThreadExitException &e) {
|
2024-07-02 19:07:06 +00:00
|
|
|
// client called exit()
|
|
|
|
|
result = e.m_result;
|
2025-08-06 11:54:59 +00:00
|
|
|
LOG_DEBUG1("caught exit on thread 0x%08x, result %p", id, result);
|
2024-07-02 19:07:06 +00:00
|
|
|
} catch (XBase &e) {
|
2025-08-06 11:54:59 +00:00
|
|
|
LOG_ERR("exception on thread 0x%08x: %s", id, e.what());
|
2024-07-02 19:07:06 +00:00
|
|
|
delete job;
|
|
|
|
|
throw;
|
|
|
|
|
} catch (std::exception &e) {
|
2025-08-06 11:54:59 +00:00
|
|
|
LOG_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 (...) {
|
2025-08-06 11:54:59 +00:00
|
|
|
LOG_ERR("non-exception throw on thread 0x%08x: <unknown>", id);
|
2024-07-02 19:07:06 +00:00
|
|
|
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
|
|
|
}
|