AuroraRuntime/Source/IO/IPC/IPCPrimitives.Linux.cpp
J Reece Wilson fd0c5b51b2 Further Linux support
[+] Begin work on IO futexes for io release on process/thread exit
[+] Linux ::readdir iteration
[+] AuConsole buffering API
[*] Fix sleep as to not get interrupted by signals
[*] Switch the type of FS lock used under Linux
[*] Linux: Use new IPCHandle encoding scheme
[*] Fix undefined behaviour: unintialized timeout values (AuLoop/Linux)
[*] Fix undefined behaviour: ConsoleTTY clear line was called of a color of a random value on stack
[-] Remainings of std dir iterator
[*] Fix pthread_kill (aka send signal to pthread handle) always kills process. This is what you expect bc signal handler inheritance.
[*] Reformat the build Aurora.json file
[+] Added clang warning ignores to the build file
[*] Fix: UNIX need to use STDOUT_FILENO. Was using CRT handle in place of fd by mistake.
[+] Linux implementation for IO yield (AuIO::IOYield() - UNIX::LinuxOverlappedYield())
[*] Fix: Linux async end of stream processing. res 0 = zero bytes consumed. <= was detecting this as an error of code 0. Should succeed with zero bytes.
[+] Linux LoopQueue missing epilogue hook for the IO processor
[*] Various refactors and minor bug fixes
[*] Linux fix: Handle pipe EOS as zero
[*] Linux fix: thread termination via a user signal of 77. Need a force terminate.
[*] IPC handle: fix improper int to bool cast in the header setup within ToString
[*] Linux fix: HWInfo CPU topology regression
[-] Linux fix: remove SIGABRT handler
[*] Missing override in compression, exit, and consoletty headers.
[+] Unix Syslog logger backend
2022-08-02 05:52:57 +01:00

281 lines
6.9 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: IPCPrimitives.Linux.cpp
Date: 2022-4-13
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "IPC.hpp"
#include "IPCHandle.hpp"
#include "IPCPrimitives.Linux.hpp"
#include <Source/IO/UNIX/FDIpcServer.hpp>
namespace Aurora::IO::IPC
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Events
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
IPCEventProxy::IPCEventProxy(bool triggered, bool atomicRelease) : event_(triggered, atomicRelease, true)
{
IPC::IPCToken token;
if (this->event_.HasValidHandle())
{
if (IO::UNIX::FDServe(this->GetHandle(), token))
{
token.word = AuUInt32(atomicRelease);
this->handle_.PushId(EIPCHandleType::eIPCPrimitiveEvent, token);
}
else
{
this->event_.~LSEvent();
}
}
}
IPCEventProxy::IPCEventProxy(int handle, bool triggered, bool atomicRelease) : event_(handle, triggered, atomicRelease)
{
IPC::IPCToken token;
if (this->event_.HasValidHandle())
{
if (IO::UNIX::FDServe(this->GetHandle(), token))
{
token.word = AuUInt32(atomicRelease);
this->handle_.PushId(EIPCHandleType::eIPCPrimitiveEvent, token);
}
else
{
this->event_.~LSEvent();
}
}
}
IPCEventProxy::~IPCEventProxy()
{
if (this->handle_.values.size() == 1)
{
IO::UNIX::FDServeEnd(this->handle_.values[0].token);
this->handle_ = {};
}
}
bool IPCEventProxy::Set()
{
return this->event_.Set();
}
bool IPCEventProxy::Reset()
{
return this->event_.Reset();
}
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();
}
AUKN_SYM AuSPtr<IPCEvent> NewEvent(bool triggered, bool atomicRelease)
{
auto object = AuMakeShared<IPCEventProxy>(triggered, atomicRelease);
if (!object)
{
SysPushErrorMem();
return {};
}
if (!object->HasValidHandle())
{
SysPushErrorIO();
return {};
}
return object;
}
AuSPtr<IPCEvent> ImportEventEx(const IPCToken &token)
{
int fd {-1};
if (!IO::UNIX::FDAccept(token, fd))
{
SysPushErrorNested();
return {};
}
auto object = AuMakeShared<IPCEventProxy>(fd, false, bool(token.word));
if (!object)
{
SysPushErrorMem();
::close(fd);
return {};
}
if (!object->HasValidHandle())
{
SysPushErrorIO();
return {};
}
return object;
}
AUKN_SYM AuSPtr<IPCEvent> ImportEvent(const AuString &handle)
{
IPC::IPCHandle decodedHandle;
if (!decodedHandle.FromString(handle))
{
SysPushErrorParseError("Invalid handle: {}", handle);
return {};
}
auto val = decodedHandle.GetToken(IPC::EIPCHandleType::eIPCPrimitiveEvent, 0);
if (!val)
{
SysPushErrorParseError("Invalid handle: {}", handle);
return {};
}
return ImportEventEx(val->token);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Semaphores
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
IPCSemaphoreProxy::IPCSemaphoreProxy(AuUInt32 initialCount) : semaphore_(initialCount)
{
IPC::IPCToken token;
if (this->semaphore_.HasValidHandle())
{
if (IO::UNIX::FDServe(this->GetHandle(), token))
{
this->handle_.PushId(EIPCHandleType::eIPCPrimitiveSemaphore, token);
}
else
{
this->semaphore_.~LSSemaphore();
}
}
}
IPCSemaphoreProxy::IPCSemaphoreProxy(int handle, int tag) : semaphore_(handle, tag)
{
IPC::IPCToken token;
if (this->semaphore_.HasValidHandle())
{
if (IO::UNIX::FDServe(this->GetHandle(), token))
{
this->handle_.PushId(EIPCHandleType::eIPCPrimitiveSemaphore, token);
}
else
{
this->semaphore_.~LSSemaphore();
}
}
}
IPCSemaphoreProxy::~IPCSemaphoreProxy()
{
if (this->handle_.values.size() == 1)
{
IO::UNIX::FDServeEnd(this->handle_.values[0].token);
this->handle_ = {};
}
}
bool IPCSemaphoreProxy::AddOne()
{
return this->semaphore_.AddOne();
}
bool IPCSemaphoreProxy::IsSignaled()
{
return this->semaphore_.IsSignaled();
}
bool IPCSemaphoreProxy::WaitOn(AuUInt32 timeout)
{
return this->semaphore_.WaitOn(timeout);
}
Loop::ELoopSource IPCSemaphoreProxy::GetType()
{
return this->semaphore_.GetType();
}
AUKN_SYM AuSPtr<IPCSemaphore> NewSemaphore(int startingValue)
{
auto object = AuMakeShared<IPCSemaphoreProxy>(startingValue);
if (!object)
{
SysPushErrorMem();
return {};
}
if (!object->HasValidHandle())
{
SysPushErrorIO();
return {};
}
return object;
}
AUKN_SYM AuSPtr<IPCSemaphore> ImportSemaphore(const AuString &handle)
{
IPC::IPCHandle decodedHandle;
if (!decodedHandle.FromString(handle))
{
SysPushErrorParseError("Invalid handle: {}", handle);
return {};
}
auto val = decodedHandle.GetToken(IPC::EIPCHandleType::eIPCPrimitiveSemaphore, 0);
if (!val)
{
SysPushErrorParseError("Invalid handle: {}", handle);
return {};
}
int fd {-1};
if (!IO::UNIX::FDAccept(val->token, fd))
{
SysPushErrorNested();
return {};
}
auto object = AuMakeShared<IPCSemaphoreProxy>(fd, 0xC001C0DE);
if (!object)
{
SysPushErrorMem();
::close(fd);
return {};
}
if (!object->HasValidHandle())
{
SysPushErrorIO();
return {};
}
return object;
}
}