J Reece Wilson
2209aeb7a8
[*] BSD: Rewrote fundamentally flawed pthread_mutex class code to use MONOTONIC clock time [+] Linus SwInfo: Added enterprise check for RedHat
185 lines
4.4 KiB
C++
185 lines
4.4 KiB
C++
/***
|
|
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"
|
|
|
|
#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);
|
|
}
|
|
|
|
Semaphore::Semaphore(long intialValue) : value_(intialValue)
|
|
{
|
|
|
|
}
|
|
|
|
Semaphore::~Semaphore()
|
|
{
|
|
}
|
|
|
|
bool Semaphore::HasOSHandle(AuMach &mach)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool Semaphore::HasLockImplementation()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
bool Semaphore::TryLock()
|
|
{
|
|
auto old = this->value_;
|
|
return (old != 0 && AuAtomicCompareExchange(&this->value_, old - 1, old) == old);
|
|
}
|
|
|
|
bool Semaphore::Lock(AuUInt64 uTimeout)
|
|
{
|
|
if (this->TryLock())
|
|
{
|
|
return true;
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
AuUInt64 uStart = AuTime::SteadyClockMS();
|
|
AuUInt64 uEnd = uStart + uTimeout;
|
|
|
|
struct timespec tspec;
|
|
if (uTimeout != 0)
|
|
{
|
|
Time::ms2tsabs(&tspec, uTimeout);
|
|
}
|
|
|
|
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)
|
|
{
|
|
uStart = Time::SteadyClockMS();
|
|
if (uStart >= uEnd)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
int ret {};
|
|
do
|
|
{
|
|
ret = futex_wait(&this->value_, 0, &tspec);
|
|
|
|
if (ret == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (ret == EAGAIN || errno == EAGAIN)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (ret == ETIMEDOUT || errno == ETIMEDOUT)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
while (ret == EINTR);
|
|
|
|
RUNTIME_ASSERT_SHUTDOWN_SAFE(false, "semaphore timed wait failed: {}", ret)
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
int ret {};
|
|
do
|
|
{
|
|
if ((ret = futex_wait(&this->value_, 0)) == 0)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (ret == EAGAIN || errno == EAGAIN)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
}
|
|
while (ret == EINTR);
|
|
|
|
RUNTIME_ASSERT_SHUTDOWN_SAFE(false, "semaphore wait failed: {}", ret)
|
|
return false;
|
|
}
|
|
|
|
old = this->value_;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void Semaphore::Lock()
|
|
{
|
|
auto status = Lock(0);
|
|
SysAssert(status, "Couldn't lock semaphore");
|
|
}
|
|
|
|
void Semaphore::Unlock(long count)
|
|
{
|
|
AuAtomicAdd<AuUInt32>(&this->value_, count);
|
|
futex_wake(&this->value_, count);
|
|
}
|
|
|
|
void Semaphore::Unlock()
|
|
{
|
|
Unlock(0);
|
|
}
|
|
|
|
AUKN_SYM ISemaphore *SemaphoreNew(int iInitialCount)
|
|
{
|
|
return _new Semaphore(iInitialCount);
|
|
}
|
|
|
|
AUKN_SYM void SemaphoreRelease(ISemaphore *pSemaphore)
|
|
{
|
|
AuSafeDelete<Semaphore *>(pSemaphore);
|
|
}
|
|
}
|
|
#endif |