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