Skip to content

Threadsafe connection #1395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 129 additions & 3 deletions dev/connection_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,136 @@
#include <sqlite3.h>
#ifndef SQLITE_ORM_IMPORT_STD_MODULE
#include <atomic>
#ifdef SQLITE_ORM_CPP20_SEMAPHORE_SUPPORTED
#include <semaphore>
#endif
#include <functional> // std::function
#include <string> // std::string
#endif

#include "functional/cxx_new.h"
#include "error_code.h"

namespace sqlite_orm {
namespace internal {

#ifdef SQLITE_ORM_CPP20_SEMAPHORE_SUPPORTED
/**
* The connection holder should be performant in all variants:
1. single-threaded use
2. opened once (open forever)
3. concurrent open/close

Hence, a light-weight binary semaphore is used to synchronize opening and closing a database connection.
*/
struct connection_holder {
struct maybe_lock {
maybe_lock(std::binary_semaphore& sync, bool shouldLock) noexcept(noexcept(sync.acquire())) :
isSynced{shouldLock}, sync{sync} {
if (shouldLock) {
sync.acquire();
}
}

~maybe_lock() {
if (isSynced) {
sync.release();
}
}

const bool isSynced;
std::binary_semaphore& sync;
};

connection_holder(std::string filename, bool openedForeverHint, std::function<void(sqlite3*)> didOpenDb) :
_openedForeverHint{openedForeverHint}, _didOpenDb{std::move(didOpenDb)}, filename(std::move(filename)) {
}

connection_holder(const connection_holder&) = delete;

connection_holder(const connection_holder& other, std::function<void(sqlite3*)> didOpenDb) :
_openedForeverHint{other._openedForeverHint}, _didOpenDb{std::move(didOpenDb)},
filename{other.filename} {}

void retain() {
const maybe_lock maybeLock{_sync, !_openedForeverHint};

// `maybeLock.isSynced`: the lock above already synchronized everything, so we can just atomically increment the counter
// `!maybeLock.isSynced`: we presume that the connection is opened once in a single-threaded context [also open forever].
// therefore we can just use an atomic increment but don't need sequencing due to `prevCount > 0`.
if (int prevCount = _retainCount.fetch_add(1, std::memory_order_relaxed); prevCount > 0) {
return;
}

// first one opens and sets up the connection.

if (int rc = sqlite3_open_v2(this->filename.c_str(),
&this->db,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
nullptr);
rc != SQLITE_OK) [[unlikely]] /*possible, but unexpected*/ {
throw_translated_sqlite_error(this->db);
}

if (_didOpenDb) {
_didOpenDb(this->db);
}
}

void release() {
const maybe_lock maybeLock{_sync, !_openedForeverHint};

if (int prevCount = _retainCount.fetch_sub(
1,
maybeLock.isSynced
// the lock above already synchronized everything, so we can just atomically decrement the counter
? std::memory_order_relaxed
// the counter must serve as a synchronization point
: std::memory_order_acq_rel);
prevCount > 1) {
return;
}

// last one closes the connection.

if (int rc = sqlite3_close_v2(this->db); rc != SQLITE_OK) [[unlikely]] {
throw_translated_sqlite_error(this->db);
} else {
this->db = nullptr;
}
}

sqlite3* get() const {
// note: ensuring a valid DB handle was already memory ordered with `retain()`
return this->db;
}

/**
* @attention While retrieving the reference count value is atomic it makes only sense at single-threaded points in code.
*/
int retain_count() const {
return _retainCount.load(std::memory_order_relaxed);
}

protected:
alignas(polyfill::hardware_destructive_interference_size) sqlite3* db = nullptr;

private:
std::atomic_int _retainCount{};
const bool _openedForeverHint = false;
std::binary_semaphore _sync{1};

private:
alignas(polyfill::hardware_destructive_interference_size) const std::function<void(sqlite3* db)> _didOpenDb;

public:
const std::string filename;
};
#else
struct connection_holder {
connection_holder(std::string filename, std::function<void(sqlite3*)> didOpenDb) :
connection_holder(std::string filename,
bool /*openedForeverHint*/,
std::function<void(sqlite3*)> didOpenDb) :
_didOpenDb{std::move(didOpenDb)}, filename(std::move(filename)) {}

connection_holder(const connection_holder&) = delete;
Expand Down Expand Up @@ -66,17 +185,24 @@ namespace sqlite_orm {
}

protected:
sqlite3* db = nullptr;
#ifdef SQLITE_ORM_ALIGNED_NEW_SUPPORTED
alignas(polyfill::hardware_destructive_interference_size)
#endif
sqlite3* db = nullptr;

private:
std::atomic_int _retainCount{};

private:
const std::function<void(sqlite3* db)> _didOpenDb;
#ifdef SQLITE_ORM_ALIGNED_NEW_SUPPORTED
alignas(polyfill::hardware_destructive_interference_size)
#endif
const std::function<void(sqlite3* db)> _didOpenDb;

public:
const std::string filename;
};
#endif

struct connection_ref {
connection_ref(connection_holder& holder) : holder(&holder) {
Expand Down
4 changes: 4 additions & 0 deletions dev/functional/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
#define SQLITE_ORM_CPP20_RANGES_SUPPORTED
#endif

#if __cpp_lib_semaphore >= 201907L
#define SQLITE_ORM_CPP20_SEMAPHORE_SUPPORTED
#endif

// C++20 or later (unfortunately there's no feature test macro).
// Stupidly, clang says C++20, but `std::default_sentinel_t` was only implemented in libc++ 13 and libstd++-v3 10
// (the latter is used on Linux).
Expand Down
4 changes: 4 additions & 0 deletions dev/functional/cxx_core_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
#define SQLITE_ORM_STRUCTURED_BINDINGS_SUPPORTED
#endif

#if __cpp_aligned_new >= 201606L
#define SQLITE_ORM_ALIGNED_NEW_SUPPORTED
#endif

#if __cpp_deduction_guides >= 201703L
#define SQLITE_ORM_CTAD_SUPPORTED
#endif
Expand Down
23 changes: 23 additions & 0 deletions dev/functional/cxx_new.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#ifdef SQLITE_ORM_IMPORT_STD_MODULE
#include <version>
#else
#include <new>
#endif

namespace sqlite_orm {
namespace internal {
namespace polyfill {
#if __cpp_lib_hardware_interference_size >= 201703L
using std::hardware_constructive_interference_size;
using std::hardware_destructive_interference_size;
#else
constexpr size_t hardware_constructive_interference_size = 64;
constexpr size_t hardware_destructive_interference_size = 64;
#endif
}
}

namespace polyfill = internal::polyfill;
}
3 changes: 0 additions & 3 deletions dev/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1756,9 +1756,6 @@ SQLITE_ORM_EXPORT namespace sqlite_orm {
create_from_tuple<std::tuple>(std::move(specTuple), opt_index_sequence<decltype(specTuple)>{}));
}
#else
/*
* Factory function for a storage instance, from a database file and a bunch of database object definitions.
*/
template<class... DBO>
internal::storage_t<DBO...> make_storage(std::string filename, DBO... dbObjects) {
return {std::move(filename), {std::forward<DBO>(dbObjects)...}, std::tuple<>{}};
Expand Down
5 changes: 3 additions & 2 deletions dev/storage_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ namespace sqlite_orm {
}

backup_t make_backup_to(const std::string& filename) {
auto holder = std::make_unique<connection_holder>(filename, nullptr);
auto holder = std::make_unique<connection_holder>(filename, true, nullptr);
connection_ref conRef{*holder};
return {conRef, "main", this->get_connection(), "main", std::move(holder)};
}
Expand All @@ -619,7 +619,7 @@ namespace sqlite_orm {
}

backup_t make_backup_from(const std::string& filename) {
auto holder = std::make_unique<connection_holder>(filename, nullptr);
auto holder = std::make_unique<connection_holder>(filename, true, nullptr);
connection_ref conRef{*holder};
return {this->get_connection(), "main", conRef, "main", std::move(holder)};
}
Expand Down Expand Up @@ -673,6 +673,7 @@ namespace sqlite_orm {
inMemory(filename.empty() || filename == ":memory:"),
connection(std::make_unique<connection_holder>(
std::move(filename),
inMemory || isOpenedForever,
std::bind(&storage_base::on_open_internal, this, std::placeholders::_1))),
cachedForeignKeysCount(foreignKeysCount) {
if (this->inMemory) {
Expand Down
Loading