AuroraRuntime/Include/Aurora/Threading/WaitFor.hpp

58 lines
1.9 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
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
{
2022-01-19 18:37:00 +00:00
using PollCallback_cb = AuFunction<bool()>;
2021-06-27 21:25:29 +00:00
AUKN_SYM bool YieldPollNs(bool bPermitMultipleContextSwitches, AuUInt64 qwAbsTimeoutNs, PollCallback_cb cb);
2021-06-27 21:25:29 +00:00
AUKN_SYM bool WaitForAbsNS(IWaitable *pWaitable, AuUInt64 qwAbsTimeout = 0);
2023-09-12 17:47:25 +00:00
AUKN_SYM bool TryWait(IWaitable *pWaitable);
2021-06-27 21:25:29 +00:00
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)
2021-06-27 21:25:29 +00:00
{
return WaitFor(waitables, 0, uTimeout);
2021-06-27 21:25:29 +00:00
}
/// legacy api (~3 years old, relative to 2023)
/// @deprecated
static inline bool WaitFor(IWaitable *pWaitable, AuUInt64 uTimeoutMS)
{
2023-09-12 20:31:34 +00:00
return WaitForAbsNS(pWaitable, uTimeoutMS ? AuMSToNS<AuUInt64>(uTimeoutMS) + Time::SteadyClockNS() : 0);
}
2021-06-27 21:25:29 +00:00
}