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" |
30 #include "core/frame/csp/ContentSecurityPolicy.h" | 31 #include "core/frame/csp/ContentSecurityPolicy.h" |
31 #include "core/workers/WorkerRunLoop.h" | 32 #include "core/workers/WorkerGlobalScope.h" |
| 33 #include "platform/SharedTimer.h" |
32 #include "platform/weborigin/SecurityOrigin.h" | 34 #include "platform/weborigin/SecurityOrigin.h" |
| 35 #include "public/platform/WebThread.h" |
33 #include "wtf/Forward.h" | 36 #include "wtf/Forward.h" |
| 37 #include "wtf/MessageQueue.h" |
34 #include "wtf/OwnPtr.h" | 38 #include "wtf/OwnPtr.h" |
35 #include "wtf/PassRefPtr.h" | 39 #include "wtf/PassRefPtr.h" |
36 #include "wtf/RefCounted.h" | 40 #include "wtf/RefCounted.h" |
37 | 41 |
38 namespace blink { | 42 namespace blink { |
39 class WebWaitableEvent; | 43 class WebWaitableEvent; |
40 } | 44 } |
41 | 45 |
42 namespace blink { | 46 namespace blink { |
43 | 47 |
44 class KURL; | 48 class KURL; |
45 class WorkerGlobalScope; | 49 class WorkerGlobalScope; |
46 class WorkerLoaderProxy; | 50 class WorkerLoaderProxy; |
47 class WorkerReportingProxy; | 51 class WorkerReportingProxy; |
| 52 class WorkerSharedTimer; |
| 53 class WorkerThreadShutdownFinishTask; |
48 class WorkerThreadStartupData; | 54 class WorkerThreadStartupData; |
| 55 class WorkerThreadTask; |
49 | 56 |
50 enum WorkerThreadStartMode { DontPauseWorkerGlobalScopeOnStart, PauseWorkerG
lobalScopeOnStart }; | 57 enum WorkerThreadStartMode { DontPauseWorkerGlobalScopeOnStart, PauseWorkerG
lobalScopeOnStart }; |
51 | 58 |
| 59 |
52 class WorkerThread : public RefCounted<WorkerThread> { | 60 class WorkerThread : public RefCounted<WorkerThread> { |
53 public: | 61 public: |
54 virtual ~WorkerThread(); | 62 virtual ~WorkerThread(); |
55 | 63 |
56 bool start(); | 64 virtual void start(); |
57 void stop(); | 65 virtual void stop(); |
58 | 66 |
59 // Can be used to wait for this worker thread to shut down. | 67 // Can be used to wait for this worker thread to shut down. |
60 // (This is signalled on the main thread, so it's assumed to be waited o
n the worker context thread) | 68 // (This is signalled on the main thread, so it's assumed to be waited o
n the worker context thread) |
61 blink::WebWaitableEvent* shutdownEvent() { return m_shutdownEvent.get();
} | 69 blink::WebWaitableEvent* shutdownEvent() { return m_shutdownEvent.get();
} |
62 | 70 |
63 bool isCurrentThread() const; | 71 bool isCurrentThread() const; |
64 WorkerRunLoop& runLoop() { return m_runLoop; } | |
65 WorkerLoaderProxy& workerLoaderProxy() const { return m_workerLoaderProx
y; } | 72 WorkerLoaderProxy& workerLoaderProxy() const { return m_workerLoaderProx
y; } |
66 WorkerReportingProxy& workerReportingProxy() const { return m_workerRepo
rtingProxy; } | 73 WorkerReportingProxy& workerReportingProxy() const { return m_workerRepo
rtingProxy; } |
67 | 74 |
68 void postTask(PassOwnPtr<ExecutionContextTask>); | 75 void postTask(PassOwnPtr<ExecutionContextTask>); |
69 void postDebuggerTask(PassOwnPtr<ExecutionContextTask>); | 76 void postDebuggerTask(PassOwnPtr<ExecutionContextTask>); |
70 | 77 |
71 MessageQueueWaitResult runDebuggerTask(WorkerRunLoop::WaitMode = WorkerR
unLoop::WaitForMessage); | 78 enum WaitMode { WaitForMessage, DontWaitForMessage }; |
72 bool terminated() { return m_runLoop.terminated(); } | 79 MessageQueueWaitResult runDebuggerTask(WaitMode = WaitForMessage); |
| 80 |
| 81 // These methods should be called if the holder of the thread is |
| 82 // going to call runDebuggerTask in a loop. |
| 83 void willEnterNestedLoop(); |
| 84 void didLeaveNestedLoop(); |
| 85 |
| 86 blink::WebThread* webThread() { return m_thread.get(); } |
| 87 WorkerGlobalScope* workerGlobalScope() const { return m_workerGlobalScop
e.get(); } |
| 88 bool terminated() const { return m_terminated; } |
73 | 89 |
74 // Number of active worker threads. | 90 // Number of active worker threads. |
75 static unsigned workerThreadCount(); | 91 static unsigned workerThreadCount(); |
76 | 92 |
77 protected: | 93 protected: |
78 WorkerThread(WorkerLoaderProxy&, WorkerReportingProxy&, PassOwnPtrWillBe
RawPtr<WorkerThreadStartupData>); | 94 WorkerThread(WorkerLoaderProxy&, WorkerReportingProxy&, PassOwnPtrWillBe
RawPtr<WorkerThreadStartupData>); |
79 | 95 |
80 // Factory method for creating a new worker context for the thread. | 96 // Factory method for creating a new worker context for the thread. |
81 virtual PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScop
e(PassOwnPtrWillBeRawPtr<WorkerThreadStartupData>) = 0; | 97 virtual PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScop
e(PassOwnPtrWillBeRawPtr<WorkerThreadStartupData>) = 0; |
82 | 98 |
83 // Executes the event loop for the worker thread. Derived classes can ov
erride to perform actions before/after entering the event loop. | 99 virtual void postInitialize() { } |
84 virtual void runEventLoop(); | |
85 | |
86 WorkerGlobalScope* workerGlobalScope() { return m_workerGlobalScope.get(
); } | |
87 | 100 |
88 private: | 101 private: |
89 // Static function executed as the core routine on the new thread. Passe
d a pointer to a WorkerThread object. | 102 friend class WorkerThreadShutdownFinishTask; |
90 static void workerThreadStart(void*); | |
91 | 103 |
92 void workerThread(); | 104 void initialize(); |
| 105 void cleanup(); |
| 106 void idleHandler(); |
93 | 107 |
94 ThreadIdentifier m_threadID; | 108 bool m_terminated; |
95 WorkerRunLoop m_runLoop; | 109 OwnPtr<blink::WebThread> m_thread; |
| 110 OwnPtr<WorkerSharedTimer> m_sharedTimer; |
| 111 MessageQueue<WorkerThreadTask> m_debuggerMessageQueue; |
| 112 |
96 WorkerLoaderProxy& m_workerLoaderProxy; | 113 WorkerLoaderProxy& m_workerLoaderProxy; |
97 WorkerReportingProxy& m_workerReportingProxy; | 114 WorkerReportingProxy& m_workerReportingProxy; |
98 | 115 |
| 116 Mutex m_threadCreationMutex; |
99 RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope; | 117 RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope; |
100 Mutex m_threadCreationMutex; | 118 OwnPtrWillBePersistent<WorkerThreadStartupData> m_startupData; |
101 | 119 |
102 OwnPtrWillBePersistent<WorkerThreadStartupData> m_startupData; | 120 OwnPtr<WeakPtrFactory<WorkerThread> > m_weakFactory; |
103 | 121 |
104 // Used to signal thread shutdown. | 122 // Used to signal thread shutdown. |
105 OwnPtr<blink::WebWaitableEvent> m_shutdownEvent; | 123 OwnPtr<blink::WebWaitableEvent> m_shutdownEvent; |
106 }; | 124 }; |
107 | 125 |
108 } // namespace blink | 126 } // namespace blink |
109 | 127 |
110 #endif // WorkerThread_h | 128 #endif // WorkerThread_h |
OLD | NEW |