From 035650967d9374a0c88e2dfab5b222e4facd7f83 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 7 Feb 2022 01:11:40 +0000 Subject: Remove C++17/Clang12 semaphore polyfills --- libadhocutil/polyfill-semaphore.cpp | 43 ------------------- libadhocutil/polyfill-semaphore.h | 53 ----------------------- libadhocutil/resourcePool.cpp | 6 +-- libadhocutil/resourcePool.h | 5 +-- libadhocutil/unittests/Jamfile.jam | 11 ----- libadhocutil/unittests/testResourcePool.cpp | 5 --- libadhocutil/unittests/testSemaphore.cpp | 66 ----------------------------- 7 files changed, 2 insertions(+), 187 deletions(-) delete mode 100644 libadhocutil/polyfill-semaphore.cpp delete mode 100644 libadhocutil/polyfill-semaphore.h delete mode 100644 libadhocutil/unittests/testSemaphore.cpp diff --git a/libadhocutil/polyfill-semaphore.cpp b/libadhocutil/polyfill-semaphore.cpp deleted file mode 100644 index efe5be6..0000000 --- a/libadhocutil/polyfill-semaphore.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "polyfill-semaphore.h" -#include - -namespace AdHoc { - Semaphore::Semaphore(std::ptrdiff_t initial) : count(initial) { } - - void - Semaphore::release() - { - std::scoped_lock lock(mutex); - ++count; - condition.notify_one(); - } - - void - Semaphore::acquire() - { - std::unique_lock lock(mutex); - while (!count) { - condition.wait(lock); - } - --count; - } - - bool - Semaphore::try_acquire_for(std::chrono::milliseconds timeout) - { - std::unique_lock lock(mutex); - while (!count) { - if (condition.wait_for(lock, timeout) == std::cv_status::timeout) { - return false; - } - } - --count; - return true; - } - - std::ptrdiff_t - Semaphore::freeCount() const - { - return count; - } -} diff --git a/libadhocutil/polyfill-semaphore.h b/libadhocutil/polyfill-semaphore.h deleted file mode 100644 index 9279b26..0000000 --- a/libadhocutil/polyfill-semaphore.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef ADHOCUTIL_SEMAPHORE_H -#define ADHOCUTIL_SEMAPHORE_H - -// Borrowed from StackOverflow -// http://stackoverflow.com/questions/4792449/c0x-has-no-semaphores-how-to-synchronize-threads - -#include "visibility.h" -#include -#include -#include -#include - -namespace AdHoc { - /// A portable semaphore with timeout support - class DLL_PUBLIC Semaphore { - public: - /// Construct a new semaphore with optional initial count. - explicit Semaphore(std::ptrdiff_t initial); - - /// Notify one waiting thread. - void release(); - /// Wait for a single count. - void acquire(); - /// Wait for a single count with timeout. - /// @param ms Timeout how long to wait. - bool try_acquire_for(std::chrono::milliseconds ms); - /// Wait for a single count with timeout. - /// @param ms Timeout how long to wait. - bool - try_acquire_for(unsigned int ms) - { - return try_acquire_for(std::chrono::milliseconds(ms)); - } - /// Wait for a single count with timeout. - /// @param ms Timeout how long to wait. - template - bool - try_acquire_for(const std::chrono::duration & rel_time) - { - return std::chrono::duration_cast(rel_time); - } - - /// Free - [[nodiscard]] std::ptrdiff_t freeCount() const; - - private: - std::mutex mutex; - std::condition_variable condition; - std::ptrdiff_t count; - }; -} - -#endif diff --git a/libadhocutil/resourcePool.cpp b/libadhocutil/resourcePool.cpp index 5e9bbb0..c7980d0 100644 --- a/libadhocutil/resourcePool.cpp +++ b/libadhocutil/resourcePool.cpp @@ -1,10 +1,6 @@ #include "resourcePool.h" #include "compileTimeFormatter.h" -#ifdef __cpp_lib_semaphore -# include -#else -# include "polyfill-semaphore.h" -#endif +#include namespace AdHoc { ResourcePoolBase::ResourcePoolBase(std::ptrdiff_t maxSize, std::size_t keep_) : diff --git a/libadhocutil/resourcePool.h b/libadhocutil/resourcePool.h index 1a29dc9..495eb61 100644 --- a/libadhocutil/resourcePool.h +++ b/libadhocutil/resourcePool.h @@ -3,9 +3,7 @@ #include "c++11Helpers.h" #include "exception.h" -#ifdef __cpp_lib_semaphore -# include -#endif +#include #include "visibility.h" #include #include @@ -16,7 +14,6 @@ #include #include #include -// IWYU pragma: no_include "polyfill-semaphore.h" namespace AdHoc { template class ResourcePool; diff --git a/libadhocutil/unittests/Jamfile.jam b/libadhocutil/unittests/Jamfile.jam index adb2645..9547322 100644 --- a/libadhocutil/unittests/Jamfile.jam +++ b/libadhocutil/unittests/Jamfile.jam @@ -251,17 +251,6 @@ run testFactory ; -run - testSemaphore.cpp - : : : - BOOST_TEST_DYN_LINK - ..//adhocutil - boost_utf - pthread - : - testSemaphore - ; - run testException.cpp : : : diff --git a/libadhocutil/unittests/testResourcePool.cpp b/libadhocutil/unittests/testResourcePool.cpp index cecfb46..79c53ad 100644 --- a/libadhocutil/unittests/testResourcePool.cpp +++ b/libadhocutil/unittests/testResourcePool.cpp @@ -4,11 +4,6 @@ #include "c++11Helpers.h" #include "lockHelpers.h" #include "resourcePool.impl.h" -#ifdef __cpp_lib_semaphore -# include -#else -# include "polyfill-semaphore.h" // IWYU pragma: keep -#endif #include #include #include diff --git a/libadhocutil/unittests/testSemaphore.cpp b/libadhocutil/unittests/testSemaphore.cpp deleted file mode 100644 index 999cff3..0000000 --- a/libadhocutil/unittests/testSemaphore.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#define BOOST_TEST_MODULE Semaphore -#include - -#include -#include -#include - -BOOST_AUTO_TEST_CASE(addRemoveOne) -{ - AdHoc::Semaphore s {0}; - s.release(); - s.acquire(); -} - -BOOST_AUTO_TEST_CASE(initial) -{ - AdHoc::Semaphore s(2); - s.acquire(); - s.acquire(); - // cppcheck-suppress assertWithSideEffect - BOOST_REQUIRE_EQUAL(false, s.try_acquire_for(0)); -} - -BOOST_AUTO_TEST_CASE(addRemoveSome) -{ - AdHoc::Semaphore s {0}; - BOOST_REQUIRE_EQUAL(0, s.freeCount()); - s.release(); - BOOST_REQUIRE_EQUAL(1, s.freeCount()); - s.release(); - BOOST_REQUIRE_EQUAL(2, s.freeCount()); - s.release(); - BOOST_REQUIRE_EQUAL(3, s.freeCount()); - s.acquire(); - BOOST_REQUIRE_EQUAL(2, s.freeCount()); - s.acquire(); - BOOST_REQUIRE_EQUAL(1, s.freeCount()); - s.acquire(); - BOOST_REQUIRE_EQUAL(0, s.freeCount()); -} - -BOOST_AUTO_TEST_CASE(addRemoveTimeOut) -{ - AdHoc::Semaphore s {0}; - s.release(); - s.acquire(); - // cppcheck-suppress assertWithSideEffect - BOOST_REQUIRE_EQUAL(false, s.try_acquire_for(100)); - // cppcheck-suppress assertWithSideEffect - BOOST_REQUIRE_EQUAL(false, s.try_acquire_for(0)); -} - -BOOST_AUTO_TEST_CASE(addRemoveWait) -{ - AdHoc::Semaphore s {0}; - s.release(); - s.acquire(); - std::thread th([&s]() { - usleep(100000); - s.release(); - }); - // cppcheck-suppress assertWithSideEffect - BOOST_REQUIRE_EQUAL(false, s.try_acquire_for(1)); - s.acquire(); - th.join(); -} -- cgit v1.2.3