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.Linux.cpp
|
2022-04-14 19:40:35 +00:00
|
|
|
Date: 2022-4-13
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "IPC.hpp"
|
2022-12-16 00:41:01 +00:00
|
|
|
#include "AuIPCHandle.hpp"
|
2022-04-14 19:40:35 +00:00
|
|
|
|
2022-12-16 00:41:01 +00:00
|
|
|
#include "AuIPCPrimitives.Linux.hpp"
|
2022-04-14 19:40:35 +00:00
|
|
|
|
|
|
|
#include <Source/IO/UNIX/FDIpcServer.hpp>
|
|
|
|
|
2022-06-11 23:52:46 +00:00
|
|
|
namespace Aurora::IO::IPC
|
2022-04-14 19:40:35 +00:00
|
|
|
{
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Events
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
IPCEventProxy::IPCEventProxy(bool triggered, bool atomicRelease) : event_(triggered, atomicRelease, true)
|
|
|
|
{
|
2022-08-02 04:52:17 +00:00
|
|
|
IPC::IPCToken token;
|
|
|
|
|
2022-04-14 19:40:35 +00:00
|
|
|
if (this->event_.HasValidHandle())
|
|
|
|
{
|
2022-08-02 04:52:17 +00:00
|
|
|
if (IO::UNIX::FDServe(this->GetHandle(), token))
|
|
|
|
{
|
|
|
|
token.word = AuUInt32(atomicRelease);
|
|
|
|
this->handle_.PushId(EIPCHandleType::eIPCPrimitiveEvent, token);
|
|
|
|
}
|
|
|
|
else
|
2022-04-14 19:40:35 +00:00
|
|
|
{
|
|
|
|
this->event_.~LSEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IPCEventProxy::IPCEventProxy(int handle, bool triggered, bool atomicRelease) : event_(handle, triggered, atomicRelease)
|
|
|
|
{
|
2022-08-02 04:52:17 +00:00
|
|
|
IPC::IPCToken token;
|
|
|
|
|
2022-04-14 19:40:35 +00:00
|
|
|
if (this->event_.HasValidHandle())
|
|
|
|
{
|
2022-08-02 04:52:17 +00:00
|
|
|
if (IO::UNIX::FDServe(this->GetHandle(), token))
|
|
|
|
{
|
|
|
|
token.word = AuUInt32(atomicRelease);
|
|
|
|
this->handle_.PushId(EIPCHandleType::eIPCPrimitiveEvent, token);
|
|
|
|
}
|
|
|
|
else
|
2022-04-14 19:40:35 +00:00
|
|
|
{
|
|
|
|
this->event_.~LSEvent();
|
|
|
|
}
|
2022-08-02 04:52:17 +00:00
|
|
|
}
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IPCEventProxy::~IPCEventProxy()
|
|
|
|
{
|
2022-08-02 04:52:17 +00:00
|
|
|
if (this->handle_.values.size() == 1)
|
|
|
|
{
|
|
|
|
IO::UNIX::FDServeEnd(this->handle_.values[0].token);
|
|
|
|
this->handle_ = {};
|
|
|
|
}
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IPCEventProxy::Set()
|
|
|
|
{
|
2022-04-15 10:01:43 +00:00
|
|
|
return this->event_.Set();
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IPCEventProxy::Reset()
|
|
|
|
{
|
2022-04-15 10:01:43 +00:00
|
|
|
return this->event_.Reset();
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IPCEventProxy::IsSignaled()
|
|
|
|
{
|
2022-04-15 10:01:43 +00:00
|
|
|
return this->event_.IsSignaled();
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IPCEventProxy::WaitOn(AuUInt32 timeout)
|
|
|
|
{
|
2022-04-15 10:01:43 +00:00
|
|
|
return this->event_.WaitOn(timeout);
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Loop::ELoopSource IPCEventProxy::GetType()
|
|
|
|
{
|
2022-04-15 10:01:43 +00:00
|
|
|
return this->event_.GetType();
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 10:33:55 +00:00
|
|
|
bool IPCEventProxy::IsSignaledNoSpinIfUserland()
|
|
|
|
{
|
|
|
|
return this->IsSignaled();
|
|
|
|
}
|
|
|
|
|
2022-04-14 19:40:35 +00:00
|
|
|
AUKN_SYM AuSPtr<IPCEvent> NewEvent(bool triggered, bool atomicRelease)
|
|
|
|
{
|
|
|
|
auto object = AuMakeShared<IPCEventProxy>(triggered, atomicRelease);
|
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-04-15 10:01:43 +00:00
|
|
|
if (!object->HasValidHandle())
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-04-14 19:40:35 +00:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
AuSPtr<IPCEvent> ImportEventEx(const IPCToken &token)
|
2022-04-14 19:40:35 +00:00
|
|
|
{
|
|
|
|
int fd {-1};
|
2022-08-02 04:52:17 +00:00
|
|
|
if (!IO::UNIX::FDAccept(token, fd))
|
2022-04-14 19:40:35 +00:00
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
auto object = AuMakeShared<IPCEventProxy>(fd, false, bool(token.word));
|
2022-04-14 19:40:35 +00:00
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
::close(fd);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-04-15 10:01:43 +00:00
|
|
|
if (!object->HasValidHandle())
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-04-14 19:40:35 +00:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
AUKN_SYM AuSPtr<IPCEvent> ImportEvent(const AuString &handle)
|
2022-04-14 19:40:35 +00:00
|
|
|
{
|
2022-08-02 04:52:17 +00:00
|
|
|
IPC::IPCHandle decodedHandle;
|
2022-04-14 19:40:35 +00:00
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
if (!decodedHandle.FromString(handle))
|
|
|
|
{
|
|
|
|
SysPushErrorParseError("Invalid handle: {}", handle);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto val = decodedHandle.GetToken(IPC::EIPCHandleType::eIPCPrimitiveEvent, 0);
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
SysPushErrorParseError("Invalid handle: {}", handle);
|
|
|
|
return {};
|
|
|
|
}
|
2022-04-14 19:40:35 +00:00
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
return ImportEventEx(val->token);
|
|
|
|
}
|
2022-04-14 19:40:35 +00:00
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Semaphores
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2022-04-14 19:40:35 +00:00
|
|
|
|
|
|
|
IPCSemaphoreProxy::IPCSemaphoreProxy(AuUInt32 initialCount) : semaphore_(initialCount)
|
|
|
|
{
|
2022-08-02 04:52:17 +00:00
|
|
|
IPC::IPCToken token;
|
|
|
|
|
2022-04-14 19:40:35 +00:00
|
|
|
if (this->semaphore_.HasValidHandle())
|
|
|
|
{
|
2022-08-02 04:52:17 +00:00
|
|
|
if (IO::UNIX::FDServe(this->GetHandle(), token))
|
|
|
|
{
|
|
|
|
this->handle_.PushId(EIPCHandleType::eIPCPrimitiveSemaphore, token);
|
|
|
|
}
|
|
|
|
else
|
2022-04-14 19:40:35 +00:00
|
|
|
{
|
|
|
|
this->semaphore_.~LSSemaphore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IPCSemaphoreProxy::IPCSemaphoreProxy(int handle, int tag) : semaphore_(handle, tag)
|
|
|
|
{
|
2022-08-02 04:52:17 +00:00
|
|
|
IPC::IPCToken token;
|
|
|
|
|
2022-04-14 19:40:35 +00:00
|
|
|
if (this->semaphore_.HasValidHandle())
|
|
|
|
{
|
2022-08-02 04:52:17 +00:00
|
|
|
if (IO::UNIX::FDServe(this->GetHandle(), token))
|
|
|
|
{
|
|
|
|
this->handle_.PushId(EIPCHandleType::eIPCPrimitiveSemaphore, token);
|
|
|
|
}
|
|
|
|
else
|
2022-04-14 19:40:35 +00:00
|
|
|
{
|
|
|
|
this->semaphore_.~LSSemaphore();
|
|
|
|
}
|
2022-08-02 04:52:17 +00:00
|
|
|
}
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IPCSemaphoreProxy::~IPCSemaphoreProxy()
|
|
|
|
{
|
2022-08-02 04:52:17 +00:00
|
|
|
if (this->handle_.values.size() == 1)
|
|
|
|
{
|
|
|
|
IO::UNIX::FDServeEnd(this->handle_.values[0].token);
|
|
|
|
this->handle_ = {};
|
|
|
|
}
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IPCSemaphoreProxy::AddOne()
|
|
|
|
{
|
2022-04-15 10:01:43 +00:00
|
|
|
return this->semaphore_.AddOne();
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-22 05:11:39 +00:00
|
|
|
bool IPCSemaphoreProxy::AddMany(AuUInt32 uCount)
|
|
|
|
{
|
|
|
|
return this->semaphore_.AddMany(uCount);
|
|
|
|
}
|
|
|
|
|
2022-04-14 19:40:35 +00:00
|
|
|
bool IPCSemaphoreProxy::IsSignaled()
|
|
|
|
{
|
2022-04-15 10:01:43 +00:00
|
|
|
return this->semaphore_.IsSignaled();
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 10:33:55 +00:00
|
|
|
bool IPCSemaphoreProxy::IsSignaledNoSpinIfUserland()
|
|
|
|
{
|
|
|
|
return this->IsSignaled();
|
|
|
|
}
|
|
|
|
|
2022-04-14 19:40:35 +00:00
|
|
|
bool IPCSemaphoreProxy::WaitOn(AuUInt32 timeout)
|
|
|
|
{
|
2022-04-15 10:01:43 +00:00
|
|
|
return this->semaphore_.WaitOn(timeout);
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Loop::ELoopSource IPCSemaphoreProxy::GetType()
|
|
|
|
{
|
2022-04-15 10:01:43 +00:00
|
|
|
return this->semaphore_.GetType();
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IPCSemaphore> NewSemaphore(int startingValue)
|
|
|
|
{
|
|
|
|
auto object = AuMakeShared<IPCSemaphoreProxy>(startingValue);
|
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-04-15 10:01:43 +00:00
|
|
|
if (!object->HasValidHandle())
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-04-14 19:40:35 +00:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuSPtr<IPCSemaphore> ImportSemaphore(const AuString &handle)
|
|
|
|
{
|
|
|
|
IPC::IPCHandle decodedHandle;
|
|
|
|
|
|
|
|
if (!decodedHandle.FromString(handle))
|
|
|
|
{
|
|
|
|
SysPushErrorParseError("Invalid handle: {}", handle);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
auto val = decodedHandle.GetToken(IPC::EIPCHandleType::eIPCPrimitiveSemaphore, 0);
|
|
|
|
if (!val)
|
2022-04-15 10:01:43 +00:00
|
|
|
{
|
|
|
|
SysPushErrorParseError("Invalid handle: {}", handle);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd {-1};
|
2022-08-02 04:52:17 +00:00
|
|
|
if (!IO::UNIX::FDAccept(val->token, fd))
|
2022-04-15 10:01:43 +00:00
|
|
|
{
|
|
|
|
SysPushErrorNested();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-02 04:52:17 +00:00
|
|
|
auto object = AuMakeShared<IPCSemaphoreProxy>(fd, 0xC001C0DE);
|
2022-04-15 10:01:43 +00:00
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
::close(fd);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!object->HasValidHandle())
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
2022-04-14 19:40:35 +00:00
|
|
|
}
|
|
|
|
}
|