2022-04-10 15:40:49 +00:00
|
|
|
/***
|
|
|
|
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"
|
|
|
|
|
2022-06-11 23:52:46 +00:00
|
|
|
namespace Aurora::IO::Loop
|
2022-04-10 15:40:49 +00:00
|
|
|
{
|
|
|
|
LSEvent::LSEvent(bool triggered, bool atomicRelease, bool permitMultipleTriggers) : atomicRelease_(atomicRelease)
|
|
|
|
{
|
|
|
|
Init(triggered);
|
|
|
|
}
|
|
|
|
|
2022-04-14 19:40:35 +00:00
|
|
|
LSEvent::LSEvent(int handle, bool triggered, bool atomicRelease) : atomicRelease_(atomicRelease)
|
|
|
|
{
|
|
|
|
this->handle = handle;
|
|
|
|
}
|
|
|
|
|
2022-04-10 15:40:49 +00:00
|
|
|
LSEvent::~LSEvent()
|
|
|
|
{
|
|
|
|
if ((this->handle != 0) &&
|
|
|
|
(this->handle != -1))
|
|
|
|
{
|
2022-04-19 21:50:34 +00:00
|
|
|
::close(AuExchange(this->handle, -1));
|
2022-04-10 15:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LSEvent::OnTrigger(AuUInt handle)
|
|
|
|
{
|
|
|
|
AuUInt64 oldSemaphoreValue {};
|
|
|
|
|
|
|
|
if (!this->atomicRelease_)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-19 21:50:34 +00:00
|
|
|
return ::read(this->handle, &oldSemaphoreValue, sizeof(oldSemaphoreValue)) == 8;
|
2022-04-10 15:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LSEvent::Init(bool init)
|
|
|
|
{
|
2022-04-19 21:50:34 +00:00
|
|
|
this->handle = ::eventfd(init ? 1 : 0, EFD_NONBLOCK);
|
2022-04-10 15:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LSEvent::Set()
|
|
|
|
{
|
|
|
|
AuUInt64 plsNoOverflow {1};
|
|
|
|
|
2022-04-19 21:50:34 +00:00
|
|
|
if (::write(this->handle, &plsNoOverflow, sizeof(plsNoOverflow)) != 8)
|
2022-04-10 15:40:49 +00:00
|
|
|
{
|
|
|
|
// todo push error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LSEvent::Reset()
|
|
|
|
{
|
|
|
|
AuUInt64 oldSemaphoreValue {0};
|
|
|
|
|
|
|
|
// RETURN VALUE IS USELESS - Failure is to be expected
|
2022-04-19 21:50:34 +00:00
|
|
|
::read(this->handle, &oldSemaphoreValue, sizeof(oldSemaphoreValue));
|
2022-04-10 15:40:49 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LSEvent::IsSignaled()
|
|
|
|
{
|
|
|
|
return IsSignaledFromNonblockingImpl(this, this, &LSEvent::IsSignaledNonblocking);
|
|
|
|
}
|
|
|
|
|
2022-04-12 21:26:15 +00:00
|
|
|
bool LSEvent::WaitOn(AuUInt32 timeout)
|
|
|
|
{
|
|
|
|
return LSHandle::WaitOn(timeout);
|
|
|
|
}
|
|
|
|
|
2022-04-10 15:40:49 +00:00
|
|
|
bool LSEvent::IsSignaledNonblocking()
|
|
|
|
{
|
|
|
|
if (!this->atomicRelease_)
|
|
|
|
{
|
|
|
|
fd_set set;
|
|
|
|
struct timeval tv {};
|
|
|
|
|
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(this->handle, &set);
|
|
|
|
|
2022-04-19 21:50:34 +00:00
|
|
|
auto active = ::select(this->handle + 1, &set, NULL, NULL, &tv);
|
2022-04-10 15:40:49 +00:00
|
|
|
if (active == -1)
|
|
|
|
{
|
|
|
|
// todo push error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return active == 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AuUInt64 oldSemaphoreValue {};
|
2022-04-19 21:50:34 +00:00
|
|
|
return ::read(this->handle, &oldSemaphoreValue, sizeof(oldSemaphoreValue)) == 8;
|
2022-04-10 15:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-04-05 01:19:37 +00:00
|
|
|
}
|