[+] ILSSemaphore::AddMany

This commit is contained in:
Reece Wilson 2023-10-22 06:11:39 +01:00
parent 3d01d6cc8f
commit 591c0fe48b
10 changed files with 62 additions and 27 deletions

View File

@ -27,6 +27,7 @@ namespace Aurora::IO::Loop
struct ILSSemaphore : virtual ILoopSource
{
virtual bool AddOne() = 0;
virtual bool AddMany(AuUInt32 uCount) = 0;
};
struct ILSEvent : virtual ILoopSource

View File

@ -205,6 +205,11 @@ namespace Aurora::IO::IPC
return this->semaphore_.AddOne();
}
bool IPCSemaphoreProxy::AddMany(AuUInt32 uCount)
{
return this->semaphore_.AddMany(uCount);
}
bool IPCSemaphoreProxy::IsSignaled()
{
return this->semaphore_.IsSignaled();

View File

@ -59,6 +59,7 @@ namespace Aurora::IO::IPC
IMPLEMENT_HANDLE
bool AddOne() override;
bool AddMany(AuUInt32 uCount) override;
bool IsSignaled() override;
bool WaitOn(AuUInt32 timeout) override;

View File

@ -267,6 +267,8 @@ namespace Aurora::IO::IPC
IMPLEMENT_HANDLE
bool AddOne() override;
bool AddMany(AuUInt32 uCount) override;
bool IsSignaled() override;
bool WaitOn(AuUInt32 timeout) override;
@ -292,6 +294,11 @@ namespace Aurora::IO::IPC
return this->semaphore_.AddOne();
}
bool IPCSemaphoreProxy::AddMany(AuUInt32 uCount)
{
return this->semaphore_.AddMany(uCount);
}
bool IPCSemaphoreProxy::IsSignaled()
{
return this->semaphore_.IsSignaled();

View File

@ -23,13 +23,13 @@ namespace Aurora::IO::Loop
bool LSLocalSemaphore::TryInit(AuUInt32 initialCount)
{
if (!LSSemaphore::TryInit(1))
if (!LSSemaphore::TryInit(initialCount))
{
return false;
}
this->uAtomicSemaphore = initialCount;
this->uAtomicWord = 1;
this->uAtomicKernelSemaphore = initialCount;
return true;
}
@ -40,25 +40,22 @@ namespace Aurora::IO::Loop
while (true)
{
auto uOld = this->uAtomicWord;
auto uOld = this->uAtomicKernelSemaphore;
if (uOld == 0)
{
break;
}
if (AuAtomicCompareExchange(&this->uAtomicWord, uOld - 1, uOld) == uOld)
if (AuAtomicCompareExchange(&this->uAtomicKernelSemaphore, uOld - 1, uOld) == uOld)
{
auto uCount = AuAtomicLoad(&this->uAtomicSemaphore);
if (uOld - 1 == 0 &&
uCount)
{
AuAtomicAdd(&this->uAtomicWord, uCount);
for (AU_ITERATE_N(i, uCount))
{
LSSemaphore::AddOne();
}
AuAtomicAdd(&this->uAtomicKernelSemaphore, uCount);
LSSemaphore::AddMany(uCount);
}
break;
@ -72,16 +69,30 @@ namespace Aurora::IO::Loop
{
auto uNext = AuAtomicAdd(&this->uAtomicSemaphore, 1u);
if (AuAtomicLoad(&this->uAtomicWord) >= uNext)
if (AuAtomicLoad(&this->uAtomicKernelSemaphore) >= uNext)
{
return true;
}
AuAtomicAdd(&this->uAtomicWord, 1u);
AuAtomicAdd(&this->uAtomicKernelSemaphore, 1u);
LSSemaphore::AddOne();
return true;
}
bool LSLocalSemaphore::AddMany(AuUInt32 uCount)
{
auto uNext = AuAtomicAdd(&this->uAtomicSemaphore, 1u);
if (AuAtomicLoad(&this->uAtomicKernelSemaphore) >= uNext)
{
return true;
}
AuAtomicAdd(&this->uAtomicKernelSemaphore, uCount);
LSSemaphore::AddMany(uCount);
return true;
}
bool LSLocalSemaphore::IsSignaled()
{
return this->TryTake();
@ -165,25 +176,12 @@ namespace Aurora::IO::Loop
void LSLocalSemaphore::OnPresleep()
{
#if 0
while (auto uActiveSemaphore = AuAtomicLoad(&this->uAtomicSemaphore))
{
if (AuAtomicLoad(&this->uAtomicWord) >= uActiveSemaphore)
{
return;
}
AuAtomicAdd(&this->uAtomicWord, uActiveSemaphore);
for (AU_ITERATE_N(i, uActiveSemaphore))
{
LSSemaphore::AddOne();
}
}
#endif
}
void LSLocalSemaphore::OnFinishSleep()
{
}
AUKN_SYM AuSPtr<ILSSemaphore> NewLSSemaphore(AuUInt32 initialCount)

View File

@ -23,6 +23,7 @@ namespace Aurora::IO::Loop
ELoopSource GetType() override;
bool AddOne() override;
bool AddMany(AuUInt32 uCount);
virtual bool OnTrigger(AuUInt handle) override;
@ -35,6 +36,6 @@ namespace Aurora::IO::Loop
void OnFinishSleep() override;
AuAUInt32 uAtomicSemaphore {};
AuAUInt32 uAtomicWord {};
AuAUInt32 uAtomicKernelSemaphore {};
};
}

View File

@ -54,7 +54,20 @@ namespace Aurora::IO::Loop
bool LSSemaphore::AddOne()
{
AuUInt64 plsNoOverflow {1};
AuUInt64 plsNoOverflow { 1 };
if (::write(this->handle, &plsNoOverflow, sizeof(plsNoOverflow)) != 8)
{
// todo push error
return false;
}
return true;
}
bool LSSemaphore::AddMany(AuUInt32 uCount)
{
AuUInt64 plsNoOverflow { uCount };
if (::write(this->handle, &plsNoOverflow, sizeof(plsNoOverflow)) != 8)
{

View File

@ -20,6 +20,8 @@ namespace Aurora::IO::Loop
bool TryInit(AuUInt32 initialCount = 0);
bool AddOne() override;
bool AddMany(AuUInt32 uCount) override;
virtual bool OnTrigger(AuUInt handle) override;

View File

@ -29,6 +29,12 @@ namespace Aurora::IO::Loop
return ::ReleaseSemaphore(AuReinterpretCast<HANDLE>(handle), 1, &atomicOld);
}
bool LSSemaphore::AddMany(AuUInt32 uCount)
{
LONG atomicOld;
return ::ReleaseSemaphore(AuReinterpretCast<HANDLE>(handle), uCount, &atomicOld);
}
bool LSSemaphore::IsSignaled()
{
return LSHandle::IsSignaled();

View File

@ -19,6 +19,7 @@ namespace Aurora::IO::Loop
bool TryInit(AuUInt32 initialCount = 0);
bool AddOne() override;
bool AddMany(AuUInt32 uCount) override;
bool ILSSemaphore::IsSignaled() override;
bool ILSSemaphore::WaitOn(AuUInt32 timeout) override;