os: implement SdkRecursiveMutex

This commit is contained in:
Michael Scire 2021-09-29 14:56:53 -07:00
parent c949779b3d
commit b25218c918
7 changed files with 226 additions and 22 deletions

View file

@ -45,4 +45,58 @@ namespace ams::os {
return status == ConditionVariableStatus::Success;
}
void SdkConditionVariableType::Wait(SdkRecursiveMutexType &mutex) {
/* Check preconditions. */
AMS_ABORT_UNLESS(os::IsSdkRecursiveMutexLockedByCurrentThread(std::addressof(mutex)));
AMS_ABORT_UNLESS(mutex.recursive_count == 1);
/* Decrement the mutex's recursive count. */
--mutex.recursive_count;
/* Wait on the mutex. */
GetReference(this->_storage).Wait(GetPointer(mutex._storage));
/* Increment the mutex's recursive count. */
++mutex.recursive_count;
/* Check that the mutex's recursive count is valid. */
AMS_ABORT_UNLESS(mutex.recursive_count != 0);
}
bool SdkConditionVariableType::TimedWait(SdkRecursiveMutexType &mutex, TimeSpan timeout) {
/* Check preconditions. */
AMS_ASSERT(timeout.GetNanoSeconds() >= 0);
AMS_ABORT_UNLESS(os::IsSdkRecursiveMutexLockedByCurrentThread(std::addressof(mutex)));
/* Handle zero timeout by unlocking and re-locking. */
if (timeout == TimeSpan(0)) {
/* NOTE: Nintendo doesn't check recursive_count here...seems really suspicious? */
/* Not sure that this is correct, or if they just forgot to check. */
GetReference(mutex._storage).Leave();
GetReference(mutex._storage).Enter();
return false;
}
/* Check that the mutex is held exactly once. */
AMS_ABORT_UNLESS(mutex.recursive_count == 1);
/* Decrement the mutex's recursive count. */
--mutex.recursive_count;
/* Create timeout helper. */
impl::TimeoutHelper timeout_helper(timeout);
/* Perform timed wait. */
auto status = GetReference(this->_storage).TimedWait(GetPointer(mutex._storage), timeout_helper);
/* Increment the mutex's recursive count. */
++mutex.recursive_count;
/* Check that the mutex's recursive count is valid. */
AMS_ABORT_UNLESS(mutex.recursive_count != 0);
/* Return whether we succeeded. */
return status == ConditionVariableStatus::Success;
}
}