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"
22 class WorkletThread final
: public nsThread
, public nsIObserver
{
24 NS_DECL_ISUPPORTS_INHERITED
27 static already_AddRefed
<WorkletThread
> Create(WorkletImpl
* aWorkletImpl
);
29 // Threads that call EnsureCycleCollectedJSContext must call
30 // DeleteCycleCollectedJSContext::Get() before terminating. Clients of
31 // Create() do not need to do this as Terminate() will ensure this happens.
32 void EnsureCycleCollectedJSContext(JSRuntime
* aParentRuntime
);
33 static void DeleteCycleCollectedJSContext();
35 static bool IsOnWorkletThread();
37 static void AssertIsOnWorkletThread();
39 nsresult
DispatchRunnable(already_AddRefed
<nsIRunnable
> aRunnable
);
44 explicit WorkletThread(WorkletImpl
* aWorkletImpl
);
48 class PrimaryRunnable
;
50 void TerminateInternal();
51 class TerminateRunnable
;
53 // This should only be called by consumers that have an
54 // nsIEventTarget/nsIThread pointer.
56 Dispatch(already_AddRefed
<nsIRunnable
> aRunnable
, uint32_t aFlags
) override
;
59 DispatchFromScript(nsIRunnable
* aRunnable
, uint32_t aFlags
) override
;
62 DelayedDispatch(already_AddRefed
<nsIRunnable
>, uint32_t) override
;
64 const RefPtr
<WorkletImpl
> mWorkletImpl
;
66 bool mExitLoop
; // worklet execution thread
68 bool mIsTerminating
; // main thread
72 } // namespace mozilla
74 #endif // mozilla_dom_worklet_WorkletThread_h