AuroraRuntime/Source/Threading/Primitives/AuConditionVariable.Generic.hpp
Jamie Reece Wilson 62b6fa20f8 [*] Update the copyright header of most of the primitives
[*] Fix generic mutex abs yield always returning true
2024-01-29 14:48:04 +00:00

88 lines
2.6 KiB
C++

/***
Copyright (C) 2021-2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuConditionVariable.Generic.hpp
Date: 2021-6-14
Author: Reece
***/
#pragma once
#include "AuIConditionMutexEx.hpp"
#if defined(AURORA_IS_MODERNNT_DERIVED)
#include "AuConditionVariable.NT.hpp"
#elif defined(AURORA_IS_LINUX_DERIVED)
#include "AuConditionVariable.Linux.hpp"
#else
#define _AURUNTIME_GENERICCV
#include "AuConditionMutex.Generic.hpp"
namespace Aurora::Threading::Primitives
{
#pragma pack(push)
#pragma pack(4)
struct ConditionVariableGeneric final
{
ConditionVariableGeneric();
~ConditionVariableGeneric();
bool WaitForSignalNsEx(GenericConditionMutex *pMutex, AuUInt64 timeout, bool bSpin = true);
void Signal();
void Broadcast();
bool WaitOne(AuUInt64 qwTimeout, bool bSpin);
private:
bool TryTakeOneNoSpin();
bool TryTakeOneSpin();
AuUInt32 uState_ {};
AuUInt32 uSleeping_ {};
};
#pragma pack(pop)
using ConditionVariableInternal = ConditionVariableGeneric;
struct ConditionVariableGenericImpl final : IConditionVariable
{
inline ConditionVariableGenericImpl(const AuSPtr<IConditionMutex> &mutex) :
mutex(mutex)
{ }
auline AuSPtr<IConditionMutex> GetMutex() override
{
return mutex;
}
auline bool WaitForSignal(AuUInt32 timeout) override
{
return cond.WaitForSignalNsEx(&std::static_pointer_cast<ConditionMutexGenericImpl>(this->mutex)->mutex, AuMSToNS<AuUInt64>(timeout));
}
auline bool WaitForSignalNS(AuUInt64 qwTimeout) override
{
return cond.WaitForSignalNsEx(&std::static_pointer_cast<ConditionMutexGenericImpl>(this->mutex)->mutex, qwTimeout);
}
inline bool WaitForSignalNsEx(const std::shared_ptr<GenericConditionMutex> &pMutex, AuUInt64 timeout)
{
return cond.WaitForSignalNsEx(pMutex.get(), timeout);
}
auline void Signal() override
{
cond.Signal();
}
auline void Broadcast() override
{
cond.Broadcast();
}
ConditionVariableGeneric cond;
std::shared_ptr<IConditionMutex> mutex;
};
static const auto kSizeOfDummyCondVar = sizeof(ConditionVariableInternal);
}
#endif