1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-21 05:11:44 +02:00

Allow Closures to be created when running in a different thread from the receiver

This commit is contained in:
Leo Franchi
2012-07-26 22:05:28 -04:00
parent 70aa98753b
commit 9d6b5e60fc
2 changed files with 10 additions and 2 deletions

View File

@@ -17,9 +17,15 @@
*/ */
#include "Closure.h" #include "Closure.h"
#include <QApplication>
namespace _detail { namespace _detail {
static bool on_this_thread(QObject* obj)
{
return QCoreApplication::instance()->thread() == obj->thread();
}
Closure::Closure(QObject* sender, Closure::Closure(QObject* sender,
const char* signal, const char* signal,
QObject* receiver, QObject* receiver,
@@ -28,9 +34,10 @@ Closure::Closure(QObject* sender,
const ClosureArgumentWrapper* val1, const ClosureArgumentWrapper* val1,
const ClosureArgumentWrapper* val2, const ClosureArgumentWrapper* val2,
const ClosureArgumentWrapper* val3) const ClosureArgumentWrapper* val3)
: QObject(receiver), : QObject(on_this_thread(receiver) ? receiver : 0),
callback_(NULL), callback_(NULL),
autoDelete_(true), autoDelete_(true),
outOfThreadReceiver_(on_this_thread(receiver) ? 0 : receiver ),
val0_(val0), val0_(val0),
val1_(val1), val1_(val1),
val2_(val2), val2_(val2),
@@ -78,7 +85,7 @@ void Closure::Invoked() {
callback_(); callback_();
} else { } else {
slot_.invoke( slot_.invoke(
parent(), parent() ? parent() : outOfThreadReceiver_,
val0_ ? val0_->arg() : QGenericArgument(), val0_ ? val0_->arg() : QGenericArgument(),
val1_ ? val1_->arg() : QGenericArgument(), val1_ ? val1_->arg() : QGenericArgument(),
val2_ ? val2_->arg() : QGenericArgument(), val2_ ? val2_->arg() : QGenericArgument(),

View File

@@ -89,6 +89,7 @@ class DLLEXPORT Closure : public QObject, boost::noncopyable {
QMetaMethod slot_; QMetaMethod slot_;
std::tr1::function<void()> callback_; std::tr1::function<void()> callback_;
bool autoDelete_; bool autoDelete_;
QObject* outOfThreadReceiver_;
boost::scoped_ptr<const ClosureArgumentWrapper> val0_; boost::scoped_ptr<const ClosureArgumentWrapper> val0_;
boost::scoped_ptr<const ClosureArgumentWrapper> val1_; boost::scoped_ptr<const ClosureArgumentWrapper> val1_;