[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [baloo] src/engine: Make e.g. Baloo::Query thread safe.
From:       Christoph Cullmann <cullmann () kde ! org>
Date:       2016-09-11 18:27:26
Message-ID: E1bj9TS-0008EU-UX () code ! kde ! org
[Download RAW message or body]

Git commit e34da150d82a57cf417a59b8b632b2fecb32a6f7 by Christoph Cullmann.
Committed on 11/09/2016 at 18:24.
Pushed by cullmann into branch 'master'.

Make e.g. Baloo::Query thread safe.

lmdb itself is thread safe (e.g. you can use the same env in multiple threads).
Unfortunately, the Baloo:atabase itself not, as open() might race against other open \
calls (we have one unique db object in baloo).

=> add non-recursive mutex (recursive mutex not needed, one just must avoid to call \
isOpen() or path() inside open, that is done, else no unit test works).

REVIEW: 128890

M  +18   -3    src/engine/database.cpp
M  +42   -5    src/engine/database.h

http://commits.kde.org/baloo/e34da150d82a57cf417a59b8b632b2fecb32a6f7

diff --git a/src/engine/database.cpp b/src/engine/database.cpp
index ec7ae2e..8ae6b03 100644
--- a/src/engine/database.cpp
+++ b/src/engine/database.cpp
@@ -1,6 +1,7 @@
 /*
    This file is part of the KDE Baloo project.
  * Copyright (C) 2015  Vishesh Handa <vhanda@kde.org>
+ * Copyright (C) 2016  Christoph Cullmann <cullmann@kde.org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -43,23 +44,30 @@
 #include <QFile>
 #include <QFileInfo>
 #include <QDir>
+#include <QMutexLocker>
 
 using namespace Baloo;
 
 Database::Database(const QString& path)
     : m_path(path)
-    , m_env(0)
+    , m_env(nullptr)
 {
 }
 
 Database::~Database()
 {
-    mdb_env_close(m_env);
+    // try only to close if we did open the DB successfully
+    if (m_env) {
+        mdb_env_close(m_env);
+    }
 }
 
 bool Database::open(OpenMode mode)
 {
-    if (isOpen()) {
+    QMutexLocker locker(&m_mutex);
+
+    // nop if already open!
+    if (m_env) {
         return true;
     }
 
@@ -216,7 +224,14 @@ bool Database::open(OpenMode mode)
     return true;
 }
 
+bool Database::isOpen() const
+{
+    QMutexLocker locker(&m_mutex);
+    return m_env != 0;
+}
+
 QString Database::path() const
 {
+    QMutexLocker locker(&m_mutex);
     return m_path;
 }
diff --git a/src/engine/database.h b/src/engine/database.h
index e3bb175..4d1f6a1 100644
--- a/src/engine/database.h
+++ b/src/engine/database.h
@@ -1,6 +1,7 @@
 /*
    This file is part of the KDE Baloo project.
  * Copyright (C) 2015  Vishesh Handa <vhanda@kde.org>
+ * Copyright (C) 2016  Christoph Cullmann <cullmann@kde.org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -21,6 +22,8 @@
 #ifndef BALOO_DATABASE_H
 #define BALOO_DATABASE_H
 
+#include <QMutex>
+
 #include "document.h"
 #include "databasedbis.h"
 
@@ -31,21 +34,56 @@ class DatabaseTest;
 class BALOO_ENGINE_EXPORT Database
 {
 public:
+    /**
+     * Init database for given DB path, will not open it.
+     * @param path db path
+     */
     explicit Database(const QString& path);
-    ~Database();
 
-    QString path() const;
+    /**
+     * Destruct db, might close it, if opened.
+     */
+    ~Database();
 
+    /**
+     * Database open mode
+     */
     enum OpenMode {
         CreateDatabase,
         OpenDatabase
     };
+
+    /**
+     * Open database in given mode.
+     * Nop after open was done (even if mode differs).
+     * There is no close as this would invalidate the database for all threads using \
it. +     * @parmm mode create or open only?
+     * @return success?
+     */
     bool open(OpenMode mode);
 
-    bool isOpen() const { return m_env != 0; }
+    /**
+     * Is database open?
+     * @return database open?
+     */
+    bool isOpen() const;
+
+    /**
+     * Path to database.
+     * @return database path
+     */
+    QString path() const;
 
 private:
-    QString m_path;
+    /**
+     * serialize access, as open might be called from multiple threads
+     */
+    mutable QMutex m_mutex;
+
+    /**
+     * database path
+     */
+    const QString m_path;
 
     MDB_env* m_env;
     DatabaseDbis m_dbis;
@@ -56,5 +94,4 @@ private:
 };
 }
 
-
 #endif // BALOO_DATABASE_H


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic