2022-12-28 23:44:45 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: AuSemaphore.Linux.cpp
|
|
|
|
Date: 2022-12-28
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "AuSemaphore.Generic.hpp"
|
2023-03-21 03:18:09 +00:00
|
|
|
#include "SMTYield.hpp"
|
2022-12-28 23:44:45 +00:00
|
|
|
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <linux/futex.h>
|
|
|
|
|
|
|
|
#if !defined(_AURUNTIME_GENERIC_SEMAPHORE)
|
|
|
|
#include <Source/Time/Time.hpp>
|
|
|
|
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
|
|
{
|
|
|
|
static int futex(uint32_t *uaddr, int futex_op, uint32_t val,
|
|
|
|
const struct timespec *timeout,
|
|
|
|
uint32_t *uaddr2, uint32_t val3)
|
|
|
|
{
|
|
|
|
return syscall(SYS_futex, uaddr, futex_op, val, timeout, uaddr2, val3);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int futex_wait(uint32_t *addr, uint32_t expected)
|
|
|
|
{
|
|
|
|
return futex(addr, FUTEX_WAIT, expected, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int futex_wait(uint32_t *addr, uint32_t expected, const struct timespec *timeout)
|
|
|
|
{
|
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
return futex(addr, FUTEX_WAIT_BITSET, expected, timeout, 0, FUTEX_BITSET_MATCH_ANY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return futex(addr, FUTEX_WAIT, expected, timeout, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int futex_wake(uint32_t *addr, uint32_t nthreads)
|
|
|
|
{
|
|
|
|
return futex(addr, FUTEX_WAKE, nthreads, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
SemaphoreImpl::SemaphoreImpl(long intialValue) : value_(intialValue)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
SemaphoreImpl::~SemaphoreImpl()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool SemaphoreImpl::HasOSHandle(AuMach &mach)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool SemaphoreImpl::HasLockImplementation()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool SemaphoreImpl::TryLock()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-03-12 15:27:28 +00:00
|
|
|
return DoTryIf([=]()
|
|
|
|
{
|
|
|
|
auto old = this->value_;
|
|
|
|
return (old != 0 && AuAtomicCompareExchange(&this->value_, old - 1, old) == old);
|
|
|
|
});
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
2023-03-12 15:27:28 +00:00
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool SemaphoreImpl::LockMS(AuUInt64 uTimeout)
|
2023-03-12 15:27:28 +00:00
|
|
|
{
|
|
|
|
return LockNS(AuMSToNS<AuUInt64>(uTimeout));
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool SemaphoreImpl::LockNS(AuUInt64 uTimeout)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-06-26 07:33:47 +00:00
|
|
|
AuUInt64 uStart {};
|
|
|
|
AuUInt64 uEnd {};
|
|
|
|
|
2022-12-28 23:44:45 +00:00
|
|
|
if (this->TryLock())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
struct timespec tspec;
|
|
|
|
if (uTimeout != 0)
|
|
|
|
{
|
2023-06-26 07:33:47 +00:00
|
|
|
uStart = AuTime::SteadyClockNS();
|
|
|
|
uEnd = uStart + uTimeout;
|
|
|
|
|
2023-03-12 15:27:28 +00:00
|
|
|
Time::auabsns2ts(&tspec, uEnd);
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto old = this->value_;
|
|
|
|
//!tryLock (with old in a scope we can access)
|
|
|
|
while (!((old != 0) &&
|
|
|
|
(AuAtomicCompareExchange(&this->value_, old - 1, old) == old)))
|
|
|
|
{
|
|
|
|
if (uTimeout != 0)
|
|
|
|
{
|
2023-06-26 07:33:47 +00:00
|
|
|
if (Time::SteadyClockNS() >= uEnd)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret {};
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = futex_wait(&this->value_, 0, &tspec);
|
|
|
|
}
|
|
|
|
while (ret == EINTR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int ret {};
|
2023-03-12 20:59:03 +00:00
|
|
|
bool bStatus {};
|
|
|
|
|
2022-12-28 23:44:45 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if ((ret = futex_wait(&this->value_, 0)) == 0)
|
|
|
|
{
|
2023-03-12 20:59:03 +00:00
|
|
|
bStatus = true;
|
2022-12-28 23:44:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == EAGAIN || errno == EAGAIN)
|
|
|
|
{
|
2023-03-12 20:59:03 +00:00
|
|
|
bStatus = true;
|
2022-12-28 23:44:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
while (ret == EINTR);
|
|
|
|
|
2023-03-12 20:59:03 +00:00
|
|
|
RUNTIME_ASSERT_SHUTDOWN_SAFE(bStatus, "semaphore wait failed: {}", ret)
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
old = this->value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
void SemaphoreImpl::Lock()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-04-03 07:21:44 +00:00
|
|
|
auto status = LockNS(0);
|
2022-12-28 23:44:45 +00:00
|
|
|
SysAssert(status, "Couldn't lock semaphore");
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
void SemaphoreImpl::Unlock(long count)
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
|
|
|
AuAtomicAdd<AuUInt32>(&this->value_, count);
|
|
|
|
futex_wake(&this->value_, count);
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
void SemaphoreImpl::Unlock()
|
2022-12-28 23:44:45 +00:00
|
|
|
{
|
2023-06-13 10:33:29 +00:00
|
|
|
Unlock(1);
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM ISemaphore *SemaphoreNew(int iInitialCount)
|
|
|
|
{
|
2023-04-23 18:23:10 +00:00
|
|
|
return _new SemaphoreImpl(iInitialCount);
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void SemaphoreRelease(ISemaphore *pSemaphore)
|
|
|
|
{
|
2023-04-23 18:23:10 +00:00
|
|
|
AuSafeDelete<SemaphoreImpl *>(pSemaphore);
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
2023-03-21 03:18:09 +00:00
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, Semaphore, SemaphoreImpl)
|
2022-12-28 23:44:45 +00:00
|
|
|
}
|
|
|
|
#endif
|