From a139a9933b0defbacb9c25c3489fd0b58107026f Mon Sep 17 00:00:00 2001 From: sudden6 Date: Wed, 25 Jul 2018 21:17:00 +0200 Subject: [PATCH] refactor(core): cleanup thread and timer destruction in Core and CoreAV --- src/core/core.cpp | 30 +++++------------------------- src/core/core.h | 1 - src/core/coreav.cpp | 22 ++++------------------ src/core/coreav.h | 1 - 4 files changed, 9 insertions(+), 45 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index 45fbbaf6e..9631295df 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -52,21 +52,17 @@ Core::Core(QThread* coreThread) { toxTimer.setSingleShot(true); connect(&this->toxTimer, &QTimer::timeout, this, &Core::process); + connect(coreThread, &QThread::finished, &toxTimer, &QTimer::stop); } Core::~Core() { - if (QThread::currentThread() == coreThread.get()) { - killTimers(); - } else { - // ensure the timer is stopped, even if not called from this thread - QMetaObject::invokeMethod(this, "killTimers", Qt::BlockingQueuedConnection); - } - - coreThread->exit(0); - // need to reset av first, because it uses tox av.reset(); + + coreThread->exit(0); + coreThread->wait(); + tox.reset(); } @@ -308,10 +304,6 @@ void Core::process() QMutexLocker ml{coreLoopLock.get()}; ASSERT_CORE_THREAD; - if (!isReady()) { - av->stop(); - return; - } static int tolerance = CORE_DISCONNECT_TOLERANCE; tox_iterate(tox.get(), this); @@ -1482,15 +1474,3 @@ void Core::setNospam(uint32_t nospam) tox_self_set_nospam(tox.get(), nospam); emit idSet(getSelfId()); } - -/** - * @brief Stops the AV thread and the timer here - */ -void Core::killTimers() -{ - ASSERT_CORE_THREAD; - if (av) { - av->stop(); - } - toxTimer.stop(); -} diff --git a/src/core/core.h b/src/core/core.h index b6162eb8c..93ca3a85d 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -257,7 +257,6 @@ private: static void registerCallbacks(Tox* tox); private slots: - void killTimers(); void process(); void onStarted(); diff --git a/src/core/coreav.cpp b/src/core/coreav.cpp index 7c32f55d7..a9a356a98 100644 --- a/src/core/coreav.cpp +++ b/src/core/coreav.cpp @@ -91,6 +91,7 @@ CoreAV::CoreAV(Tox* tox) iterateTimer->setSingleShot(true); connect(iterateTimer.get(), &QTimer::timeout, this, &CoreAV::process); + connect(coreavThread.get(), &QThread::finished, iterateTimer.get(), &QTimer::stop); toxav = toxav_new(tox, nullptr); @@ -113,13 +114,10 @@ CoreAV::~CoreAV() for (const auto& call : calls) { cancelCall(call.first); } - killTimerFromThread(); - toxav_kill(toxav); + coreavThread->exit(0); - while (coreavThread->isRunning()) { - qApp->processEvents(); - coreavThread->wait(100); - } + coreavThread->wait(); + toxav_kill(toxav); } const ToxAV* CoreAV::getToxAv() const @@ -149,18 +147,6 @@ void CoreAV::stop() iterateTimer->stop(); } -/** - * @brief Calls itself blocking queued on the coreav thread - */ -void CoreAV::killTimerFromThread() -{ - // Timers can only be touched from their own thread - if (QThread::currentThread() != coreavThread.get()) - return (void)QMetaObject::invokeMethod(this, "killTimerFromThread", - Qt::BlockingQueuedConnection); - iterateTimer.release(); -} - void CoreAV::process() { toxav_iterate(toxav); diff --git a/src/core/coreav.h b/src/core/coreav.h index 048226d9e..b03a66f7a 100644 --- a/src/core/coreav.h +++ b/src/core/coreav.h @@ -108,7 +108,6 @@ private slots: void* self); static void audioBitrateCallback(ToxAV* toxAV, uint32_t friendNum, uint32_t rate, void* self); static void videoBitrateCallback(ToxAV* toxAV, uint32_t friendNum, uint32_t rate, void* self); - void killTimerFromThread(); private: void process();