1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:38:11 +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

@ -13,10 +13,10 @@ KResultOr<unsigned> Process::sys$alarm(unsigned seconds)
{
REQUIRE_PROMISE(stdio);
unsigned previous_alarm_remaining = 0;
if (auto alarm_timer = move(m_alarm_timer)) {
if (TimerQueue::the().cancel_timer(*alarm_timer)) {
if (m_alarm_timer) {
if (TimerQueue::the().cancel_timer(*m_alarm_timer)) {
// The timer hasn't fired. Round up the remaining time (if any)
Time remaining = alarm_timer->remaining() + Time::from_nanoseconds(999'999'999);
Time remaining = m_alarm_timer->remaining() + Time::from_nanoseconds(999'999'999);
previous_alarm_remaining = remaining.to_truncated_seconds();
}
// We had an existing alarm, must return a non-zero value here!
@ -27,9 +27,16 @@ KResultOr<unsigned> Process::sys$alarm(unsigned seconds)
if (seconds > 0) {
auto deadline = TimeManagement::the().current_time(CLOCK_REALTIME_COARSE);
deadline = deadline + Time::from_seconds(seconds);
m_alarm_timer = TimerQueue::the().add_timer_without_id(CLOCK_REALTIME_COARSE, deadline, [this]() {
if (!m_alarm_timer) {
m_alarm_timer = adopt_ref_if_nonnull(new Timer());
if (!m_alarm_timer)
return ENOMEM;
}
auto timer_was_added = TimerQueue::the().add_timer_without_id(*m_alarm_timer, CLOCK_REALTIME_COARSE, deadline, [this]() {
[[maybe_unused]] auto rc = send_signal(SIGALRM, nullptr);
});
if (!timer_was_added)
return ENOMEM;
}
return previous_alarm_remaining;
}