2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-12-09 11:57:37 +00: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2020-04-08 09:21:35 +00:00
|
|
|
#include <stratosphere/os/os_mutex_common.hpp>
|
|
|
|
#include <stratosphere/os/os_mutex_types.hpp>
|
|
|
|
#include <stratosphere/os/os_mutex_api.hpp>
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
namespace ams::os {
|
|
|
|
|
|
|
|
class Mutex {
|
|
|
|
NON_COPYABLE(Mutex);
|
|
|
|
NON_MOVEABLE(Mutex);
|
|
|
|
private:
|
2020-04-08 09:21:35 +00:00
|
|
|
MutexType mutex;
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2020-04-08 09:21:35 +00:00
|
|
|
constexpr explicit Mutex(bool recursive) : mutex{::ams::os::MutexType::State_Initialized, recursive, 0, 0, nullptr, {{0}}} { /* ... */ }
|
|
|
|
|
|
|
|
~Mutex() { FinalizeMutex(std::addressof(this->mutex)); }
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
void lock() {
|
2020-04-08 09:21:35 +00:00
|
|
|
return LockMutex(std::addressof(this->mutex));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void unlock() {
|
2020-04-08 09:21:35 +00:00
|
|
|
return UnlockMutex(std::addressof(this->mutex));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool try_lock() {
|
2020-04-08 09:21:35 +00:00
|
|
|
return TryLockMutex(std::addressof(this->mutex));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
bool IsLockedByCurrentThread() const {
|
|
|
|
return IsMutexLockedByCurrentThread(std::addressof(this->mutex));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
ALWAYS_INLINE void Lock() {
|
|
|
|
return this->lock();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
ALWAYS_INLINE void Unlock() {
|
|
|
|
return this->unlock();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
ALWAYS_INLINE bool TryLock() {
|
|
|
|
return this->try_lock();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
operator MutexType &() {
|
|
|
|
return this->mutex;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
operator const MutexType &() const {
|
|
|
|
return this->mutex;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
MutexType *GetBase() {
|
|
|
|
return std::addressof(this->mutex);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|