diff options
-rw-r--r-- | libadhocutil/semaphore.cpp | 6 | ||||
-rw-r--r-- | libadhocutil/semaphore.h | 2 | ||||
-rw-r--r-- | libadhocutil/unittests/testSemaphore.cpp | 7 |
3 files changed, 15 insertions, 0 deletions
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 ) |