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 8 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
163 changes: 154 additions & 9 deletions dev/connection_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,167 @@
#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 {
connection_holder(std::string filename) : filename(std::move(filename)) {}
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*)> onAfterOpen) :
_openedForeverHint{openedForeverHint}, _onAfterOpen{std::move(onAfterOpen)},
filename(std::move(filename)) {}

connection_holder(const connection_holder&) = delete;

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

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 (_onAfterOpen) {
_onAfterOpen(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(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)> _onAfterOpen;

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

connection_holder(const connection_holder&) = delete;

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

void retain() {
// first one opens the connection.
// 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 (this->_retain_count.fetch_add(1, std::memory_order_relaxed) == 0) {
int rc = sqlite3_open(this->filename.c_str(), &this->db);
if (_retainCount.fetch_add(1, std::memory_order_relaxed) == 0) {
int rc = sqlite3_open_v2(this->filename.c_str(),
&this->db,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
nullptr);
if (rc != SQLITE_OK) SQLITE_ORM_CPP_UNLIKELY /*possible, but unexpected*/ {
throw_translated_sqlite_error(this->db);
}
}

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

void release() {
// last one closes the connection.
// we assume that this might happen by any thread, therefore the counter must serve as a synchronization point.
if (this->_retain_count.fetch_sub(1, std::memory_order_acq_rel) == 1) {
if (_retainCount.fetch_sub(1, std::memory_order_acq_rel) == 1) {
int rc = sqlite3_close(this->db);
if (rc != SQLITE_OK) SQLITE_ORM_CPP_UNLIKELY {
throw_translated_sqlite_error(this->db);
Expand All @@ -48,17 +182,28 @@ namespace sqlite_orm {
* @attention While retrieving the reference count value is atomic it makes only sense at single-threaded points in code.
*/
int retain_count() const {
return this->_retain_count.load(std::memory_order_relaxed);
return _retainCount.load(std::memory_order_relaxed);
}

const std::string filename;

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

private:
std::atomic_int _retainCount{};

private:
std::atomic_int _retain_count{};
#if SQLITE_ORM_ALIGNED_NEW_SUPPORTED
alignas(polyfill::hardware_destructive_interference_size)
#endif
const std::function<void(sqlite3* db)> _onAfterOpen;

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
9 changes: 8 additions & 1 deletion dev/functional/cxx_core_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@
#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

#if __cpp_generic_lambdas >= 201707L
#define SQLITE_ORM_EXPLICIT_GENERIC_LAMBDA_SUPPORTED
#else
#endif

#if __cpp_init_captures >= 201803L
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;
}
14 changes: 13 additions & 1 deletion dev/functional/mpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ namespace sqlite_orm {
* Commonly used named abbreviation for `check_if<std::is_same, Type>`.
*/
template<class Type>
using check_if_is_type = mpl::bind_front_fn<std::is_same, Type>;
using check_if_is_type = check_if<std::is_same, Type>;

/*
* Quoted trait metafunction that checks if a type's template matches the specified template
Expand All @@ -463,6 +463,18 @@ namespace sqlite_orm {
using check_if_is_template =
mpl::pass_extracted_fn_to<mpl::bind_front_fn<std::is_same, mpl::quote_fn<Template>>>;

/*
* Quoted trait metafunction that checks if a type names a nested type determined by `Op`.
*/
template<template<typename...> class Op>
using check_if_names = mpl::bind_front_higherorder_fn<polyfill::is_detected, Op>;

/*
* Quoted trait metafunction that checks if a type does not name a nested type determined by `Op`.
*/
template<template<typename...> class Op>
using check_if_lacks = mpl::not_<check_if_names<Op>>;

/*
* Quoted metafunction that finds the index of the given type in a tuple.
*/
Expand Down
9 changes: 5 additions & 4 deletions dev/pragma.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,12 @@ namespace sqlite_orm {
}

void set_pragma_impl(const std::string& query, sqlite3* db = nullptr) {
auto con = this->get_connection();
if (db == nullptr) {
db = con.get();
if (db) {
perform_void_exec(db, query);
} else {
auto con = this->get_connection();
perform_void_exec(con.get(), query);
}
perform_void_exec(db, query);
}
};
}
Expand Down
Loading