diff options
-rw-r--r-- | libadhocutil/resourcePool.h | 30 | ||||
-rw-r--r-- | libadhocutil/resourcePool.impl.h | 46 | ||||
-rw-r--r-- | libadhocutil/unittests/Jamfile.jam | 1 | ||||
-rw-r--r-- | libadhocutil/unittests/testResourcePool.cpp | 14 |
4 files changed, 44 insertions, 47 deletions
diff --git a/libadhocutil/resourcePool.h b/libadhocutil/resourcePool.h index 26517bf..791737a 100644 --- a/libadhocutil/resourcePool.h +++ b/libadhocutil/resourcePool.h @@ -1,8 +1,8 @@ #ifndef ADHOCUTIL_RESOURCEPOOL_H #define ADHOCUTIL_RESOURCEPOOL_H -#include <boost/tuple/tuple.hpp> -#include <boost/thread/shared_mutex.hpp> +#include <tuple> +#include <shared_mutex> #include <atomic> #include <thread> #include <list> @@ -20,10 +20,10 @@ namespace AdHoc { class DLL_PUBLIC ResourceHandle { public: /// Handle to an allocated resource, the pool it belongs to and a count of active references. - typedef boost::tuple<Resource *, ResourcePool<Resource> *, std::atomic<unsigned int>> Object; + typedef std::tuple<std::shared_ptr<Resource>, ResourcePool<Resource> *, std::atomic<unsigned int>> Object; /// Create a reference to a new resource. - ResourceHandle(Object *); + ResourceHandle(const std::shared_ptr<Object> &); /// Create a reference to an existing resource. ResourceHandle(const ResourceHandle &); ~ResourceHandle(); @@ -45,7 +45,7 @@ namespace AdHoc { private: DLL_PRIVATE void incRef() const; DLL_PRIVATE void decRef(); - Object * resource; + std::shared_ptr<Object> resource; }; /// A fully featured resource pool for sharing and reusing a finite set of @@ -80,25 +80,25 @@ namespace AdHoc { protected: /// Create a new resource instance to add to the pool. - virtual Resource * createResource() const = 0; + virtual std::shared_ptr<Resource> createResource() const = 0; /// Destroy an existing resource (defaults to delete). - virtual void destroyResource(Resource *) const throw(); + virtual void destroyResource(const std::shared_ptr<Resource> &) const throw(); /// Test a cached resource is still suitable for use before re-use (defaults to no-op). - virtual void testResource(const Resource *) const; + virtual void testResource(const std::shared_ptr<const Resource> &) const; /// Test a cached resource is still suitable for use on return (defaults to no-op). - virtual void returnTestResource(const Resource *) const; + virtual void returnTestResource(const std::shared_ptr<const Resource> &) const; private: - typedef std::list<Resource *> Available; - typedef std::multimap<std::thread::id, typename ResourceHandle<Resource>::Object *> InUse; + typedef std::list<std::shared_ptr<Resource>> Available; + typedef std::multimap<std::thread::id, std::shared_ptr<typename ResourceHandle<Resource>::Object>> InUse; - void putBack(Resource *); - void discard(Resource *); + void putBack(const std::shared_ptr<Resource> &); + void discard(const std::shared_ptr<Resource> &); - DLL_PRIVATE static void removeFrom(Resource *, InUse &); + DLL_PRIVATE static void removeFrom(const std::shared_ptr<Resource> &, InUse &); DLL_PRIVATE ResourceHandle<Resource> getOne(); - mutable boost::upgrade_mutex lock; + mutable std::shared_mutex lock; Semaphore poolSize; unsigned int keep; Available available; diff --git a/libadhocutil/resourcePool.impl.h b/libadhocutil/resourcePool.impl.h index f49a548..8153ce0 100644 --- a/libadhocutil/resourcePool.impl.h +++ b/libadhocutil/resourcePool.impl.h @@ -13,7 +13,7 @@ namespace AdHoc { // template <typename R> - ResourceHandle<R>::ResourceHandle(Object * o) : + ResourceHandle<R>::ResourceHandle(const std::shared_ptr<Object> & o) : resource(o) { incRef(); @@ -39,7 +39,7 @@ namespace AdHoc { ResourceHandle<R>::handleCount() const { ASSERT(resource); - return boost::get<2>(*resource); + return std::get<2>(*resource); } template <typename R> @@ -47,7 +47,7 @@ namespace AdHoc { ResourceHandle<R>::get() const { ASSERT(resource); - return boost::get<0>(*resource); + return std::get<0>(*resource).get(); } template <typename R> @@ -60,7 +60,7 @@ namespace AdHoc { template <typename R> ResourceHandle<R>::operator bool() const { - return resource; + return (bool)resource; } template <typename R> @@ -68,7 +68,7 @@ namespace AdHoc { ResourceHandle<R>::operator->() const { ASSERT(resource); - return boost::get<0>(*resource); + return std::get<0>(*resource).get(); } template <typename R> @@ -87,7 +87,7 @@ namespace AdHoc { ResourceHandle<R>::incRef() const { ASSERT(resource); - ++boost::get<2>(*resource); + ++std::get<2>(*resource); } template <typename R> @@ -95,18 +95,17 @@ namespace AdHoc { ResourceHandle<R>::decRef() { ASSERT(resource); - if (!--boost::get<2>(*resource)) { - if (auto & pool = boost::get<1>(*resource)) { + if (!--std::get<2>(*resource)) { + if (auto pool = std::get<1>(*resource)) { if (std::uncaught_exception()) { - pool->discard(boost::get<0>(*resource)); + pool->discard(std::get<0>(*resource)); } else { - pool->putBack(boost::get<0>(*resource)); + pool->putBack(std::get<0>(*resource)); } } - delete resource; } - resource = nullptr; + resource.reset(); } // @@ -127,27 +126,26 @@ namespace AdHoc { destroyResource(r); } for (auto & r : inUse) { - destroyResource(boost::get<0>(*r.second)); - boost::get<1>(*r.second) = nullptr; + destroyResource(std::get<0>(*r.second)); + std::get<1>(*r.second) = nullptr; } } template <typename R> void - ResourcePool<R>::destroyResource(R * r) const throw() + ResourcePool<R>::destroyResource(const std::shared_ptr<R> &) const throw() { - delete r; } template <typename R> void - ResourcePool<R>::testResource(const R *) const + ResourcePool<R>::testResource(const std::shared_ptr<const R> &) const { } template <typename R> void - ResourcePool<R>::returnTestResource(const R *) const + ResourcePool<R>::returnTestResource(const std::shared_ptr<const R> &) const { } @@ -233,7 +231,7 @@ namespace AdHoc { auto r = available.front(); try { testResource(r); - auto ro = new typename ResourceHandle<R>::Object(r, this); + auto ro = std::make_shared<typename ResourceHandle<R>::Object>(r, this, 0); available.pop_front(); inUse.insert({ std::this_thread::get_id(), ro }); return ro; @@ -243,14 +241,14 @@ namespace AdHoc { available.pop_front(); } } - auto ro = new typename ResourceHandle<R>::Object(createResource(), this); + auto ro = std::make_shared<typename ResourceHandle<R>::Object>(createResource(), this, 0); inUse.insert({ std::this_thread::get_id(), ro }); return ro; } template <typename R> void - ResourcePool<R>::putBack(R * r) + ResourcePool<R>::putBack(const std::shared_ptr<R> & r) { Lock(lock); removeFrom(r, inUse); @@ -271,7 +269,7 @@ namespace AdHoc { template <typename R> void - ResourcePool<R>::discard(R * r) + ResourcePool<R>::discard(const std::shared_ptr<R> & r) { Lock(lock); removeFrom(r, inUse); @@ -281,11 +279,11 @@ namespace AdHoc { template <typename R> void - ResourcePool<R>::removeFrom(R * r, InUse & inUse) + ResourcePool<R>::removeFrom(const std::shared_ptr<R> & r, InUse & inUse) { auto rs = inUse.equal_range(std::this_thread::get_id()); for (auto & ri = rs.first; ri != rs.second; ri++) { - if (boost::get<0>(*ri->second) == r) { + if (std::get<0>(*ri->second) == r) { inUse.erase(ri); return; } diff --git a/libadhocutil/unittests/Jamfile.jam b/libadhocutil/unittests/Jamfile.jam index 8a58ca0..56f29c9 100644 --- a/libadhocutil/unittests/Jamfile.jam +++ b/libadhocutil/unittests/Jamfile.jam @@ -232,7 +232,6 @@ run <define>BOOST_TEST_DYN_LINK <library>..//adhocutil <library>boost_utf - <library>boost_thread <library>boost_system <library>pthread : diff --git a/libadhocutil/unittests/testResourcePool.cpp b/libadhocutil/unittests/testResourcePool.cpp index bc469c4..875179d 100644 --- a/libadhocutil/unittests/testResourcePool.cpp +++ b/libadhocutil/unittests/testResourcePool.cpp @@ -25,9 +25,9 @@ class TRP : public AdHoc::ResourcePool<MockResource> { public: TRP() : AdHoc::ResourcePool<MockResource>(10, 10) { } protected: - MockResource * createResource() const override + std::shared_ptr<MockResource> createResource() const override { - return new MockResource(); + return std::make_shared<MockResource>(); } }; @@ -35,15 +35,15 @@ class TRPSmall : public AdHoc::ResourcePool<MockResource> { public: TRPSmall() : AdHoc::ResourcePool<MockResource>(3, 1) { } protected: - MockResource * createResource() const override + std::shared_ptr<MockResource> createResource() const override { - return new MockResource(); + return std::make_shared<MockResource>(); } }; class TRPCreateFail : public TRPSmall { protected: - MockResource * createResource() const override + std::shared_ptr<MockResource> createResource() const override { throw std::exception(); } @@ -51,7 +51,7 @@ class TRPCreateFail : public TRPSmall { class TRPReturnFail : public TRPSmall { protected: - void returnTestResource(const MockResource *) const override + void returnTestResource(const std::shared_ptr<const MockResource> &) const override { throw std::exception(); } @@ -296,7 +296,7 @@ BOOST_AUTO_TEST_CASE( threading2 ) class TTRP : public TRP { public: TTRP() : n(0) { } - void testResource(const MockResource *) const override + void testResource(const std::shared_ptr<const MockResource> &) const override { n += 1; if (n % 2) { |