Bug 1885602 - Part 5: Implement navigating to the SUMO help topic from the menu heade...
[gecko.git] / dom / worklet / WorkletThread.h
blobe6d0e414dced1d781c513a0febe79f6366d20f19
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_dom_worklet_WorkletThread_h
8 #define mozilla_dom_worklet_WorkletThread_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/CondVar.h"
12 #include "mozilla/RefPtr.h"
13 #include "mozilla/UniquePtr.h"
14 #include "mozilla/dom/WorkletImpl.h"
15 #include "nsIObserver.h"
16 #include "nsThread.h"
18 class nsIRunnable;
20 namespace JS {
21 class ContextOptions;
22 }; // namespace JS
24 namespace mozilla::dom {
26 class WorkletThread final : public nsThread, public nsIObserver {
27 public:
28 NS_DECL_ISUPPORTS_INHERITED
29 NS_DECL_NSIOBSERVER
31 static already_AddRefed<WorkletThread> Create(WorkletImpl* aWorkletImpl);
33 // Threads that call EnsureCycleCollectedJSContext must call
34 // DeleteCycleCollectedJSContext::Get() before terminating. Clients of
35 // Create() do not need to do this as Terminate() will ensure this happens.
36 static void EnsureCycleCollectedJSContext(JSRuntime* aParentRuntime,
37 const JS::ContextOptions& aOptions);
38 static void DeleteCycleCollectedJSContext();
40 static bool IsOnWorkletThread();
42 static void AssertIsOnWorkletThread();
44 nsresult DispatchRunnable(already_AddRefed<nsIRunnable> aRunnable);
46 void Terminate();
48 private:
49 explicit WorkletThread(WorkletImpl* aWorkletImpl);
50 ~WorkletThread();
52 void RunEventLoop();
53 class PrimaryRunnable;
55 void TerminateInternal();
56 class TerminateRunnable;
58 // This should only be called by consumers that have an
59 // nsIEventTarget/nsIThread pointer.
60 NS_IMETHOD
61 Dispatch(already_AddRefed<nsIRunnable> aRunnable, uint32_t aFlags) override;
63 NS_IMETHOD
64 DispatchFromScript(nsIRunnable* aRunnable, uint32_t aFlags) override;
66 NS_IMETHOD
67 DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t) override;
69 const RefPtr<WorkletImpl> mWorkletImpl;
71 bool mExitLoop; // worklet execution thread
73 bool mIsTerminating; // main thread
76 } // namespace mozilla::dom
78 #endif // mozilla_dom_worklet_WorkletThread_h