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/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
2021-10-01 02:00:47 +00:00
|
|
|
#include "os_multiple_wait_holder_base.hpp"
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-04-08 09:21:35 +00:00
|
|
|
#if defined(ATMOSPHERE_OS_HORIZON)
|
2021-10-01 02:00:47 +00:00
|
|
|
#include "os_multiple_wait_target_impl.os.horizon.hpp"
|
2022-03-06 20:08:20 +00:00
|
|
|
#elif defined(ATMOSPHERE_OS_WINDOWS)
|
|
|
|
#include "os_multiple_wait_target_impl.os.windows.hpp"
|
|
|
|
#elif defined(ATMOSPHERE_OS_LINUX)
|
|
|
|
#include "os_multiple_wait_target_impl.os.linux.hpp"
|
|
|
|
#elif defined(ATMOSPHERE_OS_MACOS)
|
|
|
|
#include "os_multiple_wait_target_impl.os.macos.hpp"
|
2020-04-08 09:21:35 +00:00
|
|
|
#else
|
2021-10-01 02:00:47 +00:00
|
|
|
#error "Unknown OS for ams::os::MultiWaitTargetImpl"
|
2020-04-08 09:21:35 +00:00
|
|
|
#endif
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
namespace ams::os::impl {
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
class MultiWaitImpl {
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
2021-10-01 02:00:47 +00:00
|
|
|
static constexpr size_t MaximumHandleCount = MultiWaitTargetImpl::MaximumHandleCount;
|
2019-12-09 11:57:37 +00:00
|
|
|
static constexpr s32 WaitInvalid = -3;
|
|
|
|
static constexpr s32 WaitCancelled = -2;
|
|
|
|
static constexpr s32 WaitTimedOut = -1;
|
2021-10-17 09:39:16 +00:00
|
|
|
using MultiWaitList = util::IntrusiveListMemberTraitsByNonConstexprOffsetOf<&MultiWaitHolderBase::m_multi_wait_node>::ListType;
|
2019-12-09 11:57:37 +00:00
|
|
|
private:
|
2021-10-10 07:14:06 +00:00
|
|
|
MultiWaitList m_multi_wait_list;
|
|
|
|
MultiWaitHolderBase *m_signaled_holder;
|
|
|
|
TimeSpan m_current_time;
|
|
|
|
InternalCriticalSection m_cs_wait;
|
|
|
|
MultiWaitTargetImpl m_target_impl;
|
2019-12-09 11:57:37 +00:00
|
|
|
private:
|
2021-10-04 19:33:09 +00:00
|
|
|
Result WaitAnyImpl(MultiWaitHolderBase **out, bool infinite, TimeSpan timeout, bool reply, NativeHandle reply_target);
|
|
|
|
Result WaitAnyHandleImpl(MultiWaitHolderBase **out, bool infinite, TimeSpan timeout, bool reply, NativeHandle reply_target);
|
|
|
|
s32 BuildHandleArray(NativeHandle out_handles[], MultiWaitHolderBase *out_objects[], s32 num);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
MultiWaitHolderBase *LinkHoldersToObjectList();
|
2019-12-09 11:57:37 +00:00
|
|
|
void UnlinkHoldersFromObjectList();
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
MultiWaitHolderBase *RecalculateNextTimeout(TimeSpan *out_min_timeout, TimeSpan end_time);
|
2021-01-13 02:18:39 +00:00
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
MultiWaitHolderBase *WaitAnyImpl(bool infinite, TimeSpan timeout) {
|
|
|
|
MultiWaitHolderBase *holder = nullptr;
|
2021-09-30 04:32:40 +00:00
|
|
|
|
2021-10-04 19:33:09 +00:00
|
|
|
const Result wait_result = this->WaitAnyImpl(std::addressof(holder), infinite, timeout, false, os::InvalidNativeHandle);
|
2021-09-30 04:32:40 +00:00
|
|
|
R_ASSERT(wait_result);
|
|
|
|
AMS_UNUSED(wait_result);
|
|
|
|
|
2021-01-13 02:18:39 +00:00
|
|
|
return holder;
|
|
|
|
}
|
2019-12-09 11:57:37 +00:00
|
|
|
public:
|
|
|
|
/* Wait. */
|
2021-10-01 02:00:47 +00:00
|
|
|
MultiWaitHolderBase *WaitAny() {
|
2020-04-08 09:21:35 +00:00
|
|
|
return this->WaitAnyImpl(true, TimeSpan::FromNanoSeconds(std::numeric_limits<s64>::max()));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
MultiWaitHolderBase *TryWaitAny() {
|
2020-04-08 09:21:35 +00:00
|
|
|
return this->WaitAnyImpl(false, TimeSpan(0));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
MultiWaitHolderBase *TimedWaitAny(TimeSpan ts) {
|
2020-04-08 09:21:35 +00:00
|
|
|
return this->WaitAnyImpl(false, ts);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 19:33:09 +00:00
|
|
|
Result ReplyAndReceive(MultiWaitHolderBase **out, NativeHandle reply_target) {
|
2021-01-13 02:18:39 +00:00
|
|
|
return this->WaitAnyImpl(out, true, TimeSpan::FromNanoSeconds(std::numeric_limits<s64>::max()), true, reply_target);
|
|
|
|
}
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
/* List management. */
|
|
|
|
bool IsEmpty() const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_multi_wait_list.empty();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void LinkMultiWaitHolder(MultiWaitHolderBase &holder_base) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_multi_wait_list.push_back(holder_base);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void UnlinkMultiWaitHolder(MultiWaitHolderBase &holder_base) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_multi_wait_list.erase(m_multi_wait_list.iterator_to(holder_base));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnlinkAll() {
|
|
|
|
while (!this->IsEmpty()) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_multi_wait_list.front().SetMultiWait(nullptr);
|
|
|
|
m_multi_wait_list.pop_front();
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void MoveAllFrom(MultiWaitImpl &other) {
|
|
|
|
/* Set ourselves as multi wait for all of the other's holders. */
|
2021-10-10 07:14:06 +00:00
|
|
|
for (auto &w : other.m_multi_wait_list) {
|
2021-10-01 02:00:47 +00:00
|
|
|
w.SetMultiWait(this);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
2021-10-10 07:14:06 +00:00
|
|
|
m_multi_wait_list.splice(m_multi_wait_list.end(), other.m_multi_wait_list);
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Other. */
|
2020-04-08 09:21:35 +00:00
|
|
|
TimeSpan GetCurrentTime() const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_current_time;
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void SignalAndWakeupThread(MultiWaitHolderBase *holder_base);
|
2019-12-09 11:57:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|