/work/obj-fuzz/dist/include/mozilla/SynchronizedEventQueue.h
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #ifndef mozilla_SynchronizedEventQueue_h |
8 | | #define mozilla_SynchronizedEventQueue_h |
9 | | |
10 | | #include "mozilla/AlreadyAddRefed.h" |
11 | | #include "mozilla/AbstractEventQueue.h" |
12 | | #include "mozilla/MemoryReporting.h" |
13 | | #include "mozilla/Mutex.h" |
14 | | #include "nsTObserverArray.h" |
15 | | |
16 | | class nsIThreadObserver; |
17 | | |
18 | | namespace mozilla { |
19 | | |
20 | | // A SynchronizedEventQueue is an abstract class for event queues that can be |
21 | | // used across threads. A SynchronizedEventQueue implementation will typically |
22 | | // use locks and condition variables to guarantee consistency. The methods of |
23 | | // SynchronizedEventQueue are split between ThreadTargetSink (which contains |
24 | | // methods for posting events) and SynchronizedEventQueue (which contains |
25 | | // methods for getting events). This split allows event targets (specifically |
26 | | // ThreadEventTarget) to use a narrow interface, since they only need to post |
27 | | // events. |
28 | | // |
29 | | // ThreadEventQueue is the canonical implementation of |
30 | | // SynchronizedEventQueue. When Quantum DOM is implemented, we will use a |
31 | | // different synchronized queue on the main thread, SchedulerEventQueue, which |
32 | | // will handle the cooperative threading model. |
33 | | |
34 | | class ThreadTargetSink |
35 | | { |
36 | | public: |
37 | | NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ThreadTargetSink) |
38 | | |
39 | | virtual bool PutEvent(already_AddRefed<nsIRunnable>&& aEvent, |
40 | | EventPriority aPriority) = 0; |
41 | | |
42 | | // After this method is called, no more events can be posted. |
43 | | virtual void Disconnect(const MutexAutoLock& aProofOfLock) = 0; |
44 | | |
45 | | size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const |
46 | 0 | { |
47 | 0 | return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); |
48 | 0 | } |
49 | | |
50 | | virtual size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const = 0; |
51 | | |
52 | | protected: |
53 | 0 | virtual ~ThreadTargetSink() {} |
54 | | }; |
55 | | |
56 | | class SynchronizedEventQueue : public ThreadTargetSink |
57 | | { |
58 | | public: |
59 | | virtual already_AddRefed<nsIRunnable> GetEvent(bool aMayWait, |
60 | | EventPriority* aPriority) = 0; |
61 | | virtual bool HasPendingEvent() = 0; |
62 | | |
63 | | // This method atomically checks if there are pending events and, if there are |
64 | | // none, forbids future events from being posted. It returns true if there |
65 | | // were no pending events. |
66 | | virtual bool ShutdownIfNoPendingEvents() = 0; |
67 | | |
68 | | // These methods provide access to an nsIThreadObserver, whose methods are |
69 | | // called when posting and processing events. SetObserver should only be |
70 | | // called on the thread that processes events. GetObserver can be called from |
71 | | // any thread. GetObserverOnThread must be used from the thread that processes |
72 | | // events; it does not acquire a lock. |
73 | | virtual already_AddRefed<nsIThreadObserver> GetObserver() = 0; |
74 | | virtual already_AddRefed<nsIThreadObserver> GetObserverOnThread() = 0; |
75 | | virtual void SetObserver(nsIThreadObserver* aObserver) = 0; |
76 | | |
77 | | void AddObserver(nsIThreadObserver* aObserver); |
78 | | void RemoveObserver(nsIThreadObserver* aObserver); |
79 | | const nsTObserverArray<nsCOMPtr<nsIThreadObserver>>& EventObservers(); |
80 | | |
81 | | virtual void EnableInputEventPrioritization() = 0; |
82 | | virtual void FlushInputEventPrioritization() = 0; |
83 | | virtual void SuspendInputEventPrioritization() = 0; |
84 | | virtual void ResumeInputEventPrioritization() = 0; |
85 | | |
86 | | size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const override |
87 | 0 | { |
88 | 0 | return mEventObservers.ShallowSizeOfExcludingThis(aMallocSizeOf); |
89 | 0 | } |
90 | | |
91 | | protected: |
92 | 0 | virtual ~SynchronizedEventQueue() {} |
93 | | |
94 | | private: |
95 | | nsTObserverArray<nsCOMPtr<nsIThreadObserver>> mEventObservers; |
96 | | }; |
97 | | |
98 | | } // namespace mozilla |
99 | | |
100 | | #endif // mozilla_SynchronizedEventQueue_h |