OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "config.h" | |
6 #include "modules/compositorworker/CompositorWorkerManager.h" | |
7 | |
8 #include "platform/ThreadSafeFunctional.h" | |
9 #include "platform/WebThreadSupportingGC.h" | |
10 #include "wtf/MainThread.h" | |
11 #include "wtf/ThreadingPrimitives.h" | |
12 | |
13 namespace blink { | |
14 | |
15 namespace { | |
16 | |
17 class SharedV8Isolate final : public WorkerV8Isolate { | |
18 WTF_MAKE_NONCOPYABLE(SharedV8Isolate); | |
19 public: | |
20 explicit SharedV8Isolate(v8::Isolate* isolate) | |
21 : m_isolate(isolate) { } | |
22 ~SharedV8Isolate() override | |
23 { | |
24 CompositorWorkerManager::instance()->destroyIsolate(); | |
25 } | |
26 | |
27 private: | |
28 // WorkerV8Isolate: | |
29 v8::Isolate* isolate() const override { return m_isolate; } | |
30 void willDestroy() override | |
31 { | |
32 CompositorWorkerManager::instance()->willDestroyIsolate(); | |
33 } | |
34 | |
35 void terminateExecution() override | |
36 { | |
37 CompositorWorkerManager::instance()->terminateIsolate(); | |
38 } | |
39 | |
40 v8::Isolate* m_isolate; | |
41 }; | |
42 | |
43 } // namespace | |
44 | |
45 static CompositorWorkerManager* s_instance = nullptr; | |
kinuko
2015/05/26 14:41:25
Hmm, so we want to have single manager per process
sadrul
2015/05/27 01:28:00
That is indeed the intention, at least for now.
T
| |
46 | |
47 static Mutex& singletonMutex() | |
48 { | |
49 AtomicallyInitializedStaticReference(Mutex, mutex, new Mutex); | |
50 return mutex; | |
51 } | |
52 | |
53 void CompositorWorkerManager::initialize() | |
54 { | |
55 MutexLocker lock(singletonMutex()); | |
56 s_instance = new CompositorWorkerManager(); | |
57 } | |
58 | |
59 void CompositorWorkerManager::shutdown() | |
60 { | |
61 MutexLocker lock(singletonMutex()); | |
62 ASSERT(s_instance); | |
63 delete s_instance; | |
64 s_instance = nullptr; | |
65 } | |
66 | |
67 CompositorWorkerManager* CompositorWorkerManager::instance() | |
68 { | |
69 MutexLocker lock(singletonMutex()); | |
70 ASSERT(s_instance); | |
71 return s_instance; | |
72 } | |
73 | |
74 CompositorWorkerManager::CompositorWorkerManager() | |
75 { | |
76 } | |
77 | |
78 CompositorWorkerManager::~CompositorWorkerManager() | |
79 { | |
80 if (m_isolate) | |
81 ASSERT(m_isolate.leakPtr()); | |
82 } | |
83 | |
84 WebThreadSupportingGC& CompositorWorkerManager::compositorWorkerThread() | |
85 { | |
86 MutexLocker lock(m_mutex); | |
87 if (!m_thread) { | |
88 ASSERT(isMainThread()); | |
89 m_thread = WebThreadSupportingGC::create("CompositorWorker Thread"); | |
90 } | |
91 return *m_thread.get(); | |
92 } | |
93 | |
94 void CompositorWorkerManager::initializeBackingThread() | |
95 { | |
96 ASSERT(m_thread->isCurrentThread()); | |
97 MutexLocker lock(m_mutex); | |
98 ++m_workerCount; | |
99 if (m_workerCount == 1) | |
100 m_thread->initialize(); | |
101 } | |
102 | |
103 static void destroyThread(WebThreadSupportingGC* thread) | |
104 { | |
105 delete thread; | |
106 } | |
kinuko
2015/05/26 14:41:25
nit: moving these static local methods in the anon
sadrul
2015/05/27 01:28:00
Done.
| |
107 | |
108 void CompositorWorkerManager::shutdownBackingThread() | |
109 { | |
110 MutexLocker lock(m_mutex); | |
111 ASSERT(m_thread->isCurrentThread()); | |
112 ASSERT(m_workerCount > 0); | |
113 --m_workerCount; | |
114 if (m_workerCount == 0) { | |
115 m_thread->shutdown(); | |
116 Platform::current()->mainThread()->postTask(FROM_HERE, threadSafeBind(de stroyThread, AllowCrossThreadAccess(m_thread.leakPtr()))); | |
117 } | |
118 } | |
119 | |
120 PassOwnPtr<WorkerV8Isolate> CompositorWorkerManager::createIsolate() | |
121 { | |
122 MutexLocker lock(m_mutex); | |
123 ASSERT(m_thread->isCurrentThread()); | |
124 if (!m_isolate) | |
125 m_isolate = WorkerV8Isolate::createDefault(); | |
126 ++m_isolateCount; | |
127 return adoptPtr(new SharedV8Isolate(m_isolate->isolate())); | |
128 } | |
129 | |
130 void CompositorWorkerManager::willDestroyIsolate() | |
131 { | |
132 MutexLocker lock(m_mutex); | |
133 ASSERT(m_thread->isCurrentThread()); | |
134 --m_isolateCount; | |
135 if (m_isolateCount == 0) | |
136 m_isolate->willDestroy(); | |
137 } | |
138 | |
139 void CompositorWorkerManager::destroyIsolate() | |
140 { | |
141 MutexLocker lock(m_mutex); | |
142 if (m_isolateCount == 0) | |
143 m_isolate = nullptr; | |
144 } | |
145 | |
146 void CompositorWorkerManager::terminateIsolate() | |
147 { | |
148 MutexLocker lock(m_mutex); | |
149 ASSERT(isMainThread()); | |
150 if (m_isolateCount > 1) | |
151 return; | |
152 | |
153 m_isolate->terminateExecution(); | |
154 // TODO(sad): Reset |m_isolate| here, so that if another script is created b efore this one is destroyed, | |
155 // we would end up trying to reuse this terminated isolate. | |
156 } | |
157 | |
158 } // namespace blink | |
OLD | NEW |