ShlubluLib  v0.5
ShlubluLib is a lightweight, modular, general purpose, open-source C++ library for Linux and Windows.
MutexLock.h
Go to the documentation of this file.
1 #pragma once
2 
9 #include <mutex>
10 #include <thread>
11 
12 namespace shlublu
13 {
14 
57 class MutexLock
58 {
59 public:
88  using Guard = std::lock_guard<MutexLock>;
89 
93  class LockingError : public std::logic_error
94  {
95  public:
100  explicit LockingError(const std::string& message)
101  : logic_error(("Python binding: " + message).c_str())
102  {}
103 
108  explicit LockingError(const char* message)
109  : logic_error(std::string(message))
110  {}
111  };
112 
113 public:
117  MutexLock(MutexLock const&) = delete;
118 
122  MutexLock(MutexLock&&) = delete;
123 
128  MutexLock(bool locked = false);
129 
135  virtual ~MutexLock() noexcept;
136 
137 public:
146  void lock();
147 
152  void unlock();
153 
158  unsigned lockLevel() const;
159 
160 
165  bool currentThreadIsOwner() const;
166 
167 private:
169 
170  std::recursive_mutex mMutex;
171  std::thread::id mOwnerId;
172  unsigned mLockLevel;
173 
175 };
176 
177 }
shlublu::MutexLock::lock
void lock()
Locks the mutex once available.
shlublu::MutexLock::LockingError
MutexLock throws this exception when a locking or unlocking operation is not legal.
Definition: MutexLock.h:94
shlublu::MutexLock::MutexLock
MutexLock(bool locked=false)
Constructor.
shlublu::MutexLock::unlock
void unlock()
Unlocks the mutex.
shlublu
shlublu::MutexLock::lockLevel
unsigned lockLevel() const
Returns the lock level of the mutex.
shlublu::MutexLock::Guard
std::lock_guard< MutexLock > Guard
RAII convenience wrapper.
Definition: MutexLock.h:88
shlublu::MutexLock::~MutexLock
virtual ~MutexLock() noexcept
Destructor.
shlublu::MutexLock::MutexLock
MutexLock(MutexLock const &)=delete
Copy constructor is deleted.
shlublu::MutexLock::currentThreadIsOwner
bool currentThreadIsOwner() const
Tells whether the current thread owns the mutex.
shlublu::MutexLock
Recursive mutex.
Definition: MutexLock.h:58
shlublu::MutexLock::MutexLock
MutexLock(MutexLock &&)=delete
Move constructor is deleted.
shlublu::MutexLock::LockingError::LockingError
LockingError(const char *message)
Constructor.
Definition: MutexLock.h:108
shlublu::MutexLock::LockingError::LockingError
LockingError(const std::string &message)
Constructor.
Definition: MutexLock.h:100