2021-06-27 21:25:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: Mutex.Generic.cpp
|
|
|
|
Date: 2021-6-12
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2022-11-17 07:46:07 +00:00
|
|
|
#include "AuMutex.Generic.hpp"
|
2021-06-27 21:25:29 +00:00
|
|
|
|
|
|
|
#if defined(_AURUNTIME_GENERICMUTEX)
|
|
|
|
|
|
|
|
namespace Aurora::Threading::Primitives
|
|
|
|
{
|
2023-04-23 18:23:10 +00:00
|
|
|
MutexImpl::MutexImpl()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
value_ = 0;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
MutexImpl::~MutexImpl()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
value_ = 0;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool MutexImpl::HasOSHandle(AuMach &mach)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool MutexImpl::TryLock()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
#if defined(_AU_HAS_ATOMIC_INTRINS)
|
|
|
|
return _interlockedbittestandset(&value_, 0) == 0;
|
|
|
|
#else
|
|
|
|
return value_.fetch_or(1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool MutexImpl::HasLockImplementation()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
void MutexImpl::Lock()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
auto status = Lock(0);
|
|
|
|
SysAssert(status, "Couldn't lock Mutex object");
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
bool MutexImpl::Lock(AuUInt64 timeout)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
SysAssertExp(!HasLockImplementation());
|
|
|
|
return Aurora::Threading::WaitFor(this, timeout);
|
|
|
|
}
|
|
|
|
|
2023-04-23 18:23:10 +00:00
|
|
|
void MutexImpl::Unlock()
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
|
|
|
value_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM IWaitable *MutexNew()
|
|
|
|
{
|
|
|
|
return _new Mutex();
|
|
|
|
}
|
|
|
|
|
2022-11-17 07:46:07 +00:00
|
|
|
AUKN_SYM void MutexRelease(IWaitable *pMutex)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-11-17 07:46:07 +00:00
|
|
|
AuSafeDelete<Mutex *>(pMutex);
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|