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