AuroraRuntime/Source/Threading/Primitives/AuRWLock.hpp

152 lines
5.3 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021-2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
2021-06-27 21:25:29 +00:00
2022-11-17 07:46:07 +00:00
File: AuRWLock.hpp
2021-06-27 21:25:29 +00:00
Date: 2021-6-12
Author: Reece
***/
#pragma once
#include "AuConditionVariable.Generic.hpp"
#include "AuConditionMutex.Generic.hpp"
#include "ThreadCookie.hpp"
#if defined(AURORA_IS_LINUX_DERIVED)
// Disable Windows XP - 7 keyedevent inline optimization (not that we couldn't use AURORA_RWLOCK_IS_CONDLESS on XP/7 with WOA_ALWAYS_DUMB_OS_TARGET).
// Note that *a lot* of platforms may end up requiring all thread primitives to use more verbose primitives.
// Keeping the condvar impl around is best in the event we need to optimize for unique platforms of sensitive schedulers.
// The noisey use-case of the semaphore hash table isn't what we always want for persistent objects with QoS properties.
// In terms of platform support/what-to-do-where:
// Here's one example of a QoS senitive OS without a futex wait queue interface we might care about: > MACOS <
// Now let's ignore the updoooters and support Windows XP - 7...
// ...this is a Linux specific thing
//
// Windows XP-7: same as 11 (no such win32 alternative) (EOL support)
// Windows 11 updoooters can cope with a 56-byte primitive in the worst case scenario (who even cares case?)
// Generic: 64-byte (no config, maybe use AURORA_RWLOCK_IS_CONDLESS) (expected case)
// Linux: AURORA_RWLOCK_IS_CONDLESS best case, 64-byte without (best case)
// AURORA_RWLOCK_IS_CONDLESS: 48-byte (best case)
// vs couple hundred byte primitives in the STL
// in either case AURORA_RWLOCK_IS_CONDLESS doesnt matter so much (delta linux:16, win32: measly 8 bytes)
// in either case performance is the same
#define AURORA_RWLOCK_IS_CONDLESS
#endif
2021-06-27 21:25:29 +00:00
namespace Aurora::Threading::Primitives
{
template<bool bIsWriteRecursionAllowed>
2022-06-01 21:49:38 +00:00
struct RWLockImpl;
2021-06-27 21:25:29 +00:00
template<bool bIsReadView, typename T>
struct RWLockAccessView final : IWaitable
2021-06-27 21:25:29 +00:00
{
#if defined(RWLOCK_VIEW_HAS_PARENT)
RWLockAccessView(T &impl) :
parent_(impl)
2021-06-27 21:25:29 +00:00
{
}
#endif
2021-06-27 21:25:29 +00:00
bool LockMS(AuUInt64 timeout) override;
bool LockNS(AuUInt64 timeout) override;
bool LockAbsMS(AuUInt64 timeout) override;
bool LockAbsNS(AuUInt64 timeout) override;
2021-06-27 21:25:29 +00:00
bool TryLock() override;
2021-06-27 21:25:29 +00:00
bool HasOSHandle(AuMach &mach) override
{
return false;
}
bool HasLockImplementation() override
{
return true;
}
void Lock() override
{
SysAssert(LockNS(0));
2021-06-27 21:25:29 +00:00
}
void Unlock() override;
2021-06-27 21:25:29 +00:00
private:
#if defined(RWLOCK_VIEW_HAS_PARENT)
T &parent_;
#endif
2021-06-27 21:25:29 +00:00
};
template<bool bIsWriteRecursionAllowed>
struct RWLockImpl final : IRWLock
2021-06-27 21:25:29 +00:00
{
RWLockImpl();
~RWLockImpl();
// i dont think i'll expose a high performance interface yet
auline bool LockReadNSAbs(AuUInt64 timeout);// override;
auline bool LockReadNS(AuUInt64 timeout);// override;
auline bool LockWriteNS(AuUInt64 timeout);// override;
auline bool LockWriteNSAbs(AuUInt64 timeout);// override;
auline bool TryLockRead();// override;
2023-09-19 16:08:06 +00:00
template<bool bCheckWrite>
auline bool TryLockReadNoSpin();
auline void UnlockRead();// override;
auline void UnlockWrite();// override;
2021-09-06 10:58:08 +00:00
2023-09-19 16:08:06 +00:00
auline void WriterWake();
auline bool WriterSleep(AuUInt64 qwTimeoutNS);
auline bool WriterTryLock();
auline void FutexWriterWake();
auline bool TryLockWriteMaybeSpin();// override;
auline bool TryLockWriteNoSpin();// override;
auline bool LockWriteNSAbsUnlocked(AuUInt64 qwTimeoutNS);// override;
2021-09-06 10:58:08 +00:00
bool UpgradeReadToWrite(AuUInt64 timeout) override;
2022-03-30 11:32:03 +00:00
bool DowngradeWriteToRead() override;
auline bool UpgradeReadToWriteDoUpgrade();
2021-06-27 21:25:29 +00:00
IWaitable *AsReadable() override;
IWaitable *AsWritable() override;
2023-12-01 01:12:18 +00:00
bool CheckSelfThreadIsWriter() override;
2023-08-21 16:34:24 +00:00
auline ConditionVariableInternal &GetCondition();
auline ConditionVariableInternal &GetConditionWriter();
auline AuUInt32 *GetFutexCondition();
auline AuUInt32 *GetFutexConditionWriter();
auline void SignalOneReader();
auline void SignalOneWriter();
auline void SignalManyReader();
auline void SignalManyWriter(int iBias = 0);
2021-06-27 21:25:29 +00:00
auline AuUInt32 *GetReadSleepCounter();
[*/+/-] MEGA COMMIT. ~2 weeks compressed. The intention is to quickly improve and add util apis, enhance functionality given current demands, go back to the build pipeline, finish that, publish runtime tests, and then use what we have to go back to to linux support with a more stable api. [+] AuMakeSharedArray [+] Technet ArgvQuote [+] Grug subsystem (UNIX signal thread async safe ipc + telemetry flusher + log flusher.) [+] auEndianness -> Endian swap utils [+] AuGet<N>(...) [*] AUE_DEFINE conversion for ECompresionType, EAnsiColor, EHashType, EStreamError, EHexDump [+] ConsoleMessage ByteBuffer serialization [+] CmdLine subsystem for parsing command line arguments and simple switch/flag checks [*] Split logger from console subsystem [+] StartupParameters -> A part of a clean up effort under Process [*] Refactor SysErrors header + get caller hack [+] Atomic APIs [+] popcnt [+] Ring Buffer sink [+] Added more standard errors Catch, Submission, LockError, NoAccess, ResourceMissing, ResourceLocked, MalformedData, InSandboxContext, ParseError [+] Added ErrorCategorySet, ErrorCategoryClear, GetStackTrace [+] IExitSubscriber, ETriggerLevel [*] Write bias the high performance RWLockImpl read-lock operation operation [+] ExitHandlerAdd/ExitHandlerRemove (exit subsystem) [*] Updated API style Digests [+] CpuId::CpuBitCount [+] GetUserProgramsFolder [+] GetPackagePath [*] Split IStreamReader with an inl file [*] BlobWriter/BlobReader/BlobArbitraryReader can now take shared pointers to bytebuffers. default constructor allocates a new scalable bytebuffer [+] ICharacterProvider [+] ICharacterProviderEx [+] IBufferedCharacterConsumer [+] ProviderFromSharedString [+] ProviderFromString [+] BufferConsumerFromProvider [*] Parse Subsystem uses character io bufferer [*] Rewritten NT's high perf semaphore to use userland SRW/ConVars [like mutex, based on generic semaphore] [+] ByteBuffer::ResetReadPointer [*] Bug fix bytebuffer base not reset on free and some scaling issues [+] ProcessMap -> Added kSectionNameStack, kSectionNameFile, kSectionNameHeap for Section [*] ProcessMap -> Refactor Segment to Section. I was stupid for keeping a type conflict hack API facing [+] Added 64 *byte* fast RNG seeds [+] File Advisorys/File Lock Awareness [+] Added extended IAuroraThread from OS identifier caches for debug purposes [*] Tweaked how memory is reported on Windows. Better consistency of what values mean across functions. [*] Broke AuroraUtils/Typedefs out into a separate library [*] Update build script [+] Put some more effort into adding detail to the readme before rewriting it, plus, added some media [*] Improved public API documentation [*] Bug fix `SetConsoleCtrlHandler` [+] Locale TimeDateToFileNameISO8601 [+] Console config stdOutShortTime [*] Begin using internal UTF8/16 decoders when platform support isnt available (instead of stl) [*] Bug fixes in decoders [*] Major bug fix, AuMax [+] RateLimiter [+] Binary file sink [+] Log directory sink [*] Data header usability (more operators) [+] AuRemoveRange [+] AuRemove [+] AuTryRemove [+] AuTryRemoveRange [+] auCastUtils [+] Finish NewLSWin32Source [+] AuTryFindByTupleN, AuTryRemoveByTupleN [+] Separated AuRead/Write types, now in auTypeUtils [+] Added GetPosition/SetPosition to FileWriter [*] Fix stupid AuMin in place of AuMax in SpawnThread.Unix.Cpp [*] Refactored Arbitrary readers to SeekingReaders (as in, they could be atomic and/or parallelized, and accept an arbitrary position as a work parameter -> not Seekable, as in, you can simply set the position) [*] Hack back in the sched deinit [+] File AIO loop source interop [+] Begin to prototype a LoopQueue object I had in mind for NT, untested btw [+] Stub code for networking [+] Compression BaseStream/IngestableStreamBase [*] Major: read/write locks now support write-entrant read routines. [*] Compression subsystem now uses the MemoryView concept [*] Rewrite the base stream compressions, made them less broken [*] Update hashing api [*] WriterTryGoForward and ReaderTryGoForward now revert to the previous relative index instead of panicing [+] Added new AuByteBuffer apis Trim, Pad, WriteFrom, WriteString, [TODO: ReadString] [+] Added ByteBufferPushReadState [+] Added ByteBufferPushWriteState [*] Move from USC-16 to full UTF-16. Win32 can handle full UTF-16. [*] ELogLevel is now an Aurora enum [+] Raised arbitrary limit in header to 255, the max filter buffer [+] Explicit GZip support [+] Explicit Zip support [+] Added [some] compressors et al
2022-02-17 00:11:40 +00:00
public: // "private"
RWLockAccessView<true, RWLockImpl> read_;
RWLockAccessView<false, RWLockImpl> write_;
private:
ThreadCookie_t reentrantWriteLockHandle_ {};
#if defined(AURORA_RWLOCK_IS_CONDLESS)
AuAUInt32 uSemaphore_ {};
AuAUInt32 uRDCounter_ {};
#else
2023-08-21 16:34:24 +00:00
ConditionMutexInternal mutex_;
#if defined(AURWLOCK_NO_SIZE_OPTIMIZED_CONDVAR)
2023-08-21 16:34:24 +00:00
ConditionVariableInternal condition_;
ConditionVariableInternal conditionWriter_;
#else
char conditionVariable_[kSizeOfDummyCondVar] {};
char conditionVariableWriter_[kSizeOfDummyCondVar] {};
#endif
#endif
2023-09-19 16:08:06 +00:00
AuAInt32 iState_ {};
AuAInt32 dwWritersPending_ {};
2021-06-27 21:25:29 +00:00
};
}