diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-07-31 00:51:18 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-07-31 00:51:18 +0200 |
commit | e2e42e3b6f2b317f814aff2858a35a0993c94724 (patch) | |
tree | 97e4bfa3d83baaf9c79c34769b22ed783221c058 /common/resultprovider.h | |
parent | 2b1542d7fee7e9ef14592e88c8a974ed4ea2eacf (diff) | |
download | sink-e2e42e3b6f2b317f814aff2858a35a0993c94724.tar.gz sink-e2e42e3b6f2b317f814aff2858a35a0993c94724.zip |
Fixed possible race conditions.
* Ensure we always create the thread-local event loop before any objects
in the thread are created, and guarantee the done handler is
immediately registered before the query can execute.
* Call the callback on emitter destruction in the worker thread, where
the eventloop lives, instead of the main thread.
With this I can no longer reproduce any deadlocks or memory corruptions
that I used to get occasionally before.
Diffstat (limited to 'common/resultprovider.h')
-rw-r--r-- | common/resultprovider.h | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/common/resultprovider.h b/common/resultprovider.h index 4d985d9..a375815 100644 --- a/common/resultprovider.h +++ b/common/resultprovider.h | |||
@@ -100,7 +100,7 @@ public: | |||
100 | { | 100 | { |
101 | if (!mResultEmitter) { | 101 | if (!mResultEmitter) { |
102 | //We have to go over a separate var and return that, otherwise we'd delete the emitter immediately again | 102 | //We have to go over a separate var and return that, otherwise we'd delete the emitter immediately again |
103 | auto sharedPtr = QSharedPointer<ResultEmitter<T> >(new ResultEmitter<T>, [this](ResultEmitter<T> *emitter){ done(); delete emitter; }); | 103 | auto sharedPtr = QSharedPointer<ResultEmitter<T> >(new ResultEmitter<T>, [this](ResultEmitter<T> *emitter){ mThreadBoundary->callInMainThread([this]() {done();}); delete emitter; }); |
104 | mResultEmitter = sharedPtr; | 104 | mResultEmitter = sharedPtr; |
105 | return sharedPtr; | 105 | return sharedPtr; |
106 | } | 106 | } |
@@ -128,6 +128,7 @@ public: | |||
128 | 128 | ||
129 | void onDone(const std::function<void()> &callback) | 129 | void onDone(const std::function<void()> &callback) |
130 | { | 130 | { |
131 | mThreadBoundary = QSharedPointer<ThreadBoundary>::create(); | ||
131 | mOnDoneCallback = callback; | 132 | mOnDoneCallback = callback; |
132 | } | 133 | } |
133 | 134 | ||
@@ -151,6 +152,7 @@ private: | |||
151 | QSharedPointer<QObject> mQueryRunner; | 152 | QSharedPointer<QObject> mQueryRunner; |
152 | std::shared_ptr<void> mFacade; | 153 | std::shared_ptr<void> mFacade; |
153 | std::function<void()> mOnDoneCallback; | 154 | std::function<void()> mOnDoneCallback; |
155 | QSharedPointer<ThreadBoundary> mThreadBoundary; | ||
154 | }; | 156 | }; |
155 | 157 | ||
156 | /* | 158 | /* |