From fb7ae2aa5994040abb1baed237ad25ba5e1e7b30 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 28 Dec 2015 16:37:31 +0000 Subject: Add support for querying semaphore value --- libadhocutil/semaphore.cpp | 6 ++++++ libadhocutil/semaphore.h | 2 ++ libadhocutil/unittests/testSemaphore.cpp | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/libadhocutil/semaphore.cpp b/libadhocutil/semaphore.cpp index 0500445..ac2900e 100644 --- a/libadhocutil/semaphore.cpp +++ b/libadhocutil/semaphore.cpp @@ -36,5 +36,11 @@ namespace AdHoc { --count; return true; } + + unsigned int + Semaphore::freeCount() const + { + return count; + } } diff --git a/libadhocutil/semaphore.h b/libadhocutil/semaphore.h index 3e7e476..3411543 100644 --- a/libadhocutil/semaphore.h +++ b/libadhocutil/semaphore.h @@ -22,6 +22,8 @@ namespace AdHoc { /// Wait for a single count with timeout. /// @param ms Timeout in milliseconds. bool wait(unsigned int ms); + /// Free + unsigned int freeCount() const; private: boost::mutex mutex; diff --git a/libadhocutil/unittests/testSemaphore.cpp b/libadhocutil/unittests/testSemaphore.cpp index bde73a1..38f9b8f 100644 --- a/libadhocutil/unittests/testSemaphore.cpp +++ b/libadhocutil/unittests/testSemaphore.cpp @@ -22,12 +22,19 @@ BOOST_AUTO_TEST_CASE( initial ) BOOST_AUTO_TEST_CASE( addRemoveSome ) { AdHoc::Semaphore s; + BOOST_REQUIRE_EQUAL(0, s.freeCount()); s.notify(); + BOOST_REQUIRE_EQUAL(1, s.freeCount()); s.notify(); + BOOST_REQUIRE_EQUAL(2, s.freeCount()); s.notify(); + BOOST_REQUIRE_EQUAL(3, s.freeCount()); s.wait(); + BOOST_REQUIRE_EQUAL(2, s.freeCount()); s.wait(); + BOOST_REQUIRE_EQUAL(1, s.freeCount()); s.wait(); + BOOST_REQUIRE_EQUAL(0, s.freeCount()); } BOOST_AUTO_TEST_CASE( addRemoveTimeOut ) -- cgit v1.2.3