1 #ifndef LIBFILEZILLA_MUTEX_HEADER
2 #define LIBFILEZILLA_MUTEX_HEADER
11 #include "glue/windows.hpp"
17 #ifdef LFZ_DEBUG_MUTEXES
28 struct FZ_PUBLIC_SYMBOL lock_order final {
29 std::vector<mutex*> mutexes_;
30 std::vector<void*> backtrace_;
34 struct FZ_PUBLIC_SYMBOL mutex_debug final
36 static void record_lock(
void* m);
37 static void record_unlock(
void* m);
40 std::thread::id id_{};
41 std::vector<std::list<lock_order>::iterator> own_orders_;
48 enum class mutex_flags
52 #ifdef LFZ_DEBUG_MUTEXES
57 inline bool operator&(mutex_flags lhs, mutex_flags rhs) {
58 return (
static_cast<std::underlying_type_t<mutex_flags>
>(lhs) &
static_cast<std::underlying_type_t<mutex_flags>
>(rhs)) != 0;
60 inline mutex_flags operator|(mutex_flags lhs, mutex_flags rhs)
62 return static_cast<mutex_flags
>(
static_cast<std::underlying_type_t<mutex_flags>
>(lhs) |
static_cast<std::underlying_type_t<mutex_flags>
>(rhs));
74 class FZ_PUBLIC_SYMBOL
mutex final
77 explicit mutex(
bool recursive =
true);
78 explicit mutex(mutex_flags flags);
102 #ifdef LFZ_DEBUG_MUTEXES
128 EnterCriticalSection(m_);
130 pthread_mutex_lock(m_);
132 #ifdef LFZ_DEBUG_MUTEXES
133 mutex_debug::record_lock(m_);
148 #ifdef LFZ_DEBUG_MUTEXES
149 mutex_debug::record_unlock(m_);
152 LeaveCriticalSection(m_);
154 pthread_mutex_unlock(m_);
167 locked_ = op.locked_;
176 locked_ = op.locked_;
190 EnterCriticalSection(m_);
192 pthread_mutex_lock(m_);
194 #ifdef LFZ_DEBUG_MUTEXES
195 mutex_debug::record_lock(m_);
206 #ifdef LFZ_DEBUG_MUTEXES
207 mutex_debug::record_unlock(m_);
210 LeaveCriticalSection(m_);
212 pthread_mutex_unlock(m_);
216 explicit operator bool()
const {
return locked_; }
219 friend class condition;
222 CRITICAL_SECTION * m_;
224 pthread_mutex_t * m_;
284 CONDITION_VARIABLE cond_;
286 pthread_cond_t cond_;
A simple scoped lock.
Definition: mutex.hpp:116
bool signalled(scoped_lock const &) const
Check if condition is already signalled.
Definition: mutex.hpp:281
Waitable condition variable.
Definition: mutex.hpp:233
void lock()
Obtains the mutex.
Definition: mutex.hpp:186
Assorted classes dealing with time.
void unlock()
Releases the mutex.
Definition: mutex.hpp:203
The namespace used by libfilezilla.
Definition: apply.hpp:17
The duration class represents a time interval in milliseconds.
Definition: time.hpp:290
Sets some global macros and further includes string.hpp.
Lean replacement for std::(recursive_)mutex.
Definition: mutex.hpp:74
bool try_lock()
Beware, manual locking isn't exception safe.