Chromium Code Reviews| Index: third_party/WebKit/Source/core/workers/WorkerBackingThread.cpp |
| diff --git a/third_party/WebKit/Source/core/workers/WorkerBackingThread.cpp b/third_party/WebKit/Source/core/workers/WorkerBackingThread.cpp |
| index ac85e3f98d49f9bad4722e66f9558816d35a2b55..2c40f498902c476677624c006ad4b1b981691ed9 100644 |
| --- a/third_party/WebKit/Source/core/workers/WorkerBackingThread.cpp |
| +++ b/third_party/WebKit/Source/core/workers/WorkerBackingThread.cpp |
| @@ -4,6 +4,9 @@ |
| #include "core/workers/WorkerBackingThread.h" |
| +#include "base/task_runner.h" |
| +#include "base/threading/platform_thread.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| #include "bindings/core/v8/V8Binding.h" |
| #include "bindings/core/v8/V8GCController.h" |
| #include "bindings/core/v8/V8IdleTaskRunner.h" |
| @@ -15,15 +18,16 @@ |
| #include "public/platform/Platform.h" |
| #include "public/platform/WebTraceLocation.h" |
| #include "wtf/PtrUtil.h" |
| +#include <map> |
| #include <memory> |
| namespace blink { |
| #define DEFINE_STATIC_LOCAL_WITH_LOCK(type, name, arguments) \ |
| - ASSERT(isolatesMutex().locked()); \ |
| + DCHECK(mutex().locked()); \ |
| static type& name = *new type arguments |
| -static Mutex& isolatesMutex() { |
| +static Mutex& mutex() { |
| DEFINE_THREAD_SAFE_STATIC_LOCAL(Mutex, mutex, new Mutex); |
| return mutex; |
| } |
| @@ -34,15 +38,36 @@ static HashSet<v8::Isolate*>& isolates() { |
| } |
| static void addWorkerIsolate(v8::Isolate* isolate) { |
| - MutexLocker lock(isolatesMutex()); |
| + MutexLocker lock(mutex()); |
| isolates().add(isolate); |
| } |
| static void removeWorkerIsolate(v8::Isolate* isolate) { |
| - MutexLocker lock(isolatesMutex()); |
| + MutexLocker lock(mutex()); |
| isolates().remove(isolate); |
| } |
| +using IDToTaskRunnerMap = std::map<base::PlatformThreadId, base::TaskRunner*>; |
|
leonhsl(Using Gerrit)
2016/10/10 12:56:02
Presubmit ERRORS: Do not use Chromium class from n
|
| +static IDToTaskRunnerMap& threadRunners() { |
| + DEFINE_STATIC_LOCAL_WITH_LOCK(IDToTaskRunnerMap, threadRunners, ()); |
| + return threadRunners; |
| +} |
| + |
| +static void addCurrentThreadRunner() { |
| + DCHECK(!base::PlatformThread::CurrentRef().is_null()); |
| + MutexLocker lock(mutex()); |
| + |
| + int id = base::PlatformThread::CurrentId(); |
| + threadRunners()[id] = base::ThreadTaskRunnerHandle::Get().get(); |
| +} |
| + |
| +static void removeCurrentThreadRunner() { |
| + MutexLocker lock(mutex()); |
| + |
| + int id = base::PlatformThread::CurrentId(); |
| + threadRunners().erase(id); |
| +} |
| + |
| WorkerBackingThread::WorkerBackingThread(const char* name, |
| bool shouldCallGCOnShutdown, |
| BlinkGC::ThreadHeapMode threadHeapMode) |
| @@ -76,13 +101,17 @@ void WorkerBackingThread::initialize() { |
| V8PerIsolateData::enableIdleTasks( |
| m_isolate, wrapUnique(new V8IdleTaskRunner( |
| backingThread().platformThread().scheduler()))); |
| - if (m_isOwningThread) |
| + if (m_isOwningThread) { |
| + addCurrentThreadRunner(); |
| Platform::current()->didStartWorkerThread(); |
| + } |
| } |
| void WorkerBackingThread::shutdown() { |
| - if (m_isOwningThread) |
| + if (m_isOwningThread) { |
| Platform::current()->willStopWorkerThread(); |
| + removeCurrentThreadRunner(); |
| + } |
| V8PerIsolateData::willBeDestroyed(m_isolate); |
| // TODO(yhirano): Remove this when https://crbug.com/v8/1428 is fixed. |
| @@ -100,7 +129,7 @@ void WorkerBackingThread::shutdown() { |
| // static |
| void WorkerBackingThread::MemoryPressureNotificationToWorkerThreadIsolates( |
| v8::MemoryPressureLevel level) { |
| - MutexLocker lock(isolatesMutex()); |
| + MutexLocker lock(mutex()); |
| for (v8::Isolate* isolate : isolates()) |
| isolate->MemoryPressureNotification(level); |
| } |
| @@ -108,9 +137,17 @@ void WorkerBackingThread::MemoryPressureNotificationToWorkerThreadIsolates( |
| // static |
| void WorkerBackingThread::setRAILModeOnWorkerThreadIsolates( |
| v8::RAILMode railMode) { |
| - MutexLocker lock(isolatesMutex()); |
| + MutexLocker lock(mutex()); |
| for (v8::Isolate* isolate : isolates()) |
| isolate->SetRAILMode(railMode); |
| } |
| +// static |
| +void WorkerBackingThread::PostTaskToAllThreads(const base::Closure& closure) { |
| + MutexLocker lock(mutex()); |
| + |
| + for (const auto& it : threadRunners()) |
| + it.second->PostTask(FROM_HERE, closure); |
| +} |
| + |
| } // namespace blink |