Go to the documentation of this file.
75 #pragma warning(disable: 4127)
81 #if 0 && defined(XSENS_DEBUG)
92 TimeoutChecker(
char category =
'l',
void* lock =
nullptr);
97 inline void start(
void* lock)
101 m_tStart = XsTimeStamp::now();
105 if (m_tStart.msTime() && (XsTimeStamp::now() - m_tStart).msTime() >= m_timeout)
106 warning((XsTimeStamp::now() - m_tStart).msTime());
109 void warning(int64_t);
112 #define CHECK_LOCK_DEFINE() mutable DebugTools::TimeoutChecker m_tcCheck
113 #define CHECK_LOCK_ACQUIRE() volatile DebugTools::TimeoutChecker tcAcq('a', this); m_tcCheck.start(this)
114 #define CHECK_LOCK_RELEASE() m_tcCheck.stop(); volatile DebugTools::TimeoutChecker tcRel('r', this)
115 #define TIMEOUTCHECKER_DEFINED 1
117 #define CHECK_LOCK_DEFINE() typedef void IgnoredLockDefine
118 #define CHECK_LOCK_ACQUIRE() ((void)0)
119 #define CHECK_LOCK_RELEASE() ((void)0)
120 #define TIMEOUTCHECKER_DEFINED 0
125 class LockSuspendable;
135 #if !XSENS_USE_POSIX_LOCKING
138 pthread_mutexattr_t m_attr;
150 #if !XSENS_USE_POSIX_LOCKING
151 ::InitializeCriticalSection(&
m_mutex);
154 pthread_mutexattr_init(&m_attr);
155 pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_RECURSIVE);
156 pthread_mutex_init(&
m_mutex, &m_attr);
163 #if !XSENS_USE_POSIX_LOCKING
164 ::DeleteCriticalSection(&
m_mutex);
166 pthread_mutex_destroy(&
m_mutex);
167 pthread_mutexattr_destroy(&m_attr);
180 #if !XSENS_USE_POSIX_LOCKING
181 ::EnterCriticalSection(&
m_mutex);
197 #if !XSENS_USE_POSIX_LOCKING
198 ::LeaveCriticalSection(&
m_mutex);
200 pthread_mutex_unlock(&
m_mutex);
211 #if !XSENS_USE_POSIX_LOCKING
212 if (::TryEnterCriticalSection(&
m_mutex))
214 if (pthread_mutex_trylock(&
m_mutex) == 0)
225 inline bool haveLock()
const
313 bool readFail =
false;
348 bool readGot =
false;
442 bool readFail =
false;
481 bool readGot =
false;
648 bool readFail =
false;
683 bool readGot =
false;
805 bool stillReading =
false;
845 if (decreaseSuspendCount)
960 rhs.m_mutex =
nullptr;
961 rhs.m_locked =
false;
969 rhs.m_mutex =
nullptr;
970 rhs.m_locked =
false;
1108 #if defined(XSENS_DEBUG)
1327 inline bool haveGuardedLock()
const
1363 #if defined(XSENS_DEBUG)
1375 #if defined(XSENS_DEBUG)
1395 switch (desiredLockState)
1666 template <
typename K>
1722 HANDLE* m_handleList;
1823 CONDITION_VARIABLE
m_cond;
1873 template <
typename L1,
typename L2>
1877 while (!lock2.tryLock(state2))
1888 #pragma warning(default: 4127)
bool isUsing(GuardedMutex const *mutex) const
Check if the lock is using the supplied mutex.
LockReadWrite & operator=(LockReadWrite const &)=delete
LockReadWrite(LockReadWrite const &)=delete
bool isUsing(Mutex const *mutex) const
Check if the lock is using the supplied mutex.
Semaphore(Semaphore const &)=delete
A readers-writer mutex class.
bool tryClaimMutex()
Tries to claim (lock) a mutex.
LockSuspendable(MutexReadWriteSuspendable *mutex, LockState lockState)
Constructs a lock using a suspendable readers-writer mutex.
LockSuspendable(GuardedMutex *mutex, LockState lockState)
Constructs a lock using a guarded mutex.
volatile std::atomic_bool m_lockedR
bool lock(bool write)
Convenience function that accepts the boolean write value, forwards to lock(LockState)
volatile std::atomic< XsThreadId > m_writeLocked
bool wait()
Wait for the event to be set or object termination.
~GuardedMutex()
Destructor.
XsThreadId * m_readLocked
bool releaseMutex(bool write, bool decreaseSuspendCount)
Releases (unlocks) a mutex for read or write modes.
volatile std::atomic_bool m_iSuspended
#define CHECK_LOCK_ACQUIRE()
bool lockWrite()
Convenience function for lock(LS_Write);.
GuardedMutex & operator=(GuardedMutex const &)=delete
A readers-writer mutex class that is able to be suspended.
LockSuspendable & operator=(LockSuspendable const &)=delete
void terminate()
Terminates the thread.
bool isUsing(MutexReadWriteSuspendable const *mutex) const
Check if the lock is using the supplied mutex.
bool lock(bool write)
Make sure that the lock has exactly the given lock state.
bool isLocked() const volatile
char * m_semname
A name of the semaphore.
bool haveWriteClaim() const
volatile std::atomic_int m_writeRef
ProtectedValue(const ProtectedValue &pv)
Constructs a protected value using a reference of another protected value.
bool isLocked(LockState minimumState) const
~WaitCondition()
Destroy the wait condition.
~MutexReadWriteSuspendable()
bool haveWriteClaim() const volatile
MutexReadWriteSuspendable * m_mutex
bool haveReadClaim() const volatile
bool isLocked(bool write) const volatile
bool lock()
Locks the unlocked mutex.
A readers-writer lock class.
Mutex & operator=(Mutex const &)=delete
#define CHECK_LOCK_RELEASE()
WaitCondition & operator=(WaitCondition const &)=delete
bool lockWrite()
Convenience function for lock(true);.
LockGuarded(GuardedMutex *mutex, LockState lockit)
Constructs a guarded lock using a guarded mutex and a lock state.
#define xsYield()
Release the remainder of the timeslice so other operations can run.
Lock & operator=(Lock &&rhs)
bool tryLock(bool write, uint32_t timeout=0)
Tries to lock the write/read unlocked mutex before a given time runs out.
bool lock(LockState desiredLockState)
Make sure that the lock has exactly the given lock state.
bool releaseMutex()
Releases (unlocks) a mutex.
bool claimMutex(Mode mode)
Claims (locks) a mutex using a given mode.
MutexStatusFlags
An enum that hold mutex status flags.
MutexReadWriteSuspendable & suspendable()
bool tryLock(LockState ls)
Tries to lock the unlocked mutex.
LockGuarded & operator=(LockGuarded const &)=delete
Lock(Mutex *mutex)
Constructs a lock a given mutex.
volatile std::atomic_int m_writeLocksPending
void broadcast()
Unblock all waiting threads.
bool haveReadClaim() const
volatile std::atomic_int m_writeLockSuspendCount
sem_t * m_handle
A semaphore's handle.
void operator=(const ProtectedValue &pv)
Assignment operator, copies contents from the pv protected value.
Mode
An enum class that holds a three different modes of a suspendable mutex.
WaitCondition(WaitCondition const &)=delete
LockGuarded(LockGuarded const &)=delete
bool lock(LockState ls)
Locks the unlocked mutex.
Lock(Mutex *mutex, LockState lockit)
Constructs a lock a given mutex.
~ProtectedValue()
Destructor.
volatile std::atomic_bool m_lockedW
~LockGuarded()
Destructor.
bool wait()
Wait until we're signalled to continue.
bool resumeWriteLock()
Resumes an existing write lock so read locks are no longer allowed.
bool lockRead()
Convenience function for lock(LS_Read);.
bool lockRead()
Convenience function for lock(false);.
bool suspend()
Suspend a write lock, allowing readers access, but preventing others from getting a write lock.
An event that can be set/reset and that can be waited for.
bool tryClaimMutex(bool write, uint32_t timeout)
volatile std::atomic_int m_writeRef
LockSuspendable(LockSuspendable const &)=delete
volatile std::atomic_bool m_locked
void signal()
Unblock a single waiting thread.
bool mutexIsSuspended() const volatile
MutexReadWriteSuspendable & operator=(MutexReadWriteSuspendable const &)=delete
volatile std::atomic_bool m_terminating
volatile std::atomic_int m_waiterCount
MutexReadWrite & operator=(MutexReadWrite const &)=delete
bool unlock() noexcept
Unlocks the write or read locked mutex.
Lock(Lock const &)=delete
volatile std::atomic_int m_lockCount
void multiLock(L1 &lock1, L2 &lock2, LockState state1=LockState::LS_Write, LockState state2=LockState::LS_Write)
Lock two mutex-locks in the order in which they appear, rolling back the locks if one fails....
bool claimMutex(bool write)
K m_value
A type name of this class.
volatile std::atomic_bool m_lockedR
int status() const volatile
XSTYPES_DLL_API int64_t XsTimeStamp_now(struct XsTimeStamp *thisPtr)
pthread_condattr_t m_condattr
volatile std::atomic_bool m_lockedW
bool downgradeToRead(bool decreaseSuspend)
Downgrade an existing write lock to a read lock.
bool unlock() noexcept
Unlocks the locked mutex.
void addReadLock(XsThreadId cid)
volatile std::atomic_int m_writeLocksPending
bool isUsing(GuardedMutex const *mutex) const
Check if the lock is using the supplied mutex.
void addReadLock(XsThreadId cid)
bool releaseMutex(bool write)
bool resume()
Resume a write lock.
uint32_t m_nofHandles
A number of the semaphore's handles.
bool haveWriteClaim() const volatile
bool claimMutex()
Claims (locks) a mutex.
bool unlock() noexcept
Unlocks the locked mutex.
bool lock()
Locks the unlocked mutex.
LockGuarded(GuardedMutex *mutex, bool lockit=true)
Constructs a guarded lock using a guarded mutex.
bool downgradeToRead()
Downgrade an existing write lock to a read lock.
WaitEvent & operator=(WaitEvent const &)=delete
volatile std::atomic_bool m_locked
bool lock(LockState ls)
Convenience function that accepts the generic LockState, forwards to lock(bool)
A platform independent wait condition implementation.
bool unlock() noexcept
Unlocks the write or read locked mutex.
LockReadWrite(MutexReadWrite *mutex, LockState lockState=LS_Unlocked)
Consturctor.
bool lockSuspendedWrite()
Convenience function for lock(LS_SuspendedWrite);.
#define XSENS_MSC_WARNING_SUPPRESS(...)
A readers-writer lock class that is able to be suspended.
A two-layer mutex, typically used for status+data protection.
bool isUsing(MutexReadWriteSuspendable const *mutex) const
Check if the lock is using the supplied mutex.
void reset()
Reset the event.
volatile std::atomic< XsThreadId > m_writeLocked
#define xsGetCurrentThreadId()
bool tryLock()
Tries to lock the unlocked mutex.
Semaphore & operator=(Semaphore const &)=delete
bool isUsing(Mutex const *mutex) const
Check if the guarded mutex is equal to the supplied mutex.
int32_t post(int32_t increment=1) noexcept
Increases the count of the specified semaphore object by a specified amount.
MutexReadWriteSuspendable()
bool claimMutex(bool write)
Claims (locks) a mutex for read or write modes.
int status() const volatile
bool wait1()
Waits for an infinite time or until the semaphore is released.
Lock(Mutex *mutex, bool lockit)
Constructs a lock a given mutex.
Lock & operator=(Lock const &)=delete
XsThreadId * m_readLocked
bool releaseMutex(Mode mode, bool decreaseSuspendCount)
Releases (unlocks) a mutex using a given mode.
This class contains method to set, retrieve and compare timestamps.
ProtectedValue(MutexReadWrite *mutex, bool writeLock=false)
Constructs a protected value using a readers-writer mutex.
GuardedMutex()
Constructor.
bool suspendWriteLock()
Suspend an existing write lock so read locks are allowed, but do not allow other write locks.