WorkQueue.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <Kernel/Process.h>
  8. #include <Kernel/Sections.h>
  9. #include <Kernel/WaitQueue.h>
  10. #include <Kernel/WorkQueue.h>
  11. namespace Kernel {
  12. WorkQueue* g_io_work;
  13. WorkQueue* g_ata_work;
  14. UNMAP_AFTER_INIT void WorkQueue::initialize()
  15. {
  16. g_io_work = new WorkQueue("IO WorkQueue Task"sv);
  17. g_ata_work = new WorkQueue("ATA WorkQueue Task"sv);
  18. }
  19. UNMAP_AFTER_INIT WorkQueue::WorkQueue(StringView name)
  20. {
  21. RefPtr<Thread> thread;
  22. auto name_kstring = KString::try_create(name);
  23. if (name_kstring.is_error())
  24. TODO();
  25. (void)Process::create_kernel_process(thread, name_kstring.release_value(), [this] {
  26. for (;;) {
  27. WorkItem* item;
  28. bool have_more;
  29. m_items.with([&](auto& items) {
  30. item = items.take_first();
  31. have_more = !items.is_empty();
  32. });
  33. if (item) {
  34. item->function();
  35. delete item;
  36. if (have_more)
  37. continue;
  38. }
  39. [[maybe_unused]] auto result = m_wait_queue.wait_on({});
  40. }
  41. });
  42. // If we can't create the thread we're in trouble...
  43. m_thread = thread.release_nonnull();
  44. }
  45. void WorkQueue::do_queue(WorkItem& item)
  46. {
  47. m_items.with([&](auto& items) {
  48. items.append(item);
  49. });
  50. m_wait_queue.wake_one();
  51. }
  52. }