Commit e66dcc06 authored by Francois Doray's avatar Francois Doray Committed by Chromium LUCI CQ

[blink scheduler] Add blink::Thread::ThreadHandle.

The thread handle should be used instead of the thread id for thread
manipulations. It is available immediately after thread creation,
unlike the thread id which is available only after the main function
is entered. Also, on Windows, the thread handle remains valid after the
thread has exited.

This is a prerequisite to remove the wait after compositor thread
initialization
https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/platform/scheduler/common/thread.cc;l=96;drc=5b1ca7b1087e13d963bc4058d9406a838fb7f9ca

Bug: 1080709
Change-Id: Ia0b72aa1b3a86454cffa774131944db87ced0843
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2578172Reviewed-by: default avatarAlexander Timin <altimin@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Commit-Queue: Kentaro Hara <haraken@chromium.org>
Auto-Submit: François Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/master@{#835544}
parent 09d2698e
...@@ -60,6 +60,11 @@ PlatformThreadId SimpleThread::tid() { ...@@ -60,6 +60,11 @@ PlatformThreadId SimpleThread::tid() {
return tid_; return tid_;
} }
PlatformThreadHandle SimpleThread::handle() const {
DCHECK(!thread_.is_null());
return thread_;
}
bool SimpleThread::HasBeenStarted() { bool SimpleThread::HasBeenStarted() {
return event_.IsSignaled(); return event_.IsSignaled();
} }
......
...@@ -112,6 +112,10 @@ class BASE_EXPORT SimpleThread : public PlatformThread::Delegate { ...@@ -112,6 +112,10 @@ class BASE_EXPORT SimpleThread : public PlatformThread::Delegate {
// be called before HasBeenStarted() returns True. // be called before HasBeenStarted() returns True.
PlatformThreadId tid(); PlatformThreadId tid();
// Returns the thread handle. Valid only if the thread is joinable, started
// and not yet joined.
PlatformThreadHandle handle() const;
// Returns True if the thread has been started and initialized (i.e. if // Returns True if the thread has been started and initialized (i.e. if
// ThreadMain() has run). If the thread was started with StartAsync(), but it // ThreadMain() has run). If the thread was started with StartAsync(), but it
// hasn't been initialized yet (i.e. ThreadMain() has not run), then this will // hasn't been initialized yet (i.e. ThreadMain() has not run), then this will
......
...@@ -234,11 +234,12 @@ RendererBlinkPlatformImpl::WrapSharedURLLoaderFactory( ...@@ -234,11 +234,12 @@ RendererBlinkPlatformImpl::WrapSharedURLLoaderFactory(
} }
void RendererBlinkPlatformImpl::SetDisplayThreadPriority( void RendererBlinkPlatformImpl::SetDisplayThreadPriority(
base::PlatformThreadId thread_id) { base::PlatformThreadHandle thread_handle) {
#if defined(OS_LINUX) || defined(OS_CHROMEOS) #if defined(OS_LINUX) || defined(OS_CHROMEOS)
if (RenderThreadImpl* render_thread = RenderThreadImpl::current()) { if (RenderThreadImpl* render_thread = RenderThreadImpl::current()) {
// On Linux and ChromeOS, the thread handle is equivalent to the thread id.
render_thread->render_message_filter()->SetThreadPriority( render_thread->render_message_filter()->SetThreadPriority(
thread_id, base::ThreadPriority::DISPLAY); thread_handle.platform_handle(), base::ThreadPriority::DISPLAY);
} }
#endif #endif
} }
......
...@@ -183,7 +183,8 @@ class CONTENT_EXPORT RendererBlinkPlatformImpl : public BlinkPlatformImpl { ...@@ -183,7 +183,8 @@ class CONTENT_EXPORT RendererBlinkPlatformImpl : public BlinkPlatformImpl {
const blink::WebURL& top_document_web_url) override; const blink::WebURL& top_document_web_url) override;
gpu::GpuMemoryBufferManager* GetGpuMemoryBufferManager() override; gpu::GpuMemoryBufferManager* GetGpuMemoryBufferManager() override;
blink::WebString ConvertIDNToUnicode(const blink::WebString& host) override; blink::WebString ConvertIDNToUnicode(const blink::WebString& host) override;
void SetDisplayThreadPriority(base::PlatformThreadId thread_id) override; void SetDisplayThreadPriority(
base::PlatformThreadHandle thread_handle) override;
blink::BlameContext* GetTopLevelBlameContext() override; blink::BlameContext* GetTopLevelBlameContext() override;
std::unique_ptr<blink::WebDedicatedWorkerHostFactoryClient> std::unique_ptr<blink::WebDedicatedWorkerHostFactoryClient>
CreateDedicatedWorkerHostFactoryClient( CreateDedicatedWorkerHostFactoryClient(
......
...@@ -389,7 +389,7 @@ class BLINK_PLATFORM_EXPORT Platform { ...@@ -389,7 +389,7 @@ class BLINK_PLATFORM_EXPORT Platform {
// increase the nice value, so we need to ask the browser process). This // increase the nice value, so we need to ask the browser process). This
// function is only called from the main thread (where InitializeCompositor- // function is only called from the main thread (where InitializeCompositor-
// Thread() is called). // Thread() is called).
virtual void SetDisplayThreadPriority(base::PlatformThreadId) {} virtual void SetDisplayThreadPriority(base::PlatformThreadHandle) {}
// Returns a blame context for attributing top-level work which does not // Returns a blame context for attributing top-level work which does not
// belong to a particular frame scope. // belong to a particular frame scope.
......
...@@ -102,7 +102,7 @@ void Thread::CreateAndSetCompositorThread() { ...@@ -102,7 +102,7 @@ void Thread::CreateAndSetCompositorThread() {
// This is not possible inside the sandbox, so ask the browser to do it. // This is not possible inside the sandbox, so ask the browser to do it.
// TODO(spang): Check if we can remove this on non-Chrome OS builds. // TODO(spang): Check if we can remove this on non-Chrome OS builds.
Platform::Current()->SetDisplayThreadPriority( Platform::Current()->SetDisplayThreadPriority(
GetCompositorThread()->ThreadId()); GetCompositorThread()->ThreadHandle());
} }
} }
......
...@@ -14,7 +14,8 @@ namespace scheduler { ...@@ -14,7 +14,8 @@ namespace scheduler {
MainThread::MainThread(MainThreadSchedulerImpl* scheduler) MainThread::MainThread(MainThreadSchedulerImpl* scheduler)
: task_runner_(scheduler->DefaultTaskRunner()), : task_runner_(scheduler->DefaultTaskRunner()),
scheduler_(scheduler), scheduler_(scheduler),
thread_id_(base::PlatformThread::CurrentId()) {} thread_id_(base::PlatformThread::CurrentId()),
thread_handle_(base::PlatformThread::CurrentHandle()) {}
MainThread::~MainThread() = default; MainThread::~MainThread() = default;
...@@ -22,6 +23,10 @@ blink::PlatformThreadId MainThread::ThreadId() const { ...@@ -22,6 +23,10 @@ blink::PlatformThreadId MainThread::ThreadId() const {
return thread_id_; return thread_id_;
} }
base::PlatformThreadHandle MainThread::ThreadHandle() const {
return thread_handle_;
}
blink::ThreadScheduler* MainThread::Scheduler() { blink::ThreadScheduler* MainThread::Scheduler() {
return scheduler_; return scheduler_;
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/threading/platform_thread.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/scheduler/public/thread.h" #include "third_party/blink/renderer/platform/scheduler/public/thread.h"
...@@ -26,6 +27,7 @@ class PLATFORM_EXPORT MainThread : public Thread { ...@@ -26,6 +27,7 @@ class PLATFORM_EXPORT MainThread : public Thread {
// Thread implementation. // Thread implementation.
ThreadScheduler* Scheduler() override; ThreadScheduler* Scheduler() override;
PlatformThreadId ThreadId() const override; PlatformThreadId ThreadId() const override;
base::PlatformThreadHandle ThreadHandle() const override;
scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() const override; scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() const override;
void AddTaskTimeObserver(base::sequence_manager::TaskTimeObserver*) override; void AddTaskTimeObserver(base::sequence_manager::TaskTimeObserver*) override;
...@@ -33,9 +35,10 @@ class PLATFORM_EXPORT MainThread : public Thread { ...@@ -33,9 +35,10 @@ class PLATFORM_EXPORT MainThread : public Thread {
base::sequence_manager::TaskTimeObserver*) override; base::sequence_manager::TaskTimeObserver*) override;
private: private:
scoped_refptr<base::SingleThreadTaskRunner> task_runner_; const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
MainThreadSchedulerImpl* scheduler_; // Not owned. MainThreadSchedulerImpl* const scheduler_; // Not owned.
PlatformThreadId thread_id_; const PlatformThreadId thread_id_;
const base::PlatformThreadHandle thread_handle_;
}; };
} // namespace scheduler } // namespace scheduler
......
...@@ -26,10 +26,12 @@ ...@@ -26,10 +26,12 @@
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_PUBLIC_THREAD_H_ #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_PUBLIC_THREAD_H_
#include <stdint.h> #include <stdint.h>
#include "base/callback_forward.h" #include "base/callback_forward.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/scoped_refptr.h" #include "base/memory/scoped_refptr.h"
#include "base/task/task_observer.h" #include "base/task/task_observer.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread.h" #include "base/threading/thread.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/scheduler/public/thread_type.h" #include "third_party/blink/renderer/platform/scheduler/public/thread_type.h"
...@@ -126,7 +128,10 @@ class PLATFORM_EXPORT Thread { ...@@ -126,7 +128,10 @@ class PLATFORM_EXPORT Thread {
} }
bool IsCurrentThread() const; bool IsCurrentThread() const;
virtual PlatformThreadId ThreadId() const { return 0; } virtual PlatformThreadId ThreadId() const { return base::kInvalidThreadId; }
virtual base::PlatformThreadHandle ThreadHandle() const {
return base::PlatformThreadHandle();
}
// TaskObserver is an object that receives task notifications from the // TaskObserver is an object that receives task notifications from the
// MessageLoop. // MessageLoop.
......
...@@ -76,6 +76,10 @@ blink::PlatformThreadId WorkerThread::ThreadId() const { ...@@ -76,6 +76,10 @@ blink::PlatformThreadId WorkerThread::ThreadId() const {
return thread_->tid(); return thread_->tid();
} }
base::PlatformThreadHandle WorkerThread::ThreadHandle() const {
return thread_->handle();
}
blink::ThreadScheduler* WorkerThread::Scheduler() { blink::ThreadScheduler* WorkerThread::Scheduler() {
return thread_->GetNonMainThreadScheduler(); return thread_->GetNonMainThreadScheduler();
} }
......
...@@ -42,6 +42,7 @@ class PLATFORM_EXPORT WorkerThread : public Thread { ...@@ -42,6 +42,7 @@ class PLATFORM_EXPORT WorkerThread : public Thread {
void Init() override; void Init() override;
ThreadScheduler* Scheduler() override; ThreadScheduler* Scheduler() override;
PlatformThreadId ThreadId() const override; PlatformThreadId ThreadId() const override;
base::PlatformThreadHandle ThreadHandle() const override;
scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() const override; scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() const override;
scheduler::NonMainThreadSchedulerImpl* GetNonMainThreadScheduler() { scheduler::NonMainThreadSchedulerImpl* GetNonMainThreadScheduler() {
......
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