summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2014-12-14 23:39:48 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2014-12-15 09:23:58 +0100
commitcb0f5fb769380db5771ff8e0dba8d780cdc92edb (patch)
tree24ef4d7883035628c0f5701b6e61bcef865fcbf2
parent9cf2db435768b4917e9d322df2366fbdf478cc58 (diff)
downloadsink-cb0f5fb769380db5771ff8e0dba8d780cdc92edb.tar.gz
sink-cb0f5fb769380db5771ff8e0dba8d780cdc92edb.zip
Make lmdb work with threads.
-rw-r--r--common/storage_lmdb.cpp7
-rw-r--r--tests/storagetest.cpp8
2 files changed, 12 insertions, 3 deletions
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp
index b9a007d..ce77bbb 100644
--- a/common/storage_lmdb.cpp
+++ b/common/storage_lmdb.cpp
@@ -8,6 +8,7 @@
8#include <QReadWriteLock> 8#include <QReadWriteLock>
9#include <QString> 9#include <QString>
10#include <QTime> 10#include <QTime>
11#include <QMutex>
11 12
12#include <lmdb.h> 13#include <lmdb.h>
13 14
@@ -26,8 +27,11 @@ public:
26 AccessMode mode; 27 AccessMode mode;
27 bool readTransaction; 28 bool readTransaction;
28 bool firstOpen; 29 bool firstOpen;
30 static QMutex sMutex;
29}; 31};
30 32
33QMutex Storage::Private::sMutex;
34
31Storage::Private::Private(const QString &s, const QString &n, AccessMode m) 35Storage::Private::Private(const QString &s, const QString &n, AccessMode m)
32 : storageRoot(s), 36 : storageRoot(s),
33 name(n), 37 name(n),
@@ -41,6 +45,9 @@ Storage::Private::Private(const QString &s, const QString &n, AccessMode m)
41 dir.mkdir(storageRoot); 45 dir.mkdir(storageRoot);
42 dir.mkdir(fullPath); 46 dir.mkdir(fullPath);
43 47
48 //This seems to resolve threading related issues, not sure why though
49 QMutexLocker locker(&sMutex);
50
44 //create file 51 //create file
45 if (mdb_env_create(&env)) { 52 if (mdb_env_create(&env)) {
46 // TODO: handle error 53 // TODO: handle error
diff --git a/tests/storagetest.cpp b/tests/storagetest.cpp
index b3313c0..3c20135 100644
--- a/tests/storagetest.cpp
+++ b/tests/storagetest.cpp
@@ -126,15 +126,16 @@ private Q_SLOTS:
126 126
127 populate(count); 127 populate(count);
128 128
129 bool error = false;
129 //Try to concurrently read 130 //Try to concurrently read
130 QList<QFuture<void> > futures; 131 QList<QFuture<void> > futures;
131 const int concurrencyLevel = 4; 132 const int concurrencyLevel = 10;
132 for (int num = 0; num < concurrencyLevel; num++) { 133 for (int num = 0; num < concurrencyLevel; num++) {
133 futures << QtConcurrent::run([this, count](){ 134 futures << QtConcurrent::run([this, count, &error](){
134 Storage storage(testDataPath, dbName); 135 Storage storage(testDataPath, dbName);
135 for (int i = 0; i < count; i++) { 136 for (int i = 0; i < count; i++) {
136 if (!verify(storage, i)) { 137 if (!verify(storage, i)) {
137 qWarning() << "invalid value"; 138 error = true;
138 break; 139 break;
139 } 140 }
140 } 141 }
@@ -143,6 +144,7 @@ private Q_SLOTS:
143 for(auto future : futures) { 144 for(auto future : futures) {
144 future.waitForFinished(); 145 future.waitForFinished();
145 } 146 }
147 QVERIFY(!error);
146 148
147 Storage storage(testDataPath, dbName); 149 Storage storage(testDataPath, dbName);
148 storage.removeFromDisk(); 150 storage.removeFromDisk();