Reece Wilson
72dc0d715e
[+] IOProcessor::WakeupThread [+] NT: Begin hacking in timeBeginPeriod (must spam it in some places) [+] ConsoleTTY (more specifically the win32 calls) are too slow to run on the mainthread. Delegate to worker. [*] AuTime.CurrentClockSteady [*] AuTime.CurrentClockSteadyMS [*] AuTime.CurrentClockSteadyNS
113 lines
2.5 KiB
C++
113 lines
2.5 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: AuMutex.Unix.cpp
|
|
Date: 2021-6-12
|
|
Author: Reece
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
#include "AuMutex.Generic.hpp"
|
|
|
|
#if !defined(_AURUNTIME_GENERICMUTEX)
|
|
#include "AuMutex.Unix.hpp"
|
|
#include <Source/Time/Time.hpp>
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
{
|
|
Mutex::Mutex()
|
|
{
|
|
auto status = pthread_mutex_init(&this->value_, nullptr) == 0;
|
|
SysAssert(status, "Mutex init failed");
|
|
}
|
|
|
|
Mutex::~Mutex()
|
|
{
|
|
int status = pthread_mutex_destroy(&this->value_);
|
|
RUNTIME_ASSERT_SHUTDOWN_SAFE(status == 0, "Mutex destruct failed, {} {}", status, errno);
|
|
}
|
|
|
|
bool Mutex::HasOSHandle(AuMach &mach)
|
|
{
|
|
mach = reinterpret_cast<AuMach>(&this->value_);
|
|
return true;
|
|
}
|
|
|
|
bool Mutex::TryLock()
|
|
{
|
|
return pthread_mutex_trylock(&this->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(&this->value_)) == 0)
|
|
{
|
|
return true;
|
|
}
|
|
} while (ret == EINTR);
|
|
|
|
RUNTIME_ASSERT_SHUTDOWN_SAFE(false, "mutex lock failed: {}", ret)
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
struct timespec tspec;
|
|
Time::ms2tsabsRealtime(&tspec, timeout);
|
|
|
|
int ret {};
|
|
|
|
do
|
|
{
|
|
ret = pthread_mutex_timedlock(&this->value_, &tspec);
|
|
|
|
if (ret == 0)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (ret == ETIMEDOUT)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
} while (ret == EINTR);
|
|
|
|
RUNTIME_ASSERT_SHUTDOWN_SAFE(false, "mutex timed lock failed: {}", ret)
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void Mutex::Unlock()
|
|
{
|
|
auto status = pthread_mutex_unlock(&this->value_) == 0;
|
|
SysAssert(status, "Mutex release error");
|
|
}
|
|
|
|
AUKN_SYM IWaitable *MutexNew()
|
|
{
|
|
return _new Mutex();
|
|
}
|
|
|
|
AUKN_SYM void MutexRelease(IWaitable *waitable)
|
|
{
|
|
AuSafeDelete<Mutex *>(waitable);
|
|
}
|
|
}
|
|
|
|
#endif |