Atmosphere/libraries/libstratosphere/include/stratosphere/os/os_waitable_utils.hpp
SciresM e9849c74cf
LogManager: implement system module, client api, logging api (#1617)
Some notes:

* Unless `atmosphere!enable_log_manager` is true, Nintendo's log manager will be used instead.
  * This prevents paying memory costs for LM when not enabling logging.
  * To facilitate this, Atmosphere's log manager has a different program id from Nintendo's.
  * `atmosphere!enable_htc` implies `atmosphere!enable_log_manager`.
* LogManager logs to tma, and the SD card (if `lm!enable_sd_card_logging` is true, which it is by default).
* Binary logs are saved to `lm!sd_card_log_output_directory`, which is `atmosphere/binlogs` by default.
2021-09-11 19:32:14 -07:00

124 lines
5.2 KiB
C++

/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_message_queue_common.hpp>
#include <stratosphere/os/os_waitable_api.hpp>
#include <stratosphere/os/os_waitable_types.hpp>
namespace ams::os {
namespace impl {
class AutoWaitableHolder {
private:
WaitableHolderType m_holder;
public:
template<typename T>
ALWAYS_INLINE explicit AutoWaitableHolder(WaitableManagerType *manager, T &&arg) {
InitializeWaitableHolder(std::addressof(m_holder), std::forward<T>(arg));
LinkWaitableHolder(manager, std::addressof(m_holder));
}
ALWAYS_INLINE ~AutoWaitableHolder() {
UnlinkWaitableHolder(std::addressof(m_holder));
FinalizeWaitableHolder(std::addressof(m_holder));
}
ALWAYS_INLINE std::pair<WaitableHolderType *, int> ConvertResult(const std::pair<WaitableHolderType *, int> result, int index) {
if (result.first == std::addressof(m_holder)) {
return std::make_pair(static_cast<WaitableHolderType *>(nullptr), index);
} else {
return result;
}
}
};
template<typename F>
inline std::pair<WaitableHolderType *, int> WaitAnyImpl(F &&func, WaitableManagerType *manager, int) {
return std::pair<WaitableHolderType *, int>(func(manager), -1);
}
template<typename F, typename T, typename... Args>
inline std::pair<WaitableHolderType *, int> WaitAnyImpl(F &&func, WaitableManagerType *manager, int index, T &&x, Args &&... args) {
AutoWaitableHolder holder(manager, std::forward<T>(x));
return holder.ConvertResult(WaitAnyImpl(std::forward<F>(func), manager, index + 1, std::forward<Args>(args)...), index);
}
template<typename F, typename... Args>
inline std::pair<WaitableHolderType *, int> WaitAnyImpl(F &&func, WaitableManagerType *manager, Args &&... args) {
return WaitAnyImpl(std::forward<F>(func), manager, 0, std::forward<Args>(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<typename F, typename... Args>
inline std::pair<WaitableHolderType *, int> WaitAnyImpl(F &&func, Args &&... args) {
TempWaitableManager temp_manager;
return WaitAnyImpl(std::forward<F>(func), temp_manager.Get(), 0, std::forward<Args>(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<typename... Args> requires (sizeof...(Args) > 0)
inline std::pair<WaitableHolderType *, int> WaitAny(WaitableManagerType *manager, Args &&... args) {
return impl::WaitAnyImpl(static_cast<impl::WaitAnyFunction>(&::ams::os::WaitAny), manager, std::forward<Args>(args)...);
}
template<typename... Args> requires (sizeof...(Args) > 0)
inline int WaitAny(Args &&... args) {
return impl::WaitAnyImpl(static_cast<impl::WaitAnyFunction>(&::ams::os::WaitAny), std::forward<Args>(args)...).second;
}
template<typename... Args> requires (sizeof...(Args) > 0)
inline std::pair<WaitableHolderType *, int> TryWaitAny(WaitableManagerType *manager, Args &&... args) {
return impl::WaitAnyImpl(static_cast<impl::WaitAnyFunction>(&::ams::os::TryWaitAny), manager, std::forward<Args>(args)...);
}
template<typename... Args> requires (sizeof...(Args) > 0)
inline impl::NotBoolButInt TryWaitAny(Args &&... args) {
return impl::WaitAnyImpl(static_cast<impl::WaitAnyFunction>(&::ams::os::TryWaitAny), std::forward<Args>(args)...).second;
}
}