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/AlreadyAddRefed.h"
11 #include "mozilla/CondVar.h"
12 #include "mozilla/Mutex.h"
13 #include "mozilla/RefPtr.h"
14 #include "mozilla/dom/SafeRefPtr.h"
15 #include "nsISupports.h"
29 class WorkerPrivateParent
;
31 namespace workerinternals
{
35 // This class lets us restrict the public methods that can be called on
36 // WorkerThread to RuntimeService and WorkerPrivate without letting them gain
37 // full access to private methods (as would happen if they were simply friends).
38 class WorkerThreadFriendKey
{
39 friend class workerinternals::RuntimeService
;
40 friend class WorkerPrivate
;
41 friend class WorkerPrivateParent
<WorkerPrivate
>;
43 WorkerThreadFriendKey();
44 ~WorkerThreadFriendKey();
47 class WorkerThread final
: public nsThread
{
50 Mutex mLock MOZ_UNANNOTATED
;
51 CondVar mWorkerPrivateCondVar
;
53 // Protected by nsThread::mLock.
54 WorkerPrivate
* mWorkerPrivate
;
56 // Only touched on the target thread.
57 RefPtr
<Observer
> mObserver
;
59 // Protected by nsThread::mLock and waited on with mWorkerPrivateCondVar.
60 uint32_t mOtherThreadsDispatchingViaEventTarget
;
63 // Protected by nsThread::mLock.
64 bool mAcceptingNonWorkerRunnables
;
67 // Using this struct we restrict access to the constructor while still being
68 // able to use MakeSafeRefPtr.
69 struct ConstructorKey
{};
72 explicit WorkerThread(ConstructorKey
);
74 static SafeRefPtr
<WorkerThread
> Create(const WorkerThreadFriendKey
& aKey
);
76 void SetWorker(const WorkerThreadFriendKey
& aKey
,
77 WorkerPrivate
* aWorkerPrivate
);
79 nsresult
DispatchPrimaryRunnable(const WorkerThreadFriendKey
& aKey
,
80 already_AddRefed
<nsIRunnable
> aRunnable
);
82 nsresult
DispatchAnyThread(const WorkerThreadFriendKey
& aKey
,
83 already_AddRefed
<WorkerRunnable
> aWorkerRunnable
);
85 uint32_t RecursionDepth(const WorkerThreadFriendKey
& aKey
) const;
87 NS_INLINE_DECL_REFCOUNTING_INHERITED(WorkerThread
, nsThread
)
92 // This should only be called by consumers that have an
93 // nsIEventTarget/nsIThread pointer.
95 Dispatch(already_AddRefed
<nsIRunnable
> aRunnable
, uint32_t aFlags
) override
;
98 DispatchFromScript(nsIRunnable
* aRunnable
, uint32_t aFlags
) override
;
101 DelayedDispatch(already_AddRefed
<nsIRunnable
>, uint32_t) override
;
105 } // namespace mozilla
107 #endif // mozilla_dom_workers_WorkerThread_h__