AuroraRuntime/Source/Threading/Primitives/Semaphore.NT.cpp

77 lines
1.8 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Semaphore.Win32.cpp
Date: 2021-6-12
Author: Reece
***/
2021-09-30 14:57:41 +00:00
#include <Source/RuntimeInternal.hpp>
2021-06-27 21:25:29 +00:00
#include "Semaphore.Generic.hpp"
2021-09-06 10:58:08 +00:00
#include "Semaphore.NT.hpp"
2021-06-27 21:25:29 +00:00
#if !defined(_AURUNTIME_GENERIC_SEMAPHORE)
namespace Aurora::Threading::Primitives
{
Semaphore::Semaphore(long intialValue)
{
value_ = CreateSemaphore(nullptr, intialValue, 1024, nullptr);
SysAssert(value_ != NULL, "Win32 CreateMutex Error {}", GetLastError());
}
Semaphore::~Semaphore()
{
CloseHandle(value_);
}
bool Semaphore::HasOSHandle(AuMach &mach)
{
mach = reinterpret_cast<AuMach>(value_);
return true;
}
bool Semaphore::HasLockImplementation()
{
return false;
}
bool Semaphore::TryLock()
{
auto status = WaitForSingleObject(value_, 0);
SysAssert(status != WAIT_FAILED, "Internal Win32 Error {}", GetLastError());
return status != WAIT_TIMEOUT; // WAIT_OBJECT_0, etc
}
bool Semaphore::Lock(AuUInt64 timeout)
{
SysAssertExp(!HasLockImplementation());
return WaitFor(this, timeout);
}
void Semaphore::Lock()
{
auto status = Lock(0);
SysAssert(status, "Couldn't lock semaphore");
}
void Semaphore::Unlock(long count)
{
auto status = ReleaseSemaphore(value_, count, nullptr);
SysAssert(status, "Internal Win32 Error {}", GetLastError());
}
void Semaphore::Unlock()
{
return Unlock(1);
}
AUKN_SYM ISemaphore *SemaphoreNew(int initialCount)
{
return _new Semaphore(initialCount);
}
AUKN_SYM void SemaphoreRelease(ISemaphore *waitable)
{
SafeDelete<Semaphore *>(waitable);
}
}
#endif