AuroraRuntime/Source/Threading/Primitives/AuRWLock.hpp
Reece Wilson 8ff81df129 [*] Fix deadlock involving WaitFor under ThreadPool (shutdown race)
[*] Optimize mutex lock out of RWLockImpl::TryLockWrite
[*] Force all relevant members of RWLockImpl to be volatile just bc lol (afaik we cant justify it yet; however, i want to minimalize the risk of future issues in this type)
2023-01-30 14:35:48 +00:00

85 lines
1.8 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuRWLock.hpp
Date: 2021-6-12
Author: Reece
***/
#pragma once
#include "AuConditionVariable.Generic.hpp"
#include "AuConditionMutex.Generic.hpp"
namespace Aurora::Threading::Primitives
{
struct RWLockImpl;
template<bool isread>
struct RWLockAccessView : IWaitable
{
RWLockAccessView(RWLockImpl &impl) : parent_(impl)
{
}
bool Lock(AuUInt64 timeout) override;
bool TryLock() override;
bool HasOSHandle(AuMach &mach) override
{
return false;
}
bool HasLockImplementation() override
{
return true;
}
void Lock() override
{
SysAssert(Lock(0));
}
void Unlock() override;
private:
RWLockImpl &parent_;
};
struct RWLockImpl : IRWLock
{
RWLockImpl();
~RWLockImpl();
bool LockRead(AuUInt64 timeout);
bool LockWrite(AuUInt64 timeout);
bool TryLockRead();
bool TryLockWrite();
void UnlockRead();
void UnlockWrite();
bool UpgradeReadToWrite(AuUInt64 timeout) override;
bool DowngradeWriteToRead() override;
IWaitable *AsReadable() override;
IWaitable *AsWritable() override;
bool Init();
private:
//bool reentrantWriteLock_ {true};
AuUInt reentrantWriteLockHandle_ {};
RWLockAccessView<true> read_;
RWLockAccessView<false> write_;
ConditionMutexImpl mutex_;
ConditionVariableImpl condition_;
volatile AuInt32 state_ {};
AuInt32 writersPending_ {};
bool bElevaterPending_ {};
};
}