1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-30 07:42:13 +00:00
Atmosphere/stratosphere/libstratosphere/source/multithreadedwaitablemanager.cpp
Tony Wasserka ad636f7216 Modernize C++ usage (#144)
* Stratosphere: Use modern C++ idioms in some places

* algorithms like std::for_each are used instead of raw loops

* Stratosphere: Replace more raw loops with algorithms

* Stratosphere: Add a utility predicate function to test for equality with a reference element

This can be used to rewrite some common raw loops using algorithms instead

* fs.mitm: Use variant

* fs.mitm: Use enum class

* fs.mitm: Turn RomFSSourceInfo::Cleanup into a destructor

This obsoletes the need for a custom deleter in other places

* fs.mitm: Use enum class some more

* fs.mitm: Use unique_ptr

* fs.mitm: Simplify initialization

* Stratosphere: Simplify initialization

* fs.mitm: Use unique_ptr (fix memory leak along the way)

The previous code was using "delete" rather than "delete[]"

* fs.mitm: Use vector::emplace_back rather than push_back

emplace_back constructs elements in-place, hence avoiding a redundant element copy.

* Stratosphere: Replace more raw loops with algorithms

* Stratosphere: Use unique_ptr

* fs.mitm: Replace more raw loops with algorithms

* Stratosphere: Prefer move-construction over copy-construction when moving sink parameters around
2018-06-19 11:07:31 -07:00

105 lines
3.6 KiB
C++

#include <switch.h>
#include <algorithm>
#include <functional>
#include <stratosphere/multithreadedwaitablemanager.hpp>
void MultiThreadedWaitableManager::process() {
Result rc;
for (unsigned int i = 0; i < num_threads; i++) {
if (R_FAILED((rc = threadStart(&threads[i])))) {
fatalSimple(rc);
}
}
MultiThreadedWaitableManager::thread_func(this);
}
void MultiThreadedWaitableManager::process_until_timeout() {
/* TODO: Panic. */
}
void MultiThreadedWaitableManager::add_waitable(IWaitable *waitable) {
this->lock.Lock();
this->to_add_waitables.push_back(waitable);
waitable->set_manager(this);
this->new_waitable_event->signal_event();
this->lock.Unlock();
}
IWaitable *MultiThreadedWaitableManager::get_waitable() {
std::vector<Handle> handles;
int handle_index = 0;
Result rc;
this->get_waitable_lock.Lock();
while (1) {
/* Sort waitables by priority. */
std::sort(this->waitables.begin(), this->waitables.end(), IWaitable::compare);
/* Copy out handles. */
handles.resize(this->waitables.size());
std::transform(this->waitables.begin(), this->waitables.end(), handles.begin(), [](IWaitable *w) { return w->get_handle(); });
rc = svcWaitSynchronization(&handle_index, handles.data(), this->waitables.size(), this->timeout);
IWaitable *w = this->waitables[handle_index];
if (R_SUCCEEDED(rc)) {
std::for_each(waitables.begin(), waitables.begin() + handle_index, std::mem_fn(&IWaitable::update_priority));
this->waitables.erase(this->waitables.begin() + handle_index);
} else if (rc == 0xEA01) {
/* Timeout. */
std::for_each(waitables.begin(), waitables.end(), std::mem_fn(&IWaitable::update_priority));
} else if (rc != 0xF601 && rc != 0xE401) {
/* TODO: Panic. When can this happen? */
} else {
std::for_each(waitables.begin(), waitables.begin() + handle_index, std::mem_fn(&IWaitable::update_priority));
this->waitables.erase(this->waitables.begin() + handle_index);
delete w;
}
/* Do deferred callback for each waitable. */
for (auto & waitable : this->waitables) {
if (waitable->get_deferred()) {
waitable->handle_deferred();
}
}
/* Return waitable. */
if (R_SUCCEEDED(rc)) {
if (w == this->new_waitable_event) {
w->handle_signaled(0);
this->waitables.push_back(w);
} else {
this->get_waitable_lock.Unlock();
return w;
}
}
}
}
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();
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;
}
void MultiThreadedWaitableManager::thread_func(void *t) {
MultiThreadedWaitableManager *this_ptr = (MultiThreadedWaitableManager *)t;
while (1) {
IWaitable *w = this_ptr->get_waitable();
if (w) {
Result rc = w->handle_signaled(0);
if (rc == 0xF601) {
/* Close! */
delete w;
} else {
this_ptr->add_waitable(w);
}
}
}
}