Commit 90a7222e authored by Olivier Li's avatar Olivier Li Committed by Chromium LUCI CQ

Make HangWatcher::RegisterThread() static

This supports cases where the HangWatcher is not started and adding code
to start it would be unnecessary. For example : android_browsertests

Bug: 1157943
Change-Id: I9fdc2905a25f67acd6c0e5608eb2c3d5fac19fa2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2593551Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Commit-Queue: Oliver Li <olivierli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#837292}
parent 09f8cf62
......@@ -310,9 +310,8 @@ void WorkerThread::RunWorker() {
// If this process has a HangWatcher register this thread for watching.
base::ScopedClosureRunner unregister_for_hang_watching;
if (watch_for_hangs) {
unregister_for_hang_watching =
base::HangWatcher::GetInstance()->RegisterThread(
base::HangWatcher::ThreadType::kThreadPoolThread);
unregister_for_hang_watching = base::HangWatcher::RegisterThread(
base::HangWatcher::ThreadType::kThreadPoolThread);
}
// A WorkerThread starts out waiting for work.
......
......@@ -8,6 +8,7 @@
#include <utility>
#include "base/bind.h"
#include "base/callback_forward.h"
#include "base/callback_helpers.h"
#include "base/containers/flat_map.h"
#include "base/debug/alias.h"
......@@ -471,7 +472,8 @@ void HangWatcher::RecordHang() {
base::debug::Alias(&line_number);
}
ScopedClosureRunner HangWatcher::RegisterThread(ThreadType thread_type) {
ScopedClosureRunner HangWatcher::RegisterThreadInternal(
ThreadType thread_type) {
AutoLock auto_lock(watch_state_lock_);
watch_states_.push_back(
......@@ -482,6 +484,15 @@ ScopedClosureRunner HangWatcher::RegisterThread(ThreadType thread_type) {
Unretained(HangWatcher::GetInstance())));
}
// static
ScopedClosureRunner HangWatcher::RegisterThread(ThreadType thread_type) {
if (!GetInstance()) {
return ScopedClosureRunner();
}
return GetInstance()->RegisterThreadInternal(thread_type);
}
base::TimeTicks HangWatcher::WatchStateSnapShot::GetHighestDeadline() const {
DCHECK(!hung_watch_state_copies_.empty());
// Since entries are sorted in increasing order the last entry is the largest
......
......@@ -181,9 +181,11 @@ class BASE_EXPORT HangWatcher : public DelegateSimpleThread::Delegate {
// Sets up the calling thread to be monitored for threads. Returns a
// ScopedClosureRunner that unregisters the thread. This closure has to be
// called from the registered thread before it's joined.
ScopedClosureRunner RegisterThread(ThreadType thread_type)
LOCKS_EXCLUDED(watch_state_lock_) WARN_UNUSED_RESULT;
// called from the registered thread before it's joined. Returns a null
// closure in the case where there is no HangWatcher instance to register the
// thread with.
static ScopedClosureRunner RegisterThread(ThreadType thread_type)
WARN_UNUSED_RESULT;
// Choose a closure to be run at the end of each call to Monitor(). Use only
// for testing. Reentering the HangWatcher in the closure must be done with
......@@ -230,6 +232,10 @@ class BASE_EXPORT HangWatcher : public DelegateSimpleThread::Delegate {
void Start();
private:
// See comment of ::RegisterThread() for details.
ScopedClosureRunner RegisterThreadInternal(ThreadType thread_type)
LOCKS_EXCLUDED(watch_state_lock_) WARN_UNUSED_RESULT;
// Use to assert that functions are called on the monitoring thread.
THREAD_CHECKER(hang_watcher_thread_checker_);
......
......@@ -67,7 +67,7 @@ class BlockingThread : public DelegateSimpleThread::Delegate {
// (Un)Register the thread here instead of in ctor/dtor so that the action
// happens on the right thread.
base::ScopedClosureRunner unregister_closure =
base::HangWatcher::GetInstance()->RegisterThread(
base::HangWatcher::RegisterThread(
base::HangWatcher::ThreadType::kUIThread);
HangWatchScopeEnabled scope(timeout_);
......@@ -207,7 +207,7 @@ TEST_F(
ScopeDisabledCreateScopeDisabledDestroyScopeEnabledCreateScopeEnabledDestroy) {
// Register the main test thread for hang watching.
auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread);
HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
{
HangWatchScopeEnabled expires_instantly(base::TimeDelta{});
......@@ -235,7 +235,7 @@ TEST_F(
ScopeEnabledCreateScopeDisabledCreateScopeEnabledDestroyScopeDisabledDestroy) {
// Register the main test thread for hang watching.
auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread);
HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
base::Optional<HangWatchScopeDisabled> disabler;
......@@ -265,7 +265,7 @@ TEST_F(
ScopeDisabledCreateScopeEnabledCreateScopeDisabledDestroyScopeEnabledDestroy) {
// Register the main test thread for hang watching.
auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread);
HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
base::Optional<HangWatchScopeDisabled> disabler;
......@@ -295,7 +295,7 @@ TEST_F(
ScopeDisabledCreateScopeEnabledCreateScopeEnabledDestroyScopeDisabledDestroy) {
// Register the main test thread for hang watching.
auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread);
HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
// De-activate hang watching,
HangWatchScopeDisabled disabler;
......@@ -317,7 +317,7 @@ TEST_F(
TEST_F(HangWatcherTest, ScopeCreateTempCreateTempDestroyScopeDestroy) {
// Register the main test thread for hang watching.
auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread);
HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
{
// Start a HangWatchScopeEnabled that expires right away. Then advance
// time to make sure a hang is detected.
......@@ -343,7 +343,7 @@ TEST_F(
ScopeEnabledCreateScopeDisabledCreateScopeDisabledDestroyScopeEnabledDestroy) {
// Register the main test thread for hang watching.
auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread);
HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
{
// Start a HangWatchScopeEnabled that expires right away. Then advance
// time to make sure a hang is detected.
......@@ -367,7 +367,7 @@ TEST_F(
TEST_F(HangWatcherTest, ScopeDisabledObjectInnerScope) {
// Register the main test thread for hang watching.
auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread);
HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
// Start a HangWatchScopeEnabled that expires right away. Then advance
// time to make sure a hang is detected.
......@@ -395,7 +395,7 @@ TEST_F(HangWatcherTest, ScopeDisabledObjectInnerScope) {
TEST_F(HangWatcherTest, NewScopeAfterDisabling) {
// Register the main test thread for hang watching.
auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread);
HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
// Start a HangWatchScopeEnabled that expires right away. Then advance
// time to make sure a hang is detected.
......@@ -646,7 +646,7 @@ TEST_F(HangWatcherSnapshotTest, NonActionableReport) {
// Register the main test thread for hang watching.
auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread);
HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
{
// Start a HangWatchScopeEnabled that expires right away. Ensures that
// the first monitor will detect a hang.
......@@ -694,7 +694,7 @@ TEST_F(HangWatcherSnapshotTest, DISABLED_HungThreadIDs) {
// Register the main test thread for hang watching.
auto unregister_thread_closure =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread);
HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
BlockingThread blocking_thread(&monitor_event_, base::TimeDelta{});
blocking_thread.StartAndWaitForScopeEntered();
......@@ -849,7 +849,7 @@ TEST_F(HangWatcherPeriodicMonitoringTest, PeriodicCallsTakePlace) {
// Register a thread,
unregister_thread_closure_ =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread);
HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
run_loop.Run();
......@@ -878,7 +878,7 @@ TEST_F(HangWatcherPeriodicMonitoringTest, NoMonitorOnOverSleep) {
// Register a thread.
unregister_thread_closure_ =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread);
HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
// Unblock the test thread. All waits were perceived as oversleeping so all
// monitoring was inhibited.
......@@ -922,7 +922,7 @@ class HangWatchScopeEnabledBlockingTest : public testing::Test {
// Register the test main thread for hang watching.
unregister_thread_closure_ =
hang_watcher_.RegisterThread(base::HangWatcher::ThreadType::kUIThread);
HangWatcher::RegisterThread(base::HangWatcher::ThreadType::kUIThread);
}
void TearDown() override { hang_watcher_.UnitializeOnMainThreadForTesting(); }
......
......@@ -488,9 +488,8 @@ BrowserMainLoop::BrowserMainLoop(
// works since the UI thread running the message loop is the main thread and
// that makes it the same as the current one.
if (base::HangWatcher::IsUIThreadHangWatchingEnabled()) {
unregister_thread_closure_ =
base::HangWatcher::GetInstance()->RegisterThread(
base::HangWatcher::ThreadType::kUIThread);
unregister_thread_closure_ = base::HangWatcher::RegisterThread(
base::HangWatcher::ThreadType::kUIThread);
}
if (GetContentClient()->browser()->ShouldCreateThreadPool()) {
......
......@@ -137,9 +137,8 @@ NOINLINE void BrowserProcessSubThread::IOThreadRun(base::RunLoop* run_loop) {
// up a closure to automatically unregister it when Run() returns.
base::ScopedClosureRunner unregister_thread_closure;
if (base::HangWatcher::IsIOThreadHangWatchingEnabled()) {
unregister_thread_closure =
base::HangWatcher::GetInstance()->RegisterThread(
base::HangWatcher::ThreadType::kIOThread);
unregister_thread_closure = base::HangWatcher::RegisterThread(
base::HangWatcher::ThreadType::kIOThread);
}
Thread::Run(run_loop);
......
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