2022-04-14 19:40:35 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-12-14 05:03:37 +00:00
|
|
|
File: AuIPCPrimitives.NT.cpp
|
2022-04-14 19:40:35 +00:00
|
|
|
Date: 2022-4-13
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "IPC.hpp"
|
2022-12-14 05:03:37 +00:00
|
|
|
#include "AuIPCHandle.hpp"
|
|
|
|
#include "AuIPCPrimitives.NT.hpp"
|
2022-04-14 19:40:35 +00:00
|
|
|
|
2022-06-11 23:52:46 +00:00
|
|
|
#include <Source/IO/Loop/LSMutex.hpp>
|
|
|
|
#include <Source/IO/Loop/LSSemaphore.hpp>
|
|
|
|
#include <Source/IO/Loop/LSEvent.hpp>
|
2022-04-16 18:26:01 +00:00
|
|
|
|
2022-06-11 23:52:46 +00:00
|
|
|
namespace Aurora::IO::IPC
|
2022-04-14 19:40:35 +00:00
|
|
|
{
|
2022-04-16 18:26:01 +00:00
|
|
|
#define IMPLEMENT_HANDLE \
|
|
|
|
IPC::IPCHandle handle_; \
|
|
|
|
AuString ExportToString() override \
|
|
|
|
{ \
|
|
|
|
return handle_.ToString(); \
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Mutexes
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct IPCMutexProxy : IPCMutex, Loop::ILoopSourceEx
|
|
|
|
{
|
|
|
|
IPCMutexProxy(HANDLE h, const IPCHandle &handle);
|
|
|
|
~IPCMutexProxy();
|
|
|
|
|
|
|
|
PROXY_INTERNAL_INTERFACE(mutex_)
|
2024-09-07 21:45:34 +00:00
|
|
|
PROXY_LOOPSOURCE_EXT_APIS(mutex_)
|
2022-04-16 18:26:01 +00:00
|
|
|
IMPLEMENT_HANDLE
|
|
|
|
|
|
|
|
bool Unlock() override;
|
|
|
|
|
2023-12-01 10:33:55 +00:00
|
|
|
bool IsSignaledNoSpinIfUserland() override;
|
2022-04-16 18:26:01 +00:00
|
|
|
bool IsSignaled() override;
|
|
|
|
bool WaitOn(AuUInt32 timeout) override;
|
|
|
|
Loop::ELoopSource GetType() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Loop::LSMutex mutex_;
|
|
|
|
};
|
|
|
|
|
|
|
|
IPCMutexProxy::IPCMutexProxy(HANDLE h, const IPCHandle &handle) : handle_(handle), mutex_(h)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IPCMutexProxy::~IPCMutexProxy()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IPCMutexProxy::Unlock()
|
2022-04-16 17:40:36 +00:00
|
|
|
{
|
2022-04-16 18:26:01 +00:00
|
|
|
return this->mutex_.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IPCMutexProxy::IsSignaled()
|
|
|
|
{
|
|
|
|
return this->mutex_.IsSignaled();
|
|
|
|
}
|
|
|
|
|
2023-12-01 10:33:55 +00:00
|
|
|
bool IPCMutexProxy::IsSignaledNoSpinIfUserland()
|
|
|
|
{
|
|
|
|
return this->IsSignaled();
|
|
|
|
}
|
|
|
|
|
2022-04-16 18:26:01 +00:00
|
|
|
bool IPCMutexProxy::WaitOn(AuUInt32 timeout)
|
|
|
|
{
|
|
|
|
return this->mutex_.WaitOn(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
Loop::ELoopSource IPCMutexProxy::GetType()
|
|
|
|
{
|
|
|
|
return this->mutex_.GetType();
|
2022-04-16 17:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IPCMutex> NewMutex()
|
|
|
|
{
|
2022-07-21 06:18:53 +00:00
|
|
|
IPC::IPCHandle handle;
|
|
|
|
IPC::IPCToken token;
|
|
|
|
token.NewId();
|
|
|
|
|
|
|
|
handle.PushId(EIPCHandleType::eIPCPrimitiveMutex, token);
|
2022-04-16 18:26:01 +00:00
|
|
|
|
2022-07-21 06:18:53 +00:00
|
|
|
auto mutex = CreateMutexA(nullptr, false, token.ToNTPath().c_str());
|
2022-04-16 18:26:01 +00:00
|
|
|
if (mutex == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto object = AuMakeShared<IPCMutexProxy>(mutex, handle);
|
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
AuWin32CloseHandle(mutex);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IPCMutex> ImportMutex(const AuString &handleString)
|
|
|
|
{
|
|
|
|
IPCHandle handle;
|
|
|
|
|
|
|
|
if (!handle.FromString(handleString))
|
|
|
|
{
|
|
|
|
SysPushErrorParseError();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-07-21 06:18:53 +00:00
|
|
|
auto token = handle.GetToken(EIPCHandleType::eIPCPrimitiveMutex, 0);
|
|
|
|
if (!token)
|
|
|
|
{
|
|
|
|
SysPushErrorParseError();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto mutex = OpenMutexA(MUTEX_ALL_ACCESS, false, token->token.ToNTPath().c_str());
|
2022-04-16 18:26:01 +00:00
|
|
|
if (mutex == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto object = AuMakeShared<IPCMutexProxy>(mutex, handle);
|
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
AuWin32CloseHandle(mutex);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Events
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct IPCEventProxy : IPCEvent, Loop::ILoopSourceEx
|
|
|
|
{
|
|
|
|
IPCEventProxy(HANDLE h, const IPCHandle &handle);
|
|
|
|
~IPCEventProxy();
|
|
|
|
|
|
|
|
PROXY_INTERNAL_INTERFACE(event_)
|
2024-09-07 21:45:34 +00:00
|
|
|
PROXY_LOOPSOURCE_EXT_APIS(event_)
|
2022-04-16 18:26:01 +00:00
|
|
|
IMPLEMENT_HANDLE
|
|
|
|
|
|
|
|
bool Set() override;
|
|
|
|
bool Reset() override;
|
|
|
|
|
2023-12-01 10:33:55 +00:00
|
|
|
bool IsSignaledNoSpinIfUserland() override;
|
2022-04-16 18:26:01 +00:00
|
|
|
bool IsSignaled() override;
|
|
|
|
bool WaitOn(AuUInt32 timeout) override;
|
|
|
|
Loop::ELoopSource GetType() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Loop::LSEvent event_;
|
|
|
|
};
|
|
|
|
|
|
|
|
IPCEventProxy::IPCEventProxy(HANDLE h, const IPCHandle &handle) : event_(h), handle_(handle)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IPCEventProxy::~IPCEventProxy()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IPCEventProxy::Set()
|
|
|
|
{
|
|
|
|
return this->event_.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IPCEventProxy::Reset()
|
|
|
|
{
|
|
|
|
return this->event_.Reset();
|
2022-04-16 17:40:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 18:26:01 +00:00
|
|
|
bool IPCEventProxy::IsSignaled()
|
|
|
|
{
|
|
|
|
return this->event_.IsSignaled();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IPCEventProxy::WaitOn(AuUInt32 timeout)
|
|
|
|
{
|
|
|
|
return this->event_.WaitOn(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
Loop::ELoopSource IPCEventProxy::GetType()
|
|
|
|
{
|
|
|
|
return this->event_.GetType();
|
|
|
|
}
|
|
|
|
|
2023-12-01 10:33:55 +00:00
|
|
|
bool IPCEventProxy::IsSignaledNoSpinIfUserland()
|
|
|
|
{
|
|
|
|
return this->IsSignaled();
|
|
|
|
}
|
|
|
|
|
2022-04-16 17:40:36 +00:00
|
|
|
AUKN_SYM AuSPtr<IPCEvent> NewEvent(bool triggered, bool atomicRelease)
|
|
|
|
{
|
2022-07-21 06:18:53 +00:00
|
|
|
IPC::IPCHandle handle;
|
|
|
|
IPC::IPCToken token;
|
|
|
|
token.NewId();
|
2022-04-16 18:26:01 +00:00
|
|
|
|
2022-07-21 06:18:53 +00:00
|
|
|
handle.PushId(EIPCHandleType::eIPCPrimitiveEvent, token);
|
|
|
|
|
|
|
|
auto mutex = ::CreateEventA(nullptr, !atomicRelease, triggered, token.ToNTPath().c_str());
|
|
|
|
if ((!mutex) ||
|
|
|
|
mutex == INVALID_HANDLE_VALUE)
|
2022-04-16 18:26:01 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto object = AuMakeShared<IPCEventProxy>(mutex, handle);
|
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
AuWin32CloseHandle(mutex);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IPCEvent> ImportEvent(const AuString &handleString)
|
|
|
|
{
|
|
|
|
IPCHandle handle;
|
|
|
|
|
|
|
|
if (!handle.FromString(handleString))
|
|
|
|
{
|
|
|
|
SysPushErrorParseError();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-07-21 06:18:53 +00:00
|
|
|
auto token = handle.GetToken(EIPCHandleType::eIPCPrimitiveEvent, 0);
|
|
|
|
if (!token)
|
|
|
|
{
|
|
|
|
SysPushErrorParseError();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto eventE = ::OpenEventA(EVENT_ALL_ACCESS, false, token->token.ToNTPath().c_str());
|
|
|
|
if ((!eventE) ||
|
|
|
|
(eventE == INVALID_HANDLE_VALUE))
|
2022-04-16 18:26:01 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-07-21 06:18:53 +00:00
|
|
|
auto object = AuMakeShared<IPCEventProxy>(eventE, handle);
|
2022-04-16 18:26:01 +00:00
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
2022-07-21 06:18:53 +00:00
|
|
|
AuWin32CloseHandle(eventE);
|
2022-04-16 18:26:01 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Semaphores
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct IPCSemaphoreProxy : IPCSemaphore, Loop::ILoopSourceEx
|
|
|
|
{
|
|
|
|
IPCSemaphoreProxy(HANDLE h, const IPCHandle &handle);
|
|
|
|
~IPCSemaphoreProxy();
|
|
|
|
|
|
|
|
PROXY_INTERNAL_INTERFACE(semaphore_)
|
2024-09-07 21:45:34 +00:00
|
|
|
PROXY_LOOPSOURCE_EXT_APIS(semaphore_)
|
2022-04-16 18:26:01 +00:00
|
|
|
IMPLEMENT_HANDLE
|
|
|
|
|
|
|
|
bool AddOne() override;
|
2023-10-22 05:11:39 +00:00
|
|
|
bool AddMany(AuUInt32 uCount) override;
|
|
|
|
|
2023-12-01 10:33:55 +00:00
|
|
|
bool IsSignaledNoSpinIfUserland() override;
|
2022-04-16 18:26:01 +00:00
|
|
|
bool IsSignaled() override;
|
|
|
|
bool WaitOn(AuUInt32 timeout) override;
|
|
|
|
Loop::ELoopSource GetType() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Loop::LSSemaphore semaphore_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
IPCSemaphoreProxy::IPCSemaphoreProxy(HANDLE h, const IPCHandle &handle) : handle_(handle), semaphore_(h)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
IPCSemaphoreProxy::~IPCSemaphoreProxy()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IPCSemaphoreProxy::AddOne()
|
|
|
|
{
|
|
|
|
return this->semaphore_.AddOne();
|
2022-04-16 17:40:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-22 05:11:39 +00:00
|
|
|
bool IPCSemaphoreProxy::AddMany(AuUInt32 uCount)
|
|
|
|
{
|
|
|
|
return this->semaphore_.AddMany(uCount);
|
|
|
|
}
|
|
|
|
|
2022-04-16 18:26:01 +00:00
|
|
|
bool IPCSemaphoreProxy::IsSignaled()
|
2022-04-16 17:40:36 +00:00
|
|
|
{
|
2022-04-16 18:26:01 +00:00
|
|
|
return this->semaphore_.IsSignaled();
|
2022-04-16 17:40:36 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 10:33:55 +00:00
|
|
|
bool IPCSemaphoreProxy::IsSignaledNoSpinIfUserland()
|
|
|
|
{
|
|
|
|
return this->IsSignaled();
|
|
|
|
}
|
|
|
|
|
2022-04-16 18:26:01 +00:00
|
|
|
bool IPCSemaphoreProxy::WaitOn(AuUInt32 timeout)
|
2022-04-16 17:40:36 +00:00
|
|
|
{
|
2022-04-16 18:26:01 +00:00
|
|
|
return this->semaphore_.WaitOn(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
Loop::ELoopSource IPCSemaphoreProxy::GetType()
|
|
|
|
{
|
|
|
|
return this->semaphore_.GetType();
|
2022-04-16 17:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IPCSemaphore> NewSemaphore(int startingValue)
|
|
|
|
{
|
2022-07-21 06:18:53 +00:00
|
|
|
IPC::IPCHandle handle;
|
|
|
|
IPC::IPCToken token;
|
|
|
|
token.NewId();
|
2022-04-16 18:26:01 +00:00
|
|
|
|
2022-07-21 06:18:53 +00:00
|
|
|
handle.PushId(EIPCHandleType::eIPCPrimitiveSemaphore, token);
|
2023-12-19 03:43:11 +00:00
|
|
|
|
|
|
|
auto semaphore = ::CreateSemaphoreA(nullptr, startingValue, /*AuNumericLimits<LONG>::max()*/ LONG_MAX, token.ToNTPath().c_str());
|
2022-07-21 06:18:53 +00:00
|
|
|
if ((!semaphore) ||
|
|
|
|
(semaphore == INVALID_HANDLE_VALUE))
|
2022-04-16 18:26:01 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-07-21 06:18:53 +00:00
|
|
|
auto object = AuMakeShared<IPCSemaphoreProxy>(semaphore, handle);
|
2022-04-16 18:26:01 +00:00
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
2022-07-21 06:18:53 +00:00
|
|
|
AuWin32CloseHandle(semaphore);
|
2022-04-16 18:26:01 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IPCSemaphore> ImportSemaphore(const AuString &handleString)
|
|
|
|
{
|
|
|
|
IPCHandle handle;
|
|
|
|
|
|
|
|
if (!handle.FromString(handleString))
|
|
|
|
{
|
|
|
|
SysPushErrorParseError();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-07-21 06:18:53 +00:00
|
|
|
auto token = handle.GetToken(EIPCHandleType::eIPCPrimitiveSemaphore, 0);
|
|
|
|
if (!token)
|
|
|
|
{
|
|
|
|
SysPushErrorParseError();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto semaphore = ::OpenSemaphoreA(SEMAPHORE_ALL_ACCESS, false, token->token.ToNTPath().c_str());
|
|
|
|
if ((!semaphore) ||
|
|
|
|
(semaphore == INVALID_HANDLE_VALUE))
|
2022-04-16 18:26:01 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-07-21 06:18:53 +00:00
|
|
|
auto object = AuMakeShared<IPCSemaphoreProxy>(semaphore, handle);
|
2022-04-16 18:26:01 +00:00
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
2022-07-21 06:18:53 +00:00
|
|
|
AuWin32CloseHandle(semaphore);
|
2022-04-16 18:26:01 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
2022-04-16 17:40:36 +00:00
|
|
|
}
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|