AuroraRuntime/Source/Threading/Primitives/RWLock.hpp

82 lines
1.7 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: RWLock.hpp
Date: 2021-6-12
Author: Reece
***/
#pragma once
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_;
ConditionMutexUnique_t mutex_;
ConditionVariableUnique_t condition_;
AuInt32 state_ {};
AuInt32 writersPending_ {};
bool bElevaterPending_ {};
};
}