ThreadEventQueue.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullOwnPtr.h>
  8. #include <AK/OwnPtr.h>
  9. namespace Core {
  10. // Per-thread global event queue. This is where events are queued for the EventLoop to process.
  11. // There is only one ThreadEventQueue per thread, and it is accessed via ThreadEventQueue::current().
  12. // It is allowed to post events to other threads' event queues.
  13. class ThreadEventQueue {
  14. AK_MAKE_NONCOPYABLE(ThreadEventQueue);
  15. AK_MAKE_NONMOVABLE(ThreadEventQueue);
  16. public:
  17. static ThreadEventQueue& current();
  18. // Process all queued events. Returns the number of events that were processed.
  19. size_t process();
  20. // Posts an event to the event queue.
  21. void post_event(EventReceiver& receiver, NonnullOwnPtr<Event>);
  22. // Used by Threading::BackgroundAction.
  23. void add_job(NonnullRefPtr<Promise<NonnullRefPtr<EventReceiver>>>);
  24. void cancel_all_pending_jobs();
  25. // Returns true if there are events waiting to be flushed.
  26. bool has_pending_events() const;
  27. private:
  28. ThreadEventQueue();
  29. ~ThreadEventQueue();
  30. struct Private;
  31. OwnPtr<Private> m_private;
  32. };
  33. }