Commit e2428d07 authored by sadrul's avatar sadrul Committed by Commit bot

web-threads: Create a single instance of WebThread for each blink thread.

BlinkPlatformImpl currently creates an instance of WebThreadImpl when blink
creates a thread using 'createThread()'. When some code runs in the newly
created thread and tries to get hold of the WebThread that represents the
current thread using 'currentThread()', BlinkPlatformImpl creates a new
instance of a WebThreadImplForMessageLoop and returns that. For subsequent
calls to 'currentThread()', this same WebThreadImplForMessageLoop is
returned. So BlinkPlatformImpl ends up creating two WebThread instances
for each thread created in blink.

This patch changes this to only create a single instance of WebThread for
each thread created in blink. WebThreadImplForMessageLoop is no longer
necessary, so it is removed.

BUG=462067

Review URL: https://codereview.chromium.org/959803003

Cr-Commit-Position: refs/heads/master@{#318913}
parent a611afd2
......@@ -13,7 +13,6 @@
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/metrics/sparse_histogram.h"
#include "base/process/process_metrics.h"
......@@ -417,8 +416,7 @@ BlinkPlatformImpl::BlinkPlatformImpl()
shared_timer_func_(NULL),
shared_timer_fire_time_(0.0),
shared_timer_fire_time_was_set_while_suspended_(false),
shared_timer_suspended_(0),
current_thread_slot_(&DestroyCurrentThread) {
shared_timer_suspended_(0) {
InternalInit();
}
......@@ -428,8 +426,7 @@ BlinkPlatformImpl::BlinkPlatformImpl(
shared_timer_func_(NULL),
shared_timer_fire_time_(0.0),
shared_timer_fire_time_was_set_while_suspended_(false),
shared_timer_suspended_(0),
current_thread_slot_(&DestroyCurrentThread) {
shared_timer_suspended_(0) {
// TODO(alexclarke): Use c++11 delegated constructors when allowed.
InternalInit();
}
......@@ -452,6 +449,11 @@ void BlinkPlatformImpl::InternalInit() {
}
}
void BlinkPlatformImpl::UpdateWebThreadTLS(blink::WebThread* thread) {
DCHECK(!current_thread_slot_.Get());
current_thread_slot_.Set(thread);
}
BlinkPlatformImpl::~BlinkPlatformImpl() {
}
......@@ -499,23 +501,15 @@ bool BlinkPlatformImpl::isReservedIPAddress(
}
blink::WebThread* BlinkPlatformImpl::createThread(const char* name) {
return new WebThreadImpl(name);
WebThreadImpl* thread = new WebThreadImpl(name);
thread->TaskRunner()->PostTask(
FROM_HERE, base::Bind(&BlinkPlatformImpl::UpdateWebThreadTLS,
base::Unretained(this), thread));
return thread;
}
blink::WebThread* BlinkPlatformImpl::currentThread() {
WebThreadImplForMessageLoop* thread =
static_cast<WebThreadImplForMessageLoop*>(current_thread_slot_.Get());
if (thread)
return (thread);
scoped_refptr<base::SingleThreadTaskRunner> task_runner =
MainTaskRunnerForCurrentThread();
if (!task_runner.get())
return NULL;
thread = new WebThreadImplForMessageLoop(task_runner);
current_thread_slot_.Set(thread);
return thread;
return static_cast<blink::WebThread*>(current_thread_slot_.Get());
}
void BlinkPlatformImpl::yieldCurrentThread() {
......@@ -1244,13 +1238,6 @@ BlinkPlatformImpl::MainTaskRunnerForCurrentThread() {
}
}
// static
void BlinkPlatformImpl::DestroyCurrentThread(void* thread) {
WebThreadImplForMessageLoop* impl =
static_cast<WebThreadImplForMessageLoop*>(thread);
delete impl;
}
WebString BlinkPlatformImpl::domCodeStringFromEnum(int dom_code) {
return WebString::fromUTF8(ui::KeycodeConverter::DomCodeToCodeString(
static_cast<ui::DomCode>(dom_code)));
......
......@@ -168,14 +168,13 @@ class CONTENT_EXPORT BlinkPlatformImpl
virtual int domEnumFromCodeString(const blink::WebString& codeString);
private:
static void DestroyCurrentThread(void*);
void DoTimeout() {
if (shared_timer_func_ && !shared_timer_suspended_)
shared_timer_func_();
}
void InternalInit();
void UpdateWebThreadTLS(blink::WebThread* thread);
scoped_refptr<base::SingleThreadTaskRunner> MainTaskRunnerForCurrentThread();
......
......@@ -9,7 +9,6 @@
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/message_loop/message_loop.h"
#include "base/pending_task.h"
#include "base/threading/platform_thread.h"
#include "third_party/WebKit/public/platform/WebTraceLocation.h"
......@@ -119,12 +118,14 @@ void WebThreadBase::postDelayedTask(const blink::WebTraceLocation& web_location,
void WebThreadBase::enterRunLoop() {
CHECK(isCurrentThread());
CHECK(MessageLoop());
CHECK(!MessageLoop()->is_running()); // We don't support nesting.
MessageLoop()->Run();
}
void WebThreadBase::exitRunLoop() {
CHECK(isCurrentThread());
CHECK(MessageLoop());
CHECK(MessageLoop()->is_running());
MessageLoop()->Quit();
}
......@@ -147,33 +148,11 @@ WebThreadImpl::~WebThreadImpl() {
}
base::MessageLoop* WebThreadImpl::MessageLoop() const {
return thread_->message_loop();
return nullptr;
}
base::SingleThreadTaskRunner* WebThreadImpl::TaskRunner() const {
return thread_->message_loop_proxy().get();
}
WebThreadImplForMessageLoop::WebThreadImplForMessageLoop(
scoped_refptr<base::SingleThreadTaskRunner> owning_thread_task_runner)
: owning_thread_task_runner_(owning_thread_task_runner),
thread_id_(base::PlatformThread::CurrentId()) {
}
blink::PlatformThreadId WebThreadImplForMessageLoop::threadId() const {
return thread_id_;
}
WebThreadImplForMessageLoop::~WebThreadImplForMessageLoop() {
}
base::MessageLoop* WebThreadImplForMessageLoop::MessageLoop() const {
DCHECK(isCurrentThread());
return base::MessageLoop::current();
}
base::SingleThreadTaskRunner* WebThreadImplForMessageLoop::TaskRunner() const {
return owning_thread_task_runner_.get();
}
} // namespace content
......@@ -80,25 +80,6 @@ class CONTENT_EXPORT WebThreadImpl : public WebThreadBase {
scoped_ptr<base::Thread> thread_;
};
class WebThreadImplForMessageLoop : public WebThreadBase {
public:
CONTENT_EXPORT explicit WebThreadImplForMessageLoop(
scoped_refptr<base::SingleThreadTaskRunner> owning_thread_task_runner);
CONTENT_EXPORT virtual ~WebThreadImplForMessageLoop();
// blink::WebThread implementation.
blink::PlatformThreadId threadId() const override;
// WebThreadBase implementation.
base::MessageLoop* MessageLoop() const override;
private:
base::SingleThreadTaskRunner* TaskRunner() const override;
scoped_refptr<base::SingleThreadTaskRunner> owning_thread_task_runner_;
blink::PlatformThreadId thread_id_;
};
} // namespace content
#endif // CONTENT_CHILD_WEBTHREAD_IMPL_H_
......@@ -13,6 +13,7 @@
#include "content/public/common/content_switches.h"
#include "content/renderer/scheduler/renderer_scheduler.h"
#include "content/renderer/scheduler/web_scheduler_impl.h"
#include "content/renderer/scheduler/webthread_impl_for_scheduler.h"
#include "content/test/mock_webclipboard_impl.h"
#include "content/test/web_gesture_curve_mock.h"
#include "content/test/web_layer_tree_view_impl_for_testing.h"
......@@ -60,6 +61,7 @@ TestBlinkWebUnitTestSupport::TestBlinkWebUnitTestSupport() {
if (base::MessageLoopProxy::current()) {
renderer_scheduler_ = RendererScheduler::Create();
web_scheduler_.reset(new WebSchedulerImpl(renderer_scheduler_.get()));
web_thread_.reset(new WebThreadImplForScheduler(renderer_scheduler_.get()));
}
blink::initialize(this);
......@@ -319,4 +321,10 @@ blink::WebScheduler* TestBlinkWebUnitTestSupport::scheduler() {
return web_scheduler_.get();
}
blink::WebThread* TestBlinkWebUnitTestSupport::currentThread() {
if (web_thread_ && web_thread_->isCurrentThread())
return web_thread_.get();
return BlinkPlatformImpl::currentThread();
}
} // namespace content
......@@ -86,6 +86,7 @@ class TestBlinkWebUnitTestSupport : public blink::WebUnitTestSupport,
virtual bool getBlobItems(const blink::WebString& uuid,
blink::WebVector<blink::WebBlobData::Item*>* items);
virtual blink::WebScheduler* scheduler();
virtual blink::WebThread* currentThread();
private:
MockWebBlobRegistryImpl blob_registry_;
......@@ -97,6 +98,7 @@ class TestBlinkWebUnitTestSupport : public blink::WebUnitTestSupport,
cc_blink::WebCompositorSupportImpl compositor_support_;
scoped_ptr<RendererScheduler> renderer_scheduler_;
scoped_ptr<WebSchedulerImpl> web_scheduler_;
scoped_ptr<blink::WebThread> web_thread_;
#if defined(OS_WIN) || defined(OS_MACOSX)
blink::WebThemeEngine* active_theme_engine_;
......
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