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 // WorkerBackingThread is created on main but needs to be initialized on the | |
34 // thread in order to attach that thread. | |
35 thread->getWebTaskRunner()->postTask(BLINK_FROM_HERE, threadSafeBind(&Worker BackingThread::initialize, AllowCrossThreadAccess(this))); | |
haraken
2016/05/11 00:18:31
I still don't understand why you have to call init
flackr
2016/05/18 05:26:50
I've changed WorkerBackingThread so that when you
| |
32 } | 36 } |
33 | 37 |
34 WorkerBackingThread::~WorkerBackingThread() | 38 WorkerBackingThread::~WorkerBackingThread() |
35 { | 39 { |
40 if (!m_isOwningThread) | |
41 signalShutdownAndWait(); | |
haraken
2016/05/11 00:18:31
I'd move this to CompositorWorkerThread::clearShar
flackr
2016/05/18 05:26:50
Done.
| |
36 #if DCHECK_IS_ON() | 42 #if DCHECK_IS_ON() |
37 MutexLocker locker(m_mutex); | 43 MutexLocker locker(m_mutex); |
38 DCHECK_EQ(0u, m_workerScriptCount); | 44 DCHECK_EQ(0u, m_workerScriptCount); |
39 #endif | 45 #endif |
40 } | 46 } |
41 | 47 |
42 void WorkerBackingThread::attach() | 48 void WorkerBackingThread::attach() |
43 { | 49 { |
44 { | 50 { |
45 MutexLocker locker(m_mutex); | 51 MutexLocker locker(m_mutex); |
46 if (++m_workerScriptCount > 1) | 52 if (++m_workerScriptCount > 1) |
47 return; | 53 return; |
48 } | 54 } |
49 initialize(); | 55 if (m_isOwningThread) |
56 initialize(); | |
50 } | 57 } |
51 | 58 |
52 void WorkerBackingThread::detach() | 59 void WorkerBackingThread::detach() |
53 { | 60 { |
54 { | 61 { |
55 MutexLocker locker(m_mutex); | 62 MutexLocker locker(m_mutex); |
56 if (--m_workerScriptCount > 0) | 63 if (--m_workerScriptCount > 0) |
57 return; | 64 return; |
58 } | 65 } |
59 shutdown(); | 66 if (m_isOwningThread) |
67 shutdown(); | |
60 } | 68 } |
61 | 69 |
62 void WorkerBackingThread::initialize() | 70 void WorkerBackingThread::initialize() |
63 { | 71 { |
64 DCHECK(!m_isolate); | 72 DCHECK(!m_isolate); |
65 m_isolate = V8PerIsolateData::initialize(); | 73 m_isolate = V8PerIsolateData::initialize(); |
66 V8Initializer::initializeWorker(m_isolate); | 74 V8Initializer::initializeWorker(m_isolate); |
67 m_backingThread->initialize(); | 75 m_backingThread->initialize(); |
68 | 76 |
69 OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterruptor (m_isolate)); | 77 OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterruptor (m_isolate)); |
70 ThreadState::current()->addInterruptor(interruptor.release()); | 78 ThreadState::current()->addInterruptor(interruptor.release()); |
71 ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCController:: traceDOMWrappers); | 79 ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCController:: traceDOMWrappers); |
72 if (RuntimeEnabledFeatures::v8IdleTasksEnabled()) | 80 if (RuntimeEnabledFeatures::v8IdleTasksEnabled()) |
73 V8PerIsolateData::enableIdleTasks(m_isolate, adoptPtr(new V8IdleTaskRunn er(backingThread().platformThread().scheduler()))); | 81 V8PerIsolateData::enableIdleTasks(m_isolate, adoptPtr(new V8IdleTaskRunn er(backingThread().platformThread().scheduler()))); |
74 if (m_isOwningThread) | 82 if (m_isOwningThread) |
75 Platform::current()->didStartWorkerThread(); | 83 Platform::current()->didStartWorkerThread(); |
76 } | 84 } |
77 | 85 |
78 void WorkerBackingThread::shutdown() | 86 void WorkerBackingThread::shutdown(WaitableEvent* doneEvent) |
79 { | 87 { |
80 if (m_isOwningThread) | 88 if (m_isOwningThread) |
81 Platform::current()->willStopWorkerThread(); | 89 Platform::current()->willStopWorkerThread(); |
82 | 90 |
83 V8PerIsolateData::willBeDestroyed(m_isolate); | 91 V8PerIsolateData::willBeDestroyed(m_isolate); |
84 // TODO(yhirano): Remove this when https://crbug.com/v8/1428 is fixed. | 92 // TODO(yhirano): Remove this when https://crbug.com/v8/1428 is fixed. |
85 if (m_shouldCallGCOnShutdown) { | 93 if (m_shouldCallGCOnShutdown) { |
86 // This statement runs only in tests. | 94 // This statement runs only in tests. |
87 V8GCController::collectAllGarbageForTesting(m_isolate); | 95 V8GCController::collectAllGarbageForTesting(m_isolate); |
88 } | 96 } |
89 m_backingThread->shutdown(); | 97 m_backingThread->shutdown(); |
90 | 98 |
91 V8PerIsolateData::destroy(m_isolate); | 99 V8PerIsolateData::destroy(m_isolate); |
92 m_isolate = nullptr; | 100 m_isolate = nullptr; |
101 if (doneEvent) | |
102 doneEvent->signal(); | |
103 } | |
104 | |
105 void WorkerBackingThread::signalShutdownAndWait() | |
106 { | |
107 WaitableEvent doneEvent; | |
108 m_backingThread->platformThread().getWebTaskRunner()->postTask(BLINK_FROM_HE RE, threadSafeBind(&WorkerBackingThread::shutdown, AllowCrossThreadAccess(this), AllowCrossThreadAccess(&doneEvent))); | |
109 doneEvent.wait(); | |
93 } | 110 } |
94 | 111 |
95 } // namespace blink | 112 } // namespace blink |
OLD | NEW |