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

71 lines
2.4 KiB
C++
Raw Normal View History

2020-04-08 09:21:35 +00:00
/*
* Copyright (c) Atmosphère-NX
2020-04-08 09:21:35 +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
#include <stratosphere.hpp>
#include "os_tick_manager.hpp"
#if defined(ATMOSPHERE_OS_HORIZON)
#include "os_timeout_helper_impl.os.horizon.hpp"
#elif defined(ATMOSPHERE_OS_WINDOWS)
#include "os_timeout_helper_impl.os.windows.hpp"
#elif defined(ATMOSPHERE_OS_LINUX)
#include "os_timeout_helper_impl.os.linux.hpp"
#elif defined(ATMOSPHERE_OS_MACOS)
#include "os_timeout_helper_impl.os.macos.hpp"
2020-04-08 09:21:35 +00:00
#else
#error "Unknown OS for ams::os::TimeoutHelper"
#endif
namespace ams::os::impl {
class TimeoutHelper {
private:
2021-10-10 07:14:06 +00:00
Tick m_absolute_end_tick;
2020-04-08 09:21:35 +00:00
public:
explicit TimeoutHelper(TimeSpan timeout) {
if (timeout == 0) {
/* If timeout is zero, don't do relative tick calculations. */
2021-10-10 07:14:06 +00:00
m_absolute_end_tick = Tick(0);
2020-04-08 09:21:35 +00:00
} else {
const auto &tick_manager = impl::GetTickManager();
const u64 cur_tick = tick_manager.GetTick().GetInt64Value();
const u64 timeout_tick = tick_manager.ConvertToTick(timeout).GetInt64Value();
const u64 end_tick = cur_tick + timeout_tick + 1;
2021-10-10 07:14:06 +00:00
m_absolute_end_tick = Tick(std::min<u64>(std::numeric_limits<s64>::max(), end_tick));
2020-04-08 09:21:35 +00:00
}
}
static void Sleep(TimeSpan tm) {
TimeoutHelperImpl::Sleep(tm);
}
bool TimedOut() const {
2021-10-10 07:14:06 +00:00
if (m_absolute_end_tick.GetInt64Value() == 0) {
2020-04-08 09:21:35 +00:00
return true;
}
const Tick cur_tick = impl::GetTickManager().GetTick();
2021-10-10 07:14:06 +00:00
return cur_tick >= m_absolute_end_tick;
2020-04-08 09:21:35 +00:00
}
TargetTimeSpan GetTimeLeftOnTarget() const;
};
}