Skip to content

Commit

Permalink
lib: allow shrinking of resource_pools
Browse files Browse the repository at this point in the history
References: DESK-1497
  • Loading branch information
jengelh committed Dec 11, 2023
1 parent bc738ce commit 8061c4e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions include/gromox/resource_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ namespace gromox {
* of sense to construct them a-priori, so the API only supports lazy
* construction. For this, get_wait() needs to know some ctor args for when a
* new T needs construction.
*
* @m_numslots: maximum number of objects the pool is willing to hand out
* @m_list: previously-constructed objects that the pool can immediately
* hand out again (if m_list.size() < m_numslots, the pool will
* create objects on-the-fly)
*/
template<typename Tp> class resource_pool {
public:
Expand Down Expand Up @@ -92,8 +97,15 @@ template<typename Tp> class resource_pool {

public:
void resize(size_t n) {
if (m_numslots < n)
m_numslots = n;
/*
* Lock-free: Updating m_numslots is atomic; and n>o check can
* happen unguarded because the "erroneously" woken-up thread
* itself will check m_numslots.
*/
size_t o = m_numslots.load();
m_numslots = n;
if (n > o)
m_cv.notify_one();
}
void clear() {
std::lock_guard lk(m_mtx);
Expand Down

0 comments on commit 8061c4e

Please sign in to comment.