Bug 1861467 - [wpt-sync] Update web-platform-tests to eedf737ce39c512d0ca3471f988972e...
[gecko.git] / dom / workers / WorkerThread.h
blobd56b76ff5b100273db6ebe2f6b1a67fbbef359d3
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"
16 #include "nsThread.h"
17 #include "nscore.h"
19 class nsIRunnable;
21 namespace mozilla {
22 class Runnable;
24 namespace dom {
26 class WorkerRunnable;
27 class WorkerPrivate;
28 template <class>
29 class WorkerPrivateParent;
31 namespace workerinternals {
32 class RuntimeService;
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 {
48 class Observer;
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;
62 #ifdef DEBUG
63 // Protected by nsThread::mLock.
64 bool mAcceptingNonWorkerRunnables;
65 #endif
67 // Using this struct we restrict access to the constructor while still being
68 // able to use MakeSafeRefPtr.
69 struct ConstructorKey {};
71 public:
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)
89 private:
90 ~WorkerThread();
92 // This should only be called by consumers that have an
93 // nsIEventTarget/nsIThread pointer.
94 NS_IMETHOD
95 Dispatch(already_AddRefed<nsIRunnable> aRunnable, uint32_t aFlags) override;
97 NS_IMETHOD
98 DispatchFromScript(nsIRunnable* aRunnable, uint32_t aFlags) override;
100 NS_IMETHOD
101 DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t) override;
104 } // namespace dom
105 } // namespace mozilla
107 #endif // mozilla_dom_workers_WorkerThread_h__