AuroraRuntime/Source/Threading/Primitives/AuWoASemaphore.Unix.hpp

80 lines
2.1 KiB
C++

/***
Copyright (C) 2021-2024 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuWoASemaphore.Unix.hpp
Date: 2021-6-12
Date: 2023-12-29
Author: Reece
Note: Implements the basic primitives for use when the generic WoA implementations of thread primitives are incorporated into a given target platform
(Win9x - NT 2000 + Xbox [360] kernel objects, iOS/libdispatch semaphores, Nintendo Switch (NX semaphores), PlayStation X (kqueue yielding or not-pthread ~p~sceThreads perhaps?), etc)
***/
#pragma once
namespace Aurora::Threading::Primitives
{
// Moved from ./AuConditionMutex.Unix.[c/h]pp
// now ./_removed/AuConditionMutex.Unix.[c/h]pp.bak
struct WoAConditionMutex final
{
WoAConditionMutex();
~WoAConditionMutex();
bool TryLock();
void Lock();
void Unlock();
AuUInt GetOSHandle();
inline WoAConditionMutex *operator ->()
{
return this;
}
private:
pthread_mutex_t value_;
};
struct WoAConditionVariable final
{
WoAConditionVariable();
~WoAConditionVariable();
bool WaitForSignalNsEx(WoAConditionMutex *pMutex,
AuUInt64 qwTimeout,
bool bSpin = true);
void Signal();
void Broadcast();
inline WoAConditionVariable *operator ->()
{
return this;
}
private:
friend struct WoAConditionMutex;
pthread_cond_t pthreadCv_;
};
struct WoASemaphoreImpl final
{
WoASemaphoreImpl(AuUInt16 intialValue = 0);
~WoASemaphoreImpl();
bool HasOSHandle(AuMach &mach);
bool HasLockImplementation();
auline bool TryLockNoSpin();
bool TryLock() override;
bool LockMS(AuUInt64 timeout);
bool LockNS(AuUInt64 timeout);
void Lock();
void Unlock(AuUInt16 count);
void Unlock();
inline WoASemaphoreImpl *operator ->()
{
return this;
}
private:
AuInt32 value_ {};
WoAConditionMutex mutex_;
WoAConditionVariable cond_;
};
}