2020-04-08 16:13:49 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-08 16:13:49 +03:00
|
|
|
*/
|
|
|
|
|
2021-06-22 18:40:16 +03:00
|
|
|
#include <Kernel/Sections.h>
|
2020-04-08 16:13:49 +03:00
|
|
|
#include <Kernel/Tasks/FinalizerTask.h>
|
2023-02-24 20:45:37 +03:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
|
|
|
#include <Kernel/Tasks/Scheduler.h>
|
2020-04-08 16:13:49 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2022-06-05 07:44:48 +03:00
|
|
|
static constexpr StringView finalizer_task_name = "Finalizer Task"sv;
|
|
|
|
|
2021-05-23 22:45:58 +03:00
|
|
|
static void finalizer_task(void*)
|
|
|
|
{
|
|
|
|
Thread::current()->set_priority(THREAD_PRIORITY_LOW);
|
2023-06-27 16:19:39 +03:00
|
|
|
while (!Process::current().is_dying()) {
|
2022-01-26 19:34:31 +03:00
|
|
|
// The order of this if-else is important: We want to continue trying to finalize the threads in case
|
|
|
|
// Thread::finalize_dying_threads set g_finalizer_has_work back to true due to OOM conditions
|
2021-05-23 22:45:58 +03:00
|
|
|
if (g_finalizer_has_work.exchange(false, AK::MemoryOrder::memory_order_acq_rel) == true)
|
|
|
|
Thread::finalize_dying_threads();
|
2022-01-26 19:34:31 +03:00
|
|
|
else
|
2022-06-05 07:44:48 +03:00
|
|
|
g_finalizer_wait_queue->wait_forever(finalizer_task_name);
|
2021-05-23 22:45:58 +03:00
|
|
|
}
|
2023-06-27 16:19:39 +03:00
|
|
|
Process::current().sys$exit(0);
|
|
|
|
VERIFY_NOT_REACHED();
|
2023-07-08 05:48:11 +03:00
|
|
|
}
|
2021-05-23 22:45:58 +03:00
|
|
|
|
2021-06-09 10:52:31 +03:00
|
|
|
UNMAP_AFTER_INIT void FinalizerTask::spawn()
|
2020-04-08 16:13:49 +03:00
|
|
|
{
|
2023-07-17 18:34:19 +03:00
|
|
|
auto [_, finalizer_thread] = MUST(Process::create_kernel_process(finalizer_task_name, finalizer_task, nullptr));
|
2023-04-02 20:25:36 +03:00
|
|
|
g_finalizer = move(finalizer_thread);
|
2020-04-08 16:13:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|