summaryrefslogtreecommitdiffstats
path: root/src/corelib/statemachine
diff options
context:
space:
mode:
authorKent Hansen <kent.hansen@nokia.com>2012-07-12 20:07:18 +0200
committerQt by Nokia <qt-info@nokia.com>2012-07-13 12:09:42 +0200
commitf9a17d7f0f02f7af849afdf653a763ffdaf78a1b (patch)
tree84327be9aadac62ba02492a6503b33b100108609 /src/corelib/statemachine
parent058246c53767dc99ff466882cd890db8ebd7bd49 (diff)
statemachine: Fix signal transition handling in multi-threaded setup
By default, QStateMachine lazily registers signal transitions (i.e., connects to the signal) when the transition's source state is entered. The connections are established in Qt::AutoConnection mode, which means that if the sender object lives in a different thread, the signal processing will be queued. But if a sender object's signal is used in an out-going transition of the target state of the queued transition, it's possible that a second signal emission on the sender object's thread will be "missed" by the state machine; before the machine gets around to processing the first queued emission (and registering the transitions of the new state), a sender object on the other thread could have emitted a new signal. The solution employed here is to eagerly register any signal transition whose sender object is on a different thread; that is, register it regardless of whether the transition's source state is active. Conversely, when a machine's transitions are unregistered (i.e., because the machine finished), signal transitions with sender objects on other threads should be left as-is, in case the machine will be run again. This doesn't solve the case where the sender object is moved to a different thread _after_ the transition has been initialized. Theoretically, we could catch that by installing an event filter on every sender object and handle the ThreadChange events, but that would be very expensive, and likely useless in most cases. So let's just say that that case isn't supported for now. Task-number: QTBUG-19789 Change-Id: Ibc87bfbf2ed83217ac61ae9401fe4f179ef26c24 Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>
Diffstat (limited to 'src/corelib/statemachine')
-rw-r--r--src/corelib/statemachine/qstatemachine.cpp10
1 files changed, 8 insertions, 2 deletions
diff --git a/src/corelib/statemachine/qstatemachine.cpp b/src/corelib/statemachine/qstatemachine.cpp
index 59b6b55494..ec4f3dea4c 100644
--- a/src/corelib/statemachine/qstatemachine.cpp
+++ b/src/corelib/statemachine/qstatemachine.cpp
@@ -1703,8 +1703,10 @@ void QStateMachinePrivate::unregisterTransition(QAbstractTransition *transition)
void QStateMachinePrivate::maybeRegisterSignalTransition(QSignalTransition *transition)
{
Q_Q(QStateMachine);
- if ((state == Running) && configuration.contains(transition->sourceState()))
+ if (((state == Running) && configuration.contains(transition->sourceState()))
+ || (transition->senderObject() && (transition->senderObject()->thread() != q->thread()))) {
registerSignalTransition(transition);
+ }
}
void QStateMachinePrivate::registerSignalTransition(QSignalTransition *transition)
@@ -1766,11 +1768,15 @@ void QStateMachinePrivate::registerSignalTransition(QSignalTransition *transitio
void QStateMachinePrivate::unregisterSignalTransition(QSignalTransition *transition)
{
+ Q_Q(QStateMachine);
int signalIndex = QSignalTransitionPrivate::get(transition)->signalIndex;
if (signalIndex == -1)
return; // not registered
- QSignalTransitionPrivate::get(transition)->signalIndex = -1;
const QObject *sender = QSignalTransitionPrivate::get(transition)->sender;
+ // Don't unregister if the sender is on another thread
+ if (!sender || (sender->thread() != q->thread()))
+ return;
+ QSignalTransitionPrivate::get(transition)->signalIndex = -1;
QVector<int> &connectedSignalIndexes = connections[sender];
Q_ASSERT(connectedSignalIndexes.size() > signalIndex);
Q_ASSERT(connectedSignalIndexes.at(signalIndex) != 0);