2018-04-18 18:41:17 +01:00
|
|
|
#include <switch.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
2018-06-19 19:07:31 +01:00
|
|
|
#include <functional>
|
2018-07-02 15:10:57 +01:00
|
|
|
#include <mutex>
|
2018-04-18 18:41:17 +01:00
|
|
|
|
2018-04-22 03:31:06 +01:00
|
|
|
#include <stratosphere/waitablemanager.hpp>
|
2018-04-18 18:41:17 +01:00
|
|
|
|
|
|
|
void WaitableManager::add_waitable(IWaitable *waitable) {
|
2018-07-02 15:10:57 +01:00
|
|
|
std::scoped_lock lk{this->lock};
|
2018-06-12 23:00:09 +01:00
|
|
|
this->to_add_waitables.push_back(waitable);
|
|
|
|
waitable->set_manager(this);
|
|
|
|
this->has_new_items = true;
|
2018-04-18 18:41:17 +01:00
|
|
|
}
|
|
|
|
|
2018-05-04 06:58:25 +01:00
|
|
|
void WaitableManager::process_internal(bool break_on_timeout) {
|
2018-04-18 18:41:17 +01:00
|
|
|
std::vector<Handle> handles;
|
|
|
|
|
|
|
|
int handle_index = 0;
|
|
|
|
Result rc;
|
|
|
|
|
|
|
|
while (1) {
|
2018-06-12 23:00:09 +01:00
|
|
|
/* Add new items, if relevant. */
|
|
|
|
if (this->has_new_items) {
|
2018-07-02 15:10:57 +01:00
|
|
|
std::scoped_lock lk{this->lock};
|
2018-06-12 23:00:09 +01:00
|
|
|
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;
|
2018-04-18 18:41:17 +01:00
|
|
|
}
|
2018-06-12 23:00:09 +01:00
|
|
|
|
|
|
|
/* Sort waitables by priority. */
|
|
|
|
std::sort(this->waitables.begin(), this->waitables.end(), IWaitable::compare);
|
2018-04-18 18:41:17 +01:00
|
|
|
|
|
|
|
/* Copy out handles. */
|
2018-06-12 23:00:09 +01:00
|
|
|
handles.resize(this->waitables.size());
|
|
|
|
std::transform(this->waitables.begin(), this->waitables.end(), handles.begin(), [](IWaitable *w) { return w->get_handle(); });
|
2018-04-18 18:41:17 +01:00
|
|
|
|
2018-04-22 10:02:08 +01:00
|
|
|
|
2018-06-12 23:00:09 +01:00
|
|
|
rc = svcWaitSynchronization(&handle_index, handles.data(), this->waitables.size(), this->timeout);
|
2018-04-18 18:41:17 +01:00
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
/* Handle a signaled waitable. */
|
2018-04-18 19:53:04 +01:00
|
|
|
/* TODO: What timeout should be passed here? */
|
2018-04-18 23:24:40 +01:00
|
|
|
|
2018-06-12 23:00:09 +01:00
|
|
|
rc = this->waitables[handle_index]->handle_signaled(0);
|
2018-04-18 19:23:06 +01:00
|
|
|
|
2018-06-19 19:07:31 +01:00
|
|
|
std::for_each(waitables.begin(), waitables.begin() + handle_index, std::mem_fn(&IWaitable::update_priority));
|
2018-04-18 18:41:17 +01:00
|
|
|
} else if (rc == 0xEA01) {
|
|
|
|
/* Timeout. */
|
2018-06-19 19:07:31 +01:00
|
|
|
std::for_each(waitables.begin(), waitables.end(), std::mem_fn(&IWaitable::update_priority));
|
2018-05-04 06:58:25 +01:00
|
|
|
if (break_on_timeout) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-18 19:53:04 +01:00
|
|
|
} else if (rc != 0xF601) {
|
|
|
|
/* TODO: Panic. When can this happen? */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == 0xF601) {
|
2018-04-18 18:41:17 +01:00
|
|
|
/* handles[handle_index] was closed! */
|
|
|
|
|
|
|
|
/* Close the handle. */
|
|
|
|
svcCloseHandle(handles[handle_index]);
|
|
|
|
|
2018-06-12 23:00:09 +01:00
|
|
|
IWaitable *to_delete = this->waitables[handle_index];
|
|
|
|
|
2018-04-18 18:41:17 +01:00
|
|
|
/* If relevant, remove from waitables. */
|
2018-06-12 23:00:09 +01:00
|
|
|
this->waitables.erase(this->waitables.begin() + handle_index);
|
2018-04-18 18:41:17 +01:00
|
|
|
|
|
|
|
/* Delete it. */
|
2018-06-12 23:00:09 +01:00
|
|
|
delete to_delete;
|
|
|
|
|
2018-06-19 19:07:31 +01:00
|
|
|
std::for_each(waitables.begin(), waitables.begin() + handle_index, std::mem_fn(&IWaitable::update_priority));
|
2018-04-22 10:02:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do deferred callback for each waitable. */
|
2018-06-12 23:00:09 +01:00
|
|
|
for (auto & waitable : this->waitables) {
|
2018-05-01 05:27:07 +01:00
|
|
|
if (waitable->get_deferred()) {
|
|
|
|
waitable->handle_deferred();
|
|
|
|
}
|
|
|
|
}
|
2018-04-18 18:41:17 +01:00
|
|
|
}
|
2018-05-04 06:58:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaitableManager::process() {
|
|
|
|
WaitableManager::process_internal(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitableManager::process_until_timeout() {
|
|
|
|
WaitableManager::process_internal(true);
|
2018-06-19 19:07:31 +01:00
|
|
|
}
|