2021-02-06 09:36:38 +03:00
|
|
|
/*
|
2021-04-28 23:46:44 +03:00
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
2021-10-26 11:44:10 +03:00
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
2021-02-06 09:36:38 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-06 09:36:38 +03:00
|
|
|
*/
|
|
|
|
|
2023-02-23 01:49:34 +03:00
|
|
|
#include <Kernel/Arch/Processor.h>
|
2021-06-22 18:40:16 +03:00
|
|
|
#include <Kernel/Sections.h>
|
2023-02-24 20:45:37 +03:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
|
|
|
#include <Kernel/Tasks/WaitQueue.h>
|
|
|
|
#include <Kernel/Tasks/WorkQueue.h>
|
2021-02-06 09:36:38 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
WorkQueue* g_io_work;
|
2021-11-26 20:39:26 +03:00
|
|
|
WorkQueue* g_ata_work;
|
2021-02-06 09:36:38 +03:00
|
|
|
|
2021-06-09 10:51:36 +03:00
|
|
|
UNMAP_AFTER_INIT void WorkQueue::initialize()
|
2021-02-06 09:36:38 +03:00
|
|
|
{
|
2022-07-11 20:32:29 +03:00
|
|
|
g_io_work = new WorkQueue("IO WorkQueue Task"sv);
|
2021-11-26 20:39:26 +03:00
|
|
|
g_ata_work = new WorkQueue("ATA WorkQueue Task"sv);
|
2021-02-06 09:36:38 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:53:28 +03:00
|
|
|
UNMAP_AFTER_INIT WorkQueue::WorkQueue(StringView name)
|
2021-02-06 09:36:38 +03:00
|
|
|
{
|
2023-07-17 18:34:19 +03:00
|
|
|
auto [_, thread] = Process::create_kernel_process(name, [this] {
|
2023-06-27 16:19:39 +03:00
|
|
|
while (!Process::current().is_dying()) {
|
2021-02-06 09:36:38 +03:00
|
|
|
WorkItem* item;
|
|
|
|
bool have_more;
|
2021-10-26 11:44:10 +03:00
|
|
|
m_items.with([&](auto& items) {
|
|
|
|
item = items.take_first();
|
|
|
|
have_more = !items.is_empty();
|
|
|
|
});
|
2021-02-06 09:36:38 +03:00
|
|
|
if (item) {
|
2021-05-19 15:42:16 +03:00
|
|
|
item->function();
|
2021-02-06 09:36:38 +03:00
|
|
|
delete item;
|
|
|
|
|
|
|
|
if (have_more)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
[[maybe_unused]] auto result = m_wait_queue.wait_on({});
|
|
|
|
}
|
2023-06-27 16:19:39 +03:00
|
|
|
Process::current().sys$exit(0);
|
|
|
|
VERIFY_NOT_REACHED();
|
2023-04-02 20:25:36 +03:00
|
|
|
}).release_value_but_fixme_should_propagate_errors();
|
|
|
|
m_thread = move(thread);
|
2021-02-06 09:36:38 +03:00
|
|
|
}
|
|
|
|
|
2022-03-09 22:26:08 +03:00
|
|
|
void WorkQueue::do_queue(WorkItem& item)
|
2021-02-06 09:36:38 +03:00
|
|
|
{
|
2021-10-26 11:44:10 +03:00
|
|
|
m_items.with([&](auto& items) {
|
2022-03-09 22:26:08 +03:00
|
|
|
items.append(item);
|
2021-10-26 11:44:10 +03:00
|
|
|
});
|
2021-02-06 09:36:38 +03:00
|
|
|
m_wait_queue.wake_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|