Skip to content
Draft
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f286d95
Corrected error handling for errors when opening the DB
trueqbit Feb 2, 2025
b8a832b
Threadsafe database connection holder
trueqbit Feb 2, 2025
8285dab
Simplified logic of connection holders 'retain' function
trueqbit Feb 5, 2025
c3dfa76
Merge branch 'upstream/dev' into upstream/experimental/threadsafe_con…
trueqbit Feb 6, 2025
458ec11
Set up the database under the same synchronization lock as opening it
trueqbit Feb 7, 2025
cbb81ac
Unqualified use of member variables beginning with an underscore
trueqbit Feb 7, 2025
23f66e0
Reordered connection holder's members for the purpose of L1 cache line
trueqbit Feb 10, 2025
14a2aa0
Ability to specify connection control options when making 'storage'
trueqbit Feb 10, 2025
de7cee0
Corrected a pre-processor condition
trueqbit Feb 11, 2025
9f5654f
Added another static unit test for `create_from_tuple()`
trueqbit Feb 13, 2025
ef12ae4
Corrected UT for tuple transformation
trueqbit Feb 16, 2025
3ed7507
Renamed public-facing connection control member variable
trueqbit Feb 16, 2025
78bd3ff
Merge branch 'upstream/dev' into upstream/experimental/threadsafe_con…
trueqbit Feb 19, 2025
9158a26
Merge branch upstream/dev into upstream/experimental/threadsafe_conne…
trueqbit Feb 19, 2025
1cd81b4
Merge branch 'upstream/dev' into upstream/experimental/threadsafe_con…
trueqbit Mar 20, 2025
e186657
Maximum efficient result row stepping loop with `goto`
trueqbit Mar 21, 2025
4adb678
Enabled SQLite extended error codes
trueqbit Mar 21, 2025
4df56f1
Maximum efficient result row stepping loop
trueqbit Mar 22, 2025
4ff1aff
Removed superfluous value cast to char
trueqbit Mar 22, 2025
815bec4
Used C API for strings from `std` namespace in unit tests
trueqbit Mar 23, 2025
f41c81a
Merge branch 'upstream/dev' into upstream/experimental/threadsafe_con…
trueqbit Aug 26, 2025
14dd6b4
Suppressed warning about over aligned structs
trueqbit Aug 26, 2025
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;
#ifdef 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{};
#ifdef 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