summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2022-02-07 01:11:40 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2022-02-07 19:46:57 +0000
commit035650967d9374a0c88e2dfab5b222e4facd7f83 (patch)
treeccddf9429568d84c9f90557ba840f4b22ac99196
parentBump iwyu path and fix includes accordingly (diff)
downloadlibadhocutil-035650967d9374a0c88e2dfab5b222e4facd7f83.tar.bz2
libadhocutil-035650967d9374a0c88e2dfab5b222e4facd7f83.tar.xz
libadhocutil-035650967d9374a0c88e2dfab5b222e4facd7f83.zip
Remove C++17/Clang12 semaphore polyfills
-rw-r--r--libadhocutil/polyfill-semaphore.cpp43
-rw-r--r--libadhocutil/polyfill-semaphore.h53
-rw-r--r--libadhocutil/resourcePool.cpp6
-rw-r--r--libadhocutil/resourcePool.h5
-rw-r--r--libadhocutil/unittests/Jamfile.jam11
-rw-r--r--libadhocutil/unittests/testResourcePool.cpp5
-rw-r--r--libadhocutil/unittests/testSemaphore.cpp66
7 files changed, 2 insertions, 187 deletions
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 <chrono>
-
-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 <chrono>
-#include <condition_variable>
-#include <cstddef>
-#include <mutex>
-
-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<class Rep, class Period>
- bool
- try_acquire_for(const std::chrono::duration<Rep, Period> & rel_time)
- {
- return std::chrono::duration_cast<std::chrono::milliseconds>(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 <semaphore>
-#else
-# include "polyfill-semaphore.h"
-#endif
+#include <semaphore>
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 <semaphore>
-#endif
+#include <semaphore>
#include "visibility.h"
#include <chrono>
#include <cstddef>
@@ -16,7 +14,6 @@
#include <string>
#include <thread>
#include <tuple>
-// IWYU pragma: no_include "polyfill-semaphore.h"
namespace AdHoc {
template<typename Resource> 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
@@ -252,17 +252,6 @@ run
;
run
- testSemaphore.cpp
- : : :
- <define>BOOST_TEST_DYN_LINK
- <library>..//adhocutil
- <library>boost_utf
- <library>pthread
- :
- testSemaphore
- ;
-
-run
testException.cpp
: : :
<define>BOOST_TEST_DYN_LINK
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 <semaphore>
-#else
-# include "polyfill-semaphore.h" // IWYU pragma: keep
-#endif
#include <atomic>
#include <list>
#include <map>
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 <boost/test/unit_test.hpp>
-
-#include <polyfill-semaphore.h>
-#include <thread>
-#include <unistd.h>
-
-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();
-}