Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / dom / workers / WorkerThread.h
blob06624f60c1ca7126422d86a4df157d99bd96d5bd
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 // Override HasPendingEvents to allow HasPendingEvents could be accessed by
88 // the parent thread. WorkerPrivate::IsEligibleForCC calls this method on the
89 // parent thread to check if there is any pending events on the worker thread.
90 NS_IMETHOD HasPendingEvents(bool* aHasPendingEvents) override;
92 NS_INLINE_DECL_REFCOUNTING_INHERITED(WorkerThread, nsThread)
94 private:
95 ~WorkerThread();
97 // This should only be called by consumers that have an
98 // nsIEventTarget/nsIThread pointer.
99 NS_IMETHOD
100 Dispatch(already_AddRefed<nsIRunnable> aRunnable, uint32_t aFlags) override;
102 NS_IMETHOD
103 DispatchFromScript(nsIRunnable* aRunnable, uint32_t aFlags) override;
105 NS_IMETHOD
106 DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t) override;
109 } // namespace dom
110 } // namespace mozilla
112 #endif // mozilla_dom_workers_WorkerThread_h__