1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(core): cleanup thread and timer destruction in Core and CoreAV

This commit is contained in:
sudden6 2018-07-25 21:17:00 +02:00
parent 5d65ab3876
commit a139a9933b
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
4 changed files with 9 additions and 45 deletions

View File

@ -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();
}

View File

@ -257,7 +257,6 @@ private:
static void registerCallbacks(Tox* tox);
private slots:
void killTimers();
void process();
void onStarted();

View File

@ -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);

View File

@ -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();