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_workers_WorkerThread_h__
8 #define mozilla_dom_workers_WorkerThread_h__
10 #include "mozilla/Attributes.h"
11 #include "mozilla/CondVar.h"
12 #include "mozilla/DebugOnly.h"
13 #include "mozilla/dom/SafeRefPtr.h"
14 #include "nsISupportsImpl.h"
15 #include "mozilla/RefPtr.h"
26 class WorkerPrivateParent
;
28 namespace workerinternals
{
32 // This class lets us restrict the public methods that can be called on
33 // WorkerThread to RuntimeService and WorkerPrivate without letting them gain
34 // full access to private methods (as would happen if they were simply friends).
35 class WorkerThreadFriendKey
{
36 friend class workerinternals::RuntimeService
;
37 friend class WorkerPrivate
;
38 friend class WorkerPrivateParent
<WorkerPrivate
>;
40 WorkerThreadFriendKey();
41 ~WorkerThreadFriendKey();
44 class WorkerThread final
: public nsThread
{
48 CondVar mWorkerPrivateCondVar
;
50 // Protected by nsThread::mLock.
51 WorkerPrivate
* mWorkerPrivate
;
53 // Only touched on the target thread.
54 RefPtr
<Observer
> mObserver
;
56 // Protected by nsThread::mLock and waited on with mWorkerPrivateCondVar.
57 uint32_t mOtherThreadsDispatchingViaEventTarget
;
60 // Protected by nsThread::mLock.
61 bool mAcceptingNonWorkerRunnables
;
64 // Using this struct we restrict access to the constructor while still being
65 // able to use MakeSafeRefPtr.
66 struct ConstructorKey
{};
69 explicit WorkerThread(ConstructorKey
);
71 static SafeRefPtr
<WorkerThread
> Create(const WorkerThreadFriendKey
& aKey
);
73 void SetWorker(const WorkerThreadFriendKey
& aKey
,
74 WorkerPrivate
* aWorkerPrivate
);
76 nsresult
DispatchPrimaryRunnable(const WorkerThreadFriendKey
& aKey
,
77 already_AddRefed
<nsIRunnable
> aRunnable
);
79 nsresult
DispatchAnyThread(const WorkerThreadFriendKey
& aKey
,
80 already_AddRefed
<WorkerRunnable
> aWorkerRunnable
);
82 uint32_t RecursionDepth(const WorkerThreadFriendKey
& aKey
) const;
84 PerformanceCounter
* GetPerformanceCounter(nsIRunnable
* aEvent
) const override
;
86 NS_INLINE_DECL_REFCOUNTING_INHERITED(WorkerThread
, nsThread
)
91 // This should only be called by consumers that have an
92 // nsIEventTarget/nsIThread pointer.
94 Dispatch(already_AddRefed
<nsIRunnable
> aRunnable
, uint32_t aFlags
) override
;
97 DispatchFromScript(nsIRunnable
* aRunnable
, uint32_t aFlags
) override
;
100 DelayedDispatch(already_AddRefed
<nsIRunnable
>, uint32_t) override
;
102 void IncrementDispatchCounter();
106 } // namespace mozilla
108 #endif // mozilla_dom_workers_WorkerThread_h__