OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "core/workers/WorkerBackingThread.h" | |
6 | |
7 #include "bindings/core/v8/V8Binding.h" | |
8 #include "bindings/core/v8/V8GCController.h" | |
9 #include "bindings/core/v8/V8IdleTaskRunner.h" | |
10 #include "bindings/core/v8/V8Initializer.h" | |
11 #include "bindings/core/v8/V8PerIsolateData.h" | |
12 #include "platform/RuntimeEnabledFeatures.h" | |
13 #include "platform/ThreadSafeFunctional.h" | |
14 #include "platform/WaitableEvent.h" | |
15 #include "platform/WebThreadSupportingGC.h" | |
16 #include "public/platform/Platform.h" | |
17 #include "public/platform/WebTraceLocation.h" | |
18 | |
19 namespace blink { | |
20 | |
21 namespace { | |
22 | |
23 void noop(PassOwnPtr<WebThreadSupportingGC> thread) {} | |
24 | |
25 } // namespace | |
26 | |
27 WorkerBackingThread::WorkerBackingThread(const char* name) | |
28 : m_backingThread(WebThreadSupportingGC::create(name)) | |
29 , m_isOwningThread(true) | |
30 { | |
31 } | |
32 | |
33 WorkerBackingThread::WorkerBackingThread(WebThread* thread) | |
34 : m_backingThread(WebThreadSupportingGC::createForThread(thread)) | |
35 , m_isOwningThread(false) | |
36 { | |
37 } | |
38 | |
39 WorkerBackingThread::~WorkerBackingThread() | |
40 { | |
41 ASSERT(!workerScriptCount()); | |
42 if (!isMainThread()) { | |
43 // We need to destroy the backing thread in the main thread. | |
44 Platform::current()->mainThread()->taskRunner()->postTask(BLINK_FROM_HER E, threadSafeBind(&noop, m_backingThread.release())); | |
kinuko
2016/02/29 09:32:53
So we make this class thread-safe-refcounted and p
yhirano
2016/02/29 23:47:32
When the WebThreadSuppotingGC changes are landed w
| |
45 } | |
46 } | |
47 | |
48 void WorkerBackingThread::attach() | |
49 { | |
50 ASSERT(m_backingThread->isCurrentThread()); | |
51 { | |
52 MutexLocker locker(m_mutex); | |
53 if (++m_workerScriptCount > 1) | |
54 return; | |
55 } | |
56 initialize(); | |
57 } | |
58 | |
59 void WorkerBackingThread::detach() | |
60 { | |
61 ASSERT(m_backingThread->isCurrentThread()); | |
62 { | |
63 MutexLocker locker(m_mutex); | |
64 if (--m_workerScriptCount > 0) | |
65 return; | |
66 } | |
67 shutdown(); | |
68 } | |
69 | |
70 void WorkerBackingThread::setInitializationEventForTest() | |
71 { | |
72 m_initializationEventForTest = adoptPtr(new WaitableEvent); | |
73 } | |
74 | |
75 void WorkerBackingThread::initialize() | |
76 { | |
77 ASSERT(m_backingThread->isCurrentThread()); | |
78 ASSERT(!m_isolate); | |
79 m_backingThread->initialize(); | |
80 m_isolate = V8PerIsolateData::initialize(); | |
81 V8Initializer::initializeWorker(m_isolate); | |
82 | |
83 OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterruptor (m_isolate)); | |
84 ThreadState::current()->addInterruptor(interruptor.release()); | |
85 ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCController:: traceDOMWrappers); | |
86 if (RuntimeEnabledFeatures::v8IdleTasksEnabled()) | |
87 V8PerIsolateData::enableIdleTasks(m_isolate, adoptPtr(new V8IdleTaskRunn er(backingThread().platformThread().scheduler()))); | |
88 if (m_isOwningThread) | |
89 Platform::current()->didStartWorkerThread(); | |
90 | |
91 if (m_initializationEventForTest) | |
92 m_initializationEventForTest->signal(); | |
93 } | |
94 | |
95 void WorkerBackingThread::shutdown() | |
96 { | |
97 ASSERT(m_backingThread->isCurrentThread()); | |
98 if (m_isOwningThread) | |
99 Platform::current()->willStopWorkerThread(); | |
100 | |
101 V8PerIsolateData::willBeDestroyed(m_isolate); | |
102 m_backingThread->shutdown(); | |
103 V8PerIsolateData::destroy(m_isolate); | |
104 m_isolate = nullptr; | |
105 } | |
106 | |
107 } // namespace blink | |
OLD | NEW |