From c09260d8b792bb1b9df770d1ce3d3e2d75fa4dff Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 6 Apr 2018 11:30:10 +0100 Subject: C++17 Updates resource pool... We don't need our own usage counter when shared_ptr has a perfectly good one... So long as you remember it's one too high cos we keep one in InUse. --- libadhocutil/resourcePool.h | 3 +-- libadhocutil/resourcePool.impl.h | 21 ++++++--------------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/libadhocutil/resourcePool.h b/libadhocutil/resourcePool.h index f65bf0f..609d710 100644 --- a/libadhocutil/resourcePool.h +++ b/libadhocutil/resourcePool.h @@ -20,7 +20,7 @@ namespace AdHoc { class DLL_PUBLIC ResourceHandle { public: /// Handle to an allocated resource, the pool it belongs to and a count of active references. - typedef std::tuple, ResourcePool *, std::atomic> Object; + typedef std::tuple, ResourcePool *> Object; /// Create a reference to a new resource. ResourceHandle(const std::shared_ptr &); @@ -43,7 +43,6 @@ namespace AdHoc { unsigned int handleCount() const; private: - DLL_PRIVATE void incRef() const; DLL_PRIVATE void decRef(); std::shared_ptr resource; }; diff --git a/libadhocutil/resourcePool.impl.h b/libadhocutil/resourcePool.impl.h index 3d2f089..7dce653 100644 --- a/libadhocutil/resourcePool.impl.h +++ b/libadhocutil/resourcePool.impl.h @@ -16,14 +16,12 @@ namespace AdHoc { ResourceHandle::ResourceHandle(const std::shared_ptr & o) : resource(o) { - incRef(); } template ResourceHandle::ResourceHandle(const ResourceHandle & rh) : resource(rh.resource) { - incRef(); } template @@ -39,7 +37,8 @@ namespace AdHoc { ResourceHandle::handleCount() const { ASSERT(resource); - return std::get<2>(*resource); + // InUse has one, we don't count that + return resource.use_count() - 1; } template @@ -79,15 +78,6 @@ namespace AdHoc { decRef(); } resource = rh.resource; - incRef(); - } - - template - void - ResourceHandle::incRef() const - { - ASSERT(resource); - ++std::get<2>(*resource); } template @@ -95,7 +85,8 @@ namespace AdHoc { ResourceHandle::decRef() { ASSERT(resource); - if (!--std::get<2>(*resource)) { + // InUse has one, we don't count that + if (resource.use_count() == 2) { if (auto pool = std::get<1>(*resource)) { if (std::uncaught_exception()) { pool->discard(std::get<0>(*resource)); @@ -231,7 +222,7 @@ namespace AdHoc { auto & r = available.front(); try { testResource(r.get()); - auto ro = std::make_shared::Object>(r, this, 0); + auto ro = std::make_shared::Object>(r, this); available.pop_front(); inUse.insert({ std::this_thread::get_id(), ro }); return ro; @@ -241,7 +232,7 @@ namespace AdHoc { available.pop_front(); } } - auto ro = std::make_shared::Object>(createResource(), this, 0); + auto ro = std::make_shared::Object>(createResource(), this); inUse.insert({ std::this_thread::get_id(), ro }); return ro; } -- cgit v1.2.3