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) {
// static
MemoryCoordinator& MemoryCoordinator::Instance() {
DEFINE_STATIC_LOCAL(Persistent<MemoryCoordinator>, external,
(new MemoryCoordinator));
DCHECK(IsMainThread());
DEFINE_THREAD_SAFE_STATIC_LOCAL(CrossThreadPersistent<MemoryCoordinator>,
external, (new MemoryCoordinator));
return *external.Get();
}
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) {
MemoryCoordinator::Instance().web_threads_.erase(thread);
MutexLocker lock(web_threads_mutex_);
web_threads_.erase(thread);
}
MemoryCoordinator::MemoryCoordinator() = default;
......@@ -110,6 +111,7 @@ void MemoryCoordinator::OnPurgeMemory() {
WTF::Partitions::DecommitFreeableMemory();
// Thread-specific data never issues a layout, so we are safe here.
MutexLocker lock(web_threads_mutex_);
for (auto thread : web_threads_) {
if (!thread->GetTaskRunner())
continue;
......
......@@ -11,6 +11,7 @@
#include "third_party/blink/renderer/platform/heap/handle.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/threading_primitives.h"
namespace blink {
......@@ -51,8 +52,8 @@ class PLATFORM_EXPORT MemoryCoordinator final
// the heap size.
static void Initialize();
static void RegisterThread(WebThread*);
static void UnregisterThread(WebThread*);
void RegisterThread(WebThread*) LOCKS_EXCLUDED(web_threads_mutex_);
void UnregisterThread(WebThread*) LOCKS_EXCLUDED(web_threads_mutex_);
void RegisterClient(MemoryCoordinatorClient*);
void UnregisterClient(MemoryCoordinatorClient*);
......@@ -81,6 +82,7 @@ class PLATFORM_EXPORT MemoryCoordinator final
HeapHashSet<WeakMember<MemoryCoordinatorClient>> clients_;
HashSet<WebThread*> web_threads_;
Mutex web_threads_mutex_;
};
} // namespace blink
......
......@@ -28,7 +28,7 @@ WebThreadSupportingGC::WebThreadSupportingGC(
const WebThreadCreationParams* params,
WebThread* thread)
: thread_(thread) {
DCHECK(IsMainThread());
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
DCHECK(!params || !thread);
#if DCHECK_IS_ON()
// We call this regardless of whether an existing thread is given or not,
......@@ -43,14 +43,14 @@ WebThreadSupportingGC::WebThreadSupportingGC(
params ? *params : WebThreadCreationParams(WebThreadType::kTestThread));
thread_ = owning_thread_.get();
}
MemoryCoordinator::RegisterThread(thread_);
MemoryCoordinator::Instance().RegisterThread(thread_);
}
WebThreadSupportingGC::~WebThreadSupportingGC() {
DCHECK(IsMainThread());
DETACH_FROM_THREAD(thread_checker_);
// WebThread's destructor blocks until all the tasks are processed.
owning_thread_.reset();
MemoryCoordinator::UnregisterThread(thread_);
MemoryCoordinator::Instance().UnregisterThread(thread_);
}
void WebThreadSupportingGC::InitializeOnThread() {
......
......@@ -6,6 +6,7 @@
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_WEB_THREAD_SUPPORTING_GC_H_
#include <memory>
#include "base/threading/thread_checker.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_thread.h"
#include "third_party/blink/renderer/platform/heap/gc_task_runner.h"
......@@ -86,6 +87,8 @@ class PLATFORM_EXPORT WebThreadSupportingGC final {
// existing thread via createForThread().
WebThread* thread_ = nullptr;
std::unique_ptr<WebThread> owning_thread_;
THREAD_CHECKER(thread_checker_);
};
} // 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