[+] Begin work on the loop source subsystem for Windows

This commit is contained in:
Reece Wilson 2021-10-03 13:47:16 +01:00
parent 6435eaf5fc
commit ce47277488
25 changed files with 426 additions and 18 deletions

View File

@ -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<AuUInt> GetHandles() = 0;
};
}

View File

@ -10,5 +10,13 @@
namespace Aurora::Loop
{
AUKN_SYM AuSPtr<IConditionVar> NewLSCondVar(const AuSPtr<Aurora::Threading::IWaitable> &primitive)
{
return {};
}
AUKN_SYM AuSPtr<IConditionVar> NewLSCondVar(const AuSPtr<ILSMutex> &source)
{
return {};
}
}

View File

@ -7,8 +7,59 @@
***/
#include <Source/RuntimeInternal.hpp>
#include "LSEvent.hpp"
#include "LSHandle.hpp"
namespace Aurora::Loop
{
class Event : public ILSEvent, public LSHandle
{
public:
Event(HANDLE handle) : LSHandle(reinterpret_cast<AuUInt>(handle))
{}
bool Set() override;
bool Reset() override;
bool ILSEvent::IsSignaled() override;
ELoopSource ILSEvent::GetType() override;
};
bool Event::Set()
{
return SetEvent(reinterpret_cast<HANDLE>(this->handle));
}
bool Event::Reset()
{
return ResetEvent(reinterpret_cast<HANDLE>(this->handle));
}
bool Event::IsSignaled()
{
return LSHandle::IsSignaled();
}
ELoopSource Event::GetType()
{
return ELoopSource::eSourceEvent;
}
AUKN_SYM AuSPtr<ILSEvent> NewLSEvent(bool triggerd, bool atomicRelease, bool permitMultipleTriggers)
{
AuSPtr<ILSEvent> ret;
auto mutex = CreateEvent(NULL, !atomicRelease, triggerd, NULL);
if (mutex == INVALID_HANDLE_VALUE)
{
SysPushErrorGen("Out of OS resources?");
return {};
}
if (!(ret = AuMakeShared<Event>(mutex)))
{
return {};
}
return ret;
}
}

View File

@ -7,6 +7,8 @@
***/
#pragma once
#include "WaitSingle.hpp"
#if defined(AURORA_IS_MODERNNT_DERIVED)
#include "LSEvent.NT.hpp"
// eventfd always atomically resets

View File

@ -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<AuUInt> LSHandle::GetHandles()
{
return reference;
}
ELoopSource LSHandle::GetType()
{
return ELoopSource::eSourceHandle;
}
AUKN_SYM AuSPtr<ILoopSource> NewLSOSHandle(AuUInt handle)
{
auto h = reinterpret_cast<HANDLE>(handle);
if (h == INVALID_HANDLE_VALUE)
{
return {};
}
return AuMakeShared<LSHandle>(handle);
}
}

View File

@ -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<AuUInt> GetHandles() override;
ELoopSource GetType() override;
protected:
AuUInt handle;
private:
AuList<AuUInt> reference;
};
}

View File

@ -7,8 +7,53 @@
***/
#include <Source/RuntimeInternal.hpp>
#include "LSMutex.hpp"
#include "LSHandle.hpp"
namespace Aurora::Loop
{
class Mutex : public ILSMutex, public LSHandle
{
public:
Mutex(HANDLE handle) : LSHandle(reinterpret_cast<AuUInt>(handle))
{}
bool Unlock() override;
bool ILSMutex::IsSignaled() override;
ELoopSource ILSMutex::GetType() override;
};
bool Mutex::Unlock()
{
return ReleaseMutex(reinterpret_cast<HANDLE>(handle));
}
bool Mutex::IsSignaled()
{
return LSHandle::IsSignaled();
}
ELoopSource Mutex::GetType()
{
return ELoopSource::eSourceMutex;
}
AUKN_SYM AuSPtr<ILSMutex> NewLSMutex()
{
AuSPtr<ILSMutex> ret;
auto mutex = CreateMutex(NULL, false, NULL);
if (mutex == INVALID_HANDLE_VALUE)
{
SysPushErrorGen("Out of OS resources?");
return {};
}
if (!(ret = AuMakeShared<Mutex>(mutex)))
{
return {};
}
return ret;
}
}

View File

@ -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)

View File

@ -9,5 +9,5 @@
namespace Aurora::Loop
{
}

View File

