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/clientapi.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/clientapi.h')
-rw-r--r-- | common/clientapi.h | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/common/clientapi.h b/common/clientapi.h index d860305..3ff8472 100644 --- a/common/clientapi.h +++ b/common/clientapi.h | |||
@@ -137,6 +137,10 @@ public: | |||
137 | //We must guarantee that the emitter is returned before the first result is emitted. | 137 | //We must guarantee that the emitter is returned before the first result is emitted. |
138 | //The result provider must be threadsafe. | 138 | //The result provider must be threadsafe. |
139 | async::run([query, resultSet](){ | 139 | async::run([query, resultSet](){ |
140 | QEventLoop eventLoop; | ||
141 | resultSet->onDone([&eventLoop](){ | ||
142 | eventLoop.quit(); | ||
143 | }); | ||
140 | // Query all resources and aggregate results | 144 | // Query all resources and aggregate results |
141 | KAsync::iterate(getResources(query.resources)) | 145 | KAsync::iterate(getResources(query.resources)) |
142 | .template each<void, QByteArray>([query, resultSet](const QByteArray &resource, KAsync::Future<void> &future) { | 146 | .template each<void, QByteArray>([query, resultSet](const QByteArray &resource, KAsync::Future<void> &future) { |
@@ -158,10 +162,6 @@ public: | |||
158 | 162 | ||
159 | //Keep the thread alive until the result is ready | 163 | //Keep the thread alive until the result is ready |
160 | if (!resultSet->isDone()) { | 164 | if (!resultSet->isDone()) { |
161 | QEventLoop eventLoop; | ||
162 | resultSet->onDone([&eventLoop](){ | ||
163 | eventLoop.quit(); | ||
164 | }); | ||
165 | eventLoop.exec(); | 165 | eventLoop.exec(); |
166 | } | 166 | } |
167 | }); | 167 | }); |