2021-02-05 23:36:38 -07:00
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
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
|
|
|
*/
|
|
|
|
|
2021-08-22 01:37:17 +02:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2021-02-05 23:36:38 -07:00
|
|
|
#include <Kernel/Process.h>
|
2021-06-22 17:40:16 +02:00
|
|
|
#include <Kernel/Sections.h>
|
2021-02-05 23:36:38 -07:00
|
|
|
#include <Kernel/WaitQueue.h>
|
|
|
|
#include <Kernel/WorkQueue.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
WorkQueue* g_io_work;
|
|
|
|
|
2021-06-09 00:51:36 -07:00
|
|
|
UNMAP_AFTER_INIT void WorkQueue::initialize()
|
2021-02-05 23:36:38 -07:00
|
|
|
{
|
|
|
|
g_io_work = new WorkQueue("IO WorkQueue");
|
|
|
|
}
|
|
|
|
|
2021-06-09 00:51:36 -07:00
|
|
|
UNMAP_AFTER_INIT WorkQueue::WorkQueue(const char* name)
|
2021-02-05 23:36:38 -07:00
|
|
|
{
|
|
|
|
RefPtr<Thread> thread;
|
|
|
|
Process::create_kernel_process(thread, name, [this] {
|
|
|
|
for (;;) {
|
|
|
|
WorkItem* item;
|
|
|
|
bool have_more;
|
|
|
|
{
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-05 23:36:38 -07:00
|
|
|
item = m_items.take_first();
|
|
|
|
have_more = !m_items.is_empty();
|
|
|
|
}
|
|
|
|
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({});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// If we can't create the thread we're in trouble...
|
|
|
|
m_thread = thread.release_nonnull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkQueue::do_queue(WorkItem* item)
|
|
|
|
{
|
|
|
|
{
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-02-05 23:36:38 -07:00
|
|
|
m_items.append(*item);
|
|
|
|
}
|
|
|
|
m_wait_queue.wake_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|