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_ThreadEventTarget_h
8 #define mozilla_ThreadEventTarget_h
10 #include "mozilla/MemoryReporting.h"
11 #include "mozilla/Mutex.h"
12 #include "mozilla/SynchronizedEventQueue.h" // for ThreadTargetSink
13 #include "nsIDelayedRunnableObserver.h"
14 #include "nsISerialEventTarget.h"
17 class DelayedRunnable
;
19 // ThreadEventTarget handles the details of posting an event to a thread. It can
20 // be used with any ThreadTargetSink implementation.
21 class ThreadEventTarget final
: public nsISerialEventTarget
,
22 public nsIDelayedRunnableObserver
{
24 ThreadEventTarget(ThreadTargetSink
* aSink
, bool aIsMainThread
);
26 NS_DECL_THREADSAFE_ISUPPORTS
27 NS_DECL_NSIEVENTTARGET_FULL
29 // nsIDelayedRunnableObserver
30 void OnDelayedRunnableCreated(DelayedRunnable
* aRunnable
) override
;
31 void OnDelayedRunnableScheduled(DelayedRunnable
* aRunnable
) override
;
32 void OnDelayedRunnableRan(DelayedRunnable
* aRunnable
) override
;
34 // Notification from, and on, the owner thread that it is shutting down.
35 // Cancels any scheduled DelayedRunnables.
36 void NotifyShutdown();
38 // Disconnects the target so that it can no longer post events.
39 void Disconnect(const MutexAutoLock
& aProofOfLock
) {
40 mSink
->Disconnect(aProofOfLock
);
43 // Sets the thread for which IsOnCurrentThread returns true to the current
45 void SetCurrentThread(PRThread
* aThread
);
46 // Call ClearCurrentThread() before the PRThread is deleted on thread join.
47 void ClearCurrentThread();
49 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const {
52 n
+= mSink
->SizeOfIncludingThis(aMallocSizeOf
);
54 return aMallocSizeOf(this) + n
;
60 RefPtr
<ThreadTargetSink
> mSink
;
63 // DelayedRunnables (from DelayedDispatch) that are managed by their
64 // respective timers, but have not yet run. Accessed only on this nsThread.
65 nsTArray
<RefPtr
<mozilla::DelayedRunnable
>> mScheduledDelayedRunnables
;
68 } // namespace mozilla
70 #endif // mozilla_ThreadEventTarget_h