2023-09-20 03:52:44 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: FutexBarrier.hpp
|
|
|
|
Date: 2023-09-20
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../WakeOnAddress.hpp"
|
|
|
|
#include "../SpinTime.hpp"
|
|
|
|
|
|
|
|
namespace Aurora::Threading::Waitables
|
|
|
|
{
|
2024-05-28 18:28:08 +00:00
|
|
|
/**
|
|
|
|
* Are you looking for AuFutexSemaphore::LockUntilExactlyEqualAbsNS timeline barriers/semaphores?
|
|
|
|
* This is a counting primitive of a binary signal state for syncing threads against work-group completion or a single work-group ready step.
|
|
|
|
*/
|
2023-09-20 03:52:44 +00:00
|
|
|
struct FutexBarrier final
|
|
|
|
{
|
|
|
|
inline constexpr FutexBarrier(AuUInt32 uExpectThreads) :
|
|
|
|
uAtomicSleeping(uExpectThreads),
|
|
|
|
uAtomicState(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
AU_NO_COPY_NO_MOVE(FutexBarrier);
|
|
|
|
|
2024-05-27 13:05:22 +00:00
|
|
|
/**
|
|
|
|
* @brief alert: one thread ready or task complete, wait until the work group is ready
|
|
|
|
*/
|
2023-09-20 03:52:44 +00:00
|
|
|
inline bool Enter()
|
|
|
|
{
|
|
|
|
bool bTimeout {};
|
|
|
|
return this->Park(bTimeout, true, 0);
|
|
|
|
}
|
2024-05-27 13:05:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief alert: one thread ready or task complete, wait until the work group is ready with an optional timeout
|
|
|
|
*/
|
2023-09-20 03:52:44 +00:00
|
|
|
inline AuPair<bool /* bSuccess */, bool /* bTimeOut */>
|
|
|
|
EnterTimed(AuUInt64 uTimeoutAbsNS)
|
|
|
|
{
|
|
|
|
bool bTimeout {};
|
|
|
|
bool bSuccess = this->Park(bTimeout, true, uTimeoutAbsNS);
|
|
|
|
return AuMakePair(bSuccess, bTimeout);
|
|
|
|
}
|
|
|
|
|
2024-05-27 13:05:22 +00:00
|
|
|
/**
|
|
|
|
* @brief alert: one thread ready or task complete, wait until the work group is ready with an optional timeout, call callOnce on a single thread
|
|
|
|
*/
|
2023-09-20 16:30:38 +00:00
|
|
|
inline AuPair<bool /* bSuccess */, bool /* bTimeOut */>
|
|
|
|
EnterTimedEx(AuUInt64 uTimeoutAbsNS,
|
|
|
|
AuVoidFunc callOnce)
|
|
|
|
{
|
|
|
|
bool bTimeout {}, bSuccess {};
|
|
|
|
bSuccess = this->Park(bTimeout,
|
|
|
|
true,
|
|
|
|
uTimeoutAbsNS,
|
|
|
|
callOnce);
|
|
|
|
return AuMakePair(bSuccess, bTimeout);
|
|
|
|
}
|
|
|
|
|
2024-05-27 13:05:22 +00:00
|
|
|
/**
|
|
|
|
* @brief alert: one thread ready or task complete
|
|
|
|
*/
|
2023-09-20 03:52:44 +00:00
|
|
|
inline bool TryEnter()
|
|
|
|
{
|
|
|
|
return this->TryEnterNoSpin();
|
|
|
|
}
|
|
|
|
|
2024-05-27 13:05:22 +00:00
|
|
|
/**
|
|
|
|
* @brief Has barrier met FutexBarrier(uExpectThreads) ?
|
|
|
|
*/
|
2023-09-20 03:52:44 +00:00
|
|
|
inline bool TryWait()
|
|
|
|
{
|
|
|
|
return this->TryChkNoSpin();
|
|
|
|
}
|
|
|
|
|
2024-05-27 13:05:22 +00:00
|
|
|
/**
|
|
|
|
* @brief Wait until the barrier has met FutexBarrier(uExpectThreads)
|
|
|
|
*/
|
2023-09-20 03:52:44 +00:00
|
|
|
inline bool Wait(AuUInt64 uTimeoutAbsNS = 0)
|
|
|
|
{
|
|
|
|
bool bTimeout {};
|
|
|
|
(void)this->Park(bTimeout, false, uTimeoutAbsNS);
|
|
|
|
return !bTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-09-20 16:30:38 +00:00
|
|
|
|
|
|
|
auline bool Park(bool &bTimedOut,
|
|
|
|
bool bTakeOwnership,
|
|
|
|
AuUInt64 uTimeoutAbsNS,
|
|
|
|
AuVoidFunc callOnce = {})
|
2023-09-20 03:52:44 +00:00
|
|
|
{
|
|
|
|
static const AuUInt32 kRef { 0 };
|
|
|
|
|
|
|
|
bTimedOut = false;
|
|
|
|
|
|
|
|
if (bTakeOwnership)
|
|
|
|
{
|
2023-09-20 16:30:38 +00:00
|
|
|
if (!this->TryEnterNoSpin(callOnce))
|
2023-09-20 03:52:44 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!this->TryChkNoSpin())
|
|
|
|
{
|
2023-11-14 14:44:56 +00:00
|
|
|
if (TryWaitOnAddressEx((const void *)&this->uAtomicState,
|
|
|
|
&kRef,
|
|
|
|
sizeof(kRef),
|
|
|
|
[&](const void *pTargetAddress,
|
|
|
|
const void *pCompareAddress,
|
|
|
|
AuUInt8 uWordSize)
|
|
|
|
{
|
|
|
|
return this->TryChkNoSpin();
|
|
|
|
}))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:52:44 +00:00
|
|
|
if (!WaitOnAddressSteady((const void *)&this->uAtomicState, &kRef, sizeof(kRef), uTimeoutAbsNS))
|
|
|
|
{
|
|
|
|
if (this->TryChkNoSpin())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bTimedOut = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-20 16:30:38 +00:00
|
|
|
auline bool TryEnterNoSpin(AuVoidFunc callOnce = {})
|
2023-09-20 03:52:44 +00:00
|
|
|
{
|
|
|
|
AuUInt32 uState;
|
|
|
|
|
|
|
|
while ((uState = AuAtomicLoad(&this->uAtomicSleeping)) != 0)
|
|
|
|
{
|
|
|
|
if (AuAtomicCompareExchange(&this->uAtomicSleeping, uState - 1, uState) == uState)
|
|
|
|
{
|
|
|
|
if (uState == 1)
|
|
|
|
{
|
2023-09-20 16:30:38 +00:00
|
|
|
if (callOnce)
|
|
|
|
{
|
|
|
|
callOnce();
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:52:44 +00:00
|
|
|
AuAtomicStore(&this->uAtomicState, 1u);
|
|
|
|
WakeAllOnAddress((const void *)&this->uAtomicState);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auline bool TryChkNoSpin()
|
|
|
|
{
|
|
|
|
return AuAtomicLoad(&this->uAtomicState) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
AuAUInt32 uAtomicState {};
|
|
|
|
AuAUInt32 uAtomicSleeping {};
|
|
|
|
};
|
|
|
|
}
|