8#ifndef HPP_ALIB_THREADS_SLEEPER
9#define HPP_ALIB_THREADS_SLEEPER 1
15#include "alib/time/ticks.hpp"
26#include <condition_variable>
27namespace alib {
namespace threads {
189template<
typename TDerived>
200 TDerived&
cast() {
return static_cast<TDerived&
>(*this); }
237 "Acquire: Multiple acquirements of TCondition are forbidden." );
244 "Acquire: Owner is (still) set, after std::mutex.lock()." );
264 "Release: Ownership is with a different thread" );
286 "ReleaseAndNotify called without prior acquisition" );
289 "ReleaseAndNotify: Ownership is with a different thread" );
309 "An exclusive waiter is set. Thus, notifying 'all' is not allowed.");
312 "ReleaseAndNotify called without prior acquisition" );
315 "ReleaseAndNotify: Ownership is with a different thread" );
341 "WaitForNotification called by a different thread than granted with 'Dbg.AssertExclusiveWaiter'.");
344 "WaitForNotification called without prior acquisition" );
347 "WaitForNotification: Ownership is with a different thread" );
354 std::unique_lock<std::mutex> lock(
mutex, std::adopt_lock);
380 "WaitForNotification called by a different thread than granted with 'Dbg.AssertExclusiveWaiter'.");
383 "WaitForNotification called without prior acquisition" );
386 "WaitForNotification: Ownership is with a different thread" );
393 std::unique_lock<std::mutex> lock(
mutex, std::adopt_lock);
436 "WaitForNotification called by a different thread than granted with 'Dbg.AssertExclusiveWaiter'.");
439 "WaitForNotification called without prior acquisition" );
442 "WaitForNotification: Ownership is with a different thread" );
450 std::unique_lock<std::mutex> lock(
mutex, std::adopt_lock);
452 [
this]{ return cast().isConditionMet(); } );
475 bool notified =
false;
526 WaitForNotification(ci);
531 WaitForNotification();
544 void Wait(
const Ticks::Duration::TDuration& maxWaitTimeSpan,
const CallerInfo& ci )
548 WaitForNotification(maxWaitTimeSpan, ci);
552 void Wait(
const Ticks::Duration::TDuration& maxWaitTimeSpan )
555 WaitForNotification(maxWaitTimeSpan);
573 { Wait( maxWaitTimeSpan.Export(), ci ); }
575 void Wait(
const Ticks::Duration& maxWaitTimeSpan )
576 { Wait( maxWaitTimeSpan.Export() ); }
595 WaitForNotification(wakeUpTime, ci);
599 void Wait(
const Ticks& wakeUpTime )
602 WaitForNotification(wakeUpTime);
610template<
typename T=
bool>
Condition(const character *dbgName)
void Notify(ALIB_DBG_TAKE_CI)
void Wait(const Ticks::Duration::TDuration &maxWaitTimeSpan, const CallerInfo &ci)
void Wait(const Ticks &wakeUpTime, const CallerInfo &ci)
void Wait(ALIB_DBG_TAKE_CI)
void Wait(const Ticks::Duration &maxWaitTimeSpan, const CallerInfo &ci)
void NotifyAll(ALIB_DBG_TAKE_CI)
static Thread * GetCurrent()
static ALIB_API Thread * Get(std::thread::id nativeID)
TTimePoint Export() const
#define ALIB_ASSERT_MODULE(modulename)
constexpr bool IsNotNull(const T &t)
constexpr bool IsNull(const T &t)
threads::TCondition< T > TCondition
Type alias in namespace alib.
characters::character character
Type alias in namespace alib.
std::thread::id ThreadID
The ID of the calling thread.
bool IsOwnedByCurrentThread() const
CallerInfo WaitCI
The most recent call to WaitForNotification.
CallerInfo AcqCI
Source location of the most recent acquirement.
CallerInfo RelCI
Source location of the most recent release.
std::thread::id AssertExclusiveWaiter
CallerInfo NotifyCI
The most recent call to ReleaseAndNotify or.
ALIB_API void Assert(bool cond, const CallerInfo &assertCI, const CallerInfo &ci, const NString &headline)
String Name
The name of this instance.
static ALIB_API NString ASSERTION_FORMAT
std::atomic< int > CntWaiters
The number of currently waiting threads.
Thread * Owner
Tracks the current owner.
void ReleaseAndNotifyAll(ALIB_DBG_TAKE_CI)
std::mutex mutex
The mutex used for locking this instance.
void WaitForNotification(ALIB_DBG_TAKE_CI)
void WaitForNotification(const Ticks::Duration &maxWaitTimeSpan, const CallerInfo &ci)
DbgConditionAsserter Dbg
The debug tool instance.
void WaitForNotification(const Ticks::Duration::TDuration &maxWaitTimeSpan, const CallerInfo &ci)
void Acquire(ALIB_DBG_TAKE_CI)
void Release(ALIB_DBG_TAKE_CI)
TCondition(const String &dbgName)
std::condition_variable conditionVariable
The condition variable used for blocking and notification.
void ReleaseAndNotify(ALIB_DBG_TAKE_CI)