OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 * | 24 * |
25 */ | 25 */ |
26 | 26 |
27 #ifndef WorkerThread_h | 27 #ifndef WorkerThread_h |
28 #define WorkerThread_h | 28 #define WorkerThread_h |
29 | 29 |
30 #include "core/dom/ExecutionContextTask.h" | |
31 #include "core/frame/csp/ContentSecurityPolicy.h" | 30 #include "core/frame/csp/ContentSecurityPolicy.h" |
32 #include "core/workers/WorkerGlobalScope.h" | 31 #include "core/workers/WorkerRunLoop.h" |
33 #include "platform/SharedTimer.h" | |
34 #include "platform/weborigin/SecurityOrigin.h" | 32 #include "platform/weborigin/SecurityOrigin.h" |
35 #include "public/platform/WebThread.h" | |
36 #include "wtf/Forward.h" | 33 #include "wtf/Forward.h" |
37 #include "wtf/MessageQueue.h" | |
38 #include "wtf/OwnPtr.h" | 34 #include "wtf/OwnPtr.h" |
39 #include "wtf/PassRefPtr.h" | 35 #include "wtf/PassRefPtr.h" |
40 #include "wtf/RefCounted.h" | 36 #include "wtf/RefCounted.h" |
41 | 37 |
42 namespace blink { | 38 namespace blink { |
43 class WebWaitableEvent; | 39 class WebWaitableEvent; |
44 } | 40 } |
45 | 41 |
46 namespace blink { | 42 namespace blink { |
47 | 43 |
48 class KURL; | 44 class KURL; |
49 class WorkerGlobalScope; | 45 class WorkerGlobalScope; |
50 class WorkerInspectorController; | |
51 class WorkerLoaderProxy; | 46 class WorkerLoaderProxy; |
52 class WorkerReportingProxy; | 47 class WorkerReportingProxy; |
53 class WorkerSharedTimer; | |
54 class WorkerThreadShutdownFinishTask; | |
55 class WorkerThreadStartupData; | 48 class WorkerThreadStartupData; |
56 class WorkerThreadTask; | 49 class WorkerInspectorController; |
57 | 50 |
58 enum WorkerThreadStartMode { DontPauseWorkerGlobalScopeOnStart, PauseWorkerG
lobalScopeOnStart }; | 51 enum WorkerThreadStartMode { DontPauseWorkerGlobalScopeOnStart, PauseWorkerG
lobalScopeOnStart }; |
59 | 52 |
60 | |
61 class WorkerThread : public RefCounted<WorkerThread> { | 53 class WorkerThread : public RefCounted<WorkerThread> { |
62 public: | 54 public: |
63 virtual ~WorkerThread(); | 55 virtual ~WorkerThread(); |
64 | 56 |
65 virtual void start(); | 57 bool start(); |
66 virtual void stop(); | 58 void stop(); |
67 | 59 |
68 // Can be used to wait for this worker thread to shut down. | 60 // Can be used to wait for this worker thread to shut down. |
69 // (This is signalled on the main thread, so it's assumed to be waited o
n the worker context thread) | 61 // (This is signalled on the main thread, so it's assumed to be waited o
n the worker context thread) |
70 blink::WebWaitableEvent* shutdownEvent() { return m_shutdownEvent.get();
} | 62 blink::WebWaitableEvent* shutdownEvent() { return m_shutdownEvent.get();
} |
71 | 63 |
72 bool isCurrentThread() const; | 64 bool isCurrentThread() const; |
| 65 WorkerRunLoop& runLoop() { return m_runLoop; } |
73 WorkerLoaderProxy& workerLoaderProxy() const { return m_workerLoaderProx
y; } | 66 WorkerLoaderProxy& workerLoaderProxy() const { return m_workerLoaderProx
y; } |
74 WorkerReportingProxy& workerReportingProxy() const { return m_workerRepo
rtingProxy; } | 67 WorkerReportingProxy& workerReportingProxy() const { return m_workerRepo
rtingProxy; } |
75 | 68 |
76 void postTask(PassOwnPtr<ExecutionContextTask>); | 69 void postTask(PassOwnPtr<ExecutionContextTask>); |
77 void postDebuggerTask(PassOwnPtr<ExecutionContextTask>); | 70 void postDebuggerTask(PassOwnPtr<ExecutionContextTask>); |
78 | 71 |
79 enum WaitMode { WaitForMessage, DontWaitForMessage }; | 72 MessageQueueWaitResult runDebuggerTask(WorkerRunLoop::WaitMode = WorkerR
unLoop::WaitForMessage); |
80 MessageQueueWaitResult runDebuggerTask(WaitMode = WaitForMessage); | 73 bool terminated() { return m_runLoop.terminated(); } |
81 | |
82 // These methods should be called if the holder of the thread is | |
83 // going to call runDebuggerTask in a loop. | |
84 void willEnterNestedLoop(); | |
85 void didLeaveNestedLoop(); | |
86 | |
87 blink::WebThread* webThread() { return m_thread.get(); } | |
88 WorkerGlobalScope* workerGlobalScope() const { return m_workerGlobalScop
e.get(); } | |
89 bool terminated() const { return m_terminated; } | |
90 | 74 |
91 // Number of active worker threads. | 75 // Number of active worker threads. |
92 static unsigned workerThreadCount(); | 76 static unsigned workerThreadCount(); |
93 | 77 |
| 78 WorkerGlobalScope* workerGlobalScope() { return m_workerGlobalScope.get(
); } |
| 79 |
94 void interruptAndDispatchInspectorCommands(); | 80 void interruptAndDispatchInspectorCommands(); |
95 void setWorkerInspectorController(WorkerInspectorController*); | 81 void setWorkerInspectorController(WorkerInspectorController*); |
96 | 82 |
97 protected: | 83 protected: |
98 WorkerThread(WorkerLoaderProxy&, WorkerReportingProxy&, PassOwnPtrWillBe
RawPtr<WorkerThreadStartupData>); | 84 WorkerThread(WorkerLoaderProxy&, WorkerReportingProxy&, PassOwnPtrWillBe
RawPtr<WorkerThreadStartupData>); |
99 | 85 |
100 // Factory method for creating a new worker context for the thread. | 86 // Factory method for creating a new worker context for the thread. |
101 virtual PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScop
e(PassOwnPtrWillBeRawPtr<WorkerThreadStartupData>) = 0; | 87 virtual PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScop
e(PassOwnPtrWillBeRawPtr<WorkerThreadStartupData>) = 0; |
102 | 88 |
103 virtual void postInitialize() { } | 89 // Executes the event loop for the worker thread. Derived classes can ov
erride to perform actions before/after entering the event loop. |
| 90 virtual void runEventLoop(); |
104 | 91 |
105 private: | 92 private: |
106 friend class WorkerThreadShutdownFinishTask; | 93 // Static function executed as the core routine on the new thread. Passe
d a pointer to a WorkerThread object. |
| 94 static void workerThreadStart(void*); |
107 | 95 |
108 void initialize(); | 96 void workerThread(); |
109 void cleanup(); | |
110 void idleHandler(); | |
111 | 97 |
112 bool m_terminated; | 98 ThreadIdentifier m_threadID; |
113 OwnPtr<blink::WebThread> m_thread; | 99 WorkerRunLoop m_runLoop; |
114 OwnPtr<WorkerSharedTimer> m_sharedTimer; | |
115 MessageQueue<WorkerThreadTask> m_debuggerMessageQueue; | |
116 | |
117 WorkerLoaderProxy& m_workerLoaderProxy; | 100 WorkerLoaderProxy& m_workerLoaderProxy; |
118 WorkerReportingProxy& m_workerReportingProxy; | 101 WorkerReportingProxy& m_workerReportingProxy; |
119 | 102 |
| 103 RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope; |
| 104 Mutex m_threadCreationMutex; |
| 105 |
120 RefPtrWillBePersistent<WorkerInspectorController> m_workerInspectorContr
oller; | 106 RefPtrWillBePersistent<WorkerInspectorController> m_workerInspectorContr
oller; |
121 Mutex m_workerInspectorControllerMutex; | 107 Mutex m_workerInspectorControllerMutex; |
122 | 108 |
123 Mutex m_threadCreationMutex; | |
124 RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope; | |
125 OwnPtrWillBePersistent<WorkerThreadStartupData> m_startupData; | 109 OwnPtrWillBePersistent<WorkerThreadStartupData> m_startupData; |
126 | 110 |
127 OwnPtr<WeakPtrFactory<WorkerThread> > m_weakFactory; | |
128 | |
129 // Used to signal thread shutdown. | 111 // Used to signal thread shutdown. |
130 OwnPtr<blink::WebWaitableEvent> m_shutdownEvent; | 112 OwnPtr<blink::WebWaitableEvent> m_shutdownEvent; |
131 }; | 113 }; |
132 | 114 |
133 } // namespace blink | 115 } // namespace blink |
134 | 116 |
135 #endif // WorkerThread_h | 117 #endif // WorkerThread_h |
OLD | NEW |