2021-10-02 10:28:49 +00:00
|
|
|
/***
|
2022-04-05 05:59:23 +00:00
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
2021-10-02 10:28:49 +00:00
|
|
|
|
|
|
|
File: LSSemaphore.Linux.cpp
|
2022-04-05 05:59:23 +00:00
|
|
|
Date: 2022-4-5
|
2021-10-02 10:28:49 +00:00
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "LSSemaphore.hpp"
|
2022-04-05 10:11:19 +00:00
|
|
|
#include "LSFromFdNonblocking.hpp"
|
2021-10-02 10:28:49 +00:00
|
|
|
|
2022-06-11 23:52:46 +00:00
|
|
|
namespace Aurora::IO::Loop
|
2021-10-02 10:28:49 +00:00
|
|
|
{
|
2023-10-21 03:31:00 +00:00
|
|
|
LSSemaphore::LSSemaphore()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-05 10:11:19 +00:00
|
|
|
LSSemaphore::LSSemaphore(AuUInt32 initialCount)
|
|
|
|
{
|
|
|
|
Init(initialCount);
|
|
|
|
}
|
|
|
|
|
2022-04-14 19:40:35 +00:00
|
|
|
LSSemaphore::LSSemaphore(int handle, int tag)
|
|
|
|
{
|
|
|
|
this->handle = handle;
|
|
|
|
}
|
|
|
|
|
2022-04-07 01:20:46 +00:00
|
|
|
LSSemaphore::~LSSemaphore()
|
|
|
|
{
|
|
|
|
if ((this->handle != 0) &&
|
|
|
|
(this->handle != -1))
|
|
|
|
{
|
2022-04-14 19:40:35 +00:00
|
|
|
::close(AuExchange(this->handle, -1));
|
2022-04-07 01:20:46 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-14 19:40:35 +00:00
|
|
|
|
2022-04-05 10:11:19 +00:00
|
|
|
void LSSemaphore::Init(AuUInt32 initialCount)
|
|
|
|
{
|
2022-12-16 03:48:04 +00:00
|
|
|
handle = ::eventfd(initialCount, EFD_SEMAPHORE | EFD_NONBLOCK | EFD_CLOEXEC);
|
2022-04-05 10:11:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 03:31:00 +00:00
|
|
|
bool LSSemaphore::TryInit(AuUInt32 initialCount)
|
|
|
|
{
|
|
|
|
this->Init(initialCount);
|
2023-10-21 03:57:25 +00:00
|
|
|
return this->HasValidHandle();
|
2023-10-21 03:31:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 10:11:19 +00:00
|
|
|
bool LSSemaphore::IsSignaledNonblocking()
|
|
|
|
{
|
|
|
|
AuUInt64 oldSemaphoreValue {};
|
2022-04-19 21:50:34 +00:00
|
|
|
return ::read(this->handle, &oldSemaphoreValue, sizeof(oldSemaphoreValue)) == 8;
|
2022-04-05 10:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LSSemaphore::AddOne()
|
|
|
|
{
|
2023-10-22 05:11:39 +00:00
|
|
|
AuUInt64 plsNoOverflow { 1 };
|
|
|
|
|
|
|
|
if (::write(this->handle, &plsNoOverflow, sizeof(plsNoOverflow)) != 8)
|
|
|
|
{
|
|
|
|
// todo push error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LSSemaphore::AddMany(AuUInt32 uCount)
|
|
|
|
{
|
|
|
|
AuUInt64 plsNoOverflow { uCount };
|
2022-04-05 10:11:19 +00:00
|
|
|
|
2022-04-19 21:50:34 +00:00
|
|
|
if (::write(this->handle, &plsNoOverflow, sizeof(plsNoOverflow)) != 8)
|
2022-04-05 10:11:19 +00:00
|
|
|
{
|
|
|
|
// todo push error
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LSSemaphore::OnTrigger(AuUInt handle)
|
|
|
|
{
|
|
|
|
return IsSignaledNonblocking();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LSSemaphore::IsSignaled()
|
|
|
|
{
|
|
|
|
return IsSignaledFromNonblockingImpl(this, this, &LSSemaphore::IsSignaledNonblocking);
|
|
|
|
}
|
|
|
|
|
2022-04-12 21:26:15 +00:00
|
|
|
bool LSSemaphore::WaitOn(AuUInt32 timeout)
|
|
|
|
{
|
|
|
|
return LSHandle::WaitOn(timeout);
|
|
|
|
}
|
|
|
|
|
2022-04-05 10:11:19 +00:00
|
|
|
ELoopSource LSSemaphore::GetType()
|
|
|
|
{
|
|
|
|
return ELoopSource::eSourceSemaphore;
|
|
|
|
}
|
|
|
|
|
2023-10-21 03:57:25 +00:00
|
|
|
#if 0
|
2022-04-05 10:11:19 +00:00
|
|
|
AUKN_SYM AuSPtr<ILSSemaphore> NewLSSemaphore(AuUInt32 initialCount)
|
|
|
|
{
|
2022-12-16 03:48:04 +00:00
|
|
|
auto pSemaphore = AuMakeShared<LSSemaphore>(initialCount);
|
|
|
|
if (!pSemaphore)
|
2022-04-05 10:11:19 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-16 03:48:04 +00:00
|
|
|
if (!pSemaphore->HasValidHandle())
|
2022-04-05 10:11:19 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-16 03:48:04 +00:00
|
|
|
return pSemaphore;
|
2022-04-05 10:11:19 +00:00
|
|
|
}
|
2023-10-21 03:57:25 +00:00
|
|
|
#endif
|
2021-10-02 10:28:49 +00:00
|
|
|
}
|