Skip to content

Commit

Permalink
Make 'creating' counter exception safe
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Mar 2, 2024
1 parent cf151d4 commit acc22b7
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/test/pq/connection_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
};

Expand Down

0 comments on commit acc22b7

Please sign in to comment.