Line data Source code
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 : namespace workers {
22 :
23 : class RuntimeService;
24 : class WorkerPrivate;
25 : template <class> class WorkerPrivateParent;
26 : class WorkerRunnable;
27 :
28 : // This class lets us restrict the public methods that can be called on
29 : // WorkerThread to RuntimeService and WorkerPrivate without letting them gain
30 : // full access to private methods (as would happen if they were simply friends).
31 : class WorkerThreadFriendKey
32 : {
33 : friend class RuntimeService;
34 : friend class WorkerPrivate;
35 : friend class WorkerPrivateParent<WorkerPrivate>;
36 :
37 : WorkerThreadFriendKey();
38 : ~WorkerThreadFriendKey();
39 : };
40 :
41 : class WorkerThread final
42 : : public nsThread
43 : {
44 : class Observer;
45 :
46 : CondVar mWorkerPrivateCondVar;
47 :
48 : // Protected by nsThread::mLock.
49 : WorkerPrivate* mWorkerPrivate;
50 :
51 : // Only touched on the target thread.
52 : RefPtr<Observer> mObserver;
53 :
54 : // Protected by nsThread::mLock and waited on with mWorkerPrivateCondVar.
55 : uint32_t mOtherThreadsDispatchingViaEventTarget;
56 :
57 : #ifdef DEBUG
58 : // Protected by nsThread::mLock.
59 : bool mAcceptingNonWorkerRunnables;
60 : #endif
61 :
62 : public:
63 : static already_AddRefed<WorkerThread>
64 : Create(const WorkerThreadFriendKey& aKey);
65 :
66 : void
67 : SetWorker(const WorkerThreadFriendKey& aKey, WorkerPrivate* aWorkerPrivate);
68 :
69 : nsresult
70 : DispatchPrimaryRunnable(const WorkerThreadFriendKey& aKey,
71 : already_AddRefed<nsIRunnable> aRunnable);
72 :
73 : nsresult
74 : DispatchAnyThread(const WorkerThreadFriendKey& aKey,
75 : already_AddRefed<WorkerRunnable> aWorkerRunnable);
76 :
77 : uint32_t
78 : RecursionDepth(const WorkerThreadFriendKey& aKey) const;
79 :
80 : // Required for MinGW build #1336527 to handle compiler bug:
81 : // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79582
82 : NS_IMETHOD
83 0 : RegisterIdlePeriod(already_AddRefed<nsIIdlePeriod> aIdlePeriod) override
84 : {
85 0 : return nsThread::RegisterIdlePeriod(already_AddRefed<nsIIdlePeriod>(aIdlePeriod.take()));
86 : }
87 :
88 : NS_DECL_ISUPPORTS_INHERITED
89 :
90 : private:
91 : WorkerThread();
92 : ~WorkerThread();
93 :
94 : // This should only be called by consumers that have an
95 : // nsIEventTarget/nsIThread pointer.
96 : NS_IMETHOD
97 : Dispatch(already_AddRefed<nsIRunnable> aRunnable, uint32_t aFlags) override;
98 :
99 : NS_IMETHOD
100 : DispatchFromScript(nsIRunnable* aRunnable, uint32_t aFlags) override;
101 :
102 : NS_IMETHOD
103 : DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t) override;
104 : };
105 :
106 : } // namespace workers
107 : } // namespace dom
108 : } // namespace mozilla
109 :
110 : #endif // mozilla_dom_workers_WorkerThread_h__
|