2021-06-27 21:25:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
File: AuConditionEx.cpp
|
2021-06-27 21:25:29 +00:00
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2022-11-17 07:46:07 +00:00
|
|
|
#include "AuConditionEx.hpp"
|
|
|
|
#include "AuSemaphore.Generic.hpp"
|
2024-04-13 21:49:05 +00:00
|
|
|
#include "SMTYield.hpp" // for: while_bc
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
|
|
{
|
2022-06-01 21:49:38 +00:00
|
|
|
struct SemaphoreConditionVariableImpl : ConditionEx
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
SemaphoreConditionVariableImpl();
|
|
|
|
|
2023-07-25 11:38:49 +00:00
|
|
|
void WaitForSignal(const AuSPtr<IWaitable> &pWaitable) override;
|
2021-06-27 21:25:29 +00:00
|
|
|
void WaitForSignal() override;
|
2023-07-25 11:38:49 +00:00
|
|
|
|
2023-08-12 10:18:19 +00:00
|
|
|
bool WaitForSignalNS(AuUInt64 uRelativeNanoseconds) override;
|
|
|
|
bool WaitForSignalNS(const AuSPtr<IWaitable> &waitable, AuUInt64 uRelativeNanoseconds) override;
|
2023-07-25 11:38:49 +00:00
|
|
|
|
2023-11-29 06:08:09 +00:00
|
|
|
bool WaitForSignalAbsNS(AuUInt64 uAbsNanoseconds) override;
|
|
|
|
bool WaitForSignalAbsNS(const AuSPtr<IWaitable> &pWaitable, AuUInt64 uAbsNanoseconds) override;
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
void Signal() override;
|
|
|
|
void Broadcast() override;
|
|
|
|
|
|
|
|
private:
|
2023-07-25 10:40:30 +00:00
|
|
|
SemaphoreImpl s_;
|
2023-09-02 03:55:43 +00:00
|
|
|
AuAUInt32 uWaiters_;
|
2021-06-27 21:25:29 +00:00
|
|
|
};
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
SemaphoreConditionVariableImpl::SemaphoreConditionVariableImpl() :
|
|
|
|
s_(0),
|
2023-07-25 11:38:49 +00:00
|
|
|
uWaiters_(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-12 10:18:19 +00:00
|
|
|
bool SemaphoreConditionVariableImpl::WaitForSignalNS(AuUInt64 uRelativeNanoseconds)
|
2023-07-25 11:38:49 +00:00
|
|
|
{
|
2023-08-12 10:18:19 +00:00
|
|
|
return WaitForSignalNS(nullptr, uRelativeNanoseconds);
|
2023-07-25 11:38:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 06:08:09 +00:00
|
|
|
bool SemaphoreConditionVariableImpl::WaitForSignalAbsNS(const AuSPtr<IWaitable> &pWaitable,
|
2023-12-01 03:17:56 +00:00
|
|
|
AuUInt64 uNanoseconds)
|
2023-07-25 11:38:49 +00:00
|
|
|
{
|
|
|
|
AuAtomicAdd(&this->uWaiters_, 1u);
|
|
|
|
|
|
|
|
if (pWaitable)
|
|
|
|
{
|
|
|
|
pWaitable->Unlock();
|
|
|
|
}
|
|
|
|
|
2023-12-01 03:17:56 +00:00
|
|
|
auto bSuccess = this->s_.LockAbsNS(uNanoseconds);
|
2023-07-25 11:38:49 +00:00
|
|
|
if (!bSuccess)
|
|
|
|
{
|
|
|
|
auto uWaiters = this->uWaiters_;
|
|
|
|
auto uWaitCount = 1;
|
|
|
|
|
2023-08-20 23:25:29 +00:00
|
|
|
while (uWaiters &&
|
|
|
|
AuAtomicCompareExchange(&this->uWaiters_, uWaiters - uWaitCount, uWaiters) != uWaiters)
|
2023-07-25 11:38:49 +00:00
|
|
|
{
|
|
|
|
uWaiters = this->uWaiters_;
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2023-07-25 11:38:49 +00:00
|
|
|
if (uWaiters == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pWaitable)
|
|
|
|
{
|
|
|
|
pWaitable->Lock();
|
|
|
|
}
|
2023-08-12 10:18:19 +00:00
|
|
|
|
|
|
|
return bSuccess;
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 06:08:09 +00:00
|
|
|
bool SemaphoreConditionVariableImpl::WaitForSignalNS(const AuSPtr<IWaitable> &pWaitable,
|
|
|
|
AuUInt64 uAbsNanoseconds)
|
2021-10-08 19:51:34 +00:00
|
|
|
{
|
2023-11-29 06:08:09 +00:00
|
|
|
return this->WaitForSignalAbsNS(pWaitable,
|
|
|
|
uAbsNanoseconds ? AuTime::SteadyClockNS() + uAbsNanoseconds : 0);
|
2021-10-08 19:51:34 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 11:38:49 +00:00
|
|
|
void SemaphoreConditionVariableImpl::WaitForSignal(const AuSPtr<IWaitable> &pWaitable)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2023-07-25 11:38:49 +00:00
|
|
|
AuAtomicAdd(&this->uWaiters_, 1u);
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
if (pWaitable)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-11-17 07:46:07 +00:00
|
|
|
pWaitable->Unlock();
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
this->s_.Lock();
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
if (pWaitable)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-11-17 07:46:07 +00:00
|
|
|
pWaitable->Lock();
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SemaphoreConditionVariableImpl::WaitForSignal()
|
|
|
|
{
|
|
|
|
WaitForSignal(nullptr);
|
|
|
|
}
|
|
|
|
|
2023-11-29 06:08:09 +00:00
|
|
|
bool SemaphoreConditionVariableImpl::WaitForSignalAbsNS(AuUInt64 uAbsNanoseconds)
|
|
|
|
{
|
|
|
|
return this->WaitForSignalAbsNS({}, uAbsNanoseconds);
|
|
|
|
}
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
void SemaphoreConditionVariableImpl::Signal()
|
|
|
|
{
|
2023-07-25 10:40:30 +00:00
|
|
|
AuUInt32 uWaitCount {};
|
|
|
|
AuUInt32 uWaiters {};
|
|
|
|
|
2023-08-23 19:20:12 +00:00
|
|
|
uWaiters = AuAtomicLoad(&this->uWaiters_);
|
2023-08-26 17:46:00 +00:00
|
|
|
if (uWaiters == 0)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2023-08-26 17:46:00 +00:00
|
|
|
return;
|
2023-07-25 10:40:30 +00:00
|
|
|
}
|
|
|
|
|
2023-08-26 17:46:00 +00:00
|
|
|
this->s_.Unlock();
|
|
|
|
|
2023-08-22 08:44:54 +00:00
|
|
|
while (uWaiters &&
|
2023-08-26 17:46:00 +00:00
|
|
|
AuAtomicCompareExchange(&this->uWaiters_, uWaiters - 1u, uWaiters) != uWaiters)
|
2023-07-25 10:40:30 +00:00
|
|
|
{
|
2023-07-25 11:38:49 +00:00
|
|
|
uWaiters = this->uWaiters_;
|
2023-07-25 10:40:30 +00:00
|
|
|
|
|
|
|
if (uWaiters == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SemaphoreConditionVariableImpl::Broadcast()
|
|
|
|
{
|
2023-07-25 10:40:30 +00:00
|
|
|
AuUInt32 uWaitCount {};
|
|
|
|
AuUInt32 uWaiters {};
|
|
|
|
|
2024-04-13 21:49:05 +00:00
|
|
|
while_bc ((uWaiters = AuAtomicLoad(&this->uWaiters_)))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2023-07-25 10:40:30 +00:00
|
|
|
this->s_.Unlock(uWaiters);
|
|
|
|
uWaitCount = uWaiters;
|
|
|
|
|
2023-08-26 14:53:13 +00:00
|
|
|
while (uWaiters &&
|
|
|
|
AuAtomicCompareExchange(&this->uWaiters_, uWaiters - uWaitCount, uWaiters) != uWaiters)
|
2023-07-25 10:40:30 +00:00
|
|
|
{
|
2023-08-26 14:53:13 +00:00
|
|
|
uWaiters = this->uWaiters_;
|
|
|
|
|
|
|
|
if (uWaiters <= uWaitCount)
|
|
|
|
{
|
|
|
|
uWaitCount = uWaiters;
|
|
|
|
}
|
2023-07-25 10:40:30 +00:00
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-25 11:25:22 +00:00
|
|
|
AUKN_SYM ConditionEx *FlexibleConditionVariableNew()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
return _new SemaphoreConditionVariableImpl();
|
|
|
|
}
|
|
|
|
|
2023-07-25 11:25:22 +00:00
|
|
|
AUKN_SYM void FlexibleConditionVariableRelease(ConditionEx *pCVEx)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-11-17 07:46:07 +00:00
|
|
|
AuSafeDelete<SemaphoreConditionVariableImpl *>(pCVEx);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
2023-07-25 11:25:22 +00:00
|
|
|
|
|
|
|
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, FlexibleConditionVariable, SemaphoreConditionVariableImpl)
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|