88 lines
1.8 KiB
C++
88 lines
1.8 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::IO::Loop
|
|
{
|
|
LSMutex::LSMutex()
|
|
{
|
|
Init();
|
|
}
|
|
|
|
LSMutex::LSMutex(int handle)
|
|
{
|
|
this->handle = handle;
|
|
}
|
|
|
|
LSMutex::~LSMutex()
|
|
{
|
|
if ((this->handle != 0) &&
|
|
(this->handle != -1))
|
|
{
|
|
::close(AuExchange(this->handle, -1));
|
|
}
|
|
}
|
|
|
|
bool LSMutex::OnTrigger(AuUInt handle)
|
|
{
|
|
return IsSignaledNonblocking();
|
|
}
|
|
|
|
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::WaitOn(AuUInt32 timeout)
|
|
{
|
|
return LSHandle::WaitOn(timeout);
|
|
}
|
|
|
|
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;
|
|
}
|
|
} |