2021-02-06 06:36:38 +00:00
|
|
|
/*
|
2021-04-28 20:46:44 +00:00
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
2021-10-26 08:44:10 +00:00
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
2021-02-06 06:36:38 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-06 06:36:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Kernel/Process.h>
|
2021-06-22 15:40:16 +00:00
|
|
|
#include <Kernel/Sections.h>
|
2021-02-06 06:36:38 +00:00
|
|
|
#include <Kernel/WaitQueue.h>
|
|
|
|
#include <Kernel/WorkQueue.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
WorkQueue* g_io_work;
|
|
|
|
|
2021-06-09 07:51:36 +00:00
|
|
|
UNMAP_AFTER_INIT void WorkQueue::initialize()
|
2021-02-06 06:36:38 +00:00
|
|
|
{
|
|
|
|
g_io_work = new WorkQueue("IO WorkQueue");
|
|
|
|
}
|
|
|
|
|
2021-09-07 10:53:28 +00:00
|
|
|
UNMAP_AFTER_INIT WorkQueue::WorkQueue(StringView name)
|
2021-02-06 06:36:38 +00:00
|
|
|
{
|
|
|
|
RefPtr<Thread> thread;
|
2021-09-07 10:53:28 +00:00
|
|
|
auto name_kstring = KString::try_create(name);
|
|
|
|
if (name_kstring.is_error())
|
|
|
|
TODO();
|
2021-12-03 10:23:09 +00:00
|
|
|
(void)Process::create_kernel_process(thread, name_kstring.release_value(), [this] {
|
2021-02-06 06:36:38 +00:00
|
|
|
for (;;) {
|
|
|
|
WorkItem* item;
|
|
|
|
bool have_more;
|
2021-10-26 08:44:10 +00:00
|
|
|
m_items.with([&](auto& items) {
|
|
|
|
item = items.take_first();
|
|
|
|
have_more = !items.is_empty();
|
|
|
|
});
|
2021-02-06 06:36:38 +00:00
|
|
|
if (item) {
|
2021-05-19 12:42:16 +00:00
|
|
|
item->function();
|
2021-02-06 06:36:38 +00:00
|
|
|
delete item;
|
|
|
|
|
|
|
|
if (have_more)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
[[maybe_unused]] auto result = m_wait_queue.wait_on({});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// If we can't create the thread we're in trouble...
|
|
|
|
m_thread = thread.release_nonnull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkQueue::do_queue(WorkItem* item)
|
|
|
|
{
|
2021-10-26 08:44:10 +00:00
|
|
|
m_items.with([&](auto& items) {
|
|
|
|
items.append(*item);
|
|
|
|
});
|
2021-02-06 06:36:38 +00:00
|
|
|
m_wait_queue.wake_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|