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 static CompositorWorkerManager* s_instance = nullptr; | |
44 | |
45 static Mutex& singletonMutex() | |
46 { | |
47 AtomicallyInitializedStaticReference(Mutex, mutex, new Mutex); | |
48 return mutex; | |
49 } | |
50 | |
51 static void destroyThread(WebThreadSupportingGC* thread) | |
52 { | |
53 delete thread; | |
54 } | |
55 | |
56 } // namespace | |
57 | |
58 void CompositorWorkerManager::initialize() | |
59 { | |
60 MutexLocker lock(singletonMutex()); | |
61 ASSERT(!s_instance); | |
62 s_instance = new CompositorWorkerManager(); | |
63 } | |
64 | |
65 void CompositorWorkerManager::shutdown() | |
66 { | |
67 MutexLocker lock(singletonMutex()); | |
68 if (s_instance) | |
69 delete s_instance; | |
70 s_instance = nullptr; | |
71 } | |
72 | |
73 CompositorWorkerManager* CompositorWorkerManager::instance() | |
74 { | |
75 MutexLocker lock(singletonMutex()); | |
76 ASSERT(s_instance); | |
77 return s_instance; | |
78 } | |
79 | |
80 CompositorWorkerManager::CompositorWorkerManager() | |
81 { | |
82 } | |
83 | |
84 CompositorWorkerManager::~CompositorWorkerManager() | |
85 { | |
86 } | |
87 | |
88 WebThreadSupportingGC& CompositorWorkerManager::compositorWorkerThread() | |
89 { | |
90 MutexLocker lock(m_mutex); | |
91 if (!m_thread) { | |
92 ASSERT(isMainThread()); | |
kinuko
2015/05/28 16:21:59
ASSERT(!m_workerCount) ? (does this hold?)
sadrul
2015/05/28 16:51:56
Yep. Done.
| |
93 m_thread = WebThreadSupportingGC::create("CompositorWorker Thread"); | |
94 } | |
95 return *m_thread.get(); | |
96 } | |
97 | |
98 void CompositorWorkerManager::initializeBackingThread() | |
99 { | |
100 ASSERT(m_thread->isCurrentThread()); | |
101 MutexLocker lock(m_mutex); | |
102 ++m_workerCount; | |
103 if (m_workerCount == 1) | |
104 m_thread->initialize(); | |
105 } | |
106 | |
107 void CompositorWorkerManager::shutdownBackingThread() | |
108 { | |
109 MutexLocker lock(m_mutex); | |
110 ASSERT(m_thread->isCurrentThread()); | |
111 ASSERT(m_workerCount > 0); | |
112 --m_workerCount; | |
113 if (m_workerCount == 0) { | |
114 m_thread->shutdown(); | |
115 Platform::current()->mainThread()->postTask(FROM_HERE, threadSafeBind(de stroyThread, AllowCrossThreadAccess(m_thread.leakPtr()))); | |
116 } | |
117 } | |
118 | |
119 PassOwnPtr<WorkerV8Isolate> CompositorWorkerManager::createIsolate() | |
120 { | |
121 MutexLocker lock(m_mutex); | |
122 ASSERT(m_thread->isCurrentThread()); | |
123 if (!m_isolate) | |
124 m_isolate = WorkerV8Isolate::createDefault(); | |
125 else if (m_isolate->isolate()->IsExecutionTerminating()) | |
126 m_isolate->isolate()->CancelTerminateExecution(); | |
127 ++m_isolateCount; | |
128 return adoptPtr(new SharedV8Isolate(m_isolate->isolate())); | |
129 } | |
130 | |
131 void CompositorWorkerManager::willDestroyIsolate() | |
132 { | |
133 MutexLocker lock(m_mutex); | |
134 ASSERT(m_thread->isCurrentThread()); | |
135 --m_isolateCount; | |
136 if (m_isolateCount == 0) | |
137 m_isolate->willDestroy(); | |
138 } | |
139 | |
140 void CompositorWorkerManager::destroyIsolate() | |
141 { | |
142 MutexLocker lock(m_mutex); | |
143 if (m_isolateCount == 0) | |
144 m_isolate = nullptr; | |
145 } | |
146 | |
147 void CompositorWorkerManager::terminateIsolate() | |
148 { | |
149 MutexLocker lock(m_mutex); | |
150 ASSERT(isMainThread()); | |
151 if (m_isolateCount > 1) | |
152 return; | |
153 | |
154 m_isolate->terminateExecution(); | |
155 } | |
156 | |
157 } // namespace blink | |
OLD | NEW |