Commit 37e205eb authored by Nate Chapin's avatar Nate Chapin Committed by Commit Bot

Make MemoryCoordinator and WebThreadSupportingGC usable from a worker thread.

This is necessary to support nested workers.

Bug: 829119
Change-Id: Ib48d10fc724477f720e77a15237ab106d8ed86f9
Reviewed-on: https://chromium-review.googlesource.com/1012794Reviewed-by: default avatarKenichi Ishibashi <bashi@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Commit-Queue: Nate Chapin <japhet@chromium.org>
Cr-Commit-Position: refs/heads/master@{#551389}
parent 9c11f4db
...@@ -57,18 +57,19 @@ void MemoryCoordinator::SetIsLowEndDeviceForTesting(bool is_low_end_device) { ...@@ -57,18 +57,19 @@ void MemoryCoordinator::SetIsLowEndDeviceForTesting(bool is_low_end_device) {
// static // static
MemoryCoordinator& MemoryCoordinator::Instance() { MemoryCoordinator& MemoryCoordinator::Instance() {
DEFINE_STATIC_LOCAL(Persistent<MemoryCoordinator>, external, DEFINE_THREAD_SAFE_STATIC_LOCAL(CrossThreadPersistent<MemoryCoordinator>,
(new MemoryCoordinator)); external, (new MemoryCoordinator));
DCHECK(IsMainThread());
return *external.Get(); return *external.Get();
} }
void MemoryCoordinator::RegisterThread(WebThread* thread) { void MemoryCoordinator::RegisterThread(WebThread* thread) {
MemoryCoordinator::Instance().web_threads_.insert(thread); MutexLocker lock(web_threads_mutex_);
web_threads_.insert(thread);
} }
void MemoryCoordinator::UnregisterThread(WebThread* thread) { void MemoryCoordinator::UnregisterThread(WebThread* thread) {
MemoryCoordinator::Instance().web_threads_.erase(thread); MutexLocker lock(web_threads_mutex_);
web_threads_.erase(thread);
} }
MemoryCoordinator::MemoryCoordinator() = default; MemoryCoordinator::MemoryCoordinator() = default;
...@@ -110,6 +111,7 @@ void MemoryCoordinator::OnPurgeMemory() { ...@@ -110,6 +111,7 @@ void MemoryCoordinator::OnPurgeMemory() {
WTF::Partitions::DecommitFreeableMemory(); WTF::Partitions::DecommitFreeableMemory();
// Thread-specific data never issues a layout, so we are safe here. // Thread-specific data never issues a layout, so we are safe here.
MutexLocker lock(web_threads_mutex_);
for (auto thread : web_threads_) { for (auto thread : web_threads_) {
if (!thread->GetTaskRunner()) if (!thread->GetTaskRunner())
continue; continue;
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "third_party/blink/renderer/platform/heap/handle.h" #include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/platform_export.h" #include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/noncopyable.h" #include "third_party/blink/renderer/platform/wtf/noncopyable.h"
#include "third_party/blink/renderer/platform/wtf/threading_primitives.h"
namespace blink { namespace blink {
...@@ -51,8 +52,8 @@ class PLATFORM_EXPORT MemoryCoordinator final ...@@ -51,8 +52,8 @@ class PLATFORM_EXPORT MemoryCoordinator final
// the heap size. // the heap size.
static void Initialize(); static void Initialize();
static void RegisterThread(WebThread*); void RegisterThread(WebThread*) LOCKS_EXCLUDED(web_threads_mutex_);
static void UnregisterThread(WebThread*); void UnregisterThread(WebThread*) LOCKS_EXCLUDED(web_threads_mutex_);
void RegisterClient(MemoryCoordinatorClient*); void RegisterClient(MemoryCoordinatorClient*);
void UnregisterClient(MemoryCoordinatorClient*); void UnregisterClient(MemoryCoordinatorClient*);
...@@ -81,6 +82,7 @@ class PLATFORM_EXPORT MemoryCoordinator final ...@@ -81,6 +82,7 @@ class PLATFORM_EXPORT MemoryCoordinator final
HeapHashSet<WeakMember<MemoryCoordinatorClient>> clients_; HeapHashSet<WeakMember<MemoryCoordinatorClient>> clients_;
HashSet<WebThread*> web_threads_; HashSet<WebThread*> web_threads_;
Mutex web_threads_mutex_;
}; };
} // namespace blink } // namespace blink
......
...@@ -28,7 +28,7 @@ WebThreadSupportingGC::WebThreadSupportingGC( ...@@ -28,7 +28,7 @@ WebThreadSupportingGC::WebThreadSupportingGC(
const WebThreadCreationParams* params, const WebThreadCreationParams* params,
WebThread* thread) WebThread* thread)
: thread_(thread) { : thread_(thread) {
DCHECK(IsMainThread()); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK(!params || !thread); DCHECK(!params || !thread);
#if DCHECK_IS_ON() #if DCHECK_IS_ON()
// We call this regardless of whether an existing thread is given or not, // We call this regardless of whether an existing thread is given or not,
...@@ -43,14 +43,14 @@ WebThreadSupportingGC::WebThreadSupportingGC( ...@@ -43,14 +43,14 @@ WebThreadSupportingGC::WebThreadSupportingGC(
params ? *params : WebThreadCreationParams(WebThreadType::kTestThread)); params ? *params : WebThreadCreationParams(WebThreadType::kTestThread));
thread_ = owning_thread_.get(); thread_ = owning_thread_.get();
} }
MemoryCoordinator::RegisterThread(thread_); MemoryCoordinator::Instance().RegisterThread(thread_);
} }
WebThreadSupportingGC::~WebThreadSupportingGC() { WebThreadSupportingGC::~WebThreadSupportingGC() {
DCHECK(IsMainThread()); DETACH_FROM_THREAD(thread_checker_);
// WebThread's destructor blocks until all the tasks are processed. // WebThread's destructor blocks until all the tasks are processed.
owning_thread_.reset(); owning_thread_.reset();
MemoryCoordinator::UnregisterThread(thread_); MemoryCoordinator::Instance().UnregisterThread(thread_);
} }
void WebThreadSupportingGC::InitializeOnThread() { void WebThreadSupportingGC::InitializeOnThread() {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_WEB_THREAD_SUPPORTING_GC_H_ #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_WEB_THREAD_SUPPORTING_GC_H_
#include <memory> #include <memory>
#include "base/threading/thread_checker.h"
#include "third_party/blink/public/platform/platform.h" #include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_thread.h" #include "third_party/blink/public/platform/web_thread.h"
#include "third_party/blink/renderer/platform/heap/gc_task_runner.h" #include "third_party/blink/renderer/platform/heap/gc_task_runner.h"
...@@ -86,6 +87,8 @@ class PLATFORM_EXPORT WebThreadSupportingGC final { ...@@ -86,6 +87,8 @@ class PLATFORM_EXPORT WebThreadSupportingGC final {
// existing thread via createForThread(). // existing thread via createForThread().
WebThread* thread_ = nullptr; WebThread* thread_ = nullptr;
std::unique_ptr<WebThread> owning_thread_; std::unique_ptr<WebThread> owning_thread_;
THREAD_CHECKER(thread_checker_);
}; };
} // namespace blink } // namespace blink
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment