Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix(core): ensure QTimers are moved with the objects they belong to
Browse files Browse the repository at this point in the history
We use the Qt parent/child model instead of unique_ptr to achieve this.
  • Loading branch information
sudden6 committed Oct 7, 2018
1 parent a21592a commit 26206a3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
12 changes: 6 additions & 6 deletions src/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/core/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private slots:
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;
Expand Down
21 changes: 7 additions & 14 deletions src/core/coreav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions src/core/coreav.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

class Friend;
class Group;
class QTimer;
class QThread;
class QTimer;
class CoreVideoSource;
class CameraSource;
class VideoSource;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -124,7 +123,7 @@ private slots:
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;
Expand Down

0 comments on commit 26206a3

Please sign in to comment.