summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorKent Hansen <khansen@trolltech.com>2009-10-28 17:05:50 (GMT)
committerKent Hansen <khansen@trolltech.com>2009-10-29 11:48:20 (GMT)
commitc3968d0981fd29764e3c665903f4c8db53cc1af3 (patch)
treeac42a9edc44e65b7f748bcb6a02565684b0cc0f0 /tests
parent414d5550f9ffe46faf1ee81b1a364683f2b2f066 (diff)
downloadQt-c3968d0981fd29764e3c665903f4c8db53cc1af3.zip
Qt-c3968d0981fd29764e3c665903f4c8db53cc1af3.tar.gz
Qt-c3968d0981fd29764e3c665903f4c8db53cc1af3.tar.bz2
Make QStateMachine event posting functions thread-safe
By popular demand on the Qt Labs blog. This makes it possible to readily use QStateMachine with e.g. worker threads that post events to the machine. Reviewed-by: Eskil Abrahamsen Blomfeldt
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qstatemachine/tst_qstatemachine.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/auto/qstatemachine/tst_qstatemachine.cpp b/tests/auto/qstatemachine/tst_qstatemachine.cpp
index 346afc9..97057c6 100644
--- a/tests/auto/qstatemachine/tst_qstatemachine.cpp
+++ b/tests/auto/qstatemachine/tst_qstatemachine.cpp
@@ -206,6 +206,7 @@ private slots:
void goToState();
void task260403_clonedSignals();
+ void postEventFromOtherThread();
};
tst_QStateMachine::tst_QStateMachine()
@@ -4205,5 +4206,52 @@ void tst_QStateMachine::task260403_clonedSignals()
QCOMPARE(t1->eventSignalIndex, emitter.metaObject()->indexOfSignal("signalWithDefaultArg()"));
}
+class EventPosterThread : public QThread
+{
+ Q_OBJECT
+public:
+ EventPosterThread(QStateMachine *machine, QObject *parent = 0)
+ : QThread(parent), m_machine(machine), m_count(0)
+ {
+ moveToThread(this);
+ QObject::connect(m_machine, SIGNAL(started()),
+ this, SLOT(postEvent()));
+ }
+protected:
+ virtual void run()
+ {
+ exec();
+ }
+private Q_SLOTS:
+ void postEvent()
+ {
+ m_machine->postEvent(new QEvent(QEvent::User));
+ if (++m_count < 10000)
+ QTimer::singleShot(0, this, SLOT(postEvent()));
+ else
+ quit();
+ }
+private:
+ QStateMachine *m_machine;
+ int m_count;
+};
+
+void tst_QStateMachine::postEventFromOtherThread()
+{
+ QStateMachine machine;
+ EventPosterThread poster(&machine);
+ StringEventPoster *s1 = new StringEventPoster("foo", &machine);
+ s1->addTransition(new EventTransition(QEvent::User, s1));
+ QFinalState *f = new QFinalState(&machine);
+ s1->addTransition(&poster, SIGNAL(finished()), f);
+ machine.setInitialState(s1);
+
+ poster.start();
+
+ QSignalSpy finishedSpy(&machine, SIGNAL(finished()));
+ machine.start();
+ QTRY_COMPARE(finishedSpy.count(), 1);
+}
+
QTEST_MAIN(tst_QStateMachine)
#include "tst_qstatemachine.moc"