mirror of
https://github.com/clementine-player/Clementine
synced 2024-12-16 11:19:18 +01:00
Make it possible to wait for a background thread to start
This commit is contained in:
parent
24b22b1b17
commit
c7d351f68a
@ -35,3 +35,21 @@ int BackgroundThreadBase::gettid() {
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void BackgroundThreadBase::Start(bool block) {
|
||||
if (!block) {
|
||||
// Just start the thread and return immediately
|
||||
QThread::start(cpu_priority_);
|
||||
return;
|
||||
}
|
||||
|
||||
// Lock the mutex so the new thread won't try to wake us up before we start
|
||||
// waiting.
|
||||
QMutexLocker l(&started_wait_condition_mutex_);
|
||||
|
||||
// Start the thread.
|
||||
QThread::start(cpu_priority_);
|
||||
|
||||
// Wait for the thread to initalise.
|
||||
started_wait_condition_.wait(l.mutex());
|
||||
}
|
||||
|
@ -19,6 +19,8 @@
|
||||
|
||||
#include <QThread>
|
||||
#include <QtDebug>
|
||||
#include <QWaitCondition>
|
||||
#include <QMutexLocker>
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
@ -67,7 +69,7 @@ class BackgroundThreadBase : public QThread {
|
||||
void set_io_priority(IoPriority priority) { io_priority_ = priority; }
|
||||
void set_cpu_priority(QThread::Priority priority) { cpu_priority_ = priority; }
|
||||
|
||||
virtual void Start() { QThread::start(cpu_priority_); }
|
||||
virtual void Start(bool block = false);
|
||||
|
||||
signals:
|
||||
void Initialised();
|
||||
@ -85,6 +87,9 @@ class BackgroundThreadBase : public QThread {
|
||||
|
||||
IoPriority io_priority_;
|
||||
QThread::Priority cpu_priority_;
|
||||
|
||||
QWaitCondition started_wait_condition_;
|
||||
QMutex started_wait_condition_mutex_;
|
||||
};
|
||||
|
||||
// This is the templated class that stores and returns the worker object.
|
||||
@ -161,14 +166,20 @@ BackgroundThreadImplementation<InterfaceType, DerivedType>::
|
||||
|
||||
template <typename InterfaceType, typename DerivedType>
|
||||
void BackgroundThreadImplementation<InterfaceType, DerivedType>::run() {
|
||||
BackgroundThreadBase::SetIOPriority();
|
||||
this->SetIOPriority();
|
||||
|
||||
BackgroundThread<InterfaceType>::worker_.reset(new DerivedType);
|
||||
this->worker_.reset(new DerivedType);
|
||||
|
||||
emit BackgroundThreadBase::Initialised();
|
||||
{
|
||||
// Tell the calling thread that we've initialised the worker.
|
||||
QMutexLocker l(&this->started_wait_condition_mutex_);
|
||||
this->started_wait_condition_.wakeAll();
|
||||
}
|
||||
|
||||
emit this->Initialised();
|
||||
QThread::exec();
|
||||
|
||||
BackgroundThread<InterfaceType>::worker_.reset();
|
||||
this->worker_.reset();
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user