/*** 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; /** * 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; }; AUKN_SHARED_API(RWLock, RWLock); }