| 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/WebThreadSupportingGC.h" | 14 #include "platform/WebThreadSupportingGC.h" |
| 15 #include "public/platform/Platform.h" | 15 #include "public/platform/Platform.h" |
| 16 #include "public/platform/WebTraceLocation.h" | 16 #include "public/platform/WebTraceLocation.h" |
| 17 | 17 |
| 18 namespace blink { | 18 namespace blink { |
| 19 | 19 |
| 20 #define DEFINE_STATIC_LOCAL_WITH_LOCK(type, name, arguments) \ | |
| 21 ASSERT(isolatesMutex().locked()); \ | |
| 22 static type& name = *new type arguments | |
| 23 | |
| 24 static Mutex& isolatesMutex() | |
| 25 { | |
| 26 DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, mutex, new Mutex); | |
| 27 return mutex; | |
| 28 } | |
| 29 | |
| 30 static HashSet<v8::Isolate*>& isolates() | |
| 31 { | |
| 32 DEFINE_STATIC_LOCAL_WITH_LOCK(HashSet<v8::Isolate*>, isolates, ()); | |
| 33 return isolates; | |
| 34 } | |
| 35 | |
| 36 static void addWorkerIsolate(v8::Isolate* isolate) | |
| 37 { | |
| 38 MutexLocker lock(isolatesMutex()); | |
| 39 isolates().add(isolate); | |
| 40 } | |
| 41 | |
| 42 static void removeWorkerIsolate(v8::Isolate* isolate) | |
| 43 { | |
| 44 MutexLocker lock(isolatesMutex()); | |
| 45 isolates().remove(isolate); | |
| 46 } | |
| 47 | |
| 48 WorkerBackingThread::WorkerBackingThread(const char* name, bool shouldCallGCOnSh
utdown) | 20 WorkerBackingThread::WorkerBackingThread(const char* name, bool shouldCallGCOnSh
utdown) |
| 49 : m_backingThread(WebThreadSupportingGC::create(name)) | 21 : m_backingThread(WebThreadSupportingGC::create(name)) |
| 50 , m_isOwningThread(true) | 22 , m_isOwningThread(true) |
| 51 , m_shouldCallGCOnShutdown(shouldCallGCOnShutdown) | 23 , m_shouldCallGCOnShutdown(shouldCallGCOnShutdown) |
| 52 { | 24 { |
| 53 } | 25 } |
| 54 | 26 |
| 55 WorkerBackingThread::WorkerBackingThread(WebThread* thread, bool shouldCallGCOnS
hutdown) | 27 WorkerBackingThread::WorkerBackingThread(WebThread* thread, bool shouldCallGCOnS
hutdown) |
| 56 : m_backingThread(WebThreadSupportingGC::createForThread(thread)) | 28 : m_backingThread(WebThreadSupportingGC::createForThread(thread)) |
| 57 , m_isOwningThread(false) | 29 , m_isOwningThread(false) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 84 if (--m_workerScriptCount > 0) | 56 if (--m_workerScriptCount > 0) |
| 85 return; | 57 return; |
| 86 } | 58 } |
| 87 shutdown(); | 59 shutdown(); |
| 88 } | 60 } |
| 89 | 61 |
| 90 void WorkerBackingThread::initialize() | 62 void WorkerBackingThread::initialize() |
| 91 { | 63 { |
| 92 DCHECK(!m_isolate); | 64 DCHECK(!m_isolate); |
| 93 m_isolate = V8PerIsolateData::initialize(); | 65 m_isolate = V8PerIsolateData::initialize(); |
| 94 addWorkerIsolate(m_isolate); | |
| 95 V8Initializer::initializeWorker(m_isolate); | 66 V8Initializer::initializeWorker(m_isolate); |
| 96 m_backingThread->initialize(); | 67 m_backingThread->initialize(); |
| 97 | 68 |
| 98 OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterruptor
(m_isolate)); | 69 OwnPtr<V8IsolateInterruptor> interruptor = adoptPtr(new V8IsolateInterruptor
(m_isolate)); |
| 99 ThreadState::current()->addInterruptor(interruptor.release()); | 70 ThreadState::current()->addInterruptor(interruptor.release()); |
| 100 ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCController::
traceDOMWrappers); | 71 ThreadState::current()->registerTraceDOMWrappers(m_isolate, V8GCController::
traceDOMWrappers); |
| 101 if (RuntimeEnabledFeatures::v8IdleTasksEnabled()) | 72 if (RuntimeEnabledFeatures::v8IdleTasksEnabled()) |
| 102 V8PerIsolateData::enableIdleTasks(m_isolate, adoptPtr(new V8IdleTaskRunn
er(backingThread().platformThread().scheduler()))); | 73 V8PerIsolateData::enableIdleTasks(m_isolate, adoptPtr(new V8IdleTaskRunn
er(backingThread().platformThread().scheduler()))); |
| 103 if (m_isOwningThread) | 74 if (m_isOwningThread) |
| 104 Platform::current()->didStartWorkerThread(); | 75 Platform::current()->didStartWorkerThread(); |
| 105 } | 76 } |
| 106 | 77 |
| 107 void WorkerBackingThread::shutdown() | 78 void WorkerBackingThread::shutdown() |
| 108 { | 79 { |
| 109 if (m_isOwningThread) | 80 if (m_isOwningThread) |
| 110 Platform::current()->willStopWorkerThread(); | 81 Platform::current()->willStopWorkerThread(); |
| 111 | 82 |
| 112 V8PerIsolateData::willBeDestroyed(m_isolate); | 83 V8PerIsolateData::willBeDestroyed(m_isolate); |
| 113 // TODO(yhirano): Remove this when https://crbug.com/v8/1428 is fixed. | 84 // TODO(yhirano): Remove this when https://crbug.com/v8/1428 is fixed. |
| 114 if (m_shouldCallGCOnShutdown) { | 85 if (m_shouldCallGCOnShutdown) { |
| 115 // This statement runs only in tests. | 86 // This statement runs only in tests. |
| 116 V8GCController::collectAllGarbageForTesting(m_isolate); | 87 V8GCController::collectAllGarbageForTesting(m_isolate); |
| 117 } | 88 } |
| 118 m_backingThread->shutdown(); | 89 m_backingThread->shutdown(); |
| 119 | 90 |
| 120 V8PerIsolateData::destroy(m_isolate); | 91 V8PerIsolateData::destroy(m_isolate); |
| 121 removeWorkerIsolate(m_isolate); | |
| 122 m_isolate = nullptr; | 92 m_isolate = nullptr; |
| 123 } | 93 } |
| 124 | 94 |
| 125 // static | |
| 126 void WorkerBackingThread::MemoryPressureNotificationToWorkerThreadIsolates( | |
| 127 v8::MemoryPressureLevel level) | |
| 128 { | |
| 129 MutexLocker lock(isolatesMutex()); | |
| 130 for (v8::Isolate* isolate : isolates()) | |
| 131 isolate->MemoryPressureNotification(level); | |
| 132 } | |
| 133 | |
| 134 } // namespace blink | 95 } // namespace blink |
| OLD | NEW |