[*] Continued loop work (Linux and Win32)

This commit is contained in:
Reece Wilson 2022-04-07 02:20:46 +01:00
parent da506dd0d8
commit d3428f4cd9
15 changed files with 137 additions and 14 deletions

View File

@ -266,9 +266,8 @@ namespace Aurora::IO::FS
void PosixFileStream::Close() void PosixFileStream::Close()
{ {
int handle; int handle = AuExchange(this->handle_, -1);
if (handle != -1)
if ((handle = std::exchange(this->handle_, -1)) != -1)
{ {
::close(handle); ::close(handle);
} }

View File

@ -16,6 +16,15 @@ namespace Aurora::Loop
Init(triggered); Init(triggered);
} }
LSEvent::~LSEvent()
{
if ((this->handle != 0) &&
(this->handle != -1))
{
close(this->handle);
}
}
bool LSEvent::OnTrigger(AuUInt handle) bool LSEvent::OnTrigger(AuUInt handle)
{ {
AuUInt64 oldSemaphoreValue {}; AuUInt64 oldSemaphoreValue {};

View File

@ -13,7 +13,8 @@ namespace Aurora::Loop
struct LSEvent : public ILSEvent, public LSHandle struct LSEvent : public ILSEvent, public LSHandle
{ {
LSEvent(bool triggered, bool atomicRelease, bool permitMultipleTriggers); LSEvent(bool triggered, bool atomicRelease, bool permitMultipleTriggers);
~LSEvent();
bool Set() override; bool Set() override;
bool Reset() override; bool Reset() override;

View File

@ -15,6 +15,12 @@ namespace Aurora::Loop
} }
LSEvent::~LSEvent()
{
auto handle = reinterpret_cast<HANDLE>(this->handle);
AuWin32CloseHandle(handle);
}
bool LSEvent::Set() bool LSEvent::Set()
{ {
return SetEvent(reinterpret_cast<HANDLE>(this->handle)); return SetEvent(reinterpret_cast<HANDLE>(this->handle));

View File

@ -13,6 +13,7 @@ namespace Aurora::Loop
struct LSEvent : public ILSEvent, public LSHandle struct LSEvent : public ILSEvent, public LSHandle
{ {
LSEvent(bool triggered, bool atomicRelease, bool permitMultipleTriggers); LSEvent(bool triggered, bool atomicRelease, bool permitMultipleTriggers);
~LSEvent();
bool Set() override; bool Set() override;
bool Reset() override; bool Reset() override;

View File

@ -14,6 +14,16 @@ namespace Aurora::Loop
{ {
LSMutex::LSMutex() LSMutex::LSMutex()
{ {
Init();
}
LSMutex::~LSMutex()
{
if ((this->handle != 0) &&
(this->handle != -1))
{
::close(this->handle);
}
} }
bool LSMutex::OnTrigger(AuUInt handle) bool LSMutex::OnTrigger(AuUInt handle)

View File

@ -13,6 +13,7 @@ namespace Aurora::Loop
struct LSMutex : public ILSMutex, public LSHandle struct LSMutex : public ILSMutex, public LSHandle
{ {
LSMutex(); LSMutex();
~LSMutex();
bool Unlock() override; bool Unlock() override;

View File

@ -10,6 +10,15 @@
namespace Aurora::Loop namespace Aurora::Loop
{ {
Mutex::Mutex(HANDLE handle) : LSHandle(reinterpret_cast<AuUInt>(handle))
{}
Mutex::~Mutex()
{
auto handle = reinterpret_cast<HANDLE>(this->handle);
AuWin32CloseHandle(handle);
}
bool Mutex::Unlock() bool Mutex::Unlock()
{ {
return ReleaseMutex(reinterpret_cast<HANDLE>(handle)); return ReleaseMutex(reinterpret_cast<HANDLE>(handle));

View File

@ -13,8 +13,8 @@ namespace Aurora::Loop
class Mutex : public ILSMutex, public LSHandle class Mutex : public ILSMutex, public LSHandle
{ {
public: public:
Mutex(HANDLE handle) : LSHandle(reinterpret_cast<AuUInt>(handle)) Mutex(HANDLE handle);
{} ~Mutex();
bool Unlock() override; bool Unlock() override;

View File

@ -16,6 +16,15 @@ namespace Aurora::Loop
Init(initialCount); Init(initialCount);
} }
LSSemaphore::~LSSemaphore()
{
if ((this->handle != 0) &&
(this->handle != -1))
{
::close(this->handle);
}
}
void LSSemaphore::Init(AuUInt32 initialCount) void LSSemaphore::Init(AuUInt32 initialCount)
{ {
handle = eventfd(initialCount, EFD_SEMAPHORE | EFD_NONBLOCK); handle = eventfd(initialCount, EFD_SEMAPHORE | EFD_NONBLOCK);

View File

@ -13,6 +13,7 @@ namespace Aurora::Loop
struct LSSemaphore : public ILSSemaphore, public LSHandle struct LSSemaphore : public ILSSemaphore, public LSHandle
{ {
LSSemaphore(AuUInt32 initialCount); LSSemaphore(AuUInt32 initialCount);
~LSSemaphore();
bool AddOne() override; bool AddOne() override;

View File

@ -10,6 +10,15 @@
namespace Aurora::Loop namespace Aurora::Loop
{ {
LSSemaphore::LSSemaphore(HANDLE handle) : LSHandle(reinterpret_cast<AuUInt>(handle))
{}
LSSemaphore::~LSSemaphore()
{
auto handle = reinterpret_cast<HANDLE>(this->handle);
AuWin32CloseHandle(handle);
}
bool LSSemaphore::AddOne() bool LSSemaphore::AddOne()
{ {
LONG atomicOld; LONG atomicOld;

View File

@ -13,8 +13,8 @@ namespace Aurora::Loop
class LSSemaphore : public ILSSemaphore, public LSHandle class LSSemaphore : public ILSSemaphore, public LSHandle
{ {
public: public:
LSSemaphore(HANDLE handle) : LSHandle(reinterpret_cast<AuUInt>(handle)) LSSemaphore(HANDLE handle);
{} ~LSSemaphore();
bool AddOne() override; bool AddOne() override;

View File

@ -9,6 +9,7 @@
#include "Loop.NT.hpp" #include "Loop.NT.hpp"
#include "ILoopSourceEx.hpp" #include "ILoopSourceEx.hpp"
#include "LoopQueue.Linux.hpp" #include "LoopQueue.Linux.hpp"
#include <sys/epoll.h>
namespace Aurora::Loop namespace Aurora::Loop
{ {
@ -36,7 +37,21 @@ namespace Aurora::Loop
LoopQueue::~LoopQueue() LoopQueue::~LoopQueue()
{ {
Deinit();
}
bool LoopQueue::Init()
{
this->epollFd_ = epoll_create1(-1);
if (this->epollFd_ == -1) return false;
return true;
}
void LoopQueue::Deinit()
{
auto handle = AuExchange(this->epollFd_, -1);
if (handle != -1) close(handle);
} }
bool LoopQueue::SourceAdd(const AuSPtr<ILoopSource> &source) bool LoopQueue::SourceAdd(const AuSPtr<ILoopSource> &source)
@ -56,32 +71,40 @@ namespace Aurora::Loop
AuUInt32 LoopQueue::GetSourceCount() AuUInt32 LoopQueue::GetSourceCount()
{ {
return {}; return this->sources_.size();
} }
bool LoopQueue::AddCallback(const AuSPtr<ILoopSource> &source, const AuSPtr<ILoopSourceSubscriber> &subscriber) bool LoopQueue::AddCallback(const AuSPtr<ILoopSource> &source, const AuSPtr<ILoopSourceSubscriber> &subscriber)
{ {
return {}; AU_LOCK_GUARD(this->commitQueueMutex_);
bool bAdded {};
return bAdded;
} }
bool LoopQueue::AddCallbackEx(const AuSPtr<ILoopSource> &source, const AuSPtr<ILoopSourceSubscriberEx> &subscriber) bool LoopQueue::AddCallbackEx(const AuSPtr<ILoopSource> &source, const AuSPtr<ILoopSourceSubscriberEx> &subscriber)
{ {
return {}; AU_LOCK_GUARD(this->commitQueueMutex_);
bool bAdded {};
return bAdded;
} }
bool LoopQueue::AddCallback(const AuSPtr<ILoopSourceSubscriber> &subscriber) bool LoopQueue::AddCallback(const AuSPtr<ILoopSourceSubscriber> &subscriber)
{ {
return {}; AU_LOCK_GUARD(this->globalLockMutex_);
return AuTryInsert(this->allSubscribers_, subscriber);
} }
void LoopQueue::ChugPathConfigure(AuUInt32 sectionTickTime, AuSInt sectionDequeCount) void LoopQueue::ChugPathConfigure(AuUInt32 sectionTickTime, AuSInt sectionDequeCount)
{ {
// Intentionally NO-OP under Linux
} }
void LoopQueue::ChugHint(bool value) void LoopQueue::ChugHint(bool value)
{ {
// Intentionally NO-OP under Linux
} }
bool LoopQueue::Commit() bool LoopQueue::Commit()
@ -108,4 +131,14 @@ namespace Aurora::Loop
{ {
return {}; return {};
} }
void LoopQueue::DoTick()
{
}
AUKN_SYM AuSPtr<ILoopQueue> NewLoopQueue()
{
return AuMakeShared<LoopQueue>();
}
} }

View File

@ -16,6 +16,9 @@ namespace Aurora::Loop
LoopQueue(); LoopQueue();
~LoopQueue(); ~LoopQueue();
bool Init();
void Deinit();
bool SourceAdd(const AuSPtr<ILoopSource> &source) override; bool SourceAdd(const AuSPtr<ILoopSource> &source) override;
bool SourceAddWithTimeout(const AuSPtr<ILoopSource> &source, AuUInt32 ms) override; bool SourceAddWithTimeout(const AuSPtr<ILoopSource> &source, AuUInt32 ms) override;
bool SourceRemove(const AuSPtr<ILoopSource> &source) override; bool SourceRemove(const AuSPtr<ILoopSource> &source) override;
@ -37,7 +40,39 @@ namespace Aurora::Loop
AuUInt32 WaitAny(AuUInt32 timeout) override; AuUInt32 WaitAny(AuUInt32 timeout) override;
AuList<AuSPtr<ILoopSource>> WaitAnyEx(AuUInt32 timeout) override; AuList<AuSPtr<ILoopSource>> WaitAnyEx(AuUInt32 timeout) override;
void DoTick();
private: private:
struct SpecialHandle
{
int fd;
void *priv;
};
struct SourceExtended
{
SpecialHandle internal;
AuSPtr<void> pin;
AuList<AuSPtr<ILoopSourceSubscriber>> subscribers;
AuList<AuSPtr<ILoopSourceSubscriberEx>> subscriberExs;
};
AuThreadPrimitives::SpinLock commitQueueMutex_;
AuList<AuSPtr<SourceExtended>> commitQueue_;
AuThreadPrimitives::SpinLock globalLockMutex_;
AuList<AuSPtr<ILoopSourceSubscriber>> allSubscribers_;
AuThreadPrimitives::RWLockUnique_t sourceMutex_;
AuList<AuSPtr<SourceExtended>> sources_;
int epollFd_ {-1};
bool hasActiveBeforeCommit_ {};
}; };
} }