AuroraRuntime/Source/Loop/LSMutex.Linux.cpp
J Reece Wilson 48b6994f61 [+] LSFromFdNonblocking
[*] Other Linux progression
2022-04-05 03:36:39 +01:00

71 lines
1.6 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: LSMutex.Linux.cpp
Date: 2021-10-1
Date: 2022-4-4
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "LSMutex.hpp"
#include "LSFromFdNonblocking.hpp"
namespace Aurora::Loop
{
LSMutex::LSMutex()
{
}
bool LSMutex::OnTrigger(AuUInt handle)
{
AuUInt64 oldSemaphoreValue {};
auto ok = read(this->handle, &oldSemaphoreValue, sizeof(oldSemaphoreValue)) == 8;
SysAssertDbg(oldSemaphoreValue == 1, "Double unlock caught");
return ok;
}
void LSMutex::Init()
{
handle = eventfd(1, EFD_NONBLOCK);
}
bool LSMutex::Unlock()
{
AuUInt64 plsNoOverflow {1};
return write(this->handle, &plsNoOverflow, sizeof(plsNoOverflow)) == 8;
}
bool LSMutex::IsSignaled()
{
return IsSignaledFromNonblockingImpl(this, this, &LSMutex::IsSignaledNonblocking);
}
bool LSMutex::IsSignaledNonblocking()
{
AuUInt64 oldSemaphoreValue {};
auto ok = read(this->handle, &oldSemaphoreValue, sizeof(oldSemaphoreValue)) == 8;
SysAssertDbg(!ok || oldSemaphoreValue == 1, "Double unlock caught");
return ok;
}
ELoopSource LSMutex::GetType()
{
return ELoopSource::eSourceMutex;
}
AUKN_SYM AuSPtr<ILSMutex> NewLSMutex()
{
auto mutex = AuMakeShared<LSMutex>();
if (!mutex)
{
return {};
}
if (!mutex->HasValidHandle())
{
return {};
}
return mutex;
}
}