0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-03 09:56:38 -05:00

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.
This commit is contained in:
Vasil Dimov 2022-06-28 16:39:15 +02:00
parent 3f1f5f6f1e
commit 4163093d63
No known key found for this signature in database
GPG key ID: 54DF06F64B55CBBF
2 changed files with 16 additions and 4 deletions

View file

@ -23,9 +23,6 @@
namespace wallet { namespace wallet {
static constexpr int32_t WALLET_SCHEMA_VERSION = 0; static constexpr int32_t WALLET_SCHEMA_VERSION = 0;
static GlobalMutex g_sqlite_mutex;
static int g_sqlite_count GUARDED_BY(g_sqlite_mutex) = 0;
static void ErrorLogCallback(void* arg, int code, const char* msg) static void ErrorLogCallback(void* arg, int code, const char* msg)
{ {
// From sqlite3_config() documentation for the SQLITE_CONFIG_LOG option: // From sqlite3_config() documentation for the SQLITE_CONFIG_LOG option:
@ -83,6 +80,9 @@ static void SetPragma(sqlite3* db, const std::string& key, const std::string& va
} }
} }
Mutex SQLiteDatabase::g_sqlite_mutex;
int SQLiteDatabase::g_sqlite_count = 0;
SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, const DatabaseOptions& options, bool mock) SQLiteDatabase::SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, const DatabaseOptions& options, bool mock)
: WalletDatabase(), m_mock(mock), m_dir_path(fs::PathToString(dir_path)), m_file_path(fs::PathToString(file_path)), m_use_unsafe_sync(options.use_unsafe_sync) : WalletDatabase(), m_mock(mock), m_dir_path(fs::PathToString(dir_path)), m_file_path(fs::PathToString(file_path)), m_use_unsafe_sync(options.use_unsafe_sync)
{ {
@ -146,6 +146,8 @@ SQLiteDatabase::~SQLiteDatabase()
void SQLiteDatabase::Cleanup() noexcept void SQLiteDatabase::Cleanup() noexcept
{ {
AssertLockNotHeld(g_sqlite_mutex);
Close(); Close();
LOCK(g_sqlite_mutex); LOCK(g_sqlite_mutex);

View file

@ -5,6 +5,7 @@
#ifndef BITCOIN_WALLET_SQLITE_H #ifndef BITCOIN_WALLET_SQLITE_H
#define BITCOIN_WALLET_SQLITE_H #define BITCOIN_WALLET_SQLITE_H
#include <sync.h>
#include <wallet/db.h> #include <wallet/db.h>
#include <sqlite3.h> #include <sqlite3.h>
@ -63,7 +64,16 @@ private:
const std::string m_file_path; 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: public:
SQLiteDatabase() = delete; SQLiteDatabase() = delete;