@ -7,8 +7,54 @@
***/
#include <Source/RuntimeInternal.hpp>
#include "LSSemaphore.hpp"
#include "LSHandle.hpp"
namespace Aurora::Loop
{
class Semaphore : public ILSSemaphore, public LSHandle
{
public:
Semaphore(HANDLE handle) : LSHandle(reinterpret_cast<AuUInt>(handle))
{}
bool AddOne() override;
bool ILSSemaphore::IsSignaled() override;
ELoopSource ILSSemaphore::GetType() override;
};
bool Semaphore::AddOne()
{
LONG atomicOld;
return ReleaseSemaphore(reinterpret_cast<HANDLE>(handle), 1, &atomicOld);
}
bool Semaphore::IsSignaled()
{
return LSHandle::IsSignaled();
}
ELoopSource Semaphore::GetType()
{
return ELoopSource::eSourceSemaphore;
}
AUKN_SYM AuSPtr<ILSSemaphore> NewLSSemaphore(AuUInt32 initialCount)
{
AuSPtr<Semaphore> ret;
auto mutex = CreateSemaphoreA(NULL, initialCount, std::numeric_limits<LONG>::max(), NULL);
if (mutex == INVALID_HANDLE_VALUE)
{
SysPushErrorGen("Out of OS resources?");
return {};
}
if (!(ret = AuMakeShared<Semaphore>(mutex)))
{
return {};
}
return ret;
}
}

View File

@ -7,6 +7,7 @@
***/
#pragma once
#include "WaitSingle.hpp"
#if defined(AURORA_IS_MODERNNT_DERIVED)
#include "LSSemaphore.NT.hpp"

View File

@ -10,5 +10,5 @@
namespace Aurora::Loop
{
}

View File

@ -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

View File

@ -9,5 +9,5 @@
namespace Aurora::Loop
{
}

View File

@ -10,5 +10,8 @@
namespace Aurora::Loop
{
AUKN_SYM AuList<AuSPtr<ILoopSource>> WaitMultipleObjects(const AuList<AuSPtr<ILoopSource>> &objects, AuUInt32 timeout)
{
return {};
}
}

View File

@ -10,11 +10,19 @@
namespace Aurora::Loop
{
#if !defined(AURORA_IS_MODERNNT_DERIVED)
AUKN_SYM AuSPtr<ITimer> NewLSTimer(AuUInt64 absTimeMs);
#if !defined(AURORA_IS_MODERNNT_DERIVED)
AUKN_SYM AuSPtr<ILoopSource> NewLSWin32Source()
{
return {};
}
#endif
#if !defined(AURORA_IS_XNU_DERIVED)
AUKN_SYM AuSPtr<ILoopSource> NewLSAppleSource()
{
return {};
}
#endif
}

View File

@ -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 <Source/RuntimeInternal.hpp>
#include "GenericWaitable.hpp"
#include "WaitSingle.hpp"
#if defined(IMPL_WAIT_SINGLE_GEN)
namespace Aurora::Loop
{
}
}
#endif

View File

@ -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<AuUInt> &handles, AuUInt &one) override;
virtual void OnPresleep() override;
virtual void OnFinishSleep() override;
virtual ELoopSource GetType() override;
};
}

View File

@ -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 <Source/RuntimeInternal.hpp>
#include "LSTimer.hpp"
namespace Aurora::Loop
{
}

View File

@ -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

View File

@ -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 <Source/RuntimeInternal.hpp>
#include "WaitSingle.hpp"
namespace Aurora::Loop
{
bool WaitSingleGeneric::WaitForAtleastOne(const AuList<AuUInt> &handles, AuUInt &one)
{
if (handles.empty())
{
return {};
}
if (handles.size() == 1)
{
return WaitForSingleObject(reinterpret_cast<HANDLE>(handles.at(0)), 0) == WAIT_OBJECT_0;
}
else
{
AuList<HANDLE> ntHandles;
ntHandles.reserve(handles.size());
for (const auto &handle : handles)
{
ntHandles.push_back(reinterpret_cast<HANDLE>(handle));
}
return WaitForMultipleObjects(ntHandles.size(), ntHandles.data(), false, 0) >= WAIT_OBJECT_0;
}
}
void WaitSingleGeneric::OnPresleep()
{
}
void WaitSingleGeneric::OnFinishSleep()
{
}
ELoopSource WaitSingleGeneric::GetType()
{
return ELoopSource::eSourceInternalReserved1;
}
}

View File

@ -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<AuUInt> &handles, AuUInt &one) override;
virtual void OnPresleep() override;
virtual void OnFinishSleep() override;
virtual ELoopSource GetType() override;
};
}

View File

@ -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

View File

@ -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 <Source/RuntimeInternal.hpp>
#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;
}
}

View File

@ -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<AuUInt> &handles, AuUInt &one) = 0;
};
}