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"
24 namespace mozilla::dom
{
26 class WorkletThread final
: public nsThread
, public nsIObserver
{
28 NS_DECL_ISUPPORTS_INHERITED
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
);
49 explicit WorkletThread(WorkletImpl
* aWorkletImpl
);
53 class PrimaryRunnable
;
55 void TerminateInternal();
56 class TerminateRunnable
;
58 // This should only be called by consumers that have an
59 // nsIEventTarget/nsIThread pointer.
61 Dispatch(already_AddRefed
<nsIRunnable
> aRunnable
, uint32_t aFlags
) override
;
64 DispatchFromScript(nsIRunnable
* aRunnable
, uint32_t aFlags
) override
;
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