Atmosphere/libraries/libstratosphere/source/os/impl/os_multiple_wait_impl.hpp

119 lines
4.4 KiB
C++
Raw Permalink Normal View History

/*
* Copyright (c) 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/>.
*/
#pragma once
#include "os_multiple_wait_holder_base.hpp"
2020-04-08 09:21:35 +00:00
#if defined(ATMOSPHERE_OS_HORIZON)
#include "os_multiple_wait_target_impl.os.horizon.hpp"
#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
#error "Unknown OS for ams::os::MultiWaitTargetImpl"
2020-04-08 09:21:35 +00:00
#endif
namespace ams::os::impl {
class MultiWaitImpl {
public:
static constexpr size_t MaximumHandleCount = MultiWaitTargetImpl::MaximumHandleCount;
static constexpr s32 WaitInvalid = -3;
static constexpr s32 WaitCancelled = -2;
static constexpr s32 WaitTimedOut = -1;
using MultiWaitList = util::IntrusiveListMemberTraitsByNonConstexprOffsetOf<&MultiWaitHolderBase::m_multi_wait_node>::ListType;
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;
private:
template<bool AllowReply>
Result WaitAnyImpl(MultiWaitHolderBase **out, bool infinite, TimeSpan timeout, NativeHandle reply_target);
template<bool AllowReply>
Result InternalWaitAnyImpl(MultiWaitHolderBase **out, bool infinite, TimeSpan timeout, NativeHandle reply_target);
s32 ConstructObjectsArray(NativeHandle out_handles[], MultiWaitHolderBase *out_objects[], s32 num);
2021-01-13 02:18:39 +00:00
MultiWaitHolderBase *AddToEachObjectListAndCheckObjectState();
void RemoveFromEachObjectList();
MultiWaitHolderBase *RecalcMultiWaitTimeout(TimeSpan *out_min_timeout, TimeSpan end_time);
MultiWaitHolderBase *WaitAnyImpl(bool infinite, TimeSpan timeout);
public:
/* Wait. */
MultiWaitHolderBase *WaitAny() {
2020-04-08 09:21:35 +00:00
return this->WaitAnyImpl(true, TimeSpan::FromNanoSeconds(std::numeric_limits<s64>::max()));
}
MultiWaitHolderBase *TryWaitAny() {
2020-04-08 09:21:35 +00:00
return this->WaitAnyImpl(false, TimeSpan(0));
}
MultiWaitHolderBase *TimedWaitAny(TimeSpan ts) {
2020-04-08 09:21:35 +00:00
return this->WaitAnyImpl(false, ts);
}
Result ReplyAndReceive(MultiWaitHolderBase **out, NativeHandle reply_target);
2021-01-13 02:18:39 +00:00
/* List management. */
bool IsListEmpty() const {
2021-10-10 07:14:06 +00:00
return m_multi_wait_list.empty();
}
bool IsListNotEmpty() const {
return !m_multi_wait_list.empty();
}
void PushBackToList(MultiWaitHolderBase &holder_base) {
2021-10-10 07:14:06 +00:00
m_multi_wait_list.push_back(holder_base);
}
void EraseFromList(MultiWaitHolderBase &holder_base) {
2021-10-10 07:14:06 +00:00
m_multi_wait_list.erase(m_multi_wait_list.iterator_to(holder_base));
}
void EraseAllFromList() {
while (!m_multi_wait_list.empty()) {
2021-10-10 07:14:06 +00:00
m_multi_wait_list.front().SetMultiWait(nullptr);
m_multi_wait_list.pop_front();
}
}
void MoveAllFromOther(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) {
w.SetMultiWait(this);
}
2021-10-10 07:14:06 +00:00
m_multi_wait_list.splice(m_multi_wait_list.end(), other.m_multi_wait_list);
}
/* Other. */
TimeSpan GetCurrTime() const {
2021-10-10 07:14:06 +00:00
return m_current_time;
}
void NotifyAndWakeupThread(MultiWaitHolderBase *holder_base);
};
}