Skip to content
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

Made connection holder usable in multithreaded contexts #1054

Merged
merged 3 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 4 additions & 5 deletions dev/connection_holder.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <sqlite3.h>
#include <atomic>
#include <string> // std::string

#include "error_code.h"
Expand All @@ -14,8 +15,7 @@ namespace sqlite_orm {
connection_holder(std::string filename_) : filename(move(filename_)) {}

void retain() {
++this->_retain_count;
if(1 == this->_retain_count) {
if(1 == ++this->_retain_count) {
auto rc = sqlite3_open(this->filename.c_str(), &this->db);
if(rc != SQLITE_OK) {
throw_translated_sqlite_error(db);
Expand All @@ -24,8 +24,7 @@ namespace sqlite_orm {
}

void release() {
--this->_retain_count;
if(0 == this->_retain_count) {
if(0 == --this->_retain_count) {
auto rc = sqlite3_close(this->db);
if(rc != SQLITE_OK) {
throw_translated_sqlite_error(db);
Expand All @@ -45,7 +44,7 @@ namespace sqlite_orm {

protected:
sqlite3* db = nullptr;
int _retain_count = 0;
std::atomic_int _retain_count = 0;
fnc12 marked this conversation as resolved.
Show resolved Hide resolved
};

struct connection_ref {
Expand Down
9 changes: 4 additions & 5 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -10842,6 +10842,7 @@ namespace sqlite_orm {
// #include "connection_holder.h"

#include <sqlite3.h>
#include <atomic>
#include <string> // std::string

// #include "error_code.h"
Expand All @@ -10855,8 +10856,7 @@ namespace sqlite_orm {
connection_holder(std::string filename_) : filename(move(filename_)) {}

void retain() {
++this->_retain_count;
if(1 == this->_retain_count) {
if(1 == ++this->_retain_count) {
auto rc = sqlite3_open(this->filename.c_str(), &this->db);
if(rc != SQLITE_OK) {
throw_translated_sqlite_error(db);
Expand All @@ -10865,8 +10865,7 @@ namespace sqlite_orm {
}

void release() {
--this->_retain_count;
if(0 == this->_retain_count) {
if(0 == --this->_retain_count) {
auto rc = sqlite3_close(this->db);
if(rc != SQLITE_OK) {
throw_translated_sqlite_error(db);
Expand All @@ -10886,7 +10885,7 @@ namespace sqlite_orm {

protected:
sqlite3* db = nullptr;
int _retain_count = 0;
std::atomic_int _retain_count = 0;
};

struct connection_ref {
Expand Down