diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-04-06 11:30:10 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-04-11 21:34:04 +0100 |
commit | c09260d8b792bb1b9df770d1ce3d3e2d75fa4dff (patch) | |
tree | 5f5341c4683f6fc4cb598f8fd7c396028ac72b54 | |
parent | C++17 (diff) | |
download | libadhocutil-c09260d8b792bb1b9df770d1ce3d3e2d75fa4dff.tar.bz2 libadhocutil-c09260d8b792bb1b9df770d1ce3d3e2d75fa4dff.tar.xz libadhocutil-c09260d8b792bb1b9df770d1ce3d3e2d75fa4dff.zip |
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.
-rw-r--r-- | libadhocutil/resourcePool.h | 3 | ||||
-rw-r--r-- | 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<std::shared_ptr<Resource>, ResourcePool<Resource> *, std::atomic<unsigned int>> Object; + typedef std::tuple<std::shared_ptr<Resource>, ResourcePool<Resource> *> Object; /// Create a reference to a new resource. ResourceHandle(const std::shared_ptr<Object> &); @@ -43,7 +43,6 @@ namespace AdHoc { unsigned int handleCount() const; private: - DLL_PRIVATE void incRef() const; DLL_PRIVATE void decRef(); std::shared_ptr<Object> 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<R>::ResourceHandle(const std::shared_ptr<Object> & o) : resource(o) { - incRef(); } template <typename R> ResourceHandle<R>::ResourceHandle(const ResourceHandle & rh) : resource(rh.resource) { - incRef(); } template <typename R> @@ -39,7 +37,8 @@ namespace AdHoc { ResourceHandle<R>::handleCount() const { ASSERT(resource); - return std::get<2>(*resource); + // InUse has one, we don't count that + return resource.use_count() - 1; } template <typename R> @@ -79,15 +78,6 @@ namespace AdHoc { decRef(); } resource = rh.resource; - incRef(); - } - - template <typename R> - void - ResourceHandle<R>::incRef() const - { - ASSERT(resource); - ++std::get<2>(*resource); } template <typename R> @@ -95,7 +85,8 @@ namespace AdHoc { ResourceHandle<R>::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<typename ResourceHandle<R>::Object>(r, this, 0); + auto ro = std::make_shared<typename ResourceHandle<R>::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<typename ResourceHandle<R>::Object>(createResource(), this, 0); + auto ro = std::make_shared<typename ResourceHandle<R>::Object>(createResource(), this); inUse.insert({ std::this_thread::get_id(), ro }); return ro; } |