AuroraRuntime/Include/Aurora/Threading/WaitFor.hpp

58 lines
1.9 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: WaitFor.hpp
Date: 2021-6-10
Author: Reece
***/
#pragma once
namespace Aurora::Threading
{
using PollCallback_cb = AuFunction<bool()>;
AUKN_SYM bool YieldPollNs(bool bPermitMultipleContextSwitches, AuUInt64 qwAbsTimeoutNs, PollCallback_cb cb);
AUKN_SYM bool WaitForAbsNS(IWaitable *pWaitable, AuUInt64 qwAbsTimeout = 0);
AUKN_SYM bool TryWait(IWaitable *pWaitable);
static const auto kWaitForFlagTimeoutIsNanoseconds = 1ul;
static const auto kWaitForFlagTimeoutIsAbsolute = 1ul << 1;
static const auto kWaitForFlagTimeoutIsOr = 1ul << 2;
/**
* Waits for a list of IWaitable objects to complete.
* See: Mutex, CriticalSection, Semaphore, Event, Thread, Async, and others
* On timeout, returns false
* On error, waitables are restored to their state at the point of WaitFors
*/
AUKN_SYM bool WaitFor(const AuList<IWaitable *> &waitables, AuUInt32 uFlags, AuUInt64 uTimeout = 0);
static inline bool WaitForShared(const AuList<AuSPtr<IWaitable>> &pWaitables, AuUInt32 uFlags, AuUInt64 uTimeout)
{
AU_DEBUG_MEMCRUNCH;
AuList<IWaitable *> waitables;
waitables.reserve(pWaitables.size());
for (const auto &pIWaitable : pWaitables)
{
waitables.push_back(pIWaitable.get());
}
return WaitFor(waitables, uFlags, uTimeout);
}
/// legacy api (~3 years old, relative to 2023)
/// @deprecated
static inline bool WaitFor(const AuList<IWaitable *> &waitables, AuUInt64 uTimeout)
{
return WaitFor(waitables, 0, uTimeout);
}
/// legacy api (~3 years old, relative to 2023)
/// @deprecated
static inline bool WaitFor(IWaitable *pWaitable, AuUInt64 uTimeoutMS)
{
return WaitForAbsNS(pWaitable, uTimeoutMS ? AuMSToNS<AuUInt64>(uTimeoutMS) + Time::SteadyClockNS() : 0);
}
}