J Reece Wilson
64cb7404ba
[+] Ability to bypass blocking limitation of certain io_submit reads, if the blocking subsystem is a pollable stream (ie: a pipe). [*] Fixed major Linux bug where LoopQueue items weren't being submitted, if no dequeues were in the same tick [*] Fix various Linux pipe related bugs [*] Fix futex bug where the callback was nulled on server-release
112 lines
2.3 KiB
C++
112 lines
2.3 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: Mutex.Unix.cpp
|
|
Date: 2021-6-12
|
|
Author: Reece
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
#include "Mutex.Generic.hpp"
|
|
|
|
#if !defined(_AURUNTIME_GENERICMUTEX)
|
|
#include "Mutex.Unix.hpp"
|
|
#include <Source/Time/Time.hpp>
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
{
|
|
Mutex::Mutex()
|
|
{
|
|
auto status = pthread_mutex_init(&value_, nullptr) == 0;
|
|
SysAssert(status, "Mutex init failed");
|
|
}
|
|
|
|
Mutex::~Mutex()
|
|
{
|
|
auto status = pthread_mutex_destroy(&value_);
|
|
SysAssert(status == 0, "Mutex destruct failed, {}", status);
|
|
}
|
|
|
|
bool Mutex::HasOSHandle(AuMach &mach)
|
|
{
|
|
mach = reinterpret_cast<AuMach>(&value_);
|
|
return true;
|
|
}
|
|
|
|
bool Mutex::TryLock()
|
|
{
|
|
return pthread_mutex_trylock(&value_) == 0;
|
|
}
|
|
|
|
bool Mutex::HasLockImplementation()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
void Mutex::Lock()
|
|
{
|
|
auto status = Lock(0);
|
|
SysAssert(status, "Couldn't lock Mutex object");
|
|
}
|
|
|
|
bool Mutex::Lock(AuUInt64 timeout)
|
|
{
|
|
if (timeout == 0)
|
|
{
|
|
int ret {};
|
|
do
|
|
{
|
|
if ((ret = pthread_mutex_lock(&value_)) == 0)
|
|
{
|
|
return true;
|
|
}
|
|
} while (ret == EINTR);
|
|
|
|
SysPanic("mutex lock failed {}", ret);
|
|
}
|
|
else
|
|
{
|
|
struct timespec tspec;
|
|
Time::ms2tsabs(&tspec, timeout);
|
|
|
|
int ret {};
|
|
|
|
do
|
|
{
|
|
ret = pthread_mutex_timedlock(&value_, &tspec);
|
|
|
|
if (ret == 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (ret == ETIMEDOUT)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
} while (ret == EINTR);
|
|
|
|
SysPanic("mutex timed lock failed {}", ret);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
void Mutex::Unlock()
|
|
{
|
|
auto status = pthread_mutex_unlock(&value_) == 0;
|
|
SysAssert(status, "Mutex release error");
|
|
}
|
|
|
|
AUKN_SYM IWaitable *MutexNew()
|
|
{
|
|
return _new Mutex();
|
|
}
|
|
|
|
AUKN_SYM void MutexRelease(IWaitable *waitable)
|
|
{
|
|
AuSafeDelete<Mutex *>(waitable);
|
|
}
|
|
}
|
|
|
|
#endif |