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/EventQueue.h"
11 #include "mozilla/CondVar.h"
12 #include "mozilla/SynchronizedEventQueue.h"
17 class nsISerialEventTarget
;
18 class nsIThreadObserver
;
23 class ThreadEventTarget
;
25 // A ThreadEventQueue implements normal monitor-style synchronization over the
26 // EventQueue. It also implements PushEventQueue and PopEventQueue for workers
27 // (see the documentation below for an explanation of those). All threads use a
28 // ThreadEventQueue as their event queue. Although for the main thread this
29 // simply forwards events to the TaskController.
30 class ThreadEventQueue final
: public SynchronizedEventQueue
{
32 explicit ThreadEventQueue(UniquePtr
<EventQueue
> aQueue
,
33 bool aIsMainThread
= false);
35 bool PutEvent(already_AddRefed
<nsIRunnable
>&& aEvent
,
36 EventQueuePriority aPriority
) final
;
38 already_AddRefed
<nsIRunnable
> GetEvent(
39 bool aMayWait
, mozilla::TimeDuration
* aLastEventDelay
= nullptr) final
;
40 bool HasPendingEvent() final
;
42 bool ShutdownIfNoPendingEvents() final
;
44 void Disconnect(const MutexAutoLock
& aProofOfLock
) final
{}
46 already_AddRefed
<nsISerialEventTarget
> PushEventQueue() final
;
47 void PopEventQueue(nsIEventTarget
* aTarget
) final
;
49 already_AddRefed
<nsIThreadObserver
> GetObserver() final
;
50 already_AddRefed
<nsIThreadObserver
> GetObserverOnThread() final
;
51 void SetObserver(nsIThreadObserver
* aObserver
) final
;
53 Mutex
& MutexRef() { return mLock
; }
55 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf
) override
;
60 virtual ~ThreadEventQueue();
62 bool PutEventInternal(already_AddRefed
<nsIRunnable
>&& aEvent
,
63 EventQueuePriority aPriority
, NestedSink
* aQueue
);
65 const UniquePtr
<EventQueue
> mBaseQueue
;
67 struct NestedQueueItem
{
68 UniquePtr
<EventQueue
> mQueue
;
69 RefPtr
<ThreadEventTarget
> mEventTarget
;
71 NestedQueueItem(UniquePtr
<EventQueue
> aQueue
,
72 ThreadEventTarget
* aEventTarget
);
75 nsTArray
<NestedQueueItem
> mNestedQueues
;
78 CondVar mEventsAvailable
;
80 bool mEventsAreDoomed
= false;
81 nsCOMPtr
<nsIThreadObserver
> mObserver
;
85 } // namespace mozilla
87 #endif // mozilla_ThreadEventQueue_h