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