Atmosphere/libraries/libstratosphere/source/os/os_busy_mutex.cpp

86 lines
2.6 KiB
C++
Raw Normal View History

2021-09-28 23:53:55 +02:00
/*
* Copyright (c) Atmosphère-NX
2021-09-28 23:53:55 +02:00
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
#include "impl/os_thread_manager.hpp"
#if defined(ATMOSPHERE_OS_HORIZON)
#include "impl/os_internal_busy_mutex_impl.os.horizon.hpp"
#else
/* Generic implementation already included in <stratosphere.hpp> */
2021-09-28 23:53:55 +02:00
#endif
namespace ams::os {
void InitializeBusyMutex(BusyMutexType *mutex) {
/* Create object. */
util::ConstructAt(mutex->_storage);
/* Set member variables. */
mutex->owner_thread = nullptr;
/* Mark initialized. */
mutex->state = BusyMutexType::State_Initialized;
}
void FinalizeBusyMutex(BusyMutexType *mutex) {
/* Check pre-conditions. */
AMS_ASSERT(mutex->state == BusyMutexType::State_Initialized);
AMS_ASSERT(!util::GetReference(mutex->_storage).IsLocked());
/* Mark not intialized. */
mutex->state = MutexType::State_NotInitialized;
/* Destroy object. */
util::DestroyAt(mutex->_storage);
}
void LockBusyMutex(BusyMutexType *mutex) {
/* Check pre-conditions. */
AMS_ASSERT(mutex->state == BusyMutexType::State_Initialized);
/* Lock mutex. */
util::GetReference(mutex->_storage).Lock();
/* Set owner thread. */
mutex->owner_thread = impl::GetCurrentThread();
}
bool TryLockBusyMutex(BusyMutexType *mutex) {
/* Check pre-conditions. */
AMS_ASSERT(mutex->state == BusyMutexType::State_Initialized);
/* Try to lock mutex. */
const bool locked = util::GetReference(mutex->_storage).TryLock();
/* Set owner thread. */
if (locked) {
mutex->owner_thread = impl::GetCurrentThread();
}
return locked;
}
void UnlockBusyMutex(BusyMutexType *mutex) {
/* Check pre-conditions. */
AMS_ASSERT(mutex->state == BusyMutexType::State_Initialized);
AMS_ASSERT(util::GetReference(mutex->_storage).IsLocked() && mutex->owner_thread == impl::GetCurrentThread());
/* Unlock. */
util::GetReference(mutex->_storage).Unlock();
}
}