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:
parent
5d65ab3876
commit
a139a9933b
|
@ -52,21 +52,17 @@ Core::Core(QThread* coreThread)
|
||||||
{
|
{
|
||||||
toxTimer.setSingleShot(true);
|
toxTimer.setSingleShot(true);
|
||||||
connect(&this->toxTimer, &QTimer::timeout, this, &Core::process);
|
connect(&this->toxTimer, &QTimer::timeout, this, &Core::process);
|
||||||
|
connect(coreThread, &QThread::finished, &toxTimer, &QTimer::stop);
|
||||||
}
|
}
|
||||||
|
|
||||||
Core::~Core()
|
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
|
// need to reset av first, because it uses tox
|
||||||
av.reset();
|
av.reset();
|
||||||
|
|
||||||
|
coreThread->exit(0);
|
||||||
|
coreThread->wait();
|
||||||
|
|
||||||
tox.reset();
|
tox.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -308,10 +304,6 @@ void Core::process()
|
||||||
QMutexLocker ml{coreLoopLock.get()};
|
QMutexLocker ml{coreLoopLock.get()};
|
||||||
|
|
||||||
ASSERT_CORE_THREAD;
|
ASSERT_CORE_THREAD;
|
||||||
if (!isReady()) {
|
|
||||||
av->stop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int tolerance = CORE_DISCONNECT_TOLERANCE;
|
static int tolerance = CORE_DISCONNECT_TOLERANCE;
|
||||||
tox_iterate(tox.get(), this);
|
tox_iterate(tox.get(), this);
|
||||||
|
@ -1482,15 +1474,3 @@ void Core::setNospam(uint32_t nospam)
|
||||||
tox_self_set_nospam(tox.get(), nospam);
|
tox_self_set_nospam(tox.get(), nospam);
|
||||||
emit idSet(getSelfId());
|
emit idSet(getSelfId());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Stops the AV thread and the timer here
|
|
||||||
*/
|
|
||||||
void Core::killTimers()
|
|
||||||
{
|
|
||||||
ASSERT_CORE_THREAD;
|
|
||||||
if (av) {
|
|
||||||
av->stop();
|
|
||||||
}
|
|
||||||
toxTimer.stop();
|
|
||||||
}
|
|
||||||
|
|
|
@ -257,7 +257,6 @@ private:
|
||||||
static void registerCallbacks(Tox* tox);
|
static void registerCallbacks(Tox* tox);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void killTimers();
|
|
||||||
void process();
|
void process();
|
||||||
void onStarted();
|
void onStarted();
|
||||||
|
|
||||||
|
|
|
@ -91,6 +91,7 @@ CoreAV::CoreAV(Tox* tox)
|
||||||
|
|
||||||
iterateTimer->setSingleShot(true);
|
iterateTimer->setSingleShot(true);
|
||||||
connect(iterateTimer.get(), &QTimer::timeout, this, &CoreAV::process);
|
connect(iterateTimer.get(), &QTimer::timeout, this, &CoreAV::process);
|
||||||
|
connect(coreavThread.get(), &QThread::finished, iterateTimer.get(), &QTimer::stop);
|
||||||
|
|
||||||
toxav = toxav_new(tox, nullptr);
|
toxav = toxav_new(tox, nullptr);
|
||||||
|
|
||||||
|
@ -113,13 +114,10 @@ CoreAV::~CoreAV()
|
||||||
for (const auto& call : calls) {
|
for (const auto& call : calls) {
|
||||||
cancelCall(call.first);
|
cancelCall(call.first);
|
||||||
}
|
}
|
||||||
killTimerFromThread();
|
|
||||||
toxav_kill(toxav);
|
|
||||||
coreavThread->exit(0);
|
coreavThread->exit(0);
|
||||||
while (coreavThread->isRunning()) {
|
coreavThread->wait();
|
||||||
qApp->processEvents();
|
toxav_kill(toxav);
|
||||||
coreavThread->wait(100);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const ToxAV* CoreAV::getToxAv() const
|
const ToxAV* CoreAV::getToxAv() const
|
||||||
|
@ -149,18 +147,6 @@ void CoreAV::stop()
|
||||||
iterateTimer->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()
|
void CoreAV::process()
|
||||||
{
|
{
|
||||||
toxav_iterate(toxav);
|
toxav_iterate(toxav);
|
||||||
|
|
|
@ -108,7 +108,6 @@ private slots:
|
||||||
void* self);
|
void* self);
|
||||||
static void audioBitrateCallback(ToxAV* toxAV, uint32_t friendNum, uint32_t rate, 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);
|
static void videoBitrateCallback(ToxAV* toxAV, uint32_t friendNum, uint32_t rate, void* self);
|
||||||
void killTimerFromThread();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void process();
|
void process();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user