diff options
-rw-r--r-- | libadhocutil/resourcePool.cpp | 11 | ||||
-rw-r--r-- | libadhocutil/resourcePool.h | 7 | ||||
-rw-r--r-- | libadhocutil/unittests/testResourcePool.cpp | 5 |
3 files changed, 8 insertions, 15 deletions
diff --git a/libadhocutil/resourcePool.cpp b/libadhocutil/resourcePool.cpp index c7980d0..4145068 100644 --- a/libadhocutil/resourcePool.cpp +++ b/libadhocutil/resourcePool.cpp @@ -3,27 +3,24 @@ #include <semaphore> namespace AdHoc { - ResourcePoolBase::ResourcePoolBase(std::ptrdiff_t maxSize, std::size_t keep_) : - keep {keep_}, poolSize {std::make_unique<SemaphoreType>(maxSize)} - { - } + ResourcePoolBase::ResourcePoolBase(std::ptrdiff_t maxSize, std::size_t keep_) : keep {keep_}, poolSize {maxSize} { } void ResourcePoolBase::release() { - poolSize->release(); + poolSize.release(); } void ResourcePoolBase::acquire() { - poolSize->acquire(); + poolSize.acquire(); } bool ResourcePoolBase::try_acquire_for(std::chrono::milliseconds timeout) { - return poolSize->try_acquire_for(timeout); + return poolSize.try_acquire_for(timeout); } ResourcePoolBase::~ResourcePoolBase() = default; diff --git a/libadhocutil/resourcePool.h b/libadhocutil/resourcePool.h index 6761a16..89d9b99 100644 --- a/libadhocutil/resourcePool.h +++ b/libadhocutil/resourcePool.h @@ -65,17 +65,12 @@ namespace AdHoc { bool try_acquire_for(std::chrono::milliseconds); void release(); -#ifdef __cpp_lib_semaphore - using SemaphoreType = std::counting_semaphore<>; -#else - using SemaphoreType = class Semaphore; -#endif protected: mutable std::shared_mutex lock; std::size_t keep; private: - std::unique_ptr<SemaphoreType> poolSize; + std::counting_semaphore<> poolSize; }; /// A fully featured resource pool for sharing and reusing a finite set of diff --git a/libadhocutil/unittests/testResourcePool.cpp b/libadhocutil/unittests/testResourcePool.cpp index 79c53ad..fb34c5f 100644 --- a/libadhocutil/unittests/testResourcePool.cpp +++ b/libadhocutil/unittests/testResourcePool.cpp @@ -9,6 +9,7 @@ #include <map> #include <memory> #include <mutex> +#include <semaphore> #include <stdexcept> #include <thread> #include <unistd.h> @@ -329,7 +330,7 @@ BOOST_AUTO_TEST_CASE(threading1, *boost::unit_test::timeout(10)) } static void -acquireAndKeepFor1Second(TRPSmall * pool, AdHoc::ResourcePoolBase::SemaphoreType & s) +acquireAndKeepFor1Second(TRPSmall * pool, std::counting_semaphore<> & s) { auto r = pool->get(); static std::mutex m; @@ -343,7 +344,7 @@ acquireAndKeepFor1Second(TRPSmall * pool, AdHoc::ResourcePoolBase::SemaphoreType BOOST_AUTO_TEST_CASE(threading2) { TRPSmall pool; - AdHoc::ResourcePoolBase::SemaphoreType s {0}; + std::counting_semaphore<> s {0}; std::thread t1([&pool, &s]() { acquireAndKeepFor1Second(&pool, s); }); |