diff --git a/Source/Loop/ILoopSourceEx.hpp b/Source/Loop/ILoopSourceEx.hpp new file mode 100644 index 00000000..ddea7dc7 --- /dev/null +++ b/Source/Loop/ILoopSourceEx.hpp @@ -0,0 +1,20 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: ILoopSourceEx.Generic.hpp + Date: 2021-10-3 + Author: Reece +***/ +#pragma once + +namespace Aurora::Loop +{ + class ILoopSourceEx : public ILoopSource + { + public: + virtual void OnPresleep() = 0; + virtual bool OnTrigger(AuUInt handle, bool atomicSignal) = 0; + virtual void OnFinishSleep() = 0; + virtual AuList GetHandles() = 0; + }; +} \ No newline at end of file diff --git a/Source/Loop/LSCondition.cpp b/Source/Loop/LSCondition.cpp index 0486832c..169f47eb 100644 --- a/Source/Loop/LSCondition.cpp +++ b/Source/Loop/LSCondition.cpp @@ -10,5 +10,13 @@ namespace Aurora::Loop { - + AUKN_SYM AuSPtr NewLSCondVar(const AuSPtr &primitive) + { + return {}; + } + + AUKN_SYM AuSPtr NewLSCondVar(const AuSPtr &source) + { + return {}; + } } \ No newline at end of file diff --git a/Source/Loop/LSEvent.NT.cpp b/Source/Loop/LSEvent.NT.cpp index 86050bd0..069c5b40 100644 --- a/Source/Loop/LSEvent.NT.cpp +++ b/Source/Loop/LSEvent.NT.cpp @@ -7,8 +7,59 @@ ***/ #include #include "LSEvent.hpp" +#include "LSHandle.hpp" namespace Aurora::Loop { - + class Event : public ILSEvent, public LSHandle + { + public: + Event(HANDLE handle) : LSHandle(reinterpret_cast(handle)) + {} + + bool Set() override; + bool Reset() override; + + bool ILSEvent::IsSignaled() override; + ELoopSource ILSEvent::GetType() override; + }; + + bool Event::Set() + { + return SetEvent(reinterpret_cast(this->handle)); + } + + bool Event::Reset() + { + return ResetEvent(reinterpret_cast(this->handle)); + } + + bool Event::IsSignaled() + { + return LSHandle::IsSignaled(); + } + + ELoopSource Event::GetType() + { + return ELoopSource::eSourceEvent; + } + + AUKN_SYM AuSPtr NewLSEvent(bool triggerd, bool atomicRelease, bool permitMultipleTriggers) + { + AuSPtr ret; + + auto mutex = CreateEvent(NULL, !atomicRelease, triggerd, NULL); + if (mutex == INVALID_HANDLE_VALUE) + { + SysPushErrorGen("Out of OS resources?"); + return {}; + } + + if (!(ret = AuMakeShared(mutex))) + { + return {}; + } + + return ret; + } } \ No newline at end of file diff --git a/Source/Loop/LSEvent.hpp b/Source/Loop/LSEvent.hpp index 78db53aa..aae26d47 100644 --- a/Source/Loop/LSEvent.hpp +++ b/Source/Loop/LSEvent.hpp @@ -7,6 +7,8 @@ ***/ #pragma once +#include "WaitSingle.hpp" + #if defined(AURORA_IS_MODERNNT_DERIVED) #include "LSEvent.NT.hpp" // eventfd always atomically resets diff --git a/Source/Loop/LSHandle.cpp b/Source/Loop/LSHandle.cpp index cbb4299d..a482fafe 100644 --- a/Source/Loop/LSHandle.cpp +++ b/Source/Loop/LSHandle.cpp @@ -10,5 +10,32 @@ namespace Aurora::Loop { + LSHandle::LSHandle(AuUInt handle) : handle(handle), reference({handle}) + {} + + bool LSHandle::OnTrigger(AuUInt handle, bool atomicSignal) + { + return atomicSignal; + } + + AuList LSHandle::GetHandles() + { + return reference; + } + ELoopSource LSHandle::GetType() + { + return ELoopSource::eSourceHandle; + } + + AUKN_SYM AuSPtr NewLSOSHandle(AuUInt handle) + { + auto h = reinterpret_cast(handle); + if (h == INVALID_HANDLE_VALUE) + { + return {}; + } + + return AuMakeShared(handle); + } } \ No newline at end of file diff --git a/Source/Loop/LSHandle.hpp b/Source/Loop/LSHandle.hpp index 9f0616dd..46215201 100644 --- a/Source/Loop/LSHandle.hpp +++ b/Source/Loop/LSHandle.hpp @@ -7,7 +7,23 @@ ***/ #pragma once +#include "WaitSingle.hpp" + namespace Aurora::Loop { - + class LSHandle : public WaitSingleGeneric + { + public: + LSHandle(AuUInt handle); + + bool OnTrigger(AuUInt handle, bool atomicSignal) override; + AuList GetHandles() override; + ELoopSource GetType() override; + + protected: + AuUInt handle; + + private: + AuList reference; + }; } \ No newline at end of file diff --git a/Source/Loop/LSMutex.NT.cpp b/Source/Loop/LSMutex.NT.cpp index fd083ae4..20a593d4 100644 --- a/Source/Loop/LSMutex.NT.cpp +++ b/Source/Loop/LSMutex.NT.cpp @@ -7,8 +7,53 @@ ***/ #include #include "LSMutex.hpp" +#include "LSHandle.hpp" namespace Aurora::Loop { - + class Mutex : public ILSMutex, public LSHandle + { + public: + Mutex(HANDLE handle) : LSHandle(reinterpret_cast(handle)) + {} + + bool Unlock() override; + + bool ILSMutex::IsSignaled() override; + ELoopSource ILSMutex::GetType() override; + }; + + bool Mutex::Unlock() + { + return ReleaseMutex(reinterpret_cast(handle)); + } + + bool Mutex::IsSignaled() + { + return LSHandle::IsSignaled(); + } + + ELoopSource Mutex::GetType() + { + return ELoopSource::eSourceMutex; + } + + AUKN_SYM AuSPtr NewLSMutex() + { + AuSPtr ret; + + auto mutex = CreateMutex(NULL, false, NULL); + if (mutex == INVALID_HANDLE_VALUE) + { + SysPushErrorGen("Out of OS resources?"); + return {}; + } + + if (!(ret = AuMakeShared(mutex))) + { + return {}; + } + + return ret; + } } \ No newline at end of file diff --git a/Source/Loop/LSMutex.hpp b/Source/Loop/LSMutex.hpp index 401b65c0..3768371d 100644 --- a/Source/Loop/LSMutex.hpp +++ b/Source/Loop/LSMutex.hpp @@ -7,6 +7,8 @@ ***/ #pragma once +#include "WaitSingle.hpp" + #if defined(AURORA_IS_MODERNNT_DERIVED) #include "LSMutex.NT.hpp" #elif defined(AURORA_IS_LINUX_DERIVED) diff --git a/Source/Loop/LSSemaphore.Generic.hpp b/Source/Loop/LSSemaphore.Generic.hpp index a2717b30..2b2b91e4 100644 --- a/Source/Loop/LSSemaphore.Generic.hpp +++ b/Source/Loop/LSSemaphore.Generic.hpp @@ -9,5 +9,5 @@ namespace Aurora::Loop { - + } \ No newline at end of file diff --git a/Source/Loop/LSSemaphore.NT.cpp b/Source/Loop/LSSemaphore.NT.cpp index 88988b5f..1e4c77f2 100644 --- a/Source/Loop/LSSemaphore.NT.cpp +++ b/Source/Loop/LSSemaphore.NT.cpp @@ -7,8 +7,54 @@ ***/ #include #include "LSSemaphore.hpp" +#include "LSHandle.hpp" namespace Aurora::Loop { + class Semaphore : public ILSSemaphore, public LSHandle + { + public: + Semaphore(HANDLE handle) : LSHandle(reinterpret_cast(handle)) + {} + bool AddOne() override; + + bool ILSSemaphore::IsSignaled() override; + ELoopSource ILSSemaphore::GetType() override; + }; + + bool Semaphore::AddOne() + { + LONG atomicOld; + return ReleaseSemaphore(reinterpret_cast(handle), 1, &atomicOld); + } + + bool Semaphore::IsSignaled() + { + return LSHandle::IsSignaled(); + } + + ELoopSource Semaphore::GetType() + { + return ELoopSource::eSourceSemaphore; + } + + AUKN_SYM AuSPtr NewLSSemaphore(AuUInt32 initialCount) + { + AuSPtr ret; + + auto mutex = CreateSemaphoreA(NULL, initialCount, std::numeric_limits::max(), NULL); + if (mutex == INVALID_HANDLE_VALUE) + { + SysPushErrorGen("Out of OS resources?"); + return {}; + } + + if (!(ret = AuMakeShared(mutex))) + { + return {}; + } + + return ret; + } } \ No newline at end of file diff --git a/Source/Loop/LSSemaphore.hpp b/Source/Loop/LSSemaphore.hpp index 2dbb13c7..4efe2dc9 100644 --- a/Source/Loop/LSSemaphore.hpp +++ b/Source/Loop/LSSemaphore.hpp @@ -7,6 +7,7 @@ ***/ #pragma once +#include "WaitSingle.hpp" #if defined(AURORA_IS_MODERNNT_DERIVED) #include "LSSemaphore.NT.hpp" diff --git a/Source/Loop/LSTimer.NT.cpp b/Source/Loop/LSTimer.NT.cpp index 52f03e5c..a2a65eba 100644 --- a/Source/Loop/LSTimer.NT.cpp +++ b/Source/Loop/LSTimer.NT.cpp @@ -10,5 +10,5 @@ namespace Aurora::Loop { - + } \ No newline at end of file diff --git a/Source/Loop/LSTimer.hpp b/Source/Loop/LSTimer.hpp index 248c2ff6..5193485c 100644 --- a/Source/Loop/LSTimer.hpp +++ b/Source/Loop/LSTimer.hpp @@ -1,11 +1,19 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: LSTimer.hpp + Date: 2021-10-2 + Author: Reece +***/ #pragma once +#include "WaitSingle.hpp" #if defined(AURORA_IS_MODERNNT_DERIVED) #include "LSTimer.NT.hpp" #elif defined(AURORA_IS_LINUX_DERIVED) #include "LSTimer.Linux.hpp" #else - #define IMPL_LS_TIMER_GEN - #include "LSTimer.Generic.hpp" + #define IMPL_LS_TIMER_GEN + #include "LSTimer.Generic.hpp" #endif \ No newline at end of file diff --git a/Source/Loop/LSWin32.NT.hpp b/Source/Loop/LSWin32.NT.hpp index a73cf17e..4ead4d49 100644 --- a/Source/Loop/LSWin32.NT.hpp +++ b/Source/Loop/LSWin32.NT.hpp @@ -9,5 +9,5 @@ namespace Aurora::Loop { - + } \ No newline at end of file diff --git a/Source/Loop/Loop.NT.cpp b/Source/Loop/Loop.NT.cpp index de4f541d..caf466f8 100644 --- a/Source/Loop/Loop.NT.cpp +++ b/Source/Loop/Loop.NT.cpp @@ -10,5 +10,8 @@ namespace Aurora::Loop { - + AUKN_SYM AuList> WaitMultipleObjects(const AuList> &objects, AuUInt32 timeout) + { + return {}; + } } \ No newline at end of file diff --git a/Source/Loop/Loop.cpp b/Source/Loop/Loop.cpp index dd224793..42fe32f6 100644 --- a/Source/Loop/Loop.cpp +++ b/Source/Loop/Loop.cpp @@ -10,11 +10,19 @@ namespace Aurora::Loop { -#if !defined(AURORA_IS_MODERNNT_DERIVED) + AUKN_SYM AuSPtr NewLSTimer(AuUInt64 absTimeMs); +#if !defined(AURORA_IS_MODERNNT_DERIVED) + AUKN_SYM AuSPtr NewLSWin32Source() + { + return {}; + } #endif #if !defined(AURORA_IS_XNU_DERIVED) - + AUKN_SYM AuSPtr NewLSAppleSource() + { + return {}; + } #endif } \ No newline at end of file diff --git a/Source/Loop/GenericWaitable.cpp b/Source/Loop/WaitSingle.Generic.cpp similarity index 65% rename from Source/Loop/GenericWaitable.cpp rename to Source/Loop/WaitSingle.Generic.cpp index 7bb402ec..83ce9fde 100644 --- a/Source/Loop/GenericWaitable.cpp +++ b/Source/Loop/WaitSingle.Generic.cpp @@ -1,15 +1,18 @@ /*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - File: GenericWaitable.cpp + File: WaitSingle.Generic.cpp Date: 2021-10-2 Author: Reece ***/ -#pragma once #include -#include "GenericWaitable.hpp" +#include "WaitSingle.hpp" + +#if defined(IMPL_WAIT_SINGLE_GEN) namespace Aurora::Loop { -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/Source/Loop/WaitSingle.Generic.hpp b/Source/Loop/WaitSingle.Generic.hpp new file mode 100644 index 00000000..f740f627 --- /dev/null +++ b/Source/Loop/WaitSingle.Generic.hpp @@ -0,0 +1,20 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: WaitSingle.Generic.hpp + Date: 2021-10-2 + Author: Reece +***/ +#pragma once + +namespace Aurora::Loop +{ + class WaitSingleGeneric : public WaitSingleBase + { + public: + virtual bool WaitForAtleastOne(const AuList &handles, AuUInt &one) override; + virtual void OnPresleep() override; + virtual void OnFinishSleep() override; + virtual ELoopSource GetType() override; + }; +} \ No newline at end of file diff --git a/Source/Loop/WaitSingle.Linux.cpp b/Source/Loop/WaitSingle.Linux.cpp new file mode 100644 index 00000000..6fbbaf86 --- /dev/null +++ b/Source/Loop/WaitSingle.Linux.cpp @@ -0,0 +1,14 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: LSTimer.Linux.cpp + Date: 2021-10-1 + Author: Reece +***/ +#include +#include "LSTimer.hpp" + +namespace Aurora::Loop +{ + +} \ No newline at end of file diff --git a/Source/Loop/GenericWaitable.hpp b/Source/Loop/WaitSingle.Linux.hpp similarity index 74% rename from Source/Loop/GenericWaitable.hpp rename to Source/Loop/WaitSingle.Linux.hpp index abf180ce..86be29c5 100644 --- a/Source/Loop/GenericWaitable.hpp +++ b/Source/Loop/WaitSingle.Linux.hpp @@ -1,8 +1,8 @@ /*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. - File: GenericWaitable.hpp - Date: 2021-10-2 + File: WaitSingle.Linux.hpp + Date: 2021-10-1 Author: Reece ***/ #pragma once diff --git a/Source/Loop/WaitSingle.NT.cpp b/Source/Loop/WaitSingle.NT.cpp new file mode 100644 index 00000000..efed9018 --- /dev/null +++ b/Source/Loop/WaitSingle.NT.cpp @@ -0,0 +1,51 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: WaitSingle.NT.cpp + Date: 2021-10-1 + Author: Reece +***/ +#include +#include "WaitSingle.hpp" + +namespace Aurora::Loop +{ + + bool WaitSingleGeneric::WaitForAtleastOne(const AuList &handles, AuUInt &one) + { + if (handles.empty()) + { + return {}; + } + + if (handles.size() == 1) + { + return WaitForSingleObject(reinterpret_cast(handles.at(0)), 0) == WAIT_OBJECT_0; + } + else + { + AuList ntHandles; + ntHandles.reserve(handles.size()); + for (const auto &handle : handles) + { + ntHandles.push_back(reinterpret_cast(handle)); + } + return WaitForMultipleObjects(ntHandles.size(), ntHandles.data(), false, 0) >= WAIT_OBJECT_0; + } + } + + void WaitSingleGeneric::OnPresleep() + { + + } + + void WaitSingleGeneric::OnFinishSleep() + { + + } + + ELoopSource WaitSingleGeneric::GetType() + { + return ELoopSource::eSourceInternalReserved1; + } +} \ No newline at end of file diff --git a/Source/Loop/WaitSingle.NT.hpp b/Source/Loop/WaitSingle.NT.hpp new file mode 100644 index 00000000..2682e5dc --- /dev/null +++ b/Source/Loop/WaitSingle.NT.hpp @@ -0,0 +1,20 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: WaitSingle.NT.hpp + Date: 2021-10-1 + Author: Reece +***/ +#pragma once + +namespace Aurora::Loop +{ + class WaitSingleGeneric : public WaitSingleBase + { + public: + virtual bool WaitForAtleastOne(const AuList &handles, AuUInt &one) override; + virtual void OnPresleep() override; + virtual void OnFinishSleep() override; + virtual ELoopSource GetType() override; + }; +} \ No newline at end of file diff --git a/Source/Loop/WaitSingle.hpp b/Source/Loop/WaitSingle.hpp new file mode 100644 index 00000000..1c71d6c8 --- /dev/null +++ b/Source/Loop/WaitSingle.hpp @@ -0,0 +1,19 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: WaitSingle.hpp + Date: 2021-10-2 + Author: Reece +***/ +#pragma once + +#include "WaitSingleBase.hpp" + +#if defined(AURORA_IS_MODERNNT_DERIVED) + #include "WaitSingle.NT.hpp" +#elif defined(AURORA_IS_LINUX_DERIVED) + #include "WaitSingle.Linux.hpp" +#else + #define IMPL_WAIT_SINGLE_GEN + #include "WaitSingle.Generic.hpp" +#endif \ No newline at end of file diff --git a/Source/Loop/WaitSingleBase.cpp b/Source/Loop/WaitSingleBase.cpp new file mode 100644 index 00000000..cebe0e82 --- /dev/null +++ b/Source/Loop/WaitSingleBase.cpp @@ -0,0 +1,23 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: WaitSingle.Generic.cpp + Date: 2021-10-2 + Author: Reece +***/ +#include +#include "WaitSingleBase.hpp" + +namespace Aurora::Loop +{ + bool WaitSingleBase::IsSignaled() + { + AuUInt one {}; + this->OnPresleep(); + auto handles = this->GetHandles(); + auto val = WaitForAtleastOne(handles, one); + auto ret = this->OnTrigger(one, val); + this->OnFinishSleep(); + return ret; + } +} diff --git a/Source/Loop/WaitSingleBase.hpp b/Source/Loop/WaitSingleBase.hpp new file mode 100644 index 00000000..439b81c6 --- /dev/null +++ b/Source/Loop/WaitSingleBase.hpp @@ -0,0 +1,21 @@ +/*** + Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: WaitSingle.Generic.hpp + Date: 2021-10-2 + Author: Reece +***/ +#pragma once + +#include "ILoopSourceEx.hpp" + +namespace Aurora::Loop +{ + class WaitSingleBase : public ILoopSourceEx + { + public: + bool IsSignaled() override; + + virtual bool WaitForAtleastOne(const AuList &handles, AuUInt &one) = 0; + }; +} \ No newline at end of file