1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-19 00:45:08 +00:00

Kernel: Remove an allocation when blocking a thread

When blocking a thread with a timeout we would previously allocate
a Timer object. This removes the allocation for that Timer object.
This commit is contained in:
Gunnar Beutner 2021-05-20 00:41:51 +02:00 committed by Andreas Kling
parent 96b75af5d1
commit 7557f2db90
5 changed files with 42 additions and 23 deletions

View file

@ -57,22 +57,22 @@ UNMAP_AFTER_INIT TimerQueue::TimerQueue()
m_ticks_per_second = TimeManagement::the().ticks_per_second();
}
RefPtr<Timer> TimerQueue::add_timer_without_id(clockid_t clock_id, const Time& deadline, Function<void()>&& callback)
bool TimerQueue::add_timer_without_id(NonnullRefPtr<Timer> timer, clockid_t clock_id, const Time& deadline, Function<void()>&& callback)
{
if (deadline <= TimeManagement::the().current_time(clock_id))
return {};
return false;
// Because timer handlers can execute on any processor and there is
// a race between executing a timer handler and cancel_timer() this
// *must* be a RefPtr<Timer>. Otherwise calling cancel_timer() could
// inadvertently cancel another timer that has been created between
// returning from the timer handler and a call to cancel_timer().
auto timer = adopt_ref(*new Timer(clock_id, deadline, move(callback)));
timer->setup(clock_id, deadline, move(callback));
ScopedSpinLock lock(g_timerqueue_lock);
timer->m_id = 0; // Don't generate a timer id
add_timer_locked(timer);
return timer;
add_timer_locked(move(timer));
return true;
}
TimerId TimerQueue::add_timer(NonnullRefPtr<Timer>&& timer)
@ -119,7 +119,10 @@ TimerId TimerQueue::add_timer(clockid_t clock_id, const Time& deadline, Function
{
auto expires = TimeManagement::the().current_time(clock_id);
expires = expires + deadline;
return add_timer(adopt_ref(*new Timer(clock_id, expires, move(callback))));
auto timer = new Timer();
VERIFY(timer);
timer->setup(clock_id, expires, move(callback));
return add_timer(adopt_ref(*timer));
}
bool TimerQueue::cancel_timer(TimerId id)