SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
MutexSptd.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_Mutex_h
7 #define swcdb_core_Mutex_h
8 
9 
10 #include "swcdb/core/MutexAtomic.h"
11 #include <mutex>
12 
13 
14 namespace SWC { namespace Core {
15 
16 class MutexSptd final : private MutexAtomic {
17  public:
18 
20  explicit MutexSptd() noexcept : m_mutex() { }
21 
22  MutexSptd(const MutexSptd&) = delete;
23  MutexSptd(MutexSptd&&) = delete;
24  MutexSptd& operator=(const MutexSptd&) = delete;
26 
27  ~MutexSptd() noexcept { }
28 
30  bool lock_except() {
32  return true;
33  m_mutex.lock();
35  return false;
36  }
37 
39  bool lock() noexcept {
41  return true;
42  //while(__gthread_mutex_lock(m_mutex.native_handle()))
43  // std::this_thread::yield();
44  _again: try {
45  m_mutex.lock();
46  } catch(...) {
47  std::this_thread::yield();
48  goto _again;
49  }
51  return false;
52  }
53 
54  //SWC_CAN_INLINE
55  bool try_full_lock(bool& support) noexcept {
56  if(MutexAtomic::try_lock()) {
57  support = true;
58  return true;
59  }
60  if(m_mutex.try_lock()) {
61  if(MutexAtomic::try_lock()) {
62  support = false;
63  return true;
64  }
65  m_mutex.unlock();
66  }
67  return false;
68  }
69 
71  void unlock(const bool& support) noexcept {
73  if(!support)
74  m_mutex.unlock();
75  }
76 
77  class scope_except final {
78  public:
79 
82 
84  ~scope_except() noexcept { _m.unlock(_support); }
85 
86  scope_except(const scope_except&) = delete;
88  scope_except& operator=(const scope_except&) = delete;
90 
91  private:
93  const bool _support;
94  };
95 
96  class scope final {
97  public:
98 
99  //SWC_CAN_INLINE
100  scope(MutexSptd& m) noexcept : _m(m), _support(m.lock()) { }
101 
103  ~scope() noexcept { _m.unlock(_support); }
104 
105  scope(const scope&) = delete;
106  scope(scope&&) = delete;
107  scope& operator=(const scope&) = delete;
108  scope& operator=(scope&&) = delete;
109 
110  private:
112  const bool _support;
113  };
114 
115  private:
116  std::mutex m_mutex;
117 };
118 
119 
120 
121 }} //namespace SWC::Core
122 
123 
124 #endif // swcdb_core_Mutex_h
SWC::Core::MutexAtomic::try_lock
SWC_CAN_INLINE bool try_lock() const noexcept
Definition: MutexAtomic.h:31
SWC::Core::MutexSptd::scope::operator=
scope & operator=(const scope &)=delete
SWC::Core::MutexSptd::scope::scope
scope(MutexSptd &m) noexcept
Definition: MutexSptd.h:100
SWC::Core::MutexSptd::scope_except::scope_except
SWC_CAN_INLINE scope_except(MutexSptd &m)
Definition: MutexSptd.h:81
SWC::Core::MutexSptd::scope::_m
MutexSptd & _m
Definition: MutexSptd.h:111
SWC::Core::MutexSptd::MutexSptd
MutexSptd(const MutexSptd &)=delete
SWC::Core::MutexSptd::scope
Definition: MutexSptd.h:96
SWC::Core::MutexSptd::MutexSptd
SWC_CAN_INLINE MutexSptd() noexcept
Definition: MutexSptd.h:20
SWC::Core::MutexSptd::operator=
MutexSptd & operator=(MutexSptd &&)=delete
SWC::Core::MutexSptd::unlock
SWC_CAN_INLINE void unlock(const bool &support) noexcept
Definition: MutexSptd.h:71
SWC::Core::MutexAtomic::lock
SWC_CAN_INLINE void lock() const noexcept
Definition: MutexAtomic.h:40
SWC::Core::MutexSptd::scope_except::scope_except
scope_except(const scope_except &)=delete
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::Core::MutexSptd::MutexSptd
MutexSptd(MutexSptd &&)=delete
SWC::Core::MutexSptd::scope_except::_support
const bool _support
Definition: MutexSptd.h:93
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Core::MutexAtomic
Definition: MutexAtomic.h:17
SWC::Core::MutexSptd::scope::~scope
SWC_CAN_INLINE ~scope() noexcept
Definition: MutexSptd.h:103
SWC::Core::MutexSptd::scope_except::_m
MutexSptd & _m
Definition: MutexSptd.h:92
SWC::Core::MutexSptd::lock
SWC_CAN_INLINE bool lock() noexcept
Definition: MutexSptd.h:39
SWC::Core::MutexSptd::~MutexSptd
~MutexSptd() noexcept
Definition: MutexSptd.h:27
SWC::Core::MutexSptd::m_mutex
std::mutex m_mutex
Definition: MutexSptd.h:116
SWC::Core::MutexSptd::scope::scope
scope(scope &&)=delete
MutexAtomic.h
SWC::Core::MutexSptd::lock_except
SWC_CAN_INLINE bool lock_except()
Definition: MutexSptd.h:30
SWC::Core::MutexAtomic::unlock
SWC_CAN_INLINE void unlock() const noexcept
Definition: MutexAtomic.h:72
SWC::Core::MutexSptd::scope_except
Definition: MutexSptd.h:77
SWC::Core::MutexSptd::scope_except::operator=
scope_except & operator=(scope_except &&)=delete
SWC::Core::MutexSptd::scope_except::operator=
scope_except & operator=(const scope_except &)=delete
SWC::Core::MutexSptd::scope::operator=
scope & operator=(scope &&)=delete
SWC::Core::MutexSptd::scope::scope
scope(const scope &)=delete
SWC::Core::MutexSptd::scope::_support
const bool _support
Definition: MutexSptd.h:112
SWC::Core::MutexSptd::scope_except::scope_except
scope_except(scope_except &&)=delete
SWC::Core::MutexSptd
Definition: MutexSptd.h:16
SWC::Core::MutexSptd::scope_except::~scope_except
SWC_CAN_INLINE ~scope_except() noexcept
Definition: MutexSptd.h:84
SWC::Core::MutexSptd::try_full_lock
bool try_full_lock(bool &support) noexcept
Definition: MutexSptd.h:55
SWC::Core::MutexSptd::operator=
MutexSptd & operator=(const MutexSptd &)=delete