2021-06-27 21:25:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: Mutex.Unix.cpp
|
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2021-06-27 21:25:29 +00:00
|
|
|
#include "Mutex.Generic.hpp"
|
|
|
|
|
|
|
|
#if !defined(_AURUNTIME_GENERICMUTEX)
|
|
|
|
#include "Mutex.Unix.hpp"
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/Time/Time.hpp>
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
|
|
{
|
|
|
|
Mutex::Mutex()
|
|
|
|
{
|
|
|
|
auto status = pthread_mutex_init(&value_, nullptr) == 0;
|
|
|
|
SysAssert(status, "Mutex init failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
Mutex::~Mutex()
|
|
|
|
{
|
2022-08-09 06:48:29 +00:00
|
|
|
auto status = pthread_mutex_destroy(&value_);
|
|
|
|
SysAssert(status == 0, "Mutex destruct failed, {}", status);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-04-13 14:42:36 +00:00
|
|
|
int ret {};
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if ((ret = pthread_mutex_lock(&value_)) == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while (ret == EINTR);
|
|
|
|
|
|
|
|
SysPanic("mutex lock failed {}", ret);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct timespec tspec;
|
2021-10-03 12:43:37 +00:00
|
|
|
Time::ms2tsabs(&tspec, timeout);
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2022-04-13 15:06:26 +00:00
|
|
|
int ret {};
|
|
|
|
|
|
|
|
do
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-04-13 15:06:26 +00:00
|
|
|
ret = pthread_mutex_timedlock(&value_, &tspec);
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == ETIMEDOUT)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (ret == EINTR);
|
2021-06-27 21:25:29 +00:00
|
|
|
|
2022-04-13 15:06:26 +00:00
|
|
|
SysPanic("mutex timed lock failed {}", ret);
|
2021-06-27 21:25:29 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-03-22 03:53:08 +00:00
|
|
|
AuSafeDelete<Mutex *>(waitable);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 03:53:08 +00:00
|
|
|
#endif
|