SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
MutexLock.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 #ifndef swcdb_core_MutexLock_h
7 #define swcdb_core_MutexLock_h
8 
9 
10 #include <mutex>
11 #include <shared_mutex>
12 
13 
14 
15 namespace SWC { namespace Core {
16 
17 
18 
19 struct SharedLock final : public std::shared_lock<std::shared_mutex> {
20 
22  SharedLock(std::shared_mutex& m) noexcept
23  : std::shared_lock<std::shared_mutex>(m, std::defer_lock) {
24  _again: try {
25  std::shared_lock<std::shared_mutex>::lock();
26  } catch(...) {
27  goto _again;
28  }
29  }
30 
31  ~SharedLock() noexcept { }
32  SharedLock(const SharedLock&) = delete;
33  SharedLock(SharedLock&&) = delete;
34  SharedLock& operator=(const SharedLock&) = delete;
36 };
37 
38 
39 
40 template<typename MutexT>
41 struct ScopedLock final {
42 
43  MutexT& _m;
44 
46  ScopedLock(MutexT& m) noexcept : _m(m) {
47  _again: try {
48  _m.lock();
49  } catch(...) {
50  goto _again;
51  }
52  }
53 
55  ~ScopedLock() noexcept {
56  _m.unlock();
57  }
58 
59  ScopedLock(const ScopedLock&) = delete;
60  ScopedLock(ScopedLock&&) = delete;
61  ScopedLock& operator=(const ScopedLock&) = delete;
63 };
64 
65 
66 
67 template<typename MutexT>
68 struct UniqueLock final : public std::unique_lock<MutexT> {
69 
71  UniqueLock(MutexT& m) noexcept
72  : std::unique_lock<MutexT>(m, std::defer_lock) {
73  _again: try {
74  std::unique_lock<MutexT>::lock();
75  } catch(...) {
76  goto _again;
77  }
78  }
79 
80  ~UniqueLock() noexcept { }
81  UniqueLock(const UniqueLock&) = delete;
82  UniqueLock(UniqueLock&&) = delete;
83  UniqueLock& operator=(const UniqueLock&) = delete;
85 };
86 
87 
88 
89 }} //namespace SWC::Core
90 
91 
92 
93 #endif // swcdb_core_MutexLock_h
SWC::Core::SharedLock::SharedLock
SWC_CAN_INLINE SharedLock(std::shared_mutex &m) noexcept
Definition: MutexLock.h:22
SWC::Core::UniqueLock
Definition: MutexLock.h:68
SWC::Core::ScopedLock
Definition: MutexLock.h:41
SWC::Core::SharedLock::SharedLock
SharedLock(SharedLock &&)=delete
SWC::Core::SharedLock
Definition: MutexLock.h:19
SWC::Core::UniqueLock::UniqueLock
UniqueLock(UniqueLock &&)=delete
SWC::Core::UniqueLock::operator=
UniqueLock & operator=(UniqueLock &&)=delete
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::Core::SharedLock::operator=
SharedLock & operator=(SharedLock &&)=delete
SWC::Core::ScopedLock::ScopedLock
SWC_CAN_INLINE ScopedLock(MutexT &m) noexcept
Definition: MutexLock.h:46
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Core::ScopedLock::_m
MutexT & _m
Definition: MutexLock.h:43
SWC::Core::UniqueLock::operator=
UniqueLock & operator=(const UniqueLock &)=delete
SWC::Core::ScopedLock::~ScopedLock
SWC_CAN_INLINE ~ScopedLock() noexcept
Definition: MutexLock.h:55
SWC::Core::ScopedLock::operator=
ScopedLock & operator=(ScopedLock &&)=delete
SWC::Core::SharedLock::operator=
SharedLock & operator=(const SharedLock &)=delete
SWC::Core::UniqueLock::~UniqueLock
~UniqueLock() noexcept
Definition: MutexLock.h:80
SWC::Core::ScopedLock::operator=
ScopedLock & operator=(const ScopedLock &)=delete
SWC::Core::ScopedLock::ScopedLock
ScopedLock(const ScopedLock &)=delete
SWC::Core::SharedLock::~SharedLock
~SharedLock() noexcept
Definition: MutexLock.h:31
SWC::Core::SharedLock::SharedLock
SharedLock(const SharedLock &)=delete
SWC::Core::UniqueLock::UniqueLock
SWC_CAN_INLINE UniqueLock(MutexT &m) noexcept
Definition: MutexLock.h:71
SWC::Core::UniqueLock::UniqueLock
UniqueLock(const UniqueLock &)=delete
SWC::Core::ScopedLock::ScopedLock
ScopedLock(ScopedLock &&)=delete