[*] Optimize allocations out of RWLock

[*] Fix linux regression
This commit is contained in:
Reece Wilson 2022-12-29 09:42:02 +00:00
parent d14511429a
commit 0cdbc34c06
3 changed files with 22 additions and 27 deletions

View File

@ -369,9 +369,12 @@ static bool LinuxLockFutex(AuUInt32 *futex, AuUInt32 timeout)
int res = ::futex_wait(futex, old, timeout ? &tspec : nullptr);
if (res < 0)
{
if (res == -EAGAIN || errno == EAGAIN || errno == ETIMEDOUT)
if (res == ETIMEDOUT || errno == ETIMEDOUT)
{
return false;
}
else if (res == -EAGAIN || errno == EAGAIN)
{
//EAGAIN
bContended = true;
}
else
@ -382,7 +385,6 @@ static bool LinuxLockFutex(AuUInt32 *futex, AuUInt32 timeout)
}
else
{
// SUCCESS
bContended = false;
}
}

View File

@ -49,30 +49,20 @@ namespace Aurora::Threading::Primitives
}
}
RWLockImpl::RWLockImpl() : read_(*this), write_(*this)
RWLockImpl::RWLockImpl() :
read_(*this),
write_(*this),
condition_(AuUnsafeRaiiToShared(&this->mutex_))
{
}
RWLockImpl::~RWLockImpl()
{
this->mutex_.reset();
this->condition_.reset();
}
bool RWLockImpl::Init()
{
this->mutex_ = ConditionMutexUnique();
if (!this->mutex_)
{
return false;
}
this->condition_ = ConditionVariableUnique(AuUnsafeRaiiToShared(mutex_));
if (!this->condition_)
{
return false;
}
return true;
}
@ -122,14 +112,14 @@ namespace Aurora::Threading::Primitives
iCurState = this->state_;
if (iCurState < 0)
{
if (!this->condition_->WaitForSignal(timeout))
if (!this->condition_.WaitForSignal(timeout))
{
return false;
}
if (this->writersPending_)
{
this->condition_->Broadcast();
this->condition_.Broadcast();
continue;
}
}
@ -155,7 +145,7 @@ namespace Aurora::Threading::Primitives
while (this->state_ != 0)
{
if (!this->condition_->WaitForSignal(timeout))
if (!this->condition_.WaitForSignal(timeout))
{
this->writersPending_--;
return false;
@ -208,12 +198,12 @@ namespace Aurora::Threading::Primitives
if ((val == 1) && (this->bElevaterPending_))
{
this->condition_->Signal();
this->condition_.Signal();
}
if (val == 0)
{
this->condition_->Signal();
this->condition_.Signal();
}
}
@ -221,7 +211,7 @@ namespace Aurora::Threading::Primitives
{
AU_LOCK_GUARD(this->mutex_);
this->state_ = 0;
this->condition_->Broadcast();
this->condition_.Broadcast();
this->reentrantWriteLockHandle_ = 0;
}
@ -233,7 +223,7 @@ namespace Aurora::Threading::Primitives
{
this->bElevaterPending_ = true;
if (!this->condition_->WaitForSignal(timeout))
if (!this->condition_.WaitForSignal(timeout))
{
return false;
}
@ -255,7 +245,7 @@ namespace Aurora::Threading::Primitives
}
this->state_ = 1;
this->condition_->Broadcast();
this->condition_.Broadcast();
return true;
}

View File

@ -7,6 +7,9 @@
***/
#pragma once
#include "AuConditionVariable.Generic.hpp"
#include "AuConditionMutex.Generic.hpp"
namespace Aurora::Threading::Primitives
{
struct RWLockImpl;
@ -72,8 +75,8 @@ namespace Aurora::Threading::Primitives
RWLockAccessView<true> read_;
RWLockAccessView<false> write_;
ConditionMutexUnique_t mutex_;
ConditionVariableUnique_t condition_;
ConditionMutexImpl mutex_;
ConditionVariableImpl condition_;
AuInt32 state_ {};
AuInt32 writersPending_ {};
bool bElevaterPending_ {};