2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-08-25 01:35:19 +00:00
|
|
|
#include <AK/Singleton.h>
|
2020-11-15 18:58:19 +00:00
|
|
|
#include <AK/Time.h>
|
2021-06-22 15:40:16 +00:00
|
|
|
#include <Kernel/Sections.h>
|
2023-02-24 17:45:37 +00:00
|
|
|
#include <Kernel/Tasks/Scheduler.h>
|
2020-04-26 09:23:37 +00:00
|
|
|
#include <Kernel/Time/TimeManagement.h>
|
2023-02-24 18:23:38 +00:00
|
|
|
#include <Kernel/Time/TimerQueue.h>
|
2019-12-27 00:58:28 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-08-07 19:34:11 +00:00
|
|
|
static Singleton<TimerQueue> s_the;
|
2022-11-09 10:39:58 +00:00
|
|
|
static Spinlock<LockRank::None> g_timerqueue_lock {};
|
2019-12-27 00:58:28 +00:00
|
|
|
|
2023-03-13 15:30:34 +00:00
|
|
|
Duration Timer::remaining() const
|
2020-12-01 22:44:52 +00:00
|
|
|
{
|
2021-02-27 22:56:16 +00:00
|
|
|
return m_remaining;
|
2020-12-01 23:53:47 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 15:30:34 +00:00
|
|
|
Duration Timer::now(bool is_firing) const
|
2020-12-01 23:53:47 +00:00
|
|
|
{
|
2020-12-04 05:12:50 +00:00
|
|
|
// NOTE: If is_firing is true then TimePrecision::Precise isn't really useful here.
|
|
|
|
// We already have a quite precise time stamp because we just updated the time in the
|
|
|
|
// interrupt handler. In those cases, just use coarse timestamps.
|
|
|
|
auto clock_id = m_clock_id;
|
|
|
|
if (is_firing) {
|
|
|
|
switch (clock_id) {
|
|
|
|
case CLOCK_MONOTONIC:
|
|
|
|
clock_id = CLOCK_MONOTONIC_COARSE;
|
|
|
|
break;
|
|
|
|
case CLOCK_MONOTONIC_RAW:
|
|
|
|
// TODO: use a special CLOCK_MONOTONIC_RAW_COARSE like mechanism here
|
|
|
|
break;
|
|
|
|
case CLOCK_REALTIME:
|
|
|
|
clock_id = CLOCK_REALTIME_COARSE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-05-05 16:51:06 +00:00
|
|
|
return TimeManagement::the().current_time(clock_id);
|
2020-12-01 22:44:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 00:58:28 +00:00
|
|
|
TimerQueue& TimerQueue::the()
|
|
|
|
{
|
|
|
|
return *s_the;
|
|
|
|
}
|
|
|
|
|
2021-02-19 20:29:46 +00:00
|
|
|
UNMAP_AFTER_INIT TimerQueue::TimerQueue()
|
2020-04-26 09:23:37 +00:00
|
|
|
{
|
|
|
|
m_ticks_per_second = TimeManagement::the().ticks_per_second();
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:30:34 +00:00
|
|
|
bool TimerQueue::add_timer_without_id(NonnullRefPtr<Timer> timer, clockid_t clock_id, Duration const& deadline, Function<void()>&& callback)
|
2019-12-27 00:58:28 +00:00
|
|
|
{
|
2021-05-05 16:51:06 +00:00
|
|
|
if (deadline <= TimeManagement::the().current_time(clock_id))
|
2021-05-19 22:41:51 +00:00
|
|
|
return false;
|
2020-11-15 18:58:19 +00:00
|
|
|
|
|
|
|
// Because timer handlers can execute on any processor and there is
|
|
|
|
// a race between executing a timer handler and cancel_timer() this
|
2023-04-03 11:09:38 +00:00
|
|
|
// *must* be a RefPtr<Timer>. Otherwise, calling cancel_timer() could
|
2020-11-15 18:58:19 +00:00
|
|
|
// inadvertently cancel another timer that has been created between
|
|
|
|
// returning from the timer handler and a call to cancel_timer().
|
2021-05-19 22:41:51 +00:00
|
|
|
timer->setup(clock_id, deadline, move(callback));
|
2020-11-15 18:58:19 +00:00
|
|
|
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(g_timerqueue_lock);
|
2020-12-01 20:02:54 +00:00
|
|
|
timer->m_id = 0; // Don't generate a timer id
|
2021-05-19 22:41:51 +00:00
|
|
|
add_timer_locked(move(timer));
|
|
|
|
return true;
|
2020-11-15 18:58:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:09:38 +00:00
|
|
|
TimerId TimerQueue::add_timer(NonnullRefPtr<Timer>&& timer)
|
2020-11-15 18:58:19 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(g_timerqueue_lock);
|
2019-12-27 00:58:28 +00:00
|
|
|
|
2020-12-01 20:02:54 +00:00
|
|
|
timer->m_id = ++m_timer_id_count;
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(timer->m_id != 0); // wrapped
|
2021-06-20 10:03:29 +00:00
|
|
|
auto id = timer->m_id;
|
2020-11-15 18:58:19 +00:00
|
|
|
add_timer_locked(move(timer));
|
2021-06-20 10:03:29 +00:00
|
|
|
return id;
|
2020-11-15 18:58:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:09:38 +00:00
|
|
|
void TimerQueue::add_timer_locked(NonnullRefPtr<Timer> timer)
|
2020-11-15 18:58:19 +00:00
|
|
|
{
|
2023-03-13 15:30:34 +00:00
|
|
|
Duration timer_expiration = timer->m_expires;
|
2019-12-27 00:58:28 +00:00
|
|
|
|
2021-01-30 05:49:28 +00:00
|
|
|
timer->clear_cancelled();
|
|
|
|
timer->clear_callback_finished();
|
2021-07-31 23:27:44 +00:00
|
|
|
timer->set_in_use();
|
2020-12-01 20:02:54 +00:00
|
|
|
|
2020-12-01 23:53:47 +00:00
|
|
|
auto& queue = queue_for_timer(*timer);
|
|
|
|
if (queue.list.is_empty()) {
|
2021-06-15 09:44:09 +00:00
|
|
|
queue.list.append(timer.leak_ref());
|
2020-12-01 23:53:47 +00:00
|
|
|
queue.next_timer_due = timer_expiration;
|
2020-04-26 21:00:34 +00:00
|
|
|
} else {
|
2020-12-01 20:02:54 +00:00
|
|
|
Timer* following_timer = nullptr;
|
2021-06-15 09:44:09 +00:00
|
|
|
for (auto& t : queue.list) {
|
2020-12-01 20:02:54 +00:00
|
|
|
if (t.m_expires > timer_expiration) {
|
|
|
|
following_timer = &t;
|
2021-06-15 09:44:09 +00:00
|
|
|
break;
|
2020-12-01 20:02:54 +00:00
|
|
|
}
|
2021-06-15 09:44:09 +00:00
|
|
|
}
|
2020-12-01 20:02:54 +00:00
|
|
|
if (following_timer) {
|
2021-06-15 09:44:09 +00:00
|
|
|
bool next_timer_needs_update = queue.list.first() == following_timer;
|
|
|
|
queue.list.insert_before(*following_timer, timer.leak_ref());
|
2020-04-26 21:00:34 +00:00
|
|
|
if (next_timer_needs_update)
|
2020-12-01 23:53:47 +00:00
|
|
|
queue.next_timer_due = timer_expiration;
|
2020-12-01 20:02:54 +00:00
|
|
|
} else {
|
2021-06-15 09:44:09 +00:00
|
|
|
queue.list.append(timer.leak_ref());
|
2020-04-26 21:00:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-27 00:58:28 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 23:27:44 +00:00
|
|
|
bool TimerQueue::cancel_timer(Timer& timer, bool* was_in_use)
|
2020-11-15 18:58:19 +00:00
|
|
|
{
|
2021-07-31 23:27:44 +00:00
|
|
|
bool in_use = timer.is_in_use();
|
|
|
|
if (was_in_use)
|
|
|
|
*was_in_use = in_use;
|
|
|
|
|
|
|
|
// If the timer isn't in use, the cancellation is a no-op.
|
|
|
|
if (!in_use) {
|
|
|
|
VERIFY(!timer.m_list_node.is_in_list());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-30 05:49:28 +00:00
|
|
|
bool did_already_run = timer.set_cancelled();
|
2020-12-01 23:53:47 +00:00
|
|
|
auto& timer_queue = queue_for_timer(timer);
|
2021-01-30 05:49:28 +00:00
|
|
|
if (!did_already_run) {
|
2021-07-31 23:27:44 +00:00
|
|
|
timer.clear_in_use();
|
|
|
|
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(g_timerqueue_lock);
|
2021-01-30 05:49:28 +00:00
|
|
|
if (timer_queue.list.contains(timer)) {
|
|
|
|
// The timer has not fired, remove it
|
|
|
|
VERIFY(timer.ref_count() > 1);
|
|
|
|
remove_timer_locked(timer_queue, timer);
|
|
|
|
return true;
|
2020-12-01 20:02:54 +00:00
|
|
|
}
|
2021-01-30 05:49:28 +00:00
|
|
|
|
|
|
|
// The timer was queued to execute but hasn't had a chance
|
|
|
|
// to run. In this case, it should still be in m_timers_executing
|
|
|
|
// and we don't need to spin. It still holds a reference
|
|
|
|
// that will be dropped when it does get a chance to run,
|
|
|
|
// but since we called set_cancelled it will only drop its reference
|
|
|
|
VERIFY(m_timers_executing.contains(timer));
|
|
|
|
m_timers_executing.remove(timer);
|
|
|
|
return true;
|
2020-12-01 20:02:54 +00:00
|
|
|
}
|
2020-11-15 18:58:19 +00:00
|
|
|
|
2021-01-30 05:49:28 +00:00
|
|
|
// At this point the deferred call is queued and is being executed
|
|
|
|
// on another processor. We need to wait until it's complete!
|
|
|
|
while (!timer.is_callback_finished())
|
|
|
|
Processor::wait_check();
|
|
|
|
|
2021-07-31 23:27:44 +00:00
|
|
|
return false;
|
2020-12-01 22:44:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 23:53:47 +00:00
|
|
|
void TimerQueue::remove_timer_locked(Queue& queue, Timer& timer)
|
2020-12-01 22:44:52 +00:00
|
|
|
{
|
2021-06-15 09:44:09 +00:00
|
|
|
bool was_next_timer = (queue.list.first() == &timer);
|
|
|
|
queue.list.remove(timer);
|
2020-12-04 05:12:50 +00:00
|
|
|
auto now = timer.now(false);
|
2020-12-01 22:44:52 +00:00
|
|
|
if (timer.m_expires > now)
|
|
|
|
timer.m_remaining = timer.m_expires - now;
|
2020-11-15 18:58:19 +00:00
|
|
|
|
|
|
|
if (was_next_timer)
|
2020-12-01 23:53:47 +00:00
|
|
|
update_next_timer_due(queue);
|
2020-12-11 18:07:42 +00:00
|
|
|
// Whenever we remove a timer that was still queued (but hasn't been
|
|
|
|
// fired) we added a reference to it. So, when removing it from the
|
|
|
|
// queue we need to drop that reference.
|
|
|
|
timer.unref();
|
2020-11-15 18:58:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 00:58:28 +00:00
|
|
|
void TimerQueue::fire()
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(g_timerqueue_lock);
|
2019-12-27 00:58:28 +00:00
|
|
|
|
2020-12-01 23:53:47 +00:00
|
|
|
auto fire_timers = [&](Queue& queue) {
|
2021-06-15 09:44:09 +00:00
|
|
|
auto* timer = queue.list.first();
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(timer);
|
|
|
|
VERIFY(queue.next_timer_due == timer->m_expires);
|
2019-12-27 00:58:28 +00:00
|
|
|
|
2020-12-04 05:12:50 +00:00
|
|
|
while (timer && timer->now(true) > timer->m_expires) {
|
2021-06-15 09:44:09 +00:00
|
|
|
queue.list.remove(*timer);
|
2020-12-11 20:41:51 +00:00
|
|
|
|
2021-06-15 09:44:09 +00:00
|
|
|
m_timers_executing.append(*timer);
|
2020-11-15 18:58:19 +00:00
|
|
|
|
2020-12-01 23:53:47 +00:00
|
|
|
update_next_timer_due(queue);
|
2020-11-15 18:58:19 +00:00
|
|
|
|
2020-12-01 23:53:47 +00:00
|
|
|
lock.unlock();
|
2020-12-01 20:02:54 +00:00
|
|
|
|
2020-12-11 20:41:51 +00:00
|
|
|
// Defer executing the timer outside of the irq handler
|
2021-01-30 05:49:28 +00:00
|
|
|
Processor::deferred_call_queue([this, timer]() {
|
|
|
|
// Check if we were cancelled in between being triggered
|
|
|
|
// by the timer irq handler and now. If so, just drop
|
|
|
|
// our reference and don't execute the callback.
|
|
|
|
if (!timer->set_cancelled()) {
|
|
|
|
timer->m_callback();
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(g_timerqueue_lock);
|
2021-01-30 05:49:28 +00:00
|
|
|
m_timers_executing.remove(*timer);
|
|
|
|
}
|
2021-07-31 23:27:44 +00:00
|
|
|
timer->clear_in_use();
|
2021-01-30 05:49:28 +00:00
|
|
|
timer->set_callback_finished();
|
2020-12-11 20:41:51 +00:00
|
|
|
// Drop the reference we added when queueing the timer
|
|
|
|
timer->unref();
|
|
|
|
});
|
2020-12-01 20:02:54 +00:00
|
|
|
|
2020-12-11 20:41:51 +00:00
|
|
|
lock.lock();
|
2021-06-15 09:44:09 +00:00
|
|
|
timer = queue.list.first();
|
2020-12-01 23:53:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!m_timer_queue_monotonic.list.is_empty())
|
|
|
|
fire_timers(m_timer_queue_monotonic);
|
|
|
|
if (!m_timer_queue_realtime.list.is_empty())
|
|
|
|
fire_timers(m_timer_queue_realtime);
|
2019-12-27 00:58:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 23:53:47 +00:00
|
|
|
void TimerQueue::update_next_timer_due(Queue& queue)
|
2019-12-27 00:58:28 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(g_timerqueue_lock.is_locked());
|
2020-11-15 18:58:19 +00:00
|
|
|
|
2021-06-15 09:44:09 +00:00
|
|
|
if (auto* next_timer = queue.list.first())
|
2020-12-01 23:53:47 +00:00
|
|
|
queue.next_timer_due = next_timer->m_expires;
|
2019-12-27 00:58:28 +00:00
|
|
|
else
|
2021-02-27 22:56:16 +00:00
|
|
|
queue.next_timer_due = {};
|
2019-12-27 00:58:28 +00:00
|
|
|
}
|
2020-02-16 00:27:42 +00:00
|
|
|
|
|
|
|
}
|