Remove the pointless CreateInThread method from BackgroundThread and instead move objects to the right thread after creation.

This commit is contained in:
David Sansome 2010-08-27 18:36:57 +00:00
parent 62daf29ec8
commit d980fd5ff8
7 changed files with 13 additions and 89 deletions

View File

@ -16,41 +16,13 @@
#include "backgroundthread.h"
int BackgroundThreadBase::CreateInThreadEvent::sEventType = -1;
BackgroundThreadBase::BackgroundThreadBase(QObject *parent)
: QThread(parent),
io_priority_(IOPRIO_CLASS_NONE),
cpu_priority_(InheritPriority),
object_creator_(NULL)
{
if (CreateInThreadEvent::sEventType == -1)
CreateInThreadEvent::sEventType = QEvent::registerEventType();
}
BackgroundThreadBase::CreateInThreadEvent::CreateInThreadEvent(CreateInThreadRequest *req)
: QEvent(QEvent::Type(sEventType)),
req_(req)
cpu_priority_(InheritPriority)
{
}
bool BackgroundThreadBase::ObjectCreator::event(QEvent* e) {
if (e->type() != CreateInThreadEvent::sEventType)
return false;
// Create the object, parented to this object so it gets destroyed when the
// thread ends.
CreateInThreadRequest* req = static_cast<CreateInThreadEvent*>(e)->req_;
req->object_ = req->meta_object_.newInstance(Q_ARG(QObject*, this));
// Wake up the calling thread
QMutexLocker l(&req->mutex_);
req->wait_condition_.wakeAll();
return true;
}
int BackgroundThreadBase::SetIOPriority() {
#ifdef Q_OS_LINUX
return syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, gettid(),

View File

@ -69,41 +69,10 @@ class BackgroundThreadBase : public QThread {
virtual void Start(bool block = false);
// Creates a new QObject in this thread synchronously.
// The class T needs to have a Q_INVOKABLE constructor that takes a single
// QObject* argument.
template <typename T>
T* CreateInThread();
signals:
void Initialised();
protected:
struct CreateInThreadRequest {
CreateInThreadRequest(const QMetaObject& meta_object)
: meta_object_(meta_object), object_(NULL) {}
const QMetaObject& meta_object_;
QObject* object_;
QWaitCondition wait_condition_;
QMutex mutex_;
};
struct CreateInThreadEvent : public QEvent {
CreateInThreadEvent(CreateInThreadRequest* req);
static int sEventType;
CreateInThreadRequest* req_;
};
class ObjectCreator : public QObject {
public:
bool event(QEvent *);
};
int SetIOPriority();
static int gettid();
@ -119,8 +88,6 @@ class BackgroundThreadBase : public QThread {
QWaitCondition started_wait_condition_;
QMutex started_wait_condition_mutex_;
ObjectCreator* object_creator_;
};
// This is the templated class that stores and returns the worker object.
@ -165,24 +132,6 @@ class BackgroundThreadFactoryImplementation : public BackgroundThreadFactory<Int
}
};
template <typename T>
T* BackgroundThreadBase::CreateInThread() {
// Create the request and lock it.
CreateInThreadRequest req(T::staticMetaObject);
QMutexLocker l(&req.mutex_);
// Post an event to the thread. It will create the object and signal the
// wait condition. Ownership of the event is transferred to Qt.
CreateInThreadEvent* event = new CreateInThreadEvent(&req);
QCoreApplication::postEvent(object_creator_, event);
req.wait_condition_.wait(&req.mutex_);
return static_cast<T*>(req.object_);
}
template <typename InterfaceType>
BackgroundThread<InterfaceType>::BackgroundThread(QObject *parent)
: BackgroundThreadBase(parent)
@ -216,7 +165,6 @@ void BackgroundThreadImplementation<InterfaceType, DerivedType>::run() {
this->SetIOPriority();
this->worker_.reset(new DerivedType);
this->object_creator_ = new BackgroundThreadBase::ObjectCreator;
{
// Tell the calling thread that we've initialised the worker.
@ -227,7 +175,6 @@ void BackgroundThreadImplementation<InterfaceType, DerivedType>::run() {
emit this->Initialised();
QThread::exec();
delete this->object_creator_;
this->worker_.reset();
}

View File

@ -40,8 +40,9 @@ ConnectedDevice::ConnectedDevice(const QUrl& url, DeviceLister* lister,
qDebug() << __PRETTY_FUNCTION__;
// Create the backend in the database thread.
// The backend gets parented to the database.
backend_ = manager->database()->CreateInThread<LibraryBackend>();
backend_ = new LibraryBackend();
backend_->moveToThread(manager->database());
backend_->Init(manager->database()->Worker(),
QString("device_%1_songs").arg(database_id),
QString("device_%1_directories").arg(database_id),

View File

@ -159,7 +159,8 @@ DeviceManager::DeviceManager(BackgroundThread<Database>* database,
connect(task_manager_, SIGNAL(TasksChanged()), SLOT(TasksChanged()));
// Create the backend in the database thread
backend_ = database_->CreateInThread<DeviceDatabaseBackend>();
backend_ = new DeviceDatabaseBackend;
backend_->moveToThread(database);
backend_->Init(database_->Worker());
DeviceDatabaseBackend::DeviceList devices = backend_->GetAllDevices();

View File

@ -33,7 +33,9 @@ Library::Library(BackgroundThread<Database>* db_thread, TaskManager* task_manage
watcher_factory_(new BackgroundThreadFactoryImplementation<LibraryWatcher, LibraryWatcher>),
watcher_(NULL)
{
backend_ = db_thread->CreateInThread<LibraryBackend>();
backend_ = new LibraryBackend;
backend()->moveToThread(db_thread);
backend_->Init(db_thread->Worker(), kSongsTable, kDirsTable, kSubdirsTable, kFtsTable);
model_ = new LibraryModel(backend_, this);

View File

@ -34,8 +34,8 @@ LibraryModel::LibraryModel(LibraryBackend* backend, QObject* parent)
: SimpleTreeModel<LibraryItem>(new LibraryItem(this), parent),
backend_(backend),
dir_model_(new LibraryDirectoryModel(backend, this)),
artist_icon_(IconLoader::Load("x-clementine-artist")),
album_icon_(IconLoader::Load("x-clementine-album")),
artist_icon_(":/icons/22x22/x-clementine-artist"),
album_icon_(":/icons/22x22/x-clementine-album"),
no_cover_icon_(":nocover.png")
{
root_->lazy_loaded = true;

View File

@ -153,7 +153,8 @@ MainWindow::MainWindow(NetworkAccessManager* network, Engine::Type engine, QWidg
qDebug() << t.restart() << "database startup";
// Create some objects in the database thread
playlist_backend_ = database_->CreateInThread<PlaylistBackend>();
playlist_backend_ = new PlaylistBackend;
playlist_backend_->moveToThread(database_);
playlist_backend_->SetDatabase(database_->Worker());
qDebug() << t.restart() << "playlist backend";