Merge branch 'mutex'

Reuse soci_mutex_t and clean up connection_pool implementation.

See #1303.
This commit is contained in:
Vadim Zeitlin
2025-09-20 17:12:47 +02:00
4 changed files with 152 additions and 176 deletions

View File

@@ -0,0 +1,69 @@
//
// Copyright (C) 2025 Vadim Zeitlin
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//
#ifndef SOCI_PRIVATE_SOCI_MUTEX_H_INCLUDED
#define SOCI_PRIVATE_SOCI_MUTEX_H_INCLUDED
#ifdef _WIN32
#include <windows.h>
class soci_mutex_t
{
public:
soci_mutex_t() { ::InitializeCriticalSection(&cs_); }
~soci_mutex_t() { ::DeleteCriticalSection(&cs_); }
void lock() { ::EnterCriticalSection(&cs_); }
void unlock() { ::LeaveCriticalSection(&cs_); }
soci_mutex_t(soci_mutex_t const &) = delete;
soci_mutex_t& operator=(soci_mutex_t const &) = delete;
private:
CRITICAL_SECTION cs_;
};
#else // Assume POSIX
#include <pthread.h>
class soci_mutex_t
{
public:
soci_mutex_t() { pthread_mutex_init(&m_, NULL); }
soci_mutex_t(soci_mutex_t const &) = delete;
soci_mutex_t& operator=(soci_mutex_t const &) = delete;
~soci_mutex_t() { pthread_mutex_destroy(&m_); }
void lock() { pthread_mutex_lock(&m_); }
void unlock() { pthread_mutex_unlock(&m_); }
// This should be only used with pthread_cond_[timed]wait().
pthread_mutex_t * native_handle() { return &m_; }
private:
pthread_mutex_t m_;
};
#endif // _WIN32/POSIX
class soci_scoped_lock
{
public:
explicit soci_scoped_lock(soci_mutex_t * m) : m_(m) { m_->lock(); };
~soci_scoped_lock() { m_->unlock(); };
soci_scoped_lock(soci_scoped_lock const &) = delete;
soci_scoped_lock& operator=(soci_scoped_lock const &) = delete;
private:
soci_mutex_t * const m_;
};
#endif // SOCI_PRIVATE_SOCI_MUTEX_H_INCLUDED

View File

@@ -11,6 +11,7 @@
#include "soci/soci-platform.h"
// std
#include <cstddef>
#include <memory>
namespace soci
{
@@ -31,7 +32,7 @@ public:
private:
struct connection_pool_impl;
connection_pool_impl * pimpl_;
std::unique_ptr<connection_pool_impl> pimpl_;
SOCI_NOT_COPYABLE(connection_pool)
};

View File

