2021-10-02 10:28:49 +00:00
|
|
|
/***
|
|
|
|
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"
|
|
|
|
|
2022-06-11 23:52:46 +00:00
|
|
|
namespace Aurora::IO::Loop
|
2021-10-02 10:28:49 +00:00
|
|
|
{
|
2023-10-21 03:31:00 +00:00
|
|
|
LSSemaphore::LSSemaphore()
|
|
|
|
{}
|
|
|
|
|
2022-04-18 23:51:36 +00:00
|
|
|
LSSemaphore::LSSemaphore(HANDLE handle) : LSHandle(AuReinterpretCast<AuUInt>(handle))
|
2022-04-07 01:20:46 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
LSSemaphore::~LSSemaphore()
|
|
|
|
{
|
2022-04-18 23:51:36 +00:00
|
|
|
auto handle = AuReinterpretCast<HANDLE>(this->handle);
|
2022-04-07 01:20:46 +00:00
|
|
|
AuWin32CloseHandle(handle);
|
2022-04-16 13:01:33 +00:00
|
|
|
this->handle = kInvalidHandle;
|
2022-04-07 01:20:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 17:56:56 +00:00
|
|
|
bool LSSemaphore::AddOne()
|
2021-10-03 12:47:16 +00:00
|
|
|
{
|
|
|
|
LONG atomicOld;
|
2022-04-19 21:50:34 +00:00
|
|
|
return ::ReleaseSemaphore(AuReinterpretCast<HANDLE>(handle), 1, &atomicOld);
|
2021-10-03 12:47:16 +00:00
|
|
|
}
|
|
|
|
|
2023-10-22 05:11:39 +00:00
|
|
|
bool LSSemaphore::AddMany(AuUInt32 uCount)
|
|
|
|
{
|
|
|
|
LONG atomicOld;
|
|
|
|
return ::ReleaseSemaphore(AuReinterpretCast<HANDLE>(handle), uCount, &atomicOld);
|
|
|
|
}
|
|
|
|
|
2022-03-30 17:56:56 +00:00
|
|
|
bool LSSemaphore::IsSignaled()
|
2021-10-03 12:47:16 +00:00
|
|
|
{
|
|
|
|
return LSHandle::IsSignaled();
|
|
|
|
}
|
|
|
|
|
2022-04-12 21:26:15 +00:00
|
|
|
bool LSSemaphore::WaitOn(AuUInt32 timeout)
|
|
|
|
{
|
|
|
|
return LSHandle::WaitOn(timeout);
|
|
|
|
}
|
|
|
|
|
2022-03-30 17:56:56 +00:00
|
|
|
ELoopSource LSSemaphore::GetType()
|
2021-10-03 12:47:16 +00:00
|
|
|
{
|
|
|
|
return ELoopSource::eSourceSemaphore;
|
|
|
|
}
|
|
|
|
|
2023-10-21 03:31:00 +00:00
|
|
|
bool LSSemaphore::TryInit(AuUInt32 initialCount)
|
|
|
|
{
|
|
|
|
auto semaphore = ::CreateSemaphoreA(NULL, initialCount, AuNumericLimits<LONG>::max(), NULL);
|
|
|
|
if (!semaphore)
|
|
|
|
{
|
|
|
|
SysPushErrorGen("Out of OS resources?");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
this->handle = (AuUInt)semaphore;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-21 03:57:25 +00:00
|
|
|
#if 0
|
2021-10-03 12:47:16 +00:00
|
|
|
AUKN_SYM AuSPtr<ILSSemaphore> NewLSSemaphore(AuUInt32 initialCount)
|
|
|
|
{
|
2022-03-30 17:56:56 +00:00
|
|
|
AuSPtr<LSSemaphore> ret;
|
2021-10-03 12:47:16 +00:00
|
|
|
|
2022-07-08 21:54:49 +00:00
|
|
|
auto semaphore = ::CreateSemaphoreA(NULL, initialCount, AuNumericLimits<LONG>::max(), NULL);
|
|
|
|
if (!semaphore)
|
2021-10-03 12:47:16 +00:00
|
|
|
{
|
|
|
|
SysPushErrorGen("Out of OS resources?");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-07-08 21:54:49 +00:00
|
|
|
return AuMakeShared<LSSemaphore>(semaphore);
|
2021-10-03 12:47:16 +00:00
|
|
|
}
|
2023-10-21 03:57:25 +00:00
|
|
|
#endif
|
2021-10-02 10:28:49 +00:00
|
|
|
}
|