summaryrefslogtreecommitdiffstats
path: root/comm/third_party/botan/src/lib/utils/thread_utils/rwlock.h
diff options
context:
space:
mode:
Diffstat (limited to 'comm/third_party/botan/src/lib/utils/thread_utils/rwlock.h')
-rw-r--r--comm/third_party/botan/src/lib/utils/thread_utils/rwlock.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/comm/third_party/botan/src/lib/utils/thread_utils/rwlock.h b/comm/third_party/botan/src/lib/utils/thread_utils/rwlock.h
new file mode 100644
index 0000000000..324e667538
--- /dev/null
+++ b/comm/third_party/botan/src/lib/utils/thread_utils/rwlock.h
@@ -0,0 +1,42 @@
+/*
+* (C) 2019 Jack Lloyd
+*
+* Botan is released under the Simplified BSD License (see license.txt)
+*/
+
+#ifndef BOTAN_RWLOCK_H_
+#define BOTAN_RWLOCK_H_
+
+#include <botan/types.h>
+#include <mutex>
+#include <condition_variable>
+
+namespace Botan {
+
+/**
+* A read-write lock. Writers are favored.
+*/
+class BOTAN_TEST_API RWLock final
+ {
+ public:
+ RWLock();
+
+ void lock();
+ void unlock();
+
+ void lock_shared();
+ void unlock_shared();
+ private:
+ std::mutex m_mutex;
+ std::condition_variable m_gate1;
+ std::condition_variable m_gate2;
+ uint32_t m_state;
+
+ // 2**31 concurrent readers should be enough for anyone
+ static const uint32_t is_writing = static_cast<uint32_t>(1) << 31;
+ static const uint32_t readers_mask = ~is_writing;
+ };
+
+}
+
+#endif