aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/sqlite.h
diff options
context:
space:
mode:
authorVasil Dimov <vd@FreeBSD.org>2022-06-28 16:39:15 +0200
committerVasil Dimov <vd@FreeBSD.org>2022-10-14 14:36:12 +0200
commit4163093d6374e865dc57e33dbea0e7e359062e0a (patch)
tree9b559c7b6a2b7047996623c662fc6630cbdc0a8f /src/wallet/sqlite.h
parent3f1f5f6f1ec49d0fb2acfddec4021b3582ba0553 (diff)
downloadbitcoin-4163093d6374e865dc57e33dbea0e7e359062e0a.tar.xz
wallet: use Mutex for g_sqlite_mutex instead of GlobalMutex
Using `Mutex` provides stronger guarantee than `GlobalMutex` wrt Clang's thread safety analysis. Thus it is better to reduce the usage of `GlobalMutex` in favor of `Mutex`. Using `Mutex` for `g_sqlite_mutex` is ok because its usage is limited in `wallet/sqlite.cpp` and it does not require propagating the negative annotations to not relevant code.
Diffstat (limited to 'src/wallet/sqlite.h')
-rw-r--r--src/wallet/sqlite.h12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/wallet/sqlite.h b/src/wallet/sqlite.h
index 47b7ebb0ec..d7135679ff 100644
--- a/src/wallet/sqlite.h
+++ b/src/wallet/sqlite.h
@@ -5,6 +5,7 @@
#ifndef BITCOIN_WALLET_SQLITE_H
#define BITCOIN_WALLET_SQLITE_H
+#include <sync.h>
#include <wallet/db.h>
#include <sqlite3.h>
@@ -63,7 +64,16 @@ private:
const std::string m_file_path;
- void Cleanup() noexcept;
+ /**
+ * This mutex protects SQLite initialization and shutdown.
+ * sqlite3_config() and sqlite3_shutdown() are not thread-safe (sqlite3_initialize() is).
+ * Concurrent threads that execute SQLiteDatabase::SQLiteDatabase() should have just one
+ * of them do the init and the rest wait for it to complete before all can proceed.
+ */
+ static Mutex g_sqlite_mutex;
+ static int g_sqlite_count GUARDED_BY(g_sqlite_mutex);
+
+ void Cleanup() noexcept EXCLUSIVE_LOCKS_REQUIRED(!g_sqlite_mutex);
public:
SQLiteDatabase() = delete;