AuroraRuntime/Include/Aurora/Threading/WaitFor.hpp
Jamie Reece Wilson a5a9fa3887 [*] Updated AuTheading::WaitFor prototype
[+/*] Missing YieldPoll MS prototype in public header
2024-05-27 15:04:21 +01:00

61 lines
2.4 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, const PollCallback_cb &cb);
AUKN_SYM bool YieldPoll(bool bPermitMultipleContextSwitches, AuUInt64 qwTimeoutMs, const 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
* @warning: With kWaitForFlagTimeoutIsOr, we may have to long-poll with bad scheduling characteristics. Defer to AuAsync for thread local multiple wait scheduling, and AuIO for multiple io wait.
* Are you looking for AuIO::Loop::WaitMultipleLoopSources?
*/
AUKN_SYM bool WaitFor(const AuList<IWaitable *> &waitables, AuUInt32 uFlags, AuUInt64 uTimeout = 0, AuUInt32 *pIndexAwoken = nullptr);
static inline bool WaitForShared(const AuList<AuSPtr<IWaitable>> &pWaitables, AuUInt32 uFlags, AuUInt64 uTimeout = 0, AuUInt32 *pIndexAwoken = nullptr)
{
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, pIndexAwoken);
}
/// legacy api (~3 years old, relative to 2023)
/// @deprecated
static inline bool WaitFor(const AuList<IWaitable *> &waitables, AuUInt64 uTimeout)
{
return WaitFor(waitables, 0, uTimeout, nullptr);
}
/// 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);
}
}