WorkletThread.h (2192B)
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_dom_worklet_WorkletThread_h 8 #define mozilla_dom_worklet_WorkletThread_h 9 10 #include "mozilla/CondVar.h" 11 #include "mozilla/RefPtr.h" 12 #include "mozilla/dom/WorkletImpl.h" 13 #include "nsIObserver.h" 14 #include "nsThread.h" 15 16 class nsIRunnable; 17 18 namespace JS { 19 class ContextOptions; 20 }; // namespace JS 21 22 namespace mozilla::dom { 23 24 class WorkletThread final : public nsThread, public nsIObserver { 25 public: 26 NS_DECL_ISUPPORTS_INHERITED 27 NS_DECL_NSIOBSERVER 28 29 static already_AddRefed<WorkletThread> Create(WorkletImpl* aWorkletImpl); 30 31 // Threads that call EnsureCycleCollectedJSContext must call 32 // DeleteCycleCollectedJSContext::Get() before terminating. Clients of 33 // Create() do not need to do this as Terminate() will ensure this happens. 34 static void EnsureCycleCollectedJSContext(JSRuntime* aParentRuntime, 35 const JS::ContextOptions& aOptions); 36 static void DeleteCycleCollectedJSContext(); 37 38 static bool IsOnWorkletThread(); 39 40 static void AssertIsOnWorkletThread(); 41 42 nsresult DispatchRunnable(already_AddRefed<nsIRunnable> aRunnable); 43 44 void Terminate(); 45 46 // Recommended native stack size to use, greater than the stack size 47 // internally used by SpiderMonkey. 48 static uint32_t StackSize(); 49 50 private: 51 explicit WorkletThread(WorkletImpl* aWorkletImpl); 52 ~WorkletThread(); 53 54 void RunEventLoop(); 55 class PrimaryRunnable; 56 57 void TerminateInternal(); 58 class TerminateRunnable; 59 60 // This should only be called by consumers that have an 61 // nsIEventTarget/nsIThread pointer. 62 using nsThread::DelayedDispatch; 63 using nsThread::Dispatch; 64 using nsThread::DispatchFromScript; 65 66 const RefPtr<WorkletImpl> mWorkletImpl; 67 68 bool mExitLoop; // worklet execution thread 69 70 bool mIsTerminating; // main thread 71 }; 72 73 } // namespace mozilla::dom 74 75 #endif // mozilla_dom_worklet_WorkletThread_h