Reece Wilson
51a2816f3f
[+] IConditionMutex::TryLock() ... Timed is still too much of an ask for this interface, i think. There's a good reason as to why this isn't a iwaitable. This condvar interface must be as common and primitive as possible.
81 lines
1.8 KiB
C++
81 lines
1.8 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_GENERICCM)
|
|
#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);
|
|
}
|
|
}
|
|
|
|
#endif |