AuroraRuntime/Source/Threading/Primitives/AuConditionVariable.Generic.hpp

88 lines
2.6 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021-2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
2021-06-27 21:25:29 +00:00
2022-11-17 07:46:07 +00:00
File: AuConditionVariable.Generic.hpp
2021-06-27 21:25:29 +00:00
Date: 2021-6-14
Author: Reece
***/
#pragma once
2022-11-17 07:46:07 +00:00
#include "AuIConditionMutexEx.hpp"
2021-06-27 21:25:29 +00:00
2021-09-06 10:58:08 +00:00
#if defined(AURORA_IS_MODERNNT_DERIVED)
2022-11-17 07:46:07 +00:00
#include "AuConditionVariable.NT.hpp"
#elif defined(AURORA_IS_LINUX_DERIVED)
#include "AuConditionVariable.Linux.hpp"
2021-06-27 21:25:29 +00:00
#else
#define _AURUNTIME_GENERICCV
2022-11-17 07:46:07 +00:00
#include "AuConditionMutex.Generic.hpp"
2021-06-27 21:25:29 +00:00
namespace Aurora::Threading::Primitives
{
#pragma pack(push)
#pragma pack(4)
struct ConditionVariableGeneric final
2021-06-27 21:25:29 +00:00
{
ConditionVariableGeneric();
~ConditionVariableGeneric();
bool WaitForSignalNsEx(GenericConditionMutex *pMutex, AuUInt64 timeout, bool bSpin = true);
void Signal();
void Broadcast();
bool WaitOne(AuUInt64 qwTimeout, bool bSpin);
2021-06-27 21:25:29 +00:00
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;
2021-06-27 21:25:29 +00:00
};
static const auto kSizeOfDummyCondVar = sizeof(ConditionVariableInternal);
2021-06-27 21:25:29 +00:00
}
#endif