1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-12-19 00:42:06 +00:00
Atmosphere/libraries/libstratosphere/source/os/impl/os_timeout_helper.hpp

65 lines
2.1 KiB
C++
Raw Normal View History

2020-04-08 10:21:35 +01:00
/*
* Copyright (c) Atmosphère-NX
2020-04-08 10:21:35 +01: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"
#else
#error "Unknown OS for ams::os::TimeoutHelper"
#endif
namespace ams::os::impl {
class TimeoutHelper {
private:
2021-10-10 08:14:06 +01:00
Tick m_absolute_end_tick;
2020-04-08 10:21:35 +01:00
public:
explicit TimeoutHelper(TimeSpan timeout) {
if (timeout == 0) {
/* If timeout is zero, don't do relative tick calculations. */
2021-10-10 08:14:06 +01:00
m_absolute_end_tick = Tick(0);
2020-04-08 10:21:35 +01: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 08:14:06 +01:00
m_absolute_end_tick = Tick(std::min<u64>(std::numeric_limits<s64>::max(), end_tick));
2020-04-08 10:21:35 +01:00
}
}
static void Sleep(TimeSpan tm) {
TimeoutHelperImpl::Sleep(tm);
}
bool TimedOut() const {
2021-10-10 08:14:06 +01:00
if (m_absolute_end_tick.GetInt64Value() == 0) {
2020-04-08 10:21:35 +01:00
return true;
}
const Tick cur_tick = impl::GetTickManager().GetTick();
2021-10-10 08:14:06 +01:00
return cur_tick >= m_absolute_end_tick;
2020-04-08 10:21:35 +01:00
}
TargetTimeSpan GetTimeLeftOnTarget() const;
};
}