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 "nsISupportsImpl.h"
14 #include "mozilla/RefPtr.h"
25 class WorkerPrivateParent
;
27 namespace workerinternals
{
31 // This class lets us restrict the public methods that can be called on
32 // WorkerThread to RuntimeService and WorkerPrivate without letting them gain
33 // full access to private methods (as would happen if they were simply friends).
34 class WorkerThreadFriendKey
{
35 friend class workerinternals::RuntimeService
;
36 friend class WorkerPrivate
;
37 friend class WorkerPrivateParent
<WorkerPrivate
>;
39 WorkerThreadFriendKey();
40 ~WorkerThreadFriendKey();
43 class WorkerThread final
: public nsThread
{
47 CondVar mWorkerPrivateCondVar
;
49 // Protected by nsThread::mLock.
50 WorkerPrivate
* mWorkerPrivate
;
52 // Only touched on the target thread.
53 RefPtr
<Observer
> mObserver
;
55 // Protected by nsThread::mLock and waited on with mWorkerPrivateCondVar.
56 uint32_t mOtherThreadsDispatchingViaEventTarget
;
59 // Protected by nsThread::mLock.
60 bool mAcceptingNonWorkerRunnables
;
64 static already_AddRefed
<WorkerThread
> Create(
65 const WorkerThreadFriendKey
& aKey
);
67 void SetWorker(const WorkerThreadFriendKey
& aKey
,
68 WorkerPrivate
* aWorkerPrivate
);
70 nsresult
DispatchPrimaryRunnable(const WorkerThreadFriendKey
& aKey
,
71 already_AddRefed
<nsIRunnable
> aRunnable
);
73 nsresult
DispatchAnyThread(const WorkerThreadFriendKey
& aKey
,
74 already_AddRefed
<WorkerRunnable
> aWorkerRunnable
);
76 uint32_t RecursionDepth(const WorkerThreadFriendKey
& aKey
) const;
78 PerformanceCounter
* GetPerformanceCounter(nsIRunnable
* aEvent
) override
;
80 NS_INLINE_DECL_REFCOUNTING_INHERITED(WorkerThread
, nsThread
)
86 // This should only be called by consumers that have an
87 // nsIEventTarget/nsIThread pointer.
89 Dispatch(already_AddRefed
<nsIRunnable
> aRunnable
, uint32_t aFlags
) override
;
92 DispatchFromScript(nsIRunnable
* aRunnable
, uint32_t aFlags
) override
;
95 DelayedDispatch(already_AddRefed
<nsIRunnable
>, uint32_t) override
;
97 void IncrementDispatchCounter();
101 } // namespace mozilla
103 #endif // mozilla_dom_workers_WorkerThread_h__