mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
fix(core): ensure QTimers are moved with the objects they belong to
We use the Qt parent/child model instead of unique_ptr to achieve this.
This commit is contained in:
parent
a21592a419
commit
26206a35eb
|
@ -47,12 +47,14 @@ const QString Core::TOX_EXT = ".tox";
|
|||
Core::Core(QThread* coreThread)
|
||||
: tox(nullptr)
|
||||
, av(nullptr)
|
||||
, toxTimer{new QTimer{this}}
|
||||
, coreLoopLock(new QMutex(QMutex::Recursive))
|
||||
, coreThread(coreThread)
|
||||
{
|
||||
toxTimer.setSingleShot(true);
|
||||
connect(&this->toxTimer, &QTimer::timeout, this, &Core::process);
|
||||
connect(coreThread, &QThread::finished, &toxTimer, &QTimer::stop);
|
||||
assert(toxTimer);
|
||||
toxTimer->setSingleShot(true);
|
||||
connect(toxTimer, &QTimer::timeout, this, &Core::process);
|
||||
connect(coreThread, &QThread::finished, toxTimer, &QTimer::stop);
|
||||
}
|
||||
|
||||
Core::~Core()
|
||||
|
@ -225,8 +227,6 @@ ToxCorePtr Core::makeToxCore(const QByteArray& savedata, const ICoreSettings* co
|
|||
// connect the thread with the Core
|
||||
connect(thread, &QThread::started, core.get(), &Core::onStarted);
|
||||
core->moveToThread(thread);
|
||||
// since this is allocated in the constructor move it to the other thread too
|
||||
core->toxTimer.moveToThread(thread);
|
||||
|
||||
// when leaving this function 'core' should be ready for it's start() action or
|
||||
// a nullptr
|
||||
|
@ -323,7 +323,7 @@ void Core::process()
|
|||
|
||||
unsigned sleeptime =
|
||||
qMin(tox_iteration_interval(tox.get()), CoreFile::corefileIterationInterval());
|
||||
toxTimer.start(sleeptime);
|
||||
toxTimer->start(sleeptime);
|
||||
}
|
||||
|
||||
bool Core::checkConnection()
|
||||
|
|
|
@ -273,7 +273,7 @@ private:
|
|||
ToxPtr tox;
|
||||
|
||||
std::unique_ptr<CoreAV> av;
|
||||
QTimer toxTimer;
|
||||
QTimer* toxTimer = nullptr;
|
||||
// recursive, since we might call our own functions
|
||||
// pointer so we can circumvent const functions
|
||||
std::unique_ptr<QMutex> coreLoopLock = nullptr;
|
||||
|
|
|
@ -82,16 +82,20 @@ std::map<uint32_t, ToxFriendCall> CoreAV::calls;
|
|||
std::map<int, ToxGroupCall> CoreAV::groupCalls;
|
||||
|
||||
CoreAV::CoreAV(Tox* tox)
|
||||
: coreavThread{new QThread}
|
||||
: coreavThread{new QThread{this}}
|
||||
, iterateTimer{new QTimer{this}}
|
||||
, threadSwitchLock{false}
|
||||
{
|
||||
assert(coreavThread);
|
||||
assert(iterateTimer);
|
||||
|
||||
coreavThread->setObjectName("qTox CoreAV");
|
||||
moveToThread(coreavThread.get());
|
||||
|
||||
iterateTimer->setSingleShot(true);
|
||||
connect(iterateTimer.get(), &QTimer::timeout, this, &CoreAV::process);
|
||||
connect(coreavThread.get(), &QThread::finished, iterateTimer.get(), &QTimer::stop);
|
||||
|
||||
connect(iterateTimer, &QTimer::timeout, this, &CoreAV::process);
|
||||
connect(coreavThread.get(), &QThread::finished, iterateTimer, &QTimer::stop);
|
||||
|
||||
toxav = toxav_new(tox, nullptr);
|
||||
|
||||
|
@ -136,17 +140,6 @@ void CoreAV::start()
|
|||
iterateTimer->start();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Stops the main loop
|
||||
*/
|
||||
void CoreAV::stop()
|
||||
{
|
||||
// Timers can only be touched from their own thread
|
||||
if (QThread::currentThread() != coreavThread.get())
|
||||
return (void)QMetaObject::invokeMethod(this, "stop", Qt::BlockingQueuedConnection);
|
||||
iterateTimer->stop();
|
||||
}
|
||||
|
||||
void CoreAV::process()
|
||||
{
|
||||
toxav_iterate(toxav);
|
||||
|
|
|
@ -30,8 +30,8 @@
|
|||
|
||||
class Friend;
|
||||
class Group;
|
||||
class QTimer;
|
||||
class QThread;
|
||||
class QTimer;
|
||||
class CoreVideoSource;
|
||||
class CameraSource;
|
||||
class VideoSource;
|
||||
|
@ -94,7 +94,6 @@ public slots:
|
|||
bool cancelCall(uint32_t friendNum);
|
||||
void timeoutCall(uint32_t friendNum);
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
signals:
|
||||
void avInvite(uint32_t friendId, bool video);
|
||||
|
@ -124,7 +123,7 @@ private:
|
|||
private:
|
||||
ToxAV* toxav;
|
||||
std::unique_ptr<QThread> coreavThread;
|
||||
std::unique_ptr<QTimer> iterateTimer;
|
||||
QTimer* iterateTimer = nullptr;
|
||||
static std::map<uint32_t, ToxFriendCall> calls;
|
||||
static std::map<int, ToxGroupCall> groupCalls;
|
||||
std::atomic_flag threadSwitchLock;
|
||||
|
|
Loading…
Reference in New Issue
Block a user