summaryrefslogtreecommitdiffstats
path: root/common/clientapi.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-07-31 00:51:18 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-07-31 00:51:18 +0200
commite2e42e3b6f2b317f814aff2858a35a0993c94724 (patch)
tree97e4bfa3d83baaf9c79c34769b22ed783221c058 /common/clientapi.cpp
parent2b1542d7fee7e9ef14592e88c8a974ed4ea2eacf (diff)
downloadsink-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.cpp')
-rw-r--r--common/clientapi.cpp14
1 files changed, 6 insertions, 8 deletions
diff --git a/common/clientapi.cpp b/common/clientapi.cpp
index a98340c..8bb244c 100644
--- a/common/clientapi.cpp
+++ b/common/clientapi.cpp
@@ -5,26 +5,24 @@
5#include "resourcefacade.h" 5#include "resourcefacade.h"
6#include "log.h" 6#include "log.h"
7#include <QtConcurrent/QtConcurrentRun> 7#include <QtConcurrent/QtConcurrentRun>
8#define ASYNCINTHREAD
9#ifndef ASYNCINTHREAD
10#include <QTimer> 8#include <QTimer>
11#endif 9
10#define ASYNCINTHREAD
12 11
13namespace async 12namespace async
14{ 13{
15 void run(const std::function<void()> &runner) { 14 void run(const std::function<void()> &runner) {
16#ifndef ASYNCINTHREAD
17 auto timer = new QTimer(); 15 auto timer = new QTimer();
18 timer->setSingleShot(true); 16 timer->setSingleShot(true);
19 QObject::connect(timer, &QTimer::timeout, [runner, timer]() { 17 QObject::connect(timer, &QTimer::timeout, [runner, timer]() {
20 delete timer; 18 delete timer;
19#ifndef ASYNCINTHREAD
21 runner(); 20 runner();
22 });
23 timer->start(0);
24#else 21#else
25 //TODO use a job that runs in a thread? 22 QtConcurrent::run(runner);
26 QtConcurrent::run(runner);
27#endif 23#endif
24 });
25 timer->start(0);
28 }; 26 };
29} // namespace async 27} // namespace async
30 28