[go: nahoru, domu]

Split base::PlatformThreadRef into its own file.

Since base/synchronization/lock.h only uses PlatformThreadRef, and not
PlatformThread, split part of base/threading/platform_thread.h off into
platform_thread_ref.h. Then lock.h, which is in 60% of TUs, will
transitively include fewer headers.

Change-Id: I427d1a59f58b0f3d269b4932cc36ce4e2babe954
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3342919
Reviewed-by: Kyle Charbonneau <kylechar@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Owners-Override: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#953983}
diff --git a/base/BUILD.gn b/base/BUILD.gn
index 5ec2172..aa140d1 100644
--- a/base/BUILD.gn
+++ b/base/BUILD.gn
@@ -820,6 +820,8 @@
     "threading/hang_watcher.h",
     "threading/platform_thread.cc",
     "threading/platform_thread.h",
+    "threading/platform_thread_ref.cc",
+    "threading/platform_thread_ref.h",
     "threading/post_task_and_reply_impl.cc",
     "threading/post_task_and_reply_impl.h",
     "threading/scoped_blocking_call.cc",
diff --git a/base/memory/weak_ptr.cc b/base/memory/weak_ptr.cc
index 9d80f0e..58641a1 100644
--- a/base/memory/weak_ptr.cc
+++ b/base/memory/weak_ptr.cc
@@ -5,6 +5,8 @@
 #include "base/memory/weak_ptr.h"
 
 #if DCHECK_IS_ON()
+#include <ostream>
+
 #include "base/debug/stack_trace.h"
 #endif
 
diff --git a/base/observer_list.h b/base/observer_list.h
index 0c8a2a00..f5c8f05 100644
--- a/base/observer_list.h
+++ b/base/observer_list.h
@@ -10,6 +10,7 @@
 #include <algorithm>
 #include <iterator>
 #include <limits>
+#include <ostream>
 #include <string>
 #include <utility>
 #include <vector>
diff --git a/base/synchronization/lock.cc b/base/synchronization/lock.cc
index 05ceb760..ca2f3f1 100644
--- a/base/synchronization/lock.cc
+++ b/base/synchronization/lock.cc
@@ -9,6 +9,10 @@
 #include "base/synchronization/lock.h"
 
 #if DCHECK_IS_ON()
