AuroraRuntime/Source/IO/Loop/LSSemaphore.NT.cpp

79 lines
1.9 KiB
C++
Raw Normal View History

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: LSSemaphore.NT.cpp
Date: 2021-10-1
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "LSSemaphore.hpp"
namespace Aurora::IO::Loop
{
2023-10-21 03:31:00 +00:00
LSSemaphore::LSSemaphore()
{}
LSSemaphore::LSSemaphore(HANDLE handle) : LSHandle(AuReinterpretCast<AuUInt>(handle))
{}
LSSemaphore::~LSSemaphore()
{
auto handle = AuReinterpretCast<HANDLE>(this->handle);
AuWin32CloseHandle(handle);
2022-04-16 13:01:33 +00:00
this->handle = kInvalidHandle;
}
bool LSSemaphore::AddOne()
{
LONG atomicOld;
return ::ReleaseSemaphore(AuReinterpretCast<HANDLE>(handle), 1, &atomicOld);
}
2023-10-22 05:11:39 +00:00
bool LSSemaphore::AddMany(AuUInt32 uCount)
{
LONG atomicOld;
return ::ReleaseSemaphore(AuReinterpretCast<HANDLE>(handle), uCount, &atomicOld);
}
bool LSSemaphore::IsSignaled()
{
return LSHandle::IsSignaled();
}
bool LSSemaphore::WaitOn(AuUInt32 timeout)
{
return LSHandle::WaitOn(timeout);
}
ELoopSource LSSemaphore::GetType()
{
return ELoopSource::eSourceSemaphore;
}
2023-10-21 03:31:00 +00:00
bool LSSemaphore::TryInit(AuUInt32 initialCount)
{
2023-12-19 03:43:11 +00:00
auto semaphore = ::CreateSemaphoreA(NULL, initialCount, /*AuNumericLimits<LONG>::max()*/ LONG_MAX, NULL);
2023-10-21 03:31:00 +00:00
if (!semaphore)
{
SysPushErrorGen("Out of OS resources?");
return {};
}
this->handle = (AuUInt)semaphore;
return true;
}
AUKN_SYM AuSPtr<ILSSemaphore> NewLSSemaphoreSlow(AuUInt32 initialCount)
{
AuSPtr<LSSemaphore> ret;
2023-12-19 03:43:11 +00:00
auto semaphore = ::CreateSemaphoreA(NULL, initialCount, /*AuNumericLimits<LONG>::max()*/ LONG_MAX, NULL);
if (!semaphore)
{
SysPushErrorGen("Out of OS resources?");
return {};
}
return AuMakeShared<LSSemaphore>(semaphore);
}
}