AuroraRuntime/Include/Aurora/Threading/Primitives/RWLock.hpp

41 lines
1.4 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: RWLock.hpp
Date: 2021-6-9
Author: Reece
***/
#pragma once
namespace Aurora::Threading::Primitives
{
class RWLock
{
public:
virtual IWaitable *AsReadable() = 0;
virtual IWaitable *AsWritable() = 0;
2021-09-06 10:58:08 +00:00
/**
* Solves the problem of atomic write racing
* Consider: Thread 1 | Thread 2
* ReadLock() |
* cmp internal_x, 0|
* jmp not eq +1 |
* ret |
* ReadUnlock() |
* | WriteLock()
* | internal_x = 1
* | WriteUnlock()
* WriteLock() |
* internal_x = 0 |
* WriteUnlock()
*
* where 1 and 0 should have been decided on mutually exclusively, losing the 1 state while assuming a decision in the locked state would remain true
* workaround: [1] check for the race conditions, implmeneting slow and inefficient branching and spurious checks
* workaround: [2] upgrade from read to write
*/
virtual bool UpgradeReadToWrite(AuUInt64 timeout) = 0;
2021-06-27 21:25:29 +00:00
};
AUKN_SHARED_API(RWLock, RWLock);
}