summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-08-29 00:34:09 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2015-08-29 00:36:34 +0100
commit4f57a463ef86212f1c09e440156a76554b85581b (patch)
tree25a45870e79a5db0729337601614fb1bc18eed7c
parentMigrate formatted string wrappers as macros into buffer.h (diff)
downloadlibadhocutil-4f57a463ef86212f1c09e440156a76554b85581b.tar.bz2
libadhocutil-4f57a463ef86212f1c09e440156a76554b85581b.tar.xz
libadhocutil-4f57a463ef86212f1c09e440156a76554b85581b.zip
Move and test lock helpers
-rw-r--r--libadhocutil/lockHelpers.h36
-rw-r--r--libadhocutil/unittests/Jamfile.jam13
-rw-r--r--libadhocutil/unittests/testLocks.cpp30
3 files changed, 79 insertions, 0 deletions
diff --git a/libadhocutil/lockHelpers.h b/libadhocutil/lockHelpers.h
new file mode 100644
index 0000000..85a1b73
--- /dev/null
+++ b/libadhocutil/lockHelpers.h
@@ -0,0 +1,36 @@
+#ifndef LIBADHOC_LOCKHELPERS_H
+#define LIBADHOC_LOCKHELPERS_H
+
+template <typename locktype>
+class _lockLoop {
+ public:
+ template<typename l>
+ _lockLoop(l & _l) :
+ lock(_l),
+ flag(true)
+ {
+ }
+
+ operator bool() const { return flag; }
+ void operator!() { flag = false; }
+
+ private:
+ locktype lock;
+ bool flag;
+};
+
+#define LIBADHOC_LOCK_CONCAT2(a, b) a ## b
+#define LIBADHOC_LOCK_CONCAT(a, b) LIBADHOC_LOCK_CONCAT2(a, b)
+#define LIBADHOC_LOCK_WITHLINE(a) LIBADHOC_LOCK_CONCAT(a, __LINE__)
+
+#define BaseScopeLock(l,lt,mt) for (_lockLoop<lt<mt>> LIBADHOC_LOCK_WITHLINE(_lck)(l); LIBADHOC_LOCK_WITHLINE(_lck); !LIBADHOC_LOCK_WITHLINE(_lck))
+#define BaseLock(l,lt,mt) lt<mt> LIBADHOC_LOCK_WITHLINE(_lck)(l)
+
+#define Lock(l) BaseLock(l, boost::unique_lock, boost::shared_mutex)
+#define SharedLock(l) BaseLock(l, boost::shared_lock, boost::shared_mutex)
+
+#define ScopeLock(l) BaseScopeLock(l, boost::unique_lock, boost::shared_mutex)
+#define SharedScopeLock(l) BaseScopeLock(l, boost::shared_lock, boost::shared_mutex)
+
+#endif
+
diff --git a/libadhocutil/unittests/Jamfile.jam b/libadhocutil/unittests/Jamfile.jam
index 610a3c4..e26825a 100644
--- a/libadhocutil/unittests/Jamfile.jam
+++ b/libadhocutil/unittests/Jamfile.jam
@@ -5,6 +5,7 @@ path-constant me : . ;
lib boost_utf : : <name>boost_unit_test_framework ;
lib boost_filesystem ;
lib boost_system ;
+lib boost_thread ;
run
testContext.cpp
@@ -73,3 +74,15 @@ run
testScopeExit
;
+run
+ testLocks.cpp
+ : : :
+ <define>BOOST_TEST_DYN_LINK
+ <library>..//adhocutil
+ <library>boost_utf
+ <library>boost_system
+ <library>boost_thread
+ :
+ testLocks
+ ;
+
diff --git a/libadhocutil/unittests/testLocks.cpp b/libadhocutil/unittests/testLocks.cpp
new file mode 100644
index 0000000..bdf0d18
--- /dev/null
+++ b/libadhocutil/unittests/testLocks.cpp
@@ -0,0 +1,30 @@
+#define BOOST_TEST_MODULE Locks
+#include <boost/test/unit_test.hpp>
+
+#include "lockHelpers.h"
+#include <boost/thread/shared_mutex.hpp>
+
+BOOST_AUTO_TEST_CASE ( lock )
+{
+ boost::shared_mutex _lock;
+ Lock(_lock);
+}
+
+BOOST_AUTO_TEST_CASE ( sharedlock )
+{
+ boost::shared_mutex _lock;
+ SharedLock(_lock);
+}
+
+BOOST_AUTO_TEST_CASE ( scopelock )
+{
+ boost::shared_mutex _lock;
+ ScopeLock(_lock) { }
+}
+
+BOOST_AUTO_TEST_CASE ( sharedscopelock )
+{
+ boost::shared_mutex _lock;
+ SharedScopeLock(_lock) { }
+}
+