2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) 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/>.
|
|
|
|
*/
|
2020-05-11 22:02:10 +00:00
|
|
|
#include <stratosphere.hpp>
|
2021-10-01 02:00:47 +00:00
|
|
|
#include "os_multiple_wait_impl.hpp"
|
|
|
|
#include "os_multiple_wait_object_list.hpp"
|
2020-04-08 09:21:35 +00:00
|
|
|
#include "os_tick_manager.hpp"
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
namespace ams::os::impl {
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-10-04 19:33:09 +00:00
|
|
|
Result MultiWaitImpl::WaitAnyImpl(MultiWaitHolderBase **out, bool infinite, TimeSpan timeout, bool reply, NativeHandle reply_target) {
|
2019-12-09 11:57:37 +00:00
|
|
|
/* Prepare for processing. */
|
|
|
|
this->signaled_holder = nullptr;
|
2020-04-08 09:21:35 +00:00
|
|
|
this->target_impl.SetCurrentThreadHandleForCancelWait();
|
2021-10-01 02:00:47 +00:00
|
|
|
MultiWaitHolderBase *holder = this->LinkHoldersToObjectList();
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
/* Check if we've been signaled. */
|
|
|
|
{
|
2020-04-08 09:21:35 +00:00
|
|
|
std::scoped_lock lk(this->cs_wait);
|
2019-12-09 11:57:37 +00:00
|
|
|
if (this->signaled_holder != nullptr) {
|
2021-01-13 02:18:39 +00:00
|
|
|
holder = this->signaled_holder;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process object array. */
|
2021-01-13 02:18:39 +00:00
|
|
|
Result wait_result = ResultSuccess();
|
|
|
|
if (holder != nullptr) {
|
2021-10-04 19:33:09 +00:00
|
|
|
if (reply && reply_target != os::InvalidNativeHandle) {
|
2021-01-13 02:18:39 +00:00
|
|
|
s32 index;
|
|
|
|
wait_result = this->target_impl.TimedReplyAndReceive(std::addressof(index), nullptr, 0, 0, reply_target, TimeSpan::FromNanoSeconds(0));
|
|
|
|
if (R_FAILED(wait_result)) {
|
|
|
|
holder = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
wait_result = this->WaitAnyHandleImpl(std::addressof(holder), infinite, timeout, reply, reply_target);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlink holders from the current object list. */
|
|
|
|
this->UnlinkHoldersFromObjectList();
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
this->target_impl.ClearCurrentThreadHandleForCancelWait();
|
|
|
|
|
2021-01-13 02:18:39 +00:00
|
|
|
/* Set output holder. */
|
|
|
|
*out = holder;
|
|
|
|
|
|
|
|
return wait_result;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 19:33:09 +00:00
|
|
|
Result MultiWaitImpl::WaitAnyHandleImpl(MultiWaitHolderBase **out, bool infinite, TimeSpan timeout, bool reply, NativeHandle reply_target) {
|
|
|
|
NativeHandle object_handles[MaximumHandleCount];
|
2021-10-01 02:00:47 +00:00
|
|
|
MultiWaitHolderBase *objects[MaximumHandleCount];
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
const s32 count = this->BuildHandleArray(object_handles, objects, MaximumHandleCount);
|
|
|
|
const TimeSpan end_time = infinite ? TimeSpan::FromNanoSeconds(std::numeric_limits<s64>::max()) : GetCurrentTick().ToTimeSpan() + timeout;
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
while (true) {
|
2020-04-08 09:21:35 +00:00
|
|
|
this->current_time = GetCurrentTick().ToTimeSpan();
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
TimeSpan min_timeout = 0;
|
2021-10-09 21:49:53 +00:00
|
|
|
MultiWaitHolderBase *min_timeout_object = this->RecalculateNextTimeout(std::addressof(min_timeout), end_time);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-01-13 02:18:39 +00:00
|
|
|
s32 index = WaitInvalid;
|
|
|
|
Result wait_result = ResultSuccess();
|
|
|
|
if (reply) {
|
|
|
|
if (infinite && min_timeout_object == nullptr) {
|
2021-01-13 02:23:28 +00:00
|
|
|
wait_result = this->target_impl.ReplyAndReceive(std::addressof(index), object_handles, MaximumHandleCount, count, reply_target);
|
2021-01-13 02:18:39 +00:00
|
|
|
} else {
|
2021-01-13 02:23:28 +00:00
|
|
|
wait_result = this->target_impl.TimedReplyAndReceive(std::addressof(index), object_handles, MaximumHandleCount, count, reply_target, min_timeout);
|
2021-01-13 02:18:39 +00:00
|
|
|
}
|
|
|
|
} else if (infinite && min_timeout_object == nullptr) {
|
|
|
|
wait_result = this->target_impl.WaitAny(std::addressof(index), object_handles, MaximumHandleCount, count);
|
2019-12-09 11:57:37 +00:00
|
|
|
} else {
|
2020-04-08 09:21:35 +00:00
|
|
|
if (count == 0 && min_timeout == 0) {
|
|
|
|
index = WaitTimedOut;
|
|
|
|
} else {
|
2021-01-13 02:18:39 +00:00
|
|
|
wait_result = this->target_impl.TimedWaitAny(std::addressof(index), object_handles, MaximumHandleCount, count, min_timeout);
|
2020-04-08 09:21:35 +00:00
|
|
|
AMS_ABORT_UNLESS(index != WaitInvalid);
|
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 02:18:39 +00:00
|
|
|
if (index == WaitInvalid) {
|
|
|
|
*out = nullptr;
|
|
|
|
return wait_result;
|
|
|
|
}
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
switch (index) {
|
|
|
|
case WaitTimedOut:
|
|
|
|
if (min_timeout_object) {
|
2020-04-08 09:21:35 +00:00
|
|
|
this->current_time = GetCurrentTick().ToTimeSpan();
|
2019-12-09 11:57:37 +00:00
|
|
|
if (min_timeout_object->IsSignaled() == TriBool::True) {
|
2020-04-08 09:21:35 +00:00
|
|
|
std::scoped_lock lk(this->cs_wait);
|
2019-12-09 11:57:37 +00:00
|
|
|
this->signaled_holder = min_timeout_object;
|
2021-01-13 02:18:39 +00:00
|
|
|
*out = min_timeout_object;
|
|
|
|
return wait_result;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
2021-01-13 02:18:39 +00:00
|
|
|
} else {
|
|
|
|
*out = nullptr;
|
|
|
|
return wait_result;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
2021-01-13 02:18:39 +00:00
|
|
|
break;
|
2019-12-09 11:57:37 +00:00
|
|
|
case WaitCancelled:
|
2021-01-13 02:18:39 +00:00
|
|
|
{
|
|
|
|
std::scoped_lock lk(this->cs_wait);
|
|
|
|
if (this->signaled_holder) {
|
|
|
|
*out = this->signaled_holder;
|
|
|
|
return wait_result;
|
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
2021-01-13 02:18:39 +00:00
|
|
|
break;
|
2019-12-09 11:57:37 +00:00
|
|
|
default: /* 0 - 0x3F, valid. */
|
|
|
|
{
|
2021-01-13 02:18:39 +00:00
|
|
|
AMS_ASSERT(0 <= index && index < static_cast<s32>(MaximumHandleCount));
|
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
std::scoped_lock lk(this->cs_wait);
|
2019-12-09 11:57:37 +00:00
|
|
|
this->signaled_holder = objects[index];
|
2021-01-13 02:18:39 +00:00
|
|
|
*out = objects[index];
|
|
|
|
return wait_result;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
2021-10-01 02:00:47 +00:00
|
|
|
break;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
2021-01-13 02:18:39 +00:00
|
|
|
|
2021-10-04 19:33:09 +00:00
|
|
|
reply_target = os::InvalidNativeHandle;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 19:33:09 +00:00
|
|
|
s32 MultiWaitImpl::BuildHandleArray(NativeHandle out_handles[], MultiWaitHolderBase *out_objects[], s32 num) {
|
2020-04-08 09:21:35 +00:00
|
|
|
s32 count = 0;
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
for (MultiWaitHolderBase &holder_base : this->multi_wait_list) {
|
2021-10-04 19:33:09 +00:00
|
|
|
if (auto handle = holder_base.GetHandle(); handle != os::InvalidNativeHandle) {
|
2021-10-09 21:49:53 +00:00
|
|
|
AMS_ABORT_UNLESS(count < num);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
out_handles[count] = handle;
|
2021-10-09 21:49:53 +00:00
|
|
|
out_objects[count] = std::addressof(holder_base);
|
2019-12-09 11:57:37 +00:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
MultiWaitHolderBase *MultiWaitImpl::LinkHoldersToObjectList() {
|
|
|
|
MultiWaitHolderBase *signaled_holder = nullptr;
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
for (MultiWaitHolderBase &holder_base : this->multi_wait_list) {
|
2019-12-09 11:57:37 +00:00
|
|
|
TriBool is_signaled = holder_base.LinkToObjectList();
|
|
|
|
|
|
|
|
if (signaled_holder == nullptr && is_signaled == TriBool::True) {
|
2021-10-09 21:49:53 +00:00
|
|
|
signaled_holder = std::addressof(holder_base);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return signaled_holder;
|
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void MultiWaitImpl::UnlinkHoldersFromObjectList() {
|
|
|
|
for (MultiWaitHolderBase &holder_base : this->multi_wait_list) {
|
2019-12-09 11:57:37 +00:00
|
|
|
holder_base.UnlinkFromObjectList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
MultiWaitHolderBase *MultiWaitImpl::RecalculateNextTimeout(TimeSpan *out_min_timeout, TimeSpan end_time) {
|
|
|
|
MultiWaitHolderBase *min_timeout_holder = nullptr;
|
2020-04-08 09:21:35 +00:00
|
|
|
TimeSpan min_time = end_time;
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
for (MultiWaitHolderBase &holder_base : this->multi_wait_list) {
|
2020-04-08 09:21:35 +00:00
|
|
|
if (const TimeSpan cur_time = holder_base.GetAbsoluteWakeupTime(); cur_time < min_time) {
|
2021-10-09 21:49:53 +00:00
|
|
|
min_timeout_holder = std::addressof(holder_base);
|
2019-12-09 11:57:37 +00:00
|
|
|
min_time = cur_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min_time < this->current_time) {
|
|
|
|
*out_min_timeout = 0;
|
|
|
|
} else {
|
|
|
|
*out_min_timeout = min_time - this->current_time;
|
|
|
|
}
|
|
|
|
return min_timeout_holder;
|
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void MultiWaitImpl::SignalAndWakeupThread(MultiWaitHolderBase *holder_base) {
|
2020-04-08 09:21:35 +00:00
|
|
|
std::scoped_lock lk(this->cs_wait);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
|
|
|
if (this->signaled_holder == nullptr) {
|
|
|
|
this->signaled_holder = holder_base;
|
2020-04-08 09:21:35 +00:00
|
|
|
this->target_impl.CancelWait();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|