@@ -15,6 +15,8 @@
#include <string>
#include <vector>
#include "soci-mutex.h"
using namespace soci;
using namespace soci::dynamic_backends;
@@ -27,22 +29,6 @@ extern "C" IMAGE_DOS_HEADER __ImageBase;
namespace
{
class soci_mutex_t
{
public:
soci_mutex_t() { ::InitializeCriticalSection(&cs_); }
~soci_mutex_t() { ::DeleteCriticalSection(&cs_); }
void lock() { ::EnterCriticalSection(&cs_); }
void unlock() { ::LeaveCriticalSection(&cs_); }
soci_mutex_t(soci_mutex_t const &) = delete;
soci_mutex_t& operator=(soci_mutex_t const &) = delete;
private:
CRITICAL_SECTION cs_;
};
typedef HMODULE soci_dynlib_handle_t;
std::string get_this_dynlib_path()
@@ -126,28 +112,11 @@ private:
#else
#include <pthread.h>
#include <dlfcn.h>
namespace
{
class soci_mutex_t
{
public:
soci_mutex_t() { pthread_mutex_init(&m_, NULL); }
soci_mutex_t(soci_mutex_t const &) = delete;
soci_mutex_t& operator=(soci_mutex_t const &) = delete;
~soci_mutex_t() { pthread_mutex_destroy(&m_); }
void lock() { pthread_mutex_lock(&m_); }
void unlock() { pthread_mutex_unlock(&m_); }
private:
pthread_mutex_t m_;
};
typedef void * soci_dynlib_handle_t;
std::string get_this_dynlib_path()
@@ -293,19 +262,6 @@ struct static_state_mgr
}
} static_state_mgr_;
class scoped_lock
{
public:
explicit scoped_lock(soci_mutex_t * m) : m_(m) { m_->lock(); };
~scoped_lock() { m_->unlock(); };
scoped_lock(scoped_lock const &) = delete;
scoped_lock& operator=(scoped_lock const &) = delete;
private:
soci_mutex_t * const m_;
};
// non-synchronized helpers for the other functions
factory_map::iterator do_unload(factory_map::iterator i)
{
@@ -439,7 +395,7 @@ void do_register_backend(std::string const & name, std::string const & shared_ob
backend_factory const& dynamic_backends::get(std::string const& name)
{
scoped_lock lock(&mutex_);
soci_scoped_lock lock(&mutex_);
factory_map::iterator i = factories_.find(name);
@@ -461,7 +417,7 @@ backend_factory const& dynamic_backends::get(std::string const& name)
void dynamic_backends::unget(std::string const& name)
{
scoped_lock lock(&mutex_);
soci_scoped_lock lock(&mutex_);
factory_map::iterator i = factories_.find(name);
@@ -488,7 +444,7 @@ void dynamic_backends::unget(std::string const& name)
SOCI_DECL std::vector<std::string>& dynamic_backends::search_paths()
{
scoped_lock lock(&mutex_);
soci_scoped_lock lock(&mutex_);
return get_default_search_paths();
}
@@ -496,7 +452,7 @@ SOCI_DECL std::vector<std::string>& dynamic_backends::search_paths()
SOCI_DECL void dynamic_backends::register_backend(
std::string const& name, std::string const& shared_object)
{
scoped_lock lock(&mutex_);
soci_scoped_lock lock(&mutex_);
do_register_backend(name, shared_object);
}
@@ -504,7 +460,7 @@ SOCI_DECL void dynamic_backends::register_backend(
SOCI_DECL void dynamic_backends::register_backend(
std::string const& name, backend_factory const& factory)
{
scoped_lock lock(&mutex_);
soci_scoped_lock lock(&mutex_);
do_unload_or_throw_if_in_use(name);
@@ -516,7 +472,7 @@ SOCI_DECL void dynamic_backends::register_backend(
SOCI_DECL std::vector<std::string> dynamic_backends::list_all()
{
scoped_lock lock(&mutex_);
soci_scoped_lock lock(&mutex_);
std::vector<std::string> ret;
ret.reserve(factories_.size());
@@ -532,7 +488,7 @@ SOCI_DECL std::vector<std::string> dynamic_backends::list_all()
SOCI_DECL void dynamic_backends::unload(std::string const& name)
{
scoped_lock lock(&mutex_);
soci_scoped_lock lock(&mutex_);
factory_map::iterator i = factories_.find(name);
@@ -553,7 +509,7 @@ SOCI_DECL void dynamic_backends::unload(std::string const& name)
SOCI_DECL void dynamic_backends::unload_all()
{
scoped_lock lock(&mutex_);
soci_scoped_lock lock(&mutex_);
for (factory_map::iterator i = factories_.begin(); i != factories_.end(); )
{

View File

@@ -11,17 +11,29 @@
#include <vector>
#include <utility>
#ifndef _WIN32
// POSIX implementation
#include <pthread.h>
#include <sys/time.h>
#include <errno.h>
#include "soci-mutex.h"
using namespace soci;
struct connection_pool::connection_pool_impl
// Common base class for both POSIX and Windows implementations.
struct soci_connection_pool_base_impl
{
explicit soci_connection_pool_base_impl(std::size_t size)
{
if (size == 0)
{
throw soci_error("Invalid pool size");
}
sessions_.resize(size);
for (std::size_t i = 0; i != size; ++i)
{
sessions_[i] = std::make_pair(true, std::make_unique<session>());
}
}
~soci_connection_pool_base_impl() = default;
bool find_free(std::size_t & pos)
{
for (std::size_t i = 0; i != sessions_.size(); ++i)
@@ -36,51 +48,40 @@ struct connection_pool::connection_pool_impl
return false;
}
// by convention, first == true means the entry is free (not used)
std::vector<std::pair<bool, session *> > sessions_;
pthread_mutex_t mtx_;
pthread_cond_t cond_;
std::vector<std::pair<bool, std::unique_ptr<session>> > sessions_;
};
connection_pool::connection_pool(std::size_t size)
#ifndef _WIN32
// POSIX implementation
#include <pthread.h>
#include <sys/time.h>
#include <errno.h>
struct connection_pool::connection_pool_impl : soci_connection_pool_base_impl
{
if (size == 0)
explicit connection_pool_impl(std::size_t size)
: soci_connection_pool_base_impl(size)
{
throw soci_error("Invalid pool size");
if (pthread_cond_init(&cond_, NULL) != 0)
{
throw soci_error("Synchronization error");
}
}
pimpl_ = new connection_pool_impl();
pimpl_->sessions_.resize(size);
for (std::size_t i = 0; i != size; ++i)
connection_pool_impl(connection_pool_impl const&) = delete;
connection_pool_impl& operator=(connection_pool_impl const&) = delete;
~connection_pool_impl()
{
pimpl_->sessions_[i] = std::make_pair(true, new session());
pthread_cond_destroy(&cond_);
}
int cc = pthread_mutex_init(&(pimpl_->mtx_), NULL);
if (cc != 0)
{
throw soci_error("Synchronization error");
}
cc = pthread_cond_init(&(pimpl_->cond_), NULL);
if (cc != 0)
{
throw soci_error("Synchronization error");
}
}
connection_pool::~connection_pool()
{
for (std::size_t i = 0; i != pimpl_->sessions_.size(); ++i)
{
delete pimpl_->sessions_[i].second;
}
pthread_mutex_destroy(&(pimpl_->mtx_));
pthread_cond_destroy(&(pimpl_->cond_));
delete pimpl_;
}
soci_mutex_t mtx_;
pthread_cond_t cond_;
};
bool connection_pool::try_lease(std::size_t & pos, int timeout)
{
@@ -102,24 +103,22 @@ bool connection_pool::try_lease(std::size_t & pos, int timeout)
}
}
int cc = pthread_mutex_lock(&(pimpl_->mtx_));
if (cc != 0)
{
throw soci_error("Synchronization error");
}
soci_scoped_lock lock(&pimpl_->mtx_);
int cc = 0;
while (pimpl_->find_free(pos) == false)
{
if (timeout < 0)
{
// no timeout, allow unlimited blocking
cc = pthread_cond_wait(&(pimpl_->cond_), &(pimpl_->mtx_));
cc = pthread_cond_wait(
&(pimpl_->cond_), pimpl_->mtx_.native_handle());
}
else
{
// wait with timeout
cc = pthread_cond_timedwait(
&(pimpl_->cond_), &(pimpl_->mtx_), &tm);
&(pimpl_->cond_), pimpl_->mtx_.native_handle(), &tm);
}
if (cc == ETIMEDOUT)
@@ -143,8 +142,6 @@ bool connection_pool::try_lease(std::size_t & pos, int timeout)
pimpl_->sessions_[pos].first = false;
}
pthread_mutex_unlock(&(pimpl_->mtx_));
if (cc != 0)
{
// we can only fail if timeout expired
@@ -166,22 +163,15 @@ void connection_pool::give_back(std::size_t pos)
throw soci_error("Invalid pool position");
}
int cc = pthread_mutex_lock(&(pimpl_->mtx_));
if (cc != 0)
{
throw soci_error("Synchronization error");
}
soci_scoped_lock lock(&pimpl_->mtx_);
if (pimpl_->sessions_[pos].first)
{
pthread_mutex_unlock(&(pimpl_->mtx_));
throw soci_error("Cannot release pool entry (already free)");
}
pimpl_->sessions_[pos].first = true;
pthread_mutex_unlock(&(pimpl_->mtx_));
pthread_cond_signal(&(pimpl_->cond_));
}
@@ -190,71 +180,29 @@ void connection_pool::give_back(std::size_t pos)
#include <windows.h>
using namespace soci;
struct connection_pool::connection_pool_impl
struct connection_pool::connection_pool_impl : soci_connection_pool_base_impl
{
bool find_free(std::size_t & pos)
explicit connection_pool_impl(std::size_t size)
: soci_connection_pool_base_impl(size)
{
for (std::size_t i = 0; i != sessions_.size(); ++i)
// initially all entries are available
sem_ = CreateSemaphore(NULL,
static_cast<LONG>(size), static_cast<LONG>(size), NULL);
if (sem_ == NULL)
{
if (sessions_[i].first)
{
pos = i;
return true;
}
throw soci_error("Synchronization error");
}
return false;
}
// by convention, first == true means the entry is free (not used)
std::vector<std::pair<bool, session *> > sessions_;
~connection_pool_impl()
{
CloseHandle(sem_);
}
CRITICAL_SECTION mtx_;
soci_mutex_t mtx_;
HANDLE sem_;
};
connection_pool::connection_pool(std::size_t size)
{
if (size == 0)
{
throw soci_error("Invalid pool size");
}
pimpl_ = new connection_pool_impl();
pimpl_->sessions_.resize(size);
for (std::size_t i = 0; i != size; ++i)
{
pimpl_->sessions_[i] = std::make_pair(true, new session());
}
InitializeCriticalSection(&(pimpl_->mtx_));
// initially all entries are available
HANDLE s = CreateSemaphore(NULL,
static_cast<LONG>(size), static_cast<LONG>(size), NULL);
if (s == NULL)
{
throw soci_error("Synchronization error");
}
pimpl_->sem_ = s;
}
connection_pool::~connection_pool()
{
for (std::size_t i = 0; i != pimpl_->sessions_.size(); ++i)
{
delete pimpl_->sessions_[i].second;
}
DeleteCriticalSection(&(pimpl_->mtx_));
CloseHandle(pimpl_->sem_);
delete pimpl_;
}
bool connection_pool::try_lease(std::size_t & pos, int timeout)
{
DWORD cc = WaitForSingleObject(pimpl_->sem_,
@@ -263,7 +211,7 @@ bool connection_pool::try_lease(std::size_t & pos, int timeout)
{
// semaphore acquired, there is (at least) one free entry
EnterCriticalSection(&(pimpl_->mtx_));
soci_scoped_lock lock(&pimpl_->mtx_);
if (!pimpl_->find_free(pos))
{
@@ -273,8 +221,6 @@ bool connection_pool::try_lease(std::size_t & pos, int timeout)
pimpl_->sessions_[pos].first = false;
LeaveCriticalSection(&(pimpl_->mtx_));
return true;
}
else if (cc == WAIT_TIMEOUT)
@@ -294,23 +240,27 @@ void connection_pool::give_back(std::size_t pos)
throw soci_error("Invalid pool position");
}
EnterCriticalSection(&(pimpl_->mtx_));
soci_scoped_lock lock(&pimpl_->mtx_);
if (pimpl_->sessions_[pos].first)
{
LeaveCriticalSection(&(pimpl_->mtx_));
throw soci_error("Cannot release pool entry (already free)");
}
pimpl_->sessions_[pos].first = true;
LeaveCriticalSection(&(pimpl_->mtx_));
ReleaseSemaphore(pimpl_->sem_, 1, NULL);
}
#endif // _WIN32
connection_pool::connection_pool(std::size_t size)
: pimpl_(std::make_unique<connection_pool_impl>(size))
{
}
connection_pool::~connection_pool() = default;
session & connection_pool::at(std::size_t pos)
{
if (pos >= pimpl_->sessions_.size())