os: oh geez look at the time

This commit is contained in:
Michael Scire
2020-04-29 00:41:51 -07:00
parent 72f1e85aba
commit f670949ca9
9 changed files with 621 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
/*
* 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/>.
*/
#include <stratosphere.hpp>
#include "os_timer_event_helper.hpp"
namespace ams::os::impl {
TimeSpan SaturatedAdd(TimeSpan t1, TimeSpan t2) {
AMS_ASSERT(t1 >= 0);
AMS_ASSERT(t2 >= 0);
const u64 u1 = t1.GetNanoSeconds();
const u64 u2 = t2.GetNanoSeconds();
const u64 sum = u1 + u2;
return TimeSpan::FromNanoSeconds(std::min<u64>(std::numeric_limits<s64>::max(), sum));
}
void StopTimerUnsafe(TimerEventType *event) {
GetReference(event->next_time_to_wakeup) = 0;
event->timer_state = TimerEventType::TimerState_Stop;
}
bool UpdateSignalStateAndRecalculateNextTimeToWakeupUnsafe(TimerEventType *event, TimeSpan cur_time) {
TimeSpan next_time = GetReference(event->next_time_to_wakeup);
switch (event->timer_state) {
case TimerEventType::TimerState_Stop:
break;
case TimerEventType::TimerState_OneShot:
if (next_time < cur_time) {
event->signaled = true;
StopTimerUnsafe(event);
return true;
}
break;
case TimerEventType::TimerState_Periodic:
if (next_time < cur_time) {
event->signaled = true;
next_time = SaturatedAdd(next_time, GetReference(event->interval));
if (next_time < cur_time) {
const u64 elapsed_from_first = (cur_time - GetReference(event->first)).GetNanoSeconds();
const u64 interval = GetReference(event->interval).GetNanoSeconds();
const u64 t = std::min<u64>(std::numeric_limits<s64>::max(), ((elapsed_from_first / interval) + 1) * interval);
next_time = SaturatedAdd(TimeSpan::FromNanoSeconds(t), GetReference(event->first));
}
GetReference(event->next_time_to_wakeup) = next_time;
return true;
}
break;
AMS_UNREACHABLE_DEFAULT_CASE();
}
return false;
}
}

View File

@@ -0,0 +1,31 @@
/*
* 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 <stratosphere.hpp>
namespace ams::os {
struct TimerEventType;
namespace impl {
TimeSpan SaturatedAdd(TimeSpan t1, TimeSpan t2);
void StopTimerUnsafe(TimerEventType *event);
bool UpdateSignalStateAndRecalculateNextTimeToWakeupUnsafe(TimerEventType *event, TimeSpan cur_time);
}
}

View File

@@ -18,6 +18,7 @@
#include "os_waitable_holder_of_event.hpp"
#include "os_waitable_holder_of_inter_process_event.hpp"
#include "os_waitable_holder_of_interrupt_event.hpp"
#include "os_waitable_holder_of_timer_event.hpp"
#include "os_waitable_holder_of_thread.hpp"
#include "os_waitable_holder_of_semaphore.hpp"
#include "os_waitable_holder_of_message_queue.hpp"
@@ -30,6 +31,7 @@ namespace ams::os::impl {
TYPED_STORAGE(WaitableHolderOfEvent) holder_of_event_storage;
TYPED_STORAGE(WaitableHolderOfInterProcessEvent) holder_of_inter_process_event_storage;
TYPED_STORAGE(WaitableHolderOfInterruptEvent) holder_of_interrupt_event_storage;
TYPED_STORAGE(WaitableHolderOfTimerEvent) holder_of_timer_event_storage;
TYPED_STORAGE(WaitableHolderOfThread) holder_of_thread_storage;
TYPED_STORAGE(WaitableHolderOfSemaphore) holder_of_semaphore_storage;
TYPED_STORAGE(WaitableHolderOfMessageQueueForNotFull) holder_of_mq_for_not_full_storage;
@@ -44,6 +46,7 @@ namespace ams::os::impl {
CHECK_HOLDER(WaitableHolderOfEvent);
CHECK_HOLDER(WaitableHolderOfInterProcessEvent);
CHECK_HOLDER(WaitableHolderOfInterruptEvent);
CHECK_HOLDER(WaitableHolderOfTimerEvent);
CHECK_HOLDER(WaitableHolderOfThread);
CHECK_HOLDER(WaitableHolderOfSemaphore);
CHECK_HOLDER(WaitableHolderOfMessageQueueForNotFull);

View File

@@ -0,0 +1,67 @@
/*
* 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 <stratosphere.hpp>
#include "os_timer_event_helper.hpp"
#include "os_waitable_holder_base.hpp"
#include "os_waitable_object_list.hpp"
namespace ams::os::impl {
class WaitableHolderOfTimerEvent : public WaitableHolderOfUserObject {
private:
TimerEventType *event;
private:
TriBool IsSignaledImpl() const {
TimeSpan cur_time = this->GetManager()->GetCurrentTime();
UpdateSignalStateAndRecalculateNextTimeToWakeupUnsafe(this->event, cur_time);
return this->event->signaled ? TriBool::True : TriBool::False;
}
public:
explicit WaitableHolderOfTimerEvent(TimerEventType *e) : event(e) { /* ... */ }
/* IsSignaled, Link, Unlink implemented. */
virtual TriBool IsSignaled() const override {
std::scoped_lock lk(GetReference(this->event->cs_timer_event));
return this->IsSignaledImpl();
}
virtual TriBool LinkToObjectList() override {
std::scoped_lock lk(GetReference(this->event->cs_timer_event));
GetReference(this->event->waitable_object_list_storage).LinkWaitableHolder(*this);
return this->IsSignaledImpl();
}
virtual void UnlinkFromObjectList() override {
std::scoped_lock lk(GetReference(this->event->cs_timer_event));
GetReference(this->event->waitable_object_list_storage).UnlinkWaitableHolder(*this);
}
/* Gets the amount of time remaining until this wakes up. */
virtual TimeSpan GetAbsoluteWakeupTime() const override {
std::scoped_lock lk(GetReference(this->event->cs_timer_event));
if (this->event->timer_state == TimerEventType::TimerState_Stop) {
return TimeSpan::FromNanoSeconds(std::numeric_limits<s64>::max());
}
return GetReference(this->event->next_time_to_wakeup);
}
};
}