ThreadEventQueue.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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(Object& receiver, NonnullOwnPtr<Event>);
  22. // Used by Threading::BackgroundAction.
  23. void add_job(NonnullRefPtr<Promise<NonnullRefPtr<Object>>>);
  24. // Returns true if there are events waiting to be flushed.
  25. bool has_pending_events() const;
  26. private:
  27. ThreadEventQueue();
  28. ~ThreadEventQueue();
  29. struct Private;
  30. OwnPtr<Private> m_private;
  31. };
  32. }