2021-06-27 21:25:29 +00:00
|
|
|
/***
|
2024-01-29 14:09:59 +00:00
|
|
|
Copyright (C) 2021-2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
File: ConditionVariable.Win32.hpp
|
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#pragma once
|
|
|
|
|
2023-12-29 15:35:25 +00:00
|
|
|
#include "AuConditionVariable.Generic.hpp"
|
2021-06-27 21:25:29 +00:00
|
|
|
#if !defined(_AURUNTIME_GENERICCV)
|
2023-03-15 00:35:29 +00:00
|
|
|
#include "AuConditionMutex.NT.hpp"
|
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
namespace Aurora::Threading::Primitives
|
|
|
|
{
|
2023-08-21 16:34:24 +00:00
|
|
|
struct ConditionVariableNT final
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2023-08-21 16:34:24 +00:00
|
|
|
ConditionVariableNT();
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2023-12-10 20:01:06 +00:00
|
|
|
bool WaitForSignalNsEx(Win32ConditionMutex *pMutex, AuUInt64 timeout, bool bSpin = true);
|
2023-08-21 16:34:24 +00:00
|
|
|
void Signal();
|
|
|
|
void Broadcast();
|
2023-10-12 15:58:08 +00:00
|
|
|
void BroadcastN(AuUInt32 nBroadcast);
|
2024-01-27 11:46:18 +00:00
|
|
|
|
|
|
|
auline void AddWaiter();
|
2023-07-09 21:33:16 +00:00
|
|
|
auline bool CheckOut();
|
2023-12-06 18:24:52 +00:00
|
|
|
auline bool CheckOutNoSpin();
|
2023-03-15 08:28:16 +00:00
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
private:
|
2023-03-16 18:25:23 +00:00
|
|
|
#if defined(AURORA_FORCE_SRW_LOCKS)
|
|
|
|
CONDITION_VARIABLE winCond_;
|
|
|
|
#else
|
2023-09-02 03:55:43 +00:00
|
|
|
AuAUInt32 wlist {};
|
|
|
|
AuAUInt32 signalCount {};
|
2023-03-16 18:25:23 +00:00
|
|
|
#endif
|
2021-06-27 21:25:29 +00:00
|
|
|
};
|
2023-06-12 17:31:10 +00:00
|
|
|
|
2023-08-21 16:34:24 +00:00
|
|
|
using ConditionVariableInternal = ConditionVariableNT;
|
|
|
|
|
|
|
|
struct ConditionVariableImpl final : IConditionVariable
|
2023-06-15 22:59:47 +00:00
|
|
|
{
|
2023-08-21 16:34:24 +00:00
|
|
|
inline ConditionVariableImpl(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<ConditionMutexImpl>(this->mutex)->mutex, AuMSToNS<AuUInt64>(timeout));
|
|
|
|
}
|
|
|
|
|
|
|
|
auline bool WaitForSignalNS(AuUInt64 qwTimeout) override
|
|
|
|
{
|
|
|
|
return cond.WaitForSignalNsEx(&std::static_pointer_cast<ConditionMutexImpl>(this->mutex)->mutex, qwTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool WaitForSignalNsEx(const std::shared_ptr<Win32ConditionMutex> &pMutex, AuUInt64 timeout)
|
|
|
|
{
|
|
|
|
return cond.WaitForSignalNsEx(pMutex.get(), timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
auline void Signal() override
|
|
|
|
{
|
|
|
|
cond.Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
auline void Broadcast() override
|
|
|
|
{
|
|
|
|
cond.Broadcast();
|
|
|
|
}
|
|
|
|
|
|
|
|
ConditionVariableNT cond;
|
|
|
|
std::shared_ptr<IConditionMutex> mutex;
|
2023-06-15 22:59:47 +00:00
|
|
|
};
|
|
|
|
|
2023-08-21 16:34:24 +00:00
|
|
|
static const auto kSizeOfDummyCondVar = sizeof(ConditionVariableInternal);
|
2023-06-15 22:59:47 +00:00
|
|
|
|
2023-06-12 17:31:10 +00:00
|
|
|
static const auto kBoolRequiredLateSet = true;
|
|
|
|
|
|
|
|
static const auto kShiftCountByBits = 8u;
|
|
|
|
// ...otherwise
|
|
|
|
// assume undefined behaviour past:
|
|
|
|
// * bit zero is used for atomic bit test and yield loops
|
|
|
|
// ( keyed events are an optimization mechanism for Windows XPs spinloop i had accidentally recreated in xenus. )
|
|
|
|
// ( originally, nt yielding sucked with the most barebones spinlock being dumb a hypervisor-unaware, smt-aware, spinner. )
|
2023-08-18 22:30:38 +00:00
|
|
|
// ( keyed events would then go in these spinners to serve as an early futex as early back as the year 2001-3 (?). )
|
2023-06-12 17:31:10 +00:00
|
|
|
// * bit one might be used under some niche versions of windows
|
|
|
|
// (hearsay paranoia)
|
2023-08-18 22:30:38 +00:00
|
|
|
// i actually have no reason to believe windows ever implemented lock-awareness into the kernel
|
2023-06-12 17:31:10 +00:00
|
|
|
// i think it might be fine to skip the whole bit zero thing, but still, im going to say keep the min=2
|
2023-08-18 22:30:38 +00:00
|
|
|
// worst case scenario, we end up using these bits (we did).
|
2023-06-12 17:31:10 +00:00
|
|
|
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
#endif
|