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_ThreadEventTarget_h 8 #define mozilla_ThreadEventTarget_h 9 10 #include "mozilla/MemoryReporting.h" 11 #include "mozilla/Mutex.h" 12 #include "mozilla/SynchronizedEventQueue.h" // for ThreadTargetSink 13 #include "nsISerialEventTarget.h" 14 15 namespace mozilla { 16 17 // ThreadEventTarget handles the details of posting an event to a thread. It can 18 // be used with any ThreadTargetSink implementation. 19 class ThreadEventTarget final : public nsISerialEventTarget { 20 public: 21 ThreadEventTarget(ThreadTargetSink* aSink, bool aIsMainThread); 22 23 NS_DECL_THREADSAFE_ISUPPORTS 24 NS_DECL_NSIEVENTTARGET_FULL 25 26 // Disconnects the target so that it can no longer post events. Disconnect(const MutexAutoLock & aProofOfLock)27 void Disconnect(const MutexAutoLock& aProofOfLock) { 28 mSink->Disconnect(aProofOfLock); 29 } 30 31 // Sets the thread for which IsOnCurrentThread returns true to the current 32 // thread. 33 void SetCurrentThread(); 34 // Call ClearCurrentThread() before the PRThread is deleted on thread join. 35 void ClearCurrentThread(); 36 SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf)37 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const { 38 size_t n = 0; 39 if (mSink) { 40 n += mSink->SizeOfIncludingThis(aMallocSizeOf); 41 } 42 return aMallocSizeOf(this) + n; 43 } 44 45 private: 46 ~ThreadEventTarget() = default; 47 48 RefPtr<ThreadTargetSink> mSink; 49 bool mIsMainThread; 50 }; 51 52 } // namespace mozilla 53 54 #endif // mozilla_ThreadEventTarget_h 55