mirror of
https://github.com/Atmosphere-NX/Atmosphere-libs.git
synced 2025-06-21 02:52:44 +02:00
stratosphere: Use RAII for locks
This renames the Mutex class member functions so that the mutex types satisfy Lockable. This makes them usable with standard std::scoped_lock and std::unique_lock, which lets us use RAII and avoids the need for a custom RAII wrapper :)
This commit is contained in:
parent
8e29e70a61
commit
f4212ff213
@ -9,15 +9,15 @@ class HosMutex {
|
||||
mutexInit(&this->m);
|
||||
}
|
||||
|
||||
void Lock() {
|
||||
void lock() {
|
||||
mutexLock(&this->m);
|
||||
}
|
||||
|
||||
void Unlock() {
|
||||
void unlock() {
|
||||
mutexUnlock(&this->m);
|
||||
}
|
||||
|
||||
bool TryLock() {
|
||||
bool try_lock() {
|
||||
return mutexTryLock(&this->m);
|
||||
}
|
||||
};
|
||||
@ -30,15 +30,15 @@ class HosRecursiveMutex {
|
||||
rmutexInit(&this->m);
|
||||
}
|
||||
|
||||
void Lock() {
|
||||
void lock() {
|
||||
rmutexLock(&this->m);
|
||||
}
|
||||
|
||||
void Unlock() {
|
||||
void unlock() {
|
||||
rmutexUnlock(&this->m);
|
||||
}
|
||||
|
||||
bool TryLock() {
|
||||
bool try_lock() {
|
||||
return rmutexTryLock(&this->m);
|
||||
}
|
||||
};
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
|
||||
#include <stratosphere/multithreadedwaitablemanager.hpp>
|
||||
|
||||
@ -20,11 +21,10 @@ void MultiThreadedWaitableManager::process_until_timeout() {
|
||||
}
|
||||
|
||||
void MultiThreadedWaitableManager::add_waitable(IWaitable *waitable) {
|
||||
this->lock.Lock();
|
||||
std::scoped_lock lk{this->lock};
|
||||
this->to_add_waitables.push_back(waitable);
|
||||
waitable->set_manager(this);
|
||||
this->new_waitable_event->signal_event();
|
||||
this->lock.Unlock();
|
||||
}
|
||||
|
||||
|
||||
@ -33,7 +33,7 @@ IWaitable *MultiThreadedWaitableManager::get_waitable() {
|
||||
|
||||
int handle_index = 0;
|
||||
Result rc;
|
||||
this->get_waitable_lock.Lock();
|
||||
std::scoped_lock lk{this->get_waitable_lock};
|
||||
while (1) {
|
||||
/* Sort waitables by priority. */
|
||||
std::sort(this->waitables.begin(), this->waitables.end(), IWaitable::compare);
|
||||
@ -71,7 +71,6 @@ IWaitable *MultiThreadedWaitableManager::get_waitable() {
|
||||
w->handle_signaled(0);
|
||||
this->waitables.push_back(w);
|
||||
} else {
|
||||
this->get_waitable_lock.Unlock();
|
||||
return w;
|
||||
}
|
||||
}
|
||||
@ -81,10 +80,9 @@ IWaitable *MultiThreadedWaitableManager::get_waitable() {
|
||||
Result MultiThreadedWaitableManager::add_waitable_callback(void *arg, Handle *handles, size_t num_handles, u64 timeout) {
|
||||
MultiThreadedWaitableManager *this_ptr = (MultiThreadedWaitableManager *)arg;
|
||||
svcClearEvent(handles[0]);
|
||||
this_ptr->lock.Lock();
|
||||
std::scoped_lock lk{this_ptr->lock};
|
||||
this_ptr->waitables.insert(this_ptr->waitables.end(), this_ptr->to_add_waitables.begin(), this_ptr->to_add_waitables.end());
|
||||
this_ptr->to_add_waitables.clear();
|
||||
this_ptr->lock.Unlock();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -2,15 +2,15 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
|
||||
#include <stratosphere/waitablemanager.hpp>
|
||||
|
||||
void WaitableManager::add_waitable(IWaitable *waitable) {
|
||||
this->lock.Lock();
|
||||
std::scoped_lock lk{this->lock};
|
||||
this->to_add_waitables.push_back(waitable);
|
||||
waitable->set_manager(this);
|
||||
this->has_new_items = true;
|
||||
this->lock.Unlock();
|
||||
}
|
||||
|
||||
void WaitableManager::process_internal(bool break_on_timeout) {
|
||||
@ -22,11 +22,10 @@ void WaitableManager::process_internal(bool break_on_timeout) {
|
||||
while (1) {
|
||||
/* Add new items, if relevant. */
|
||||
if (this->has_new_items) {
|
||||
this->lock.Lock();
|
||||
std::scoped_lock lk{this->lock};
|
||||
this->waitables.insert(this->waitables.end(), this->to_add_waitables.begin(), this->to_add_waitables.end());
|
||||
this->to_add_waitables.clear();
|
||||
this->has_new_items = false;
|
||||
this->lock.Unlock();
|
||||
}
|
||||
|
||||
/* Sort waitables by priority. */
|
||||
|
Loading…
Reference in New Issue
Block a user