+#include "base/threading/platform_thread.h"
+#endif
+
+#if DCHECK_IS_ON()
 
 namespace base {
 
diff --git a/base/synchronization/lock.h b/base/synchronization/lock.h
index 1b7aaf6..1f011672 100644
--- a/base/synchronization/lock.h
+++ b/base/synchronization/lock.h
@@ -13,7 +13,7 @@
 #include "build/build_config.h"
 
 #if DCHECK_IS_ON()
-#include "base/threading/platform_thread.h"
+#include "base/threading/platform_thread_ref.h"
 #endif
 
 namespace base {
diff --git a/base/threading/platform_thread.cc b/base/threading/platform_thread.cc
index 23eb14f..b603803 100644
--- a/base/threading/platform_thread.cc
+++ b/base/threading/platform_thread.cc
@@ -5,8 +5,6 @@
 #include "base/threading/platform_thread.h"
 
 #include <atomic>
-#include <memory>
-#include <ostream>
 
 #include "base/feature_list.h"
 #include "base/time/time.h"
@@ -29,11 +27,6 @@
 
 }  // namespace
 
-std::ostream& operator<<(std::ostream& os, const PlatformThreadRef& ref) {
-  os << ref.id_;
-  return os;
-}
-
 // static
 void PlatformThread::SetCurrentThreadPriority(ThreadPriority priority) {
   if (g_use_thread_priorities.load())
diff --git a/base/threading/platform_thread.h b/base/threading/platform_thread.h
index dfb6366..0acc423 100644
--- a/base/threading/platform_thread.h
+++ b/base/threading/platform_thread.h
@@ -14,6 +14,7 @@
 #include <iosfwd>
 
 #include "base/base_export.h"
+#include "base/threading/platform_thread_ref.h"
 #include "base/time/time.h"
 #include "build/build_config.h"
 #include "build/chromeos_buildflags.h"
@@ -42,45 +43,6 @@
 typedef pid_t PlatformThreadId;
 #endif
 
-// Used for thread checking and debugging.
-// Meant to be as fast as possible.
-// These are produced by PlatformThread::CurrentRef(), and used to later
-// check if we are on the same thread or not by using ==. These are safe
-// to copy between threads, but can't be copied to another process as they
-// have no meaning there. Also, the internal identifier can be re-used
-// after a thread dies, so a PlatformThreadRef cannot be reliably used
-// to distinguish a new thread from an old, dead thread.
-class PlatformThreadRef {
- public:
-#if defined(OS_WIN)
-  typedef DWORD RefType;
-#else  //  OS_POSIX
-  typedef pthread_t RefType;
-#endif
-  constexpr PlatformThreadRef() = default;
-
-  explicit constexpr PlatformThreadRef(RefType id) : id_(id) {}
-
-  bool operator==(PlatformThreadRef other) const {
-    return id_ == other.id_;
-  }
-
-  bool operator!=(PlatformThreadRef other) const { return id_ != other.id_; }
-
-  bool is_null() const {
-    return id_ == 0;
-  }
-
- private:
-  friend BASE_EXPORT std::ostream& operator<<(std::ostream& os,
-                                              const PlatformThreadRef& ref);
-
-  RefType id_ = 0;
-};
-
-BASE_EXPORT std::ostream& operator<<(std::ostream& os,
-                                     const PlatformThreadRef& ref);
-
 // Used to operate on threads.
 class PlatformThreadHandle {
  public:
diff --git a/base/threading/platform_thread_ref.cc b/base/threading/platform_thread_ref.cc
new file mode 100644
index 0000000..0b26efd3
--- /dev/null
+++ b/base/threading/platform_thread_ref.cc
@@ -0,0 +1,16 @@
+// Copyright 2021 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/threading/platform_thread_ref.h"
+
+#include <ostream>
+
+namespace base {
+
+std::ostream& operator<<(std::ostream& os, const PlatformThreadRef& ref) {
+  os << ref.id_;
+  return os;
+}
+
+}  // namespace base
diff --git a/base/threading/platform_thread_ref.h b/base/threading/platform_thread_ref.h
new file mode 100644
index 0000000..eb0b33a
--- /dev/null
+++ b/base/threading/platform_thread_ref.h
@@ -0,0 +1,62 @@
+// Copyright 2021 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// WARNING: *DO NOT* use this class directly. base::PlatformThreadRef is a
+// low-level platform-specific abstraction to the OS's threading interface.
+// Instead, consider using a message-loop driven base::Thread, see
+// base/threading/thread.h.
+
+#ifndef BASE_THREADING_PLATFORM_THREAD_REF_H_
+#define BASE_THREADING_PLATFORM_THREAD_REF_H_
+
+#include <iosfwd>
+
+#include "base/base_export.h"
+#include "build/build_config.h"
+
+#if defined(OS_WIN)
+#include "base/win/windows_types.h"
+#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
+#include <pthread.h>
+#endif
+
+namespace base {
+
+// Used for thread checking and debugging.
+// Meant to be as fast as possible.
+// These are produced by PlatformThread::CurrentRef(), and used to later
+// check if we are on the same thread or not by using ==. These are safe
+// to copy between threads, but can't be copied to another process as they
+// have no meaning there. Also, the internal identifier can be re-used
+// after a thread dies, so a PlatformThreadRef cannot be reliably used
+// to distinguish a new thread from an old, dead thread.
+class PlatformThreadRef {
+ public:
+#if defined(OS_WIN)
+  using RefType = DWORD;
+#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
+  using RefType = pthread_t;
+#endif
+
+  constexpr PlatformThreadRef() = default;
+  explicit constexpr PlatformThreadRef(RefType id) : id_(id) {}
+
+  bool operator==(PlatformThreadRef other) const { return id_ == other.id_; }
+  bool operator!=(PlatformThreadRef other) const { return id_ != other.id_; }
+
+  bool is_null() const { return id_ == 0; }
+
+ private:
+  friend BASE_EXPORT std::ostream& operator<<(std::ostream& os,
+                                              const PlatformThreadRef& ref);
+
+  RefType id_ = 0;
+};
+
+BASE_EXPORT std::ostream& operator<<(std::ostream& os,
+                                     const PlatformThreadRef& ref);
+
+}  // namespace base
+
+#endif  // BASE_THREADING_PLATFORM_THREAD_REF_H_
diff --git a/base/win/com_init_check_hook.cc b/base/win/com_init_check_hook.cc
index 86d11aed..95d91ba 100644
--- a/base/win/com_init_check_hook.cc
+++ b/base/win/com_init_check_hook.cc
@@ -10,6 +10,8 @@
 #include <stdint.h>
 #include <string.h>
 
+#include <ostream>
+
 #include "base/notreached.h"
 #include "base/strings/stringprintf.h"
 #include "base/synchronization/lock.h"
diff --git a/components/keyed_service/core/dependency_manager.cc b/components/keyed_service/core/dependency_manager.cc
index 9f603c6e..006914ba 100644
--- a/components/keyed_service/core/dependency_manager.cc
+++ b/components/keyed_service/core/dependency_manager.cc
@@ -4,6 +4,8 @@
 
 #include "components/keyed_service/core/dependency_manager.h"
 
+#include <ostream>
+
 #include "base/bind.h"
 #include "base/check.h"
 #include "base/debug/dump_without_crashing.h"
diff --git a/ui/gfx/font.cc b/ui/gfx/font.cc
index 50a071a..1ad5f7fe 100644
--- a/ui/gfx/font.cc
+++ b/ui/gfx/font.cc
@@ -10,6 +10,10 @@
 #include "build/build_config.h"
 #include "ui/gfx/platform_font.h"
 
+#ifndef NDEBUG
+#include <ostream>
+#endif
+
 namespace gfx {
 
 ////////////////////////////////////////////////////////////////////////////////