AuroraRuntime/Source/Threading/Primitives/AuMutex.Generic.cpp

71 lines
1.3 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Mutex.Generic.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuMutex.Generic.hpp"
#if defined(_AURUNTIME_GENERICMUTEX)
namespace Aurora::Threading::Primitives
{
MutexImpl::MutexImpl()
{
value_ = 0;
}
MutexImpl::~MutexImpl()
{
value_ = 0;
}
bool MutexImpl::HasOSHandle(AuMach &mach)
{
return false;
}
bool MutexImpl::TryLock()
{
#if defined(_AU_HAS_ATOMIC_INTRINS)
return _interlockedbittestandset(&value_, 0) == 0;
#else
return value_.fetch_or(1);
#endif
}
bool MutexImpl::HasLockImplementation()
{
return false;
}
void MutexImpl::Lock()
{
auto status = Lock(0);
SysAssert(status, "Couldn't lock Mutex object");
}
bool MutexImpl::Lock(AuUInt64 timeout)
{
SysAssertExp(!HasLockImplementation());
return Aurora::Threading::WaitFor(this, timeout);
}
void MutexImpl::Unlock()
{
value_ = 0;
}
AUKN_SYM IWaitable *MutexNew()
{
return _new Mutex();
}
AUKN_SYM void MutexRelease(IWaitable *pMutex)
{
AuSafeDelete<Mutex *>(pMutex);
}
}
#endif