[*] Always use unsigned integers under the semaphore classes

This commit is contained in:
Reece Wilson 2023-08-18 15:25:54 +01:00 committed by Jamie Reece Wilson
parent 2a1556d80c
commit 7dd6145dc1
9 changed files with 34 additions and 39 deletions

View File

@ -11,8 +11,8 @@ namespace Aurora::Threading::Primitives
{
struct ISemaphore : IWaitable
{
virtual void Unlock(long count) = 0;
virtual void Unlock(AuUInt16 count) = 0;
};
AUKN_SHARED_SOO_NCM(Semaphore, ISemaphore, kPrimitiveSizeSemaphore, int iInitialCount = 0);
AUKN_SHARED_SOO_NCM(Semaphore, ISemaphore, kPrimitiveSizeSemaphore, AuUInt16 uInitialCount = 0);
}

View File

@ -48,7 +48,7 @@ namespace Aurora::Threading::Primitives
SysAssert(status, "Couldn't lock semaphore");
}
void SemaphoreImpl::Unlock(long count)
void SemaphoreImpl::Unlock(AuUInt16 count)
{
value_.fetch_add(count);
}

View File

@ -29,7 +29,7 @@
bool TryLock() override;
bool Lock(AuUInt64 timeout) override;
void Lock() override;
void Unlock(long count) override;
void Unlock(AuUInt16 count) override;
void Unlock() override;
private:

View File

@ -14,7 +14,7 @@
namespace Aurora::Threading::Primitives
{
SemaphoreImpl::SemaphoreImpl(long intialValue) : dwState_(intialValue)
SemaphoreImpl::SemaphoreImpl(AuUInt16 uIntialValue) : dwState_(uIntialValue)
{
}
@ -133,7 +133,7 @@ namespace Aurora::Threading::Primitives
SysAssert(status, "Couldn't lock semaphore");
}
void SemaphoreImpl::Unlock(long count)
void SemaphoreImpl::Unlock(AuUInt16 count)
{
AuAtomicAdd<AuUInt32>(&this->dwState_, count);
if (this->dwSleeping_)
@ -147,9 +147,9 @@ namespace Aurora::Threading::Primitives
Unlock(1);
}
AUKN_SYM ISemaphore *SemaphoreNew(int iInitialCount)
AUKN_SYM ISemaphore *SemaphoreNew(AuUInt16 uInitialCount)
{
return _new SemaphoreImpl(iInitialCount);
return _new SemaphoreImpl(uInitialCount);
}
AUKN_SYM void SemaphoreRelease(ISemaphore *pSemaphore)

View File

@ -11,7 +11,7 @@ namespace Aurora::Threading::Primitives
{
struct SemaphoreImpl : ISemaphore
{
SemaphoreImpl(long intialValue = 0);
SemaphoreImpl(AuUInt16 intialValue = 0);
~SemaphoreImpl();
bool TryLockNoSpin();
@ -21,7 +21,7 @@ namespace Aurora::Threading::Primitives
bool LockMS(AuUInt64 timeout) override;
bool LockNS(AuUInt64 timeout) override;
void Lock() override;
void Unlock(long count) override;
void Unlock(AuUInt16 count) override;
void Unlock() override;
private:

View File

@ -15,15 +15,10 @@
namespace Aurora::Threading::Primitives
{
SemaphoreImpl::SemaphoreImpl(long iIntialValue) :
SemaphoreImpl::SemaphoreImpl(AuUInt16 uIntialValue) :
var(AuUnsafeRaiiToShared(&this->mutex))
{
this->value_ = iIntialValue;
if (!pWaitOnAddress && !pNtCreateKeyedEvent)
{
InitProcAddresses();
}
this->dwState_ = uIntialValue;
}
SemaphoreImpl::~SemaphoreImpl()
@ -43,8 +38,8 @@ namespace Aurora::Threading::Primitives
bool SemaphoreImpl::TryLockNoSpin()
{
auto old = this->value_;
return (old != 0 && AuAtomicCompareExchange(&this->value_, old - 1, old) == old);
auto old = this->dwState_;
return (old != 0 && AuAtomicCompareExchange(&this->dwState_, old - 1, old) == old);
}
bool SemaphoreImpl::TryLock()
@ -73,17 +68,17 @@ namespace Aurora::Threading::Primitives
if (gUseNativeWaitSemapahore)
{
AuUInt32 uYieldCounter {};
auto old = this->value_;
auto old = this->dwState_;
//!tryLock (with old in a scope we can access)
while (!((old != 0) &&
(AuAtomicCompareExchange(&this->value_, old - 1, old) == old)))
(AuAtomicCompareExchange(&this->dwState_, old - 1, old) == old)))
{
if (!InternalLTSWaitOnAddressHighRes(&this->value_, &old, sizeof(this->value_), uEnd))
if (!InternalLTSWaitOnAddressHighRes(&this->dwState_, &old, sizeof(this->dwState_), uEnd))
{
return false;
}
old = this->value_;
old = this->dwState_;
}
return true;
@ -123,24 +118,24 @@ namespace Aurora::Threading::Primitives
SysAssert(status, "Couldn't lock semaphore");
}
void SemaphoreImpl::Unlock(long count)
void SemaphoreImpl::Unlock(AuUInt16 count)
{
if (gUseNativeWaitSemapahore)
{
AuAtomicAdd<AuInt32>(&this->value_, count);
AuAtomicAdd<AuUInt32>(&this->dwState_, count);
if (count == 1)
{
pWakeByAddressSingle(&this->value_);
pWakeByAddressSingle(&this->dwState_);
}
else
{
pWakeByAddressAll(&this->value_);
pWakeByAddressAll(&this->dwState_);
}
}
else
{
this->mutex.Lock(); // do not remove
AuAtomicAdd<AuInt32>(&this->value_, count);
AuAtomicAdd<AuUInt32>(&this->dwState_, count);
this->mutex.Unlock();
if (count == 1)
@ -163,9 +158,9 @@ namespace Aurora::Threading::Primitives
return Unlock(1);
}
AUKN_SYM ISemaphore *SemaphoreNew(int iInitialCount)
AUKN_SYM ISemaphore *SemaphoreNew(AuUInt16 uIntialValue)
{
return _new SemaphoreImpl(iInitialCount);
return _new SemaphoreImpl(uIntialValue);
}
AUKN_SYM void SemaphoreRelease(ISemaphore *pSemaphore)

View File

@ -14,7 +14,7 @@ namespace Aurora::Threading::Primitives
{
struct SemaphoreImpl : ISemaphore
{
SemaphoreImpl(long iIntialValue = 0);
SemaphoreImpl(AuUInt16 uIntialValue = 0);
~SemaphoreImpl();
bool HasOSHandle(AuMach &mach) override;
@ -23,13 +23,13 @@ namespace Aurora::Threading::Primitives
bool LockMS(AuUInt64 timeout) override;
bool LockNS(AuUInt64 timeout) override;
void Lock() override;
void Unlock(long count) override;
void Unlock(AuUInt16 count) override;
void Unlock() override;
auline bool TryLockNoSpin();
private:
AuInt32 value_ {};
AuUInt32 dwState_ {};
ConditionMutexImpl mutex;
ConditionVariableImpl var;
};

View File

@ -14,7 +14,7 @@
namespace Aurora::Threading::Primitives
{
SemaphoreImpl::SemaphoreImpl(long intialValue) : value_(intialValue)
SemaphoreImpl::SemaphoreImpl(AuUInt16 uIntialValue) : value_(intialValue)
{
pthread_condattr_t attr;
@ -132,7 +132,7 @@ namespace Aurora::Threading::Primitives
SysAssert(status, "Couldn't lock semaphore");
}
void SemaphoreImpl::Unlock(long count)
void SemaphoreImpl::Unlock(AuUInt16 count)
{
{
AU_LOCK_GUARD(this->mutex_);
@ -156,9 +156,9 @@ namespace Aurora::Threading::Primitives
Unlock(1);
}
AUKN_SYM ISemaphore *SemaphoreNew(int iInitialCount)
AUKN_SYM ISemaphore *SemaphoreNew(AuUInt16 uIntialValue)
{
return _new SemaphoreImpl(iInitialCount);
return _new SemaphoreImpl(uIntialValue);
}
AUKN_SYM void SemaphoreRelease(ISemaphore *pSemaphore)

View File

@ -13,7 +13,7 @@ namespace Aurora::Threading::Primitives
{
struct SemaphoreImpl : ISemaphore
{
SemaphoreImpl(long intialValue = 0);
SemaphoreImpl(AuUInt16 intialValue = 0);
~SemaphoreImpl();
bool HasOSHandle(AuMach &mach) override;
@ -23,7 +23,7 @@ namespace Aurora::Threading::Primitives
bool LockMS(AuUInt64 timeout) override;
bool LockNS(AuUInt64 timeout) override;
void Lock() override;
void Unlock(long count) override;
void Unlock(AuUInt16 count) override;
void Unlock() override;
private: