From acc22b7d141632e5028be3c4cc96aed22114ebb2 Mon Sep 17 00:00:00 2001 From: Daniel Frey Date: Sat, 2 Mar 2024 20:50:09 +0100 Subject: [PATCH] Make 'creating' counter exception safe --- src/test/pq/connection_pool.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/test/pq/connection_pool.cpp b/src/test/pq/connection_pool.cpp index 46c0fa9..a224b65 100644 --- a/src/test/pq/connection_pool.cpp +++ b/src/test/pq/connection_pool.cpp @@ -10,6 +10,22 @@ class limited_connection_pool : public tao::pq::connection_pool { + struct guard + { + std::atomic< std::size_t >& m_counter; + + explicit guard( std::atomic< std::size_t >& counter ) noexcept + : m_counter( counter ) + { + ++m_counter; + } + + ~guard() + { + --m_counter; + } + }; + mutable std::atomic< std::size_t > m_creating = 0; using tao::pq::connection_pool::connection_pool; @@ -19,10 +35,8 @@ class limited_connection_pool if( attached() >= 4 || ( m_creating.load() > 2 ) ) { throw std::runtime_error( "connection limit reached" ); } - ++m_creating; - auto c = connection_pool::v_create(); - --m_creating; - return c; + const guard g( m_creating ); + return connection_pool::v_create(); } };