83 lines
1.9 KiB
C++
83 lines
1.9 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: AuConditionMutex.Unix.cpp
|
|
Date: 2021-6-12
|
|
Author: Reece
|
|
***/
|
|
#include <Source/RuntimeInternal.hpp>
|
|
#include "AuConditionMutex.Generic.hpp"
|
|
|
|
#if !defined(_AURUNTIME_GENERICCV) && !defined(AURORA_IS_LINUX_DERIVED)
|
|
#include <Source/Time/Time.hpp>
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
{
|
|
UnixConditionMutex::UnixConditionMutex()
|
|
{
|
|
auto status = pthread_mutex_init(&this->value_, nullptr) == 0;
|
|
SysAssert(status, "Mutex init failed");
|
|
}
|
|
|
|
UnixConditionMutex::~UnixConditionMutex()
|
|
{
|
|
int status = pthread_mutex_destroy(&this->value_);
|
|
RUNTIME_ASSERT_SHUTDOWN_SAFE(status == 0, "Mutex destruct failed, {} {}", status, errno);
|
|
}
|
|
|
|
void UnixConditionMutex::Lock()
|
|
{
|
|
int ret {};
|
|
do
|
|
{
|
|
if ((ret = pthread_mutex_lock(&this->value_)) == 0)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
while (ret == EINTR);
|
|
|
|
RUNTIME_ASSERT_SHUTDOWN_SAFE(false, "mutex lock failed: {}", ret)
|
|
}
|
|
|
|
bool UnixConditionMutex::TryLock()
|
|
{
|
|
int ret {};
|
|
|
|
do
|
|
{
|
|
if ((ret = pthread_mutex_trylock(&this->value_)) == 0)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
while (ret == EINTR);
|
|
|
|
return false;
|
|
}
|
|
|
|
void UnixConditionMutex::Unlock()
|
|
{
|
|
auto status = pthread_mutex_unlock(&this->value_) == 0;
|
|
SysAssert(status, "Mutex release error");
|
|
}
|
|
|
|
AuUInt UnixConditionMutex::GetOSHandle()
|
|
{
|
|
return AuMach(&this->value_);
|
|
}
|
|
|
|
AUKN_SYM IConditionMutex *ConditionMutexNew()
|
|
{
|
|
return _new UnixConditionMutex();
|
|
}
|
|
|
|
AUKN_SYM void ConditionMutexRelease(IConditionMutex *mutex)
|
|
{
|
|
AuSafeDelete<UnixConditionMutex *>(mutex);
|
|
}
|
|
|
|
AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, ConditionMutex, UnixConditionMutex)
|
|
}
|
|
|
|
#endif |