Chromium Code Reviews| 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 "base/task_runner.h" | |
| 8 #include "base/threading/platform_thread.h" | |
| 9 #include "base/threading/thread_task_runner_handle.h" | |
| 7 #include "bindings/core/v8/V8Binding.h" | 10 #include "bindings/core/v8/V8Binding.h" |
| 8 #include "bindings/core/v8/V8GCController.h" | 11 #include "bindings/core/v8/V8GCController.h" |
| 9 #include "bindings/core/v8/V8IdleTaskRunner.h" | 12 #include "bindings/core/v8/V8IdleTaskRunner.h" |
| 10 #include "bindings/core/v8/V8Initializer.h" | 13 #include "bindings/core/v8/V8Initializer.h" |
| 11 #include "bindings/core/v8/V8PerIsolateData.h" | 14 #include "bindings/core/v8/V8PerIsolateData.h" |
| 12 #include "platform/CrossThreadFunctional.h" | 15 #include "platform/CrossThreadFunctional.h" |
| 13 #include "platform/RuntimeEnabledFeatures.h" | 16 #include "platform/RuntimeEnabledFeatures.h" |
| 14 #include "platform/WebThreadSupportingGC.h" | 17 #include "platform/WebThreadSupportingGC.h" |
| 15 #include "public/platform/Platform.h" | 18 #include "public/platform/Platform.h" |
| 16 #include "public/platform/WebTraceLocation.h" | 19 #include "public/platform/WebTraceLocation.h" |
| 17 #include "wtf/PtrUtil.h" | 20 #include "wtf/PtrUtil.h" |
| 21 #include <map> | |
|
haraken
2016/10/11 11:45:06
Don't use std::map in Blink. You can use HashMap.
leonhsl(Using Gerrit)
2016/10/12 03:55:55
Acknowledged.
| |
| 18 #include <memory> | 22 #include <memory> |
| 19 | 23 |
| 20 namespace blink { | 24 namespace blink { |
| 21 | 25 |
| 22 #define DEFINE_STATIC_LOCAL_WITH_LOCK(type, name, arguments) \ | 26 #define DEFINE_STATIC_LOCAL_WITH_LOCK(type, name, arguments) \ |
| 23 ASSERT(isolatesMutex().locked()); \ | 27 DCHECK(mutex().locked()); \ |
| 24 static type& name = *new type arguments | 28 static type& name = *new type arguments |
| 25 | 29 |
| 26 static Mutex& isolatesMutex() { | 30 static Mutex& mutex() { |
| 27 DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, mutex, new Mutex); | 31 DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, mutex, new Mutex); |
| 28 return mutex; | 32 return mutex; |
| 29 } | 33 } |
| 30 | 34 |
| 31 static HashSet<v8::Isolate*>& isolates() { | 35 static HashSet<v8::Isolate*>& isolates() { |
| 32 DEFINE_STATIC_LOCAL_WITH_LOCK(HashSet<v8::Isolate*>, isolates, ()); | 36 DEFINE_STATIC_LOCAL_WITH_LOCK(HashSet<v8::Isolate*>, isolates, ()); |
| 33 return isolates; | 37 return isolates; |
| 34 } | 38 } |
| 35 | 39 |
| 36 static void addWorkerIsolate(v8::Isolate* isolate) { | 40 static void addWorkerIsolate(v8::Isolate* isolate) { |
| 37 MutexLocker lock(isolatesMutex()); | 41 MutexLocker lock(mutex()); |
| 38 isolates().add(isolate); | 42 isolates().add(isolate); |
| 39 } | 43 } |
| 40 | 44 |
| 41 static void removeWorkerIsolate(v8::Isolate* isolate) { | 45 static void removeWorkerIsolate(v8::Isolate* isolate) { |
| 42 MutexLocker lock(isolatesMutex()); | 46 MutexLocker lock(mutex()); |
| 43 isolates().remove(isolate); | 47 isolates().remove(isolate); |
| 44 } | 48 } |
| 45 | 49 |
| 50 using IDToTaskRunnerMap = std::map<base::PlatformThreadId, base::TaskRunner*>; | |
| 51 static IDToTaskRunnerMap& threadRunners() { | |
|
haraken
2016/10/11 11:45:06
Yeah, this looks nasty.
nhiroki@: What's an expec
leonhsl(Using Gerrit)
2016/10/12 03:55:55
Yeah the purpose is to post a task to all running
nhiroki
2016/10/12 06:20:20
As blundell@'s comment (and an email thread[1]), W
leonhsl(Using Gerrit)
2016/10/12 13:54:44
Thanks a lot! I suppose WorkerThread::isOwningBack
| |
| 52 DEFINE_STATIC_LOCAL_WITH_LOCK(IDToTaskRunnerMap, threadRunners, ()); | |
| 53 return threadRunners; | |
| 54 } | |
| 55 | |
| 56 static void addCurrentThreadRunner() { | |
| 57 DCHECK(!base::PlatformThread::CurrentRef().is_null()); | |
| 58 MutexLocker lock(mutex()); | |
| 59 | |
| 60 int id = base::PlatformThread::CurrentId(); | |
| 61 threadRunners()[id] = base::ThreadTaskRunnerHandle::Get().get(); | |
| 62 } | |
| 63 | |
| 64 static void removeCurrentThreadRunner() { | |
| 65 MutexLocker lock(mutex()); | |
| 66 | |
| 67 int id = base::PlatformThread::CurrentId(); | |
| 68 threadRunners().erase(id); | |
| 69 } | |
| 70 | |
| 46 WorkerBackingThread::WorkerBackingThread(const char* name, | 71 WorkerBackingThread::WorkerBackingThread(const char* name, |
| 47 bool shouldCallGCOnShutdown, | 72 bool shouldCallGCOnShutdown, |
| 48 BlinkGC::ThreadHeapMode threadHeapMode) | 73 BlinkGC::ThreadHeapMode threadHeapMode) |
| 49 : m_backingThread(WebThreadSupportingGC::create(name, threadHeapMode)), | 74 : m_backingThread(WebThreadSupportingGC::create(name, threadHeapMode)), |
| 50 m_isOwningThread(true), | 75 m_isOwningThread(true), |
| 51 m_shouldCallGCOnShutdown(shouldCallGCOnShutdown) {} | 76 m_shouldCallGCOnShutdown(shouldCallGCOnShutdown) {} |
| 52 | 77 |
| 53 WorkerBackingThread::WorkerBackingThread(WebThread* thread, | 78 WorkerBackingThread::WorkerBackingThread(WebThread* thread, |
| 54 bool shouldCallGCOnShutdown) | 79 bool shouldCallGCOnShutdown) |
| 55 : m_backingThread( | 80 : m_backingThread( |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 69 | 94 |
| 70 std::unique_ptr<V8IsolateInterruptor> interruptor = | 95 std::unique_ptr<V8IsolateInterruptor> interruptor = |
| 71 wrapUnique(new V8IsolateInterruptor(m_isolate)); | 96 wrapUnique(new V8IsolateInterruptor(m_isolate)); |
| 72 ThreadState::current()->addInterruptor(std::move(interruptor)); | 97 ThreadState::current()->addInterruptor(std::move(interruptor)); |
| 73 ThreadState::current()->registerTraceDOMWrappers( | 98 ThreadState::current()->registerTraceDOMWrappers( |
| 74 m_isolate, V8GCController::traceDOMWrappers, nullptr, nullptr); | 99 m_isolate, V8GCController::traceDOMWrappers, nullptr, nullptr); |
| 75 if (RuntimeEnabledFeatures::v8IdleTasksEnabled()) | 100 if (RuntimeEnabledFeatures::v8IdleTasksEnabled()) |
| 76 V8PerIsolateData::enableIdleTasks( | 101 V8PerIsolateData::enableIdleTasks( |
| 77 m_isolate, wrapUnique(new V8IdleTaskRunner( | 102 m_isolate, wrapUnique(new V8IdleTaskRunner( |
| 78 backingThread().platformThread().scheduler()))); | 103 backingThread().platformThread().scheduler()))); |
| 79 if (m_isOwningThread) | 104 if (m_isOwningThread) { |
| 105 addCurrentThreadRunner(); | |
| 80 Platform::current()->didStartWorkerThread(); | 106 Platform::current()->didStartWorkerThread(); |
| 107 } | |
| 81 } | 108 } |
| 82 | 109 |
| 83 void WorkerBackingThread::shutdown() { | 110 void WorkerBackingThread::shutdown() { |
| 84 if (m_isOwningThread) | 111 if (m_isOwningThread) { |
| 85 Platform::current()->willStopWorkerThread(); | 112 Platform::current()->willStopWorkerThread(); |
| 113 removeCurrentThreadRunner(); | |
| 114 } | |
| 86 | 115 |
| 87 V8PerIsolateData::willBeDestroyed(m_isolate); | 116 V8PerIsolateData::willBeDestroyed(m_isolate); |
| 88 // TODO(yhirano): Remove this when https://crbug.com/v8/1428 is fixed. | 117 // TODO(yhirano): Remove this when https://crbug.com/v8/1428 is fixed. |
| 89 if (m_shouldCallGCOnShutdown) { | 118 if (m_shouldCallGCOnShutdown) { |
| 90 // This statement runs only in tests. | 119 // This statement runs only in tests. |
| 91 V8GCController::collectAllGarbageForTesting(m_isolate); | 120 V8GCController::collectAllGarbageForTesting(m_isolate); |
| 92 } | 121 } |
| 93 m_backingThread->shutdown(); | 122 m_backingThread->shutdown(); |
| 94 | 123 |
| 95 removeWorkerIsolate(m_isolate); | 124 removeWorkerIsolate(m_isolate); |
| 96 V8PerIsolateData::destroy(m_isolate); | 125 V8PerIsolateData::destroy(m_isolate); |
| 97 m_isolate = nullptr; | 126 m_isolate = nullptr; |
| 98 } | 127 } |
| 99 | 128 |
| 100 // static | 129 // static |
| 101 void WorkerBackingThread::MemoryPressureNotificationToWorkerThreadIsolates( | 130 void WorkerBackingThread::MemoryPressureNotificationToWorkerThreadIsolates( |
| 102 v8::MemoryPressureLevel level) { | 131 v8::MemoryPressureLevel level) { |
| 103 MutexLocker lock(isolatesMutex()); | 132 MutexLocker lock(mutex()); |
| 104 for (v8::Isolate* isolate : isolates()) | 133 for (v8::Isolate* isolate : isolates()) |
| 105 isolate->MemoryPressureNotification(level); | 134 isolate->MemoryPressureNotification(level); |
| 106 } | 135 } |
| 107 | 136 |
| 108 // static | 137 // static |
| 109 void WorkerBackingThread::setRAILModeOnWorkerThreadIsolates( | 138 void WorkerBackingThread::setRAILModeOnWorkerThreadIsolates( |
| 110 v8::RAILMode railMode) { | 139 v8::RAILMode railMode) { |
| 111 MutexLocker lock(isolatesMutex()); | 140 MutexLocker lock(mutex()); |
| 112 for (v8::Isolate* isolate : isolates()) | 141 for (v8::Isolate* isolate : isolates()) |
| 113 isolate->SetRAILMode(railMode); | 142 isolate->SetRAILMode(railMode); |
| 114 } | 143 } |
| 115 | 144 |
| 145 // static | |
| 146 void WorkerBackingThread::PostTaskToAllThreads(const base::Closure& closure) { | |
| 147 MutexLocker lock(mutex()); | |
| 148 | |
| 149 for (const auto& it : threadRunners()) | |
| 150 it.second->PostTask(FROM_HERE, closure); | |
| 151 } | |
| 152 | |
| 116 } // namespace blink | 153 } // namespace blink |
| OLD | NEW |