AuroraRuntime/Source/Loop/LSEvent.Linux.cpp
J Reece Wilson 3defb1bb14 [+] Linux Watcher
[*] Expand watcher API -> Breaking NT
[*] Reexpand loop queue API -> Breaking NT
[*] Linux CPUInfo clean up
[*] Bug fix: mkdir should set execute flag... because directories are special
[*] Refactor: Cleanup base64
[*] Bug fix: UNIX path normalization
[*] Bug fix: missing O_CREAT flag (au auto-creates)
[*] Normalize line endings
2022-04-10 16:40:49 +01:00

119 lines
2.6 KiB
C++
Executable File

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: LSEvent.Linux.cpp
Date: 2022-4-4
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "LSEvent.hpp"
#include "LSFromFdNonblocking.hpp"
namespace Aurora::Loop
{
LSEvent::LSEvent(bool triggered, bool atomicRelease, bool permitMultipleTriggers) : atomicRelease_(atomicRelease)
{
Init(triggered);
}
LSEvent::~LSEvent()
{
if ((this->handle != 0) &&
(this->handle != -1))
{
close(this->handle);
}
}
bool LSEvent::OnTrigger(AuUInt handle)
{
AuUInt64 oldSemaphoreValue {};
if (!this->atomicRelease_)
{
return true;
}
return read(this->handle, &oldSemaphoreValue, sizeof(oldSemaphoreValue)) == 8;
}
void LSEvent::Init(bool init)
{
handle = eventfd(init ? 1 : 0, EFD_NONBLOCK);
}
bool LSEvent::Set()
{
AuUInt64 plsNoOverflow {1};
if (write(this->handle, &plsNoOverflow, sizeof(plsNoOverflow)) != 8)
{
// todo push error
return false;
}
return true;
}
bool LSEvent::Reset()
{
AuUInt64 oldSemaphoreValue {0};
// RETURN VALUE IS USELESS - Failure is to be expected
read(this->handle, &oldSemaphoreValue, sizeof(oldSemaphoreValue));
return true;
}
bool LSEvent::IsSignaled()
{
return IsSignaledFromNonblockingImpl(this, this, &LSEvent::IsSignaledNonblocking);
}
bool LSEvent::IsSignaledNonblocking()
{
if (!this->atomicRelease_)
{
fd_set set;
struct timeval tv {};
FD_ZERO(&set);
FD_SET(this->handle, &set);
auto active = select(this->handle + 1, &set, NULL, NULL, &tv);
if (active == -1)
{
// todo push error
return false;
}
return active == 1;
}
else
{
AuUInt64 oldSemaphoreValue {};
return read(this->handle, &oldSemaphoreValue, sizeof(oldSemaphoreValue)) == 8;
}
}
ELoopSource LSEvent::GetType()
{
return ELoopSource::eSourceEvent;
}
AUKN_SYM AuSPtr<ILSEvent> NewLSEvent(bool triggered, bool atomicRelease, bool permitMultipleTriggers)
{
auto event = AuMakeShared<LSEvent>(triggered, atomicRelease, permitMultipleTriggers);
if (!event)
{
return {};
}
if (!event->HasValidHandle())
{
return {};
}
return event;
}
}