/*
* Copyright (c) 2018-2020 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 .
*/
#pragma once
#include
#include
#include
#include
namespace ams::os {
namespace impl {
class AutoWaitableHolder {
private:
WaitableHolderType m_holder;
public:
template
ALWAYS_INLINE explicit AutoWaitableHolder(WaitableManagerType *manager, T &&arg) {
InitializeWaitableHolder(std::addressof(m_holder), std::forward(arg));
LinkWaitableHolder(manager, std::addressof(m_holder));
}
ALWAYS_INLINE ~AutoWaitableHolder() {
UnlinkWaitableHolder(std::addressof(m_holder));
FinalizeWaitableHolder(std::addressof(m_holder));
}
ALWAYS_INLINE std::pair ConvertResult(const std::pair result, int index) {
if (result.first == std::addressof(m_holder)) {
return std::make_pair(static_cast(nullptr), index);
} else {
return result;
}
}
};
template
inline std::pair WaitAnyImpl(F &&func, WaitableManagerType *manager, int) {
return std::pair(func(manager), -1);
}
template
inline std::pair WaitAnyImpl(F &&func, WaitableManagerType *manager, int index, T &&x, Args &&... args) {
AutoWaitableHolder holder(manager, std::forward(x));
return holder.ConvertResult(WaitAnyImpl(std::forward(func), manager, index + 1, std::forward(args)...), index);
}
template
inline std::pair WaitAnyImpl(F &&func, WaitableManagerType *manager, Args &&... args) {
return WaitAnyImpl(std::forward(func), manager, 0, std::forward(args)...);
}
class TempWaitableManager {
private:
WaitableManagerType m_manager;
public:
ALWAYS_INLINE TempWaitableManager() {
os::InitializeWaitableManager(std::addressof(m_manager));
}
ALWAYS_INLINE ~TempWaitableManager() {
os::FinalizeWaitableManager(std::addressof(m_manager));
}
WaitableManagerType *Get() {
return std::addressof(m_manager);
}
};
template
inline std::pair WaitAnyImpl(F &&func, Args &&... args) {
TempWaitableManager temp_manager;
return WaitAnyImpl(std::forward(func), temp_manager.Get(), 0, std::forward(args)...);
}
using WaitAnyFunction = WaitableHolderType * (*)(WaitableManagerType *);
class NotBoolButInt {
private:
int m_value;
public:
constexpr ALWAYS_INLINE NotBoolButInt(int v) : m_value(v) { /* ... */ }
constexpr ALWAYS_INLINE operator int() const { return m_value; }
explicit operator bool() const = delete;
};
}
template requires (sizeof...(Args) > 0)
inline std::pair WaitAny(WaitableManagerType *manager, Args &&... args) {
return impl::WaitAnyImpl(static_cast(&::ams::os::WaitAny), manager, std::forward(args)...);
}
template requires (sizeof...(Args) > 0)
inline int WaitAny(Args &&... args) {
return impl::WaitAnyImpl(static_cast(&::ams::os::WaitAny), std::forward(args)...).second;
}
template requires (sizeof...(Args) > 0)
inline std::pair TryWaitAny(WaitableManagerType *manager, Args &&... args) {
return impl::WaitAnyImpl(static_cast(&::ams::os::TryWaitAny), manager, std::forward(args)...);
}
template requires (sizeof...(Args) > 0)
inline impl::NotBoolButInt TryWaitAny(Args &&... args) {
return impl::WaitAnyImpl(static_cast(&::ams::os::TryWaitAny), std::forward(args)...).second;
}
}