/* * Copyright (c) 2021, the SerenityOS developers. * Copyright (c) 2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include namespace Kernel { extern WorkQueue* g_io_work; extern WorkQueue* g_ata_work; class WorkQueue { AK_MAKE_NONCOPYABLE(WorkQueue); AK_MAKE_NONMOVABLE(WorkQueue); public: static void initialize(); ErrorOr try_queue(void (*function)(void*), void* data = nullptr, void (*free_data)(void*) = nullptr) { auto item = new (nothrow) WorkItem; // TODO: use a pool if (!item) return Error::from_errno(ENOMEM); item->function = [function, data, free_data] { function(data); if (free_data) free_data(data); }; do_queue(*item); return {}; } template ErrorOr try_queue(Function function) { auto item = new (nothrow) WorkItem; // TODO: use a pool if (!item) return Error::from_errno(ENOMEM); item->function = Function(function); do_queue(*item); return {}; } private: explicit WorkQueue(StringView); struct WorkItem { public: IntrusiveListNode m_node; Function function; }; void do_queue(WorkItem&); RefPtr m_thread; WaitQueue m_wait_queue; SpinlockProtected, LockRank::None> m_items {}; }; }