OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/workers/WorkerBackingThread.h" | 5 #include "core/workers/WorkerBackingThread.h" |
6 | 6 |
7 #include "bindings/core/v8/V8Binding.h" | 7 #include "bindings/core/v8/V8Binding.h" |
8 #include "bindings/core/v8/V8GCController.h" | 8 #include "bindings/core/v8/V8GCController.h" |
9 #include "bindings/core/v8/V8IdleTaskRunner.h" | 9 #include "bindings/core/v8/V8IdleTaskRunner.h" |
10 #include "bindings/core/v8/V8Initializer.h" | 10 #include "bindings/core/v8/V8Initializer.h" |
11 #include "bindings/core/v8/V8PerIsolateData.h" | 11 #include "bindings/core/v8/V8PerIsolateData.h" |
12 #include "platform/RuntimeEnabledFeatures.h" | 12 #include "platform/RuntimeEnabledFeatures.h" |
13 #include "platform/ThreadSafeFunctional.h" | 13 #include "platform/ThreadSafeFunctional.h" |
14 #include "platform/WaitableEvent.h" | |
14 #include "platform/WebThreadSupportingGC.h" | 15 #include "platform/WebThreadSupportingGC.h" |
15 #include "public/platform/Platform.h" | 16 #include "public/platform/Platform.h" |
16 #include "public/platform/WebTraceLocation.h" | 17 #include "public/platform/WebTraceLocation.h" |
17 | 18 |
18 namespace blink { | 19 namespace blink { |
19 | 20 |
20 WorkerBackingThread::WorkerBackingThread(const char* name, bool shouldCallGCOnSh utdown) | 21 WorkerBackingThread::WorkerBackingThread(const char* name, bool shouldCallGCOnSh utdown) |
21 : m_backingThread(WebThreadSupportingGC::create(name)) | 22 : m_backingThread(WebThreadSupportingGC::create(name)) |
22 , m_isOwningThread(true) | 23 , m_isOwningThread(true) |
23 , m_shouldCallGCOnShutdown(shouldCallGCOnShutdown) | 24 , m_shouldCallGCOnShutdown(shouldCallGCOnShutdown) |
24 { | 25 { |
25 } | 26 } |
26 | 27 |
27 WorkerBackingThread::WorkerBackingThread(WebThread* thread, bool shouldCallGCOnS hutdown) | 28 WorkerBackingThread::WorkerBackingThread(WebThread* thread, bool shouldCallGCOnS hutdown) |
28 : m_backingThread(WebThreadSupportingGC::createForThread(thread)) | 29 : m_backingThread(WebThreadSupportingGC::createForThread(thread)) |
29 , m_isOwningThread(false) | 30 , m_isOwningThread(false) |
30 , m_shouldCallGCOnShutdown(shouldCallGCOnShutdown) | 31 , m_shouldCallGCOnShutdown(shouldCallGCOnShutdown) |
31 { | 32 { |
33 thread->getWebTaskRunner()->postTask(BLINK_FROM_HERE, threadSafeBind(&Worker BackingThread::initialize, AllowCrossThreadAccess(this))); | |
haraken
2016/05/09 02:00:53
Would you elaborate on why you need to post a task
flackr
2016/05/10 18:07:39
Added a comment - we create WorkerBackingThread on
| |
32 } | 34 } |
33 | 35 |
34 WorkerBackingThread::~WorkerBackingThread() | 36 WorkerBackingThread::~WorkerBackingThread() |
35 { | 37 { |
38 if (!m_isOwningThread) | |
39 signalShutdownAndWait(); | |
haraken
2016/05/09 02:00:53
Would you help me understand why you need to call
flackr
2016/05/10 18:07:39
Similar to initialization, the shutdown must be do
| |
36 #if DCHECK_IS_ON() | 40 #if DCHECK_IS_ON() |
37 MutexLocker locker(m_mutex); | 41 MutexLocker locker(m_mutex); |
38 DCHECK_EQ(0u, m_workerScriptCount); | 42 DCHECK_EQ(0u, m_workerScriptCount); |
39 #endif | 43 #endif |
40 } | 44 } |
41 | 45 |
42 void WorkerBackingThread::attach() | 46 void WorkerBackingThread::attach() |
43 { | 47 { |
44 { | 48 { |
45 MutexLocker locker(m_mutex); | 49 MutexLocker locker(m_mutex); |
46 if (++m_workerScriptCount > 1) | 50 if (++m_workerScriptCount > 1) |
47 return; | 51 return; |
48 } | 52 } |
49 initialize(); | 53 if (m_isOwningThread) |
54 initialize(); | |
50 } | 55 } |
51 | 56 |
52 void WorkerBackingThread::detach() | 57 void WorkerBackingThread::detach() |
53 { | 58 { |
54 { | 59 { |
55 MutexLocker locker(m_mutex); | 60 MutexLocker locker(m_mutex); |
56 if (--m_workerScriptCount > 0) | 61 if (--m_workerScriptCount > 0) |
57 return; | 62 return; |
58 } | 63 } |
59 shutdown(); | 64 if (m_isOwningThread) |
65 shutdown(); | |
60 } | 66 } |
61 | 67 |
62 void WorkerBackingThread::initialize() | 68 void WorkerBackingThread::initialize() |
63 { | 69 { |
64 DCHECK(!m_isolate); | 70 DCHECK(!m_isolate); |
65 m_isolate = V8PerIsolateData::initialize(); | 71 m_isolate = V8PerIsolateData::initialize(); |
66 V8Initializer::initializeWorker(m_isolate); | 72 V8Initializer::initializeWorker(m_isolate); |
67 m_backingThread->initialize(); | 73 m_backingThread->initialize(); |
68 | 74 |
69 OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterruptor (m_isolate)); | 75 OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterruptor (m_isolate)); |
70 ThreadState::current()->addInterruptor(interruptor.release()); | 76 ThreadState::current()->addInterruptor(interruptor.release()); |
71 ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCController:: traceDOMWrappers); | 77 ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCController:: traceDOMWrappers); |
72 if (RuntimeEnabledFeatures::v8IdleTasksEnabled()) | 78 if (RuntimeEnabledFeatures::v8IdleTasksEnabled()) |
73 V8PerIsolateData::enableIdleTasks(m_isolate, adoptPtr(new V8IdleTaskRunn er(backingThread().platformThread().scheduler()))); | 79 V8PerIsolateData::enableIdleTasks(m_isolate, adoptPtr(new V8IdleTaskRunn er(backingThread().platformThread().scheduler()))); |
74 if (m_isOwningThread) | 80 if (m_isOwningThread) |
75 Platform::current()->didStartWorkerThread(); | 81 Platform::current()->didStartWorkerThread(); |
76 } | 82 } |
77 | 83 |
78 void WorkerBackingThread::shutdown() | 84 void WorkerBackingThread::shutdown(WaitableEvent* doneEvent) |
79 { | 85 { |
80 if (m_isOwningThread) | 86 if (m_isOwningThread) |
81 Platform::current()->willStopWorkerThread(); | 87 Platform::current()->willStopWorkerThread(); |
82 | 88 |
83 V8PerIsolateData::willBeDestroyed(m_isolate); | 89 V8PerIsolateData::willBeDestroyed(m_isolate); |
84 // TODO(yhirano): Remove this when https://crbug.com/v8/1428 is fixed. | 90 // TODO(yhirano): Remove this when https://crbug.com/v8/1428 is fixed. |
85 if (m_shouldCallGCOnShutdown) { | 91 if (m_shouldCallGCOnShutdown) { |
86 // This statement runs only in tests. | 92 // This statement runs only in tests. |
87 V8GCController::collectAllGarbageForTesting(m_isolate); | 93 V8GCController::collectAllGarbageForTesting(m_isolate); |
88 } | 94 } |
89 m_backingThread->shutdown(); | 95 m_backingThread->shutdown(); |
90 | 96 |
91 V8PerIsolateData::destroy(m_isolate); | 97 V8PerIsolateData::destroy(m_isolate); |
92 m_isolate = nullptr; | 98 m_isolate = nullptr; |
99 if (doneEvent) | |
100 doneEvent->signal(); | |
101 } | |
102 | |
103 void WorkerBackingThread::signalShutdownAndWait() | |
104 { | |
105 WaitableEvent doneEvent; | |
106 m_backingThread->platformThread().getWebTaskRunner()->postTask(BLINK_FROM_HE RE, threadSafeBind(&WorkerBackingThread::shutdown, AllowCrossThreadAccess(this), AllowCrossThreadAccess(&doneEvent))); | |
107 doneEvent.wait(); | |
93 } | 108 } |
94 | 109 |
95 } // namespace blink | 110 } // namespace blink |
OLD | NEW |