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/. */
6 
7 #ifndef mozilla_dom_workers_WorkerThread_h__
8 #define mozilla_dom_workers_WorkerThread_h__
9 
10 #include "mozilla/Attributes.h"
11 #include "mozilla/CondVar.h"
12 #include "mozilla/DebugOnly.h"
13 #include "nsISupportsImpl.h"
14 #include "mozilla/RefPtr.h"
15 #include "nsThread.h"
16 
17 class nsIRunnable;
18 
19 namespace mozilla {
20 namespace dom {
21 
22 class WorkerRunnable;
23 class WorkerPrivate;
24 template <class>
25 class WorkerPrivateParent;
26 
27 namespace workerinternals {
28 class RuntimeService;
29 }
30 
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>;
38 
39   WorkerThreadFriendKey();
40   ~WorkerThreadFriendKey();
41 };
42 
43 class WorkerThread final : public nsThread {
44   class Observer;
45 
46   Mutex mLock;
47   CondVar mWorkerPrivateCondVar;
48 
49   // Protected by nsThread::mLock.
50   WorkerPrivate* mWorkerPrivate;
51 
52   // Only touched on the target thread.
53   RefPtr<Observer> mObserver;
54 
55   // Protected by nsThread::mLock and waited on with mWorkerPrivateCondVar.
56   uint32_t mOtherThreadsDispatchingViaEventTarget;
57 
58 #ifdef DEBUG
59   // Protected by nsThread::mLock.
60   bool mAcceptingNonWorkerRunnables;
61 #endif
62 
63  public:
64   static already_AddRefed<WorkerThread> Create(
65       const WorkerThreadFriendKey& aKey);
66 
67   void SetWorker(const WorkerThreadFriendKey& aKey,
68                  WorkerPrivate* aWorkerPrivate);
69 
70   nsresult DispatchPrimaryRunnable(const WorkerThreadFriendKey& aKey,
71                                    already_AddRefed<nsIRunnable> aRunnable);
72 
73   nsresult DispatchAnyThread(const WorkerThreadFriendKey& aKey,
74                              already_AddRefed<WorkerRunnable> aWorkerRunnable);
75 
76   uint32_t RecursionDepth(const WorkerThreadFriendKey& aKey) const;
77 
78   NS_INLINE_DECL_REFCOUNTING_INHERITED(WorkerThread, nsThread)
79 
80  private:
81   WorkerThread();
82   ~WorkerThread();
83 
84   // This should only be called by consumers that have an
85   // nsIEventTarget/nsIThread pointer.
86   NS_IMETHOD
87   Dispatch(already_AddRefed<nsIRunnable> aRunnable, uint32_t aFlags) override;
88 
89   NS_IMETHOD
90   DispatchFromScript(nsIRunnable* aRunnable, uint32_t aFlags) override;
91 
92   NS_IMETHOD
93   DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t) override;
94 };
95 
96 }  // namespace dom
97 }  // namespace mozilla
98 
99 #endif  // mozilla_dom_workers_WorkerThread_h__
100