summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-12-28 16:37:31 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2015-12-28 16:37:31 +0000
commitfb7ae2aa5994040abb1baed237ad25ba5e1e7b30 (patch)
tree64d206a201aeb80effae66fee59346076c4080ab
parentFix up and test behaviour of resource handles that are orphaned from a delete... (diff)
downloadlibadhocutil-fb7ae2aa5994040abb1baed237ad25ba5e1e7b30.tar.bz2
libadhocutil-fb7ae2aa5994040abb1baed237ad25ba5e1e7b30.tar.xz
libadhocutil-fb7ae2aa5994040abb1baed237ad25ba5e1e7b30.zip
Add support for querying semaphore value
-rw-r--r--libadhocutil/semaphore.cpp6
-rw-r--r--libadhocutil/semaphore.h2
-rw-r--r--libadhocutil/unittests/testSemaphore.cpp7
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 )