Bug 1586807 - Make pseudoclass locking work with Fission. r=pbro
[gecko.git] / xpcom / threads / ThreadEventQueue.h
blob5ed7c3990ed0f3eb9740472e2470715ea3b9a382
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_ThreadEventQueue_h
8 #define mozilla_ThreadEventQueue_h
10 #include "mozilla/AbstractEventQueue.h"
11 #include "mozilla/CondVar.h"
12 #include "mozilla/SynchronizedEventQueue.h"
13 #include "nsCOMPtr.h"
14 #include "nsTArray.h"
16 class nsIEventTarget;
17 class nsISerialEventTarget;
18 class nsIThreadObserver;
20 namespace mozilla {
22 class EventQueue;
23 class PrioritizedEventQueue;
24 class ThreadEventTarget;
26 // A ThreadEventQueue implements normal monitor-style synchronization over the
27 // InnerQueueT AbstractEventQueue. It also implements PushEventQueue and
28 // PopEventQueue for workers (see the documentation below for an explanation of
29 // those). All threads use a ThreadEventQueue as their event queue. InnerQueueT
30 // is a template parameter to avoid virtual dispatch overhead.
31 template <class InnerQueueT>
32 class ThreadEventQueue final : public SynchronizedEventQueue {
33 public:
34 explicit ThreadEventQueue(UniquePtr<InnerQueueT> aQueue);
36 bool PutEvent(already_AddRefed<nsIRunnable>&& aEvent,
37 EventQueuePriority aPriority) final;
39 already_AddRefed<nsIRunnable> GetEvent(bool aMayWait,
40 EventQueuePriority* aPriority) final;
41 void DidRunEvent() final;
42 bool HasPendingEvent() final;
43 bool HasPendingHighPriorityEvents() final;
45 bool ShutdownIfNoPendingEvents() final;
47 void Disconnect(const MutexAutoLock& aProofOfLock) final {}
49 void EnableInputEventPrioritization() final;
50 void FlushInputEventPrioritization() final;
51 void SuspendInputEventPrioritization() final;
52 void ResumeInputEventPrioritization() final;
54 already_AddRefed<nsISerialEventTarget> PushEventQueue() final;
55 void PopEventQueue(nsIEventTarget* aTarget) final;
57 already_AddRefed<nsIThreadObserver> GetObserver() final;
58 already_AddRefed<nsIThreadObserver> GetObserverOnThread() final;
59 void SetObserver(nsIThreadObserver* aObserver) final;
61 Mutex& MutexRef() { return mLock; }
63 size_t SizeOfExcludingThis(
64 mozilla::MallocSizeOf aMallocSizeOf) const override;
66 private:
67 class NestedSink;
69 virtual ~ThreadEventQueue();
71 bool PutEventInternal(already_AddRefed<nsIRunnable>&& aEvent,
72 EventQueuePriority aPriority, NestedSink* aQueue);
74 UniquePtr<InnerQueueT> mBaseQueue;
76 struct NestedQueueItem {
77 UniquePtr<EventQueue> mQueue;
78 RefPtr<ThreadEventTarget> mEventTarget;
80 NestedQueueItem(UniquePtr<EventQueue> aQueue,
81 ThreadEventTarget* aEventTarget)
82 : mQueue(std::move(aQueue)), mEventTarget(aEventTarget) {}
85 nsTArray<NestedQueueItem> mNestedQueues;
87 Mutex mLock;
88 CondVar mEventsAvailable;
90 bool mEventsAreDoomed = false;
91 nsCOMPtr<nsIThreadObserver> mObserver;
94 extern template class ThreadEventQueue<EventQueue>;
95 extern template class ThreadEventQueue<PrioritizedEventQueue>;
97 }; // namespace mozilla
99 #endif // mozilla_ThreadEventQueue_h