1#ifndef LIBFILEZILLA_MUTEX_HEADER
2#define LIBFILEZILLA_MUTEX_HEADER
11#include "glue/windows.hpp"
17#ifdef LFZ_DEBUG_MUTEXES
28struct FZ_PUBLIC_SYMBOL lock_order final {
29 std::vector<mutex*> mutexes_;
30 std::vector<void*> backtrace_;
34struct 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_;
52#ifdef LFZ_DEBUG_MUTEXES
57inline 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;
60inline 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));
74class FZ_PUBLIC_SYMBOL mutex final
77 explicit mutex(
bool recursive =
true);
78 explicit mutex(mutex_flags flags);
81 mutex(mutex
const&) =
delete;
82 mutex& operator=(mutex
const&) =
delete;
94 friend class condition;
95 friend class scoped_lock;
102#ifdef LFZ_DEBUG_MUTEXES
116class FZ_PUBLIC_SYMBOL scoped_lock final
124 explicit scoped_lock(
mutex& m)
128 EnterCriticalSection(m_);
130 pthread_mutex_lock(m_);
132#ifdef LFZ_DEBUG_MUTEXES
133 mutex_debug::record_lock(m_);
137 explicit scoped_lock(
mutex& m, flag)
148#ifdef LFZ_DEBUG_MUTEXES
149 mutex_debug::record_unlock(m_);
152 LeaveCriticalSection(m_);
154 pthread_mutex_unlock(m_);
160 scoped_lock(scoped_lock
const&) =
delete;
161 scoped_lock& operator=(scoped_lock
const&) =
delete;
163 scoped_lock(scoped_lock && op)
noexcept
167 locked_ = op.locked_;
171 scoped_lock& operator=(scoped_lock && op)
noexcept
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_;
233class FZ_PUBLIC_SYMBOL condition final
239 condition(condition
const&) =
delete;
240 condition& operator=(condition
const&) =
delete;
284 CONDITION_VARIABLE cond_;
286 pthread_cond_t cond_;
void wait(scoped_lock &l)
Wait indefinitely for condition to become signalled.
bool wait(scoped_lock &l, duration const &timeout)
Wait until timeout for condition to become signalled.
bool signalled(scoped_lock const &) const
Check if condition is already signalled.
Definition mutex.hpp:281
void signal(scoped_lock &l)
Signal condition variable.
The duration class represents a time interval in milliseconds.
Definition time.hpp:291
Lean replacement for std::(recursive_)mutex.
Definition mutex.hpp:75
void unlock()
Beware, manual locking isn't exception safe, use scoped_lock.
bool try_lock()
Beware, manual locking isn't exception safe.
void lock()
Beware, manual locking isn't exception safe, use scoped_lock.
A simple scoped lock.
Definition mutex.hpp:117
void unlock()
Releases the mutex.
Definition mutex.hpp:203
void lock()
Obtains the mutex.
Definition mutex.hpp:186
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition apply.hpp:17
Assorted classes dealing with time.