| 1 |
// SPDX-FileCopyrightText: 2020 - 2023 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "dthreadutils.h" |
| 6 |
|
| 7 |
DCORE_BEGIN_NAMESPACE |
| 8 |
|
| 9 |
#if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0) |
| 10 |
namespace DThreadUtil { |
| 11 |
FunctionCallProxy::FunctionCallProxy(QThread *thread) |
| 12 |
{ |
| 13 |
qRegisterMetaType<QPointer<QObject>>(); |
| 14 |
|
| 15 |
connect(this, &FunctionCallProxy::callInLiveThread, this, [] (QSemaphore *s, QPointer<QObject> target, FunctionType *func) { |
| 16 |
if (Q_LIKELY(target)) { |
| 17 |
(*func)(); |
| 18 |
} else { |
| 19 |
qWarning() << "DThreadUtils::runInThread:" << "The target object is destoryed"; |
| 20 |
} |
| 21 |
|
| 22 |
s->release(); |
| 23 |
}, Qt::QueuedConnection); |
| 24 |
connect(thread, &QThread::finished, this, [this] { |
| 25 |
qWarning() << "DThreadUtils::runInThread:" << sender() << "the thread finished"; |
| 26 |
}, Qt::DirectConnection); |
| 27 |
} |
| 28 |
|
| 29 |
void FunctionCallProxy::proxyCall(QSemaphore *s, QThread *thread, QObject *target, FunctionType fun) |
| 30 |
{ |
| 31 |
if (QThread::currentThread() == thread) |
| 32 |
return fun(); |
| 33 |
|
| 34 |
FunctionCallProxy proxy(thread); |
| 35 |
proxy.moveToThread(thread); |
| 36 |
|
| 37 |
// 如果线程未开启事件循环,且不是主线程,则需要给出严重警告信息,因为可能会导致死锁 |
| 38 |
if (thread->loopLevel() <= 0 && (!QCoreApplication::instance() || thread != QCoreApplication::instance()->thread())) { |
| 39 |
qCritical() << Q_FUNC_INFO << thread << ", the thread no event loop"; |
| 40 |
} |
| 41 |
|
| 42 |
proxy.callInLiveThread(s, target ? target : &proxy, &fun); |
| 43 |
s->acquire(); |
| 44 |
} |
| 45 |
|
| 46 |
} |
| 47 |
#else |
| 48 |
class Q_DECL_HIDDEN Caller : public QObject |
| 49 |
{ |
| 50 |
public: |
| 51 |
explicit Caller() |
| 52 |
: QObject() |
| 53 |
{ |
| 54 |
} |
| 55 |
|
| 56 |
bool event(QEvent *event) override |
| 57 |
{ |
| 58 |
if (event->type() == DThreadUtils::eventType) { |
| 59 |
auto ev = static_cast<DThreadUtils::AbstractCallEvent *>(event); |
| 60 |
ev->call(); |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
return QObject::event(event); |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
DThreadUtils::DThreadUtils(QThread *thread) |
| 69 |
: m_thread(thread) |
| 70 |
, threadContext(nullptr) |
| 71 |
{ |
| 72 |
} |
| 73 |
|
| 74 |
DThreadUtils::~DThreadUtils() |
| 75 |
{ |
| 76 |
delete threadContext.loadRelaxed(); |
| 77 |
} |
| 78 |
|
| 79 |
DThreadUtils &DThreadUtils::gui() |
| 80 |
{ |
| 81 |
static auto global = DThreadUtils(QCoreApplication::instance()->thread()); |
| 82 |
return global; |
| 83 |
} |
| 84 |
|
| 85 |
QThread *DThreadUtils::thread() const noexcept |
| 86 |
{ |
| 87 |
return m_thread; |
| 88 |
} |
| 89 |
|
| 90 |
QObject *DThreadUtils::ensureThreadContextObject() |
| 91 |
{ |
| 92 |
QObject *context; |
| 93 |
if (!threadContext.loadRelaxed()) { |
| 94 |
context = new Caller(); |
| 95 |
context->moveToThread(m_thread); |
| 96 |
if (!threadContext.testAndSetRelaxed(nullptr, context)) { |
| 97 |
context->moveToThread(nullptr); |
| 98 |
delete context; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
context = threadContext.loadRelaxed(); |
| 103 |
Q_ASSERT(context); |
| 104 |
|
| 105 |
return context; |
| 106 |
} |
| 107 |
#endif |
| 108 |
DCORE_END_NAMESPACE |