From d18affeea233170e8d61eb035e01310e2b37e842 Mon Sep 17 00:00:00 2001 From: Reece Date: Mon, 8 Nov 2021 23:59:19 +0000 Subject: [PATCH] [+] Added first attempt at the windows version of WaitMultipleObjects/ This will be removed shortly. [*] Update readme [*] Update QueryBsdHwStat --- Include/Aurora/Loop/Loop.hpp | 2 +- Source/HWInfo/HWInfo.cpp | 14 ++++- Source/HWInfo/HWInfo.hpp | 3 +- Source/HWInfo/RamInfo.cpp | 14 ++++- Source/Loop/LSWin32.NT.cpp | 32 ++++++++++- Source/Loop/Loop.NT.cpp | 104 ++++++++++++++++++++++++++++++++++- readme.md | 19 ++++--- 7 files changed, 170 insertions(+), 18 deletions(-) diff --git a/Include/Aurora/Loop/Loop.hpp b/Include/Aurora/Loop/Loop.hpp index 0b602d00..684c86c1 100644 --- a/Include/Aurora/Loop/Loop.hpp +++ b/Include/Aurora/Loop/Loop.hpp @@ -45,7 +45,7 @@ namespace Aurora::Loop virtual ELoopSource GetType() = 0; }; - AUKN_SYM AuList> WaitMultipleObjects(const AuList> &objects, AuUInt32 timeout); + AUKN_SYM AuList> WaitMultipleOrObjects(const AuList> &objects, AuUInt32 timeout); class ILSSemaphore : public ILoopSource { diff --git a/Source/HWInfo/HWInfo.cpp b/Source/HWInfo/HWInfo.cpp index 0fdb256f..22ed36a6 100644 --- a/Source/HWInfo/HWInfo.cpp +++ b/Source/HWInfo/HWInfo.cpp @@ -17,11 +17,12 @@ namespace Aurora::HWInfo { - AuOptional QueryBsdHwStat(int id) + template + AuOptional QueryBsdHwStat(int id) { #if defined(AURORA_IS_BSD_DERIVED) int mib[4]; - AuUInt stat; + T stat; size_t len = sizeof(stat); mib[0] = CTL_HW; @@ -33,6 +34,15 @@ namespace Aurora::HWInfo return {}; #endif } + + template<> + AuOptional QueryBsdHwStat(int id); + + template<> + AuOptional QueryBsdHwStat(int id); + + template<> + AuOptional QueryBsdHwStat(int id); void Init() { diff --git a/Source/HWInfo/HWInfo.hpp b/Source/HWInfo/HWInfo.hpp index b49b691a..db8037c8 100644 --- a/Source/HWInfo/HWInfo.hpp +++ b/Source/HWInfo/HWInfo.hpp @@ -9,6 +9,7 @@ namespace Aurora::HWInfo { - AuOptional QueryBsdHwStat(int id); + template + AuOptional QueryBsdHwStat(int id); void Init(); } \ No newline at end of file diff --git a/Source/HWInfo/RamInfo.cpp b/Source/HWInfo/RamInfo.cpp index cbc578cc..92df88fd 100644 --- a/Source/HWInfo/RamInfo.cpp +++ b/Source/HWInfo/RamInfo.cpp @@ -73,7 +73,7 @@ namespace Aurora::HWInfo size_t len = sizeof(info); sysctl(mib, 2, &info, &len, NULL, 0); - return info; + return info; // TODO: consider polling VM_UVMEXP } #endif @@ -93,9 +93,17 @@ namespace Aurora::HWInfo #elif defined(AURORA_IS_BSD_DERIVED) - auto maxMem = QueryBsdHwStat(HW_PHYSMEM); + #if defined(HW_PHYSMEM64) + uint64_t stat; + auto cmd = HW_PHYSMEM64; + #else + unsigned int stat; + auto cmd = HW_PHYSMEM; + #endif + + auto maxMem = QueryBsdHwStat(cmd); auto vmInfo = GetVMInfo(); - auto freeMem = AuUInt64(vmInfo.t_free) * QueryBsdHwStat(HW_PAGESIZE).value_or(4096); + auto freeMem = AuUInt64(vmInfo.t_free) * QueryBsdHwStat(HW_PAGESIZE).value_or(4096); return RamStat {maxMem.value_or(freeMem * 2) - freeMem, maxMem.value_or(0)}; diff --git a/Source/Loop/LSWin32.NT.cpp b/Source/Loop/LSWin32.NT.cpp index a906781e..7cfc2399 100644 --- a/Source/Loop/LSWin32.NT.cpp +++ b/Source/Loop/LSWin32.NT.cpp @@ -10,5 +10,35 @@ namespace Aurora::Loop { - + class Win32Dummy : public ILoopSource + { + public: + Win32Dummy() + {} + + bool IsSignaled() override; + ELoopSource GetType() override; + }; + + bool Win32Dummy::IsSignaled() + { + return {}; + } + + ELoopSource Win32Dummy::GetType() + { + return ELoopSource::eSourceWin32; + } + + AUKN_SYM AuSPtr NewLSWin32Source() + { + AuSPtr ret; + + if (!(ret = AuMakeShared())) + { + return {}; + } + + return ret; + } } \ No newline at end of file diff --git a/Source/Loop/Loop.NT.cpp b/Source/Loop/Loop.NT.cpp index caf466f8..78cac163 100644 --- a/Source/Loop/Loop.NT.cpp +++ b/Source/Loop/Loop.NT.cpp @@ -7,11 +7,111 @@ ***/ #include #include "Loop.NT.hpp" +#include "ILoopSourceEx.hpp" namespace Aurora::Loop { - AUKN_SYM AuList> WaitMultipleObjects(const AuList> &objects, AuUInt32 timeout) + AUKN_SYM AuList> WaitMultipleOrObjects(const AuList> &objects, AuUInt32 timeout) { - return {}; + bool isWinLoop; + AuList> loopSourceExs; + AuList> triggered; + AuList handleArray; + AuSPtr msgSource; + + isWinLoop = false; + loopSourceExs.reserve(objects.size()); + handleArray.reserve(objects.size()); + triggered.reserve(triggered.size()); + + for (const auto &source : objects) + { + if (!source) + { + continue; + } + + if (source->GetType() == ELoopSource::eSourceWin32) + { + isWinLoop = true; + msgSource = source; + continue; + } + + if (auto extended = std::dynamic_pointer_cast(source)) + { + loopSourceExs.push_back(extended); + for (const auto &handle : extended->GetHandles()) + { + auto nthandle = reinterpret_cast(handle); + handleArray.push_back(nthandle); + } + } + } + + for (const auto &source : loopSourceExs) + { + source->OnPresleep(); + } + + DWORD ret; + if (isWinLoop) + { + ret = ::MsgWaitForMultipleObjectsEx(handleArray.size(), handleArray.data(), timeout ? timeout : INFINITE, QS_ALLPOSTMESSAGE | QS_ALLINPUT | QS_ALLEVENTS, MWMO_INPUTAVAILABLE); + } + else + { + ret = ::WaitForMultipleObjectsEx(handleArray.size(), handleArray.data(), false, timeout ? timeout : INFINITE, true); + } + + bool error = ((ret == WAIT_TIMEOUT) || + (ret == WAIT_IO_COMPLETION) || + (ret == WAIT_FAILED)); + + bool isPump = WAIT_OBJECT_0 + handleArray.size() == ret; + + AuUInt firstTriggered {}; + if (!error) + { + if (!isPump) + { + firstTriggered = reinterpret_cast(handleArray[ret - WAIT_OBJECT_0]); + } + } + + for (const auto &source : loopSourceExs) + { + + if (!error) + { + AuUInt lastHandle {}; + bool wasTriggered {}; + + for (const auto &handle : source->GetHandles()) + { + if ((firstTriggered == handle) || + (WaitForSingleObject(reinterpret_cast(handle), 0) == WAIT_OBJECT_0)) + { + lastHandle = handle; + wasTriggered = true; + continue; + } + } + + if (source->OnTrigger(lastHandle, wasTriggered)) + { + triggered.push_back(source); + } + } + + source->OnFinishSleep(); + } + + if (isPump) + { + triggered.push_back(msgSource); + } + + return triggered; } } \ No newline at end of file diff --git a/readme.md b/readme.md index 11c69f3a..400c5dfd 100644 --- a/readme.md +++ b/readme.md @@ -8,9 +8,9 @@ scripts into your applications build pipeline to get started. ## Features -- Lightweight threading primitives backed by OS specific backends +- Lightweight threading and synchronization primitives - Async threading primitives, including WaitMultipleObjects paradigm [WIP] -- Async and regular IO abstraction +- Asynchronous and synchronous IO abstraction - Optional event driven async programming paradigm - Console; graphical and standard; binary and UTF-8 logger - Debug and Telementry; asserts, exception logging, fio, nio backends @@ -23,6 +23,7 @@ scripts into your applications build pipeline to get started. - Compression - Locale and encoding - C++ utility templates and macros +- Follows all strings are UTF-8 convention ## Links @@ -42,12 +43,14 @@ Aurora Overloadable Type Declerations: https://git.reece.sx/AuroraSupport/Aurora ## Logging -Aurora Runtime does not attempt to implement your favourite production logger. We instead
-implement a subscription based log message dispatcher with some default backends including
-a file logger, Windows debug logging, Windows conhost stdin/out using UTF-8, UNIX stdin/out
-respecting the applications codepage, a wxWidgets toolkit GUI, and hopefully more to come.
-Additionally, consoles that provide an input stream can be used in conjunction with the parse
-subsystem to provide basic command-based deserialization, tokenization, and dispatch. +Aurora Runtime does not attempt to implement your favourite production logger. We instead +implement a subscription based log message dispatcher with some default backends including +a file logger, Windows debug logging, Windows conhost stdin/out using UTF-8, UNIX stdin/out +respecting the applications codepage, a wxWidgets toolkit GUI, and hopefully more to come. +Additionally, consoles that provide an input stream can be used in conjunction with the parse +subsystem to provide basic command-based deserialization, tokenization, and dispatch of UTF-8 +translated strings regardless of the system locale + ## Loop [WIP]