summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-07-31 17:14:21 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2021-07-31 17:14:21 +0100
commit6b9fdc6ad3ac84cf8b8543b6e46b0ca8c4d61860 (patch)
tree1152b920b5b6481f9b4a9d31927ece6de658f93b
parentbefore and afterEvents virtual/protected (diff)
downloadmygrate-6b9fdc6ad3ac84cf8b8543b6e46b0ca8c4d61860.tar.bz2
mygrate-6b9fdc6ad3ac84cf8b8543b6e46b0ca8c4d61860.tar.xz
mygrate-6b9fdc6ad3ac84cf8b8543b6e46b0ca8c4d61860.zip
Build a basic hacky semaphore for compilers that don't support std::binary_semaphore yet
-rw-r--r--test/Jamfile.jam2
-rw-r--r--test/semaphore.cpp37
-rw-r--r--test/semaphore.h27
3 files changed, 65 insertions, 1 deletions
diff --git a/test/Jamfile.jam b/test/Jamfile.jam
index 8486a14..1f0dac3 100644
--- a/test/Jamfile.jam
+++ b/test/Jamfile.jam
@@ -9,7 +9,7 @@ project : requirements
lib testdb :
[ glob-tree *.sql : bin ]
- [ glob testdb-*.cpp ] :
+ [ glob *.cpp : test-*.cpp ] :
<link>static
;
diff --git a/test/semaphore.cpp b/test/semaphore.cpp
new file mode 100644
index 0000000..09124ee
--- /dev/null
+++ b/test/semaphore.cpp
@@ -0,0 +1,37 @@
+#include "semaphore.h"
+
+#ifndef __cpp_lib_semaphore
+# include <thread>
+
+semaphore::semaphore(unsigned int v_) : v {v_} { }
+
+void
+semaphore::release(unsigned int n)
+{
+ std::lock_guard lk {m};
+ v += n;
+}
+
+void
+semaphore::acquire()
+{
+ while (!try_dec()) { }
+}
+
+bool
+semaphore::try_dec()
+{
+ std::lock_guard lk {m};
+ if (v) {
+ v--;
+ m.unlock();
+ return true;
+ }
+ else {
+ m.unlock();
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
+ return false;
+ }
+}
+
+#endif
diff --git a/test/semaphore.h b/test/semaphore.h
new file mode 100644
index 0000000..f2e9355
--- /dev/null
+++ b/test/semaphore.h
@@ -0,0 +1,27 @@
+#ifndef MYGRATE_TEST_SEMAPHORE_H
+#define MYGRATE_TEST_SEMAPHORE_H
+
+#if __has_include(<semaphore>)
+# include <semaphore>
+#endif
+
+#ifdef __cpp_lib_semaphore
+using semaphore = std::binary_semaphore;
+#else
+# include <mutex>
+class semaphore {
+public:
+ semaphore(unsigned int v_);
+
+ void release(unsigned int n = 1);
+
+ void acquire();
+
+private:
+ bool try_dec();
+ unsigned int v;
+ std::mutex m;
+};
+#endif
+
+#endif