From 8efcfda41f98af51c5d7ab72b5bc38926e9c3f11 Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Wed, 1 Aug 2012 12:59:28 +0200 Subject: statemachine: Really fix signal transition handling in multi-threaded setup Commit f9a17d7f0f02f7af849afdf653a763ffdaf78a1b fixed it for the case where the sender object is in a different thread at transition setup time. However, it still didn't work if either the sender object or the state machine was moved to a different thread at some later time, before the machine was started. Therefore: Bite the sour grape and traverse all the machine's transitions when the machine is being started, registering those signal transitions whose sender objects are in other threads. This will increase the machine's startup time (proportional to the number of transitions), but at least it works in all known scenarios, meaning we don't have to document weird restrictions regarding the order in which the user's operations have to be done. Task-number: QTBUG-19789 Change-Id: I5f1dd1321994e49635f52be65cf56d2678ed1253 Reviewed-by: Eskil Abrahamsen Blomfeldt --- .../qstatemachine/tst_qstatemachine.cpp | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'tests/auto/corelib/statemachine') diff --git a/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp b/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp index dd036641b2..15f23e05cd 100644 --- a/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp +++ b/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp @@ -209,6 +209,7 @@ private slots: void createSignalTransitionWhenRunning(); void createEventTransitionWhenRunning(); void signalTransitionSenderInDifferentThread(); + void signalTransitionSenderInDifferentThread2(); void signalTransitionRegistrationThreadSafety(); void childModeConstructor(); }; @@ -4895,6 +4896,43 @@ void tst_QStateMachine::signalTransitionSenderInDifferentThread() QTRY_VERIFY(thread.wait()); } +void tst_QStateMachine::signalTransitionSenderInDifferentThread2() +{ + QStateMachine machine; + QState *s1 = new QState(&machine); + machine.setInitialState(s1); + + QState *s2 = new QState(&machine); + SignalEmitter emitter; + // At the time of the transition creation, the machine and the emitter + // are both in the same thread. + s1->addTransition(&emitter, SIGNAL(signalWithNoArg()), s2); + + QFinalState *s3 = new QFinalState(&machine); + s2->addTransition(&emitter, SIGNAL(signalWithDefaultArg()), s3); + + QThread thread; + // Move the machine and its states to a secondary thread, but let the + // SignalEmitter stay in the main thread. + machine.moveToThread(&thread); + + thread.start(); + QTRY_VERIFY(thread.isRunning()); + + QSignalSpy startedSpy(&machine, SIGNAL(started())); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.start(); + QTRY_COMPARE(startedSpy.count(), 1); + + emitter.emitSignalWithNoArg(); + // The second emission should not get "lost". + emitter.emitSignalWithDefaultArg(); + QTRY_COMPARE(finishedSpy.count(), 1); + + thread.quit(); + QTRY_VERIFY(thread.wait()); +} + class SignalTransitionMutatorThread : public QThread { public: -- cgit v1.2.3