Bug 1867190 - Initialise the PHC allocate delay later r=glandium
[gecko.git] / xpcom / threads / SynchronizedEventQueue.h
blobe4cf1a62af1b37dc0a849cc89091b7863dc1d555
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_SynchronizedEventQueue_h
8 #define mozilla_SynchronizedEventQueue_h
10 #include "mozilla/AlreadyAddRefed.h"
11 #include "mozilla/EventQueue.h"
12 #include "mozilla/MemoryReporting.h"
13 #include "mozilla/Mutex.h"
14 #include "nsIThreadInternal.h"
15 #include "nsCOMPtr.h"
16 #include "nsTObserverArray.h"
18 class nsIEventTarget;
19 class nsISerialEventTarget;
20 class nsIThreadObserver;
22 namespace mozilla {
24 // A SynchronizedEventQueue is an abstract class for event queues that can be
25 // used across threads. A SynchronizedEventQueue implementation will typically
26 // use locks and condition variables to guarantee consistency. The methods of
27 // SynchronizedEventQueue are split between ThreadTargetSink (which contains
28 // methods for posting events) and SynchronizedEventQueue (which contains
29 // methods for getting events). This split allows event targets (specifically
30 // ThreadEventTarget) to use a narrow interface, since they only need to post
31 // events.
33 // ThreadEventQueue is the canonical implementation of
34 // SynchronizedEventQueue. When Quantum DOM is implemented, we will use a
35 // different synchronized queue on the main thread, SchedulerEventQueue, which
36 // will handle the cooperative threading model.
38 class ThreadTargetSink {
39 public:
40 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ThreadTargetSink)
42 virtual bool PutEvent(already_AddRefed<nsIRunnable>&& aEvent,
43 EventQueuePriority aPriority) = 0;
45 // After this method is called, no more events can be posted.
46 virtual void Disconnect(const MutexAutoLock& aProofOfLock) = 0;
48 virtual nsresult RegisterShutdownTask(nsITargetShutdownTask* aTask) = 0;
49 virtual nsresult UnregisterShutdownTask(nsITargetShutdownTask* aTask) = 0;
51 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) {
52 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
55 // Not const because overrides may need to take a lock
56 virtual size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) = 0;
58 protected:
59 virtual ~ThreadTargetSink() = default;
62 class SynchronizedEventQueue : public ThreadTargetSink {
63 public:
64 virtual already_AddRefed<nsIRunnable> GetEvent(
65 bool aMayWait, mozilla::TimeDuration* aLastEventDelay = nullptr) = 0;
66 virtual bool HasPendingEvent() = 0;
68 // This method atomically checks if there are pending events and, if there are
69 // none, forbids future events from being posted. It returns true if there
70 // were no pending events.
71 virtual bool ShutdownIfNoPendingEvents() = 0;
73 // These methods provide access to an nsIThreadObserver, whose methods are
74 // called when posting and processing events. SetObserver should only be
75 // called on the thread that processes events. GetObserver can be called from
76 // any thread. GetObserverOnThread must be used from the thread that processes
77 // events; it does not acquire a lock.
78 virtual already_AddRefed<nsIThreadObserver> GetObserver() = 0;
79 virtual already_AddRefed<nsIThreadObserver> GetObserverOnThread() = 0;
80 virtual void SetObserver(nsIThreadObserver* aObserver) = 0;
82 void AddObserver(nsIThreadObserver* aObserver);
83 void RemoveObserver(nsIThreadObserver* aObserver);
84 const nsTObserverArray<nsCOMPtr<nsIThreadObserver>>& EventObservers();
86 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) override {
87 // Normally we'd return
88 // mEventObservers.ShallowSizeOfExcludingThis(aMallocSizeOf); However,
89 // mEventObservers may be being mutated on another thread, and we don't lock
90 // around access, so locking here wouldn't help. They're small, so
91 return 0;
94 /**
95 * This method causes any events currently enqueued on the thread to be
96 * suppressed until PopEventQueue is called, and any event dispatched to this
97 * thread's nsIEventTarget will queue as well. Calls to PushEventQueue may be
98 * nested and must each be paired with a call to PopEventQueue in order to
99 * restore the original state of the thread. The returned nsIEventTarget may
100 * be used to push events onto the nested queue. Dispatching will be disabled
101 * once the event queue is popped. The thread will only ever process pending
102 * events for the innermost event queue. Must only be called on the target
103 * thread.
105 virtual already_AddRefed<nsISerialEventTarget> PushEventQueue() = 0;
108 * Revert a call to PushEventQueue. When an event queue is popped, any events
109 * remaining in the queue are appended to the elder queue. This also causes
110 * the nsIEventTarget returned from PushEventQueue to stop dispatching events.
111 * Must only be called on the target thread, and with the innermost event
112 * queue.
114 virtual void PopEventQueue(nsIEventTarget* aTarget) = 0;
117 * Flush the list of shutdown tasks which were previously registered. After
118 * this is called, new shutdown tasks cannot be registered.
120 virtual void RunShutdownTasks() = 0;
122 protected:
123 virtual ~SynchronizedEventQueue() = default;
125 private:
126 nsTObserverArray<nsCOMPtr<nsIThreadObserver>> mEventObservers;
129 } // namespace mozilla
131 #endif // mozilla_SynchronizedEventQueue_h