Index: base/threading/thread_restrictions.h |
=================================================================== |
--- base/threading/thread_restrictions.h (revision 133458) |
+++ base/threading/thread_restrictions.h (working copy) |
@@ -8,8 +8,21 @@ |
#include "base/base_export.h" |
#include "base/basictypes.h" |
+class MetricsService; |
+class RenderWidgetHelper; |
+class TextInputClientMac; |
+namespace chrome_browser_net { |
+class Predictor; |
+} |
+namespace disk_cache { |
+class BackendImpl; |
+class InFlightIO; |
+} |
+ |
namespace base { |
+class Thread; |
+ |
// Certain behavior is disallowed on certain threads. ThreadRestrictions helps |
// enforce these rules. Examples of such rules: |
// |
@@ -83,6 +96,13 @@ |
// Check whether the current thread is allowed to use singletons (Singleton / |
// LazyInstance). DCHECKs if not. |
static void AssertSingletonAllowed(); |
+ |
+ // Disable waiting on the current thread. Threads start out in the *allowed* |
+ // state. Returns the previous value. |
+ static void DisallowWaiting(); |
+ |
+ // Check whether the current thread is allowed to wait, and DCHECK if not. |
+ static void AssertWaitAllowed(); |
#else |
// In Release builds, inline the empty definitions of these functions so |
// that they can be compiled out. |
@@ -90,9 +110,42 @@ |
static void AssertIOAllowed() {} |
static bool SetSingletonAllowed(bool allowed) { return true; } |
static void AssertSingletonAllowed() {} |
+ static void DisallowWaiting() {} |
+ static void AssertWaitAllowed() {} |
#endif |
private: |
+ // DO NOT ADD ANY OTHER FRIEND STATEMENTS. |
+ friend class ::RenderWidgetHelper; // This is allowed, see documentation. |
+ friend class Thread; // This is allowed. |
+ friend class chrome_browser_net::Predictor; // http://crbug.com/78451 |
+ friend class disk_cache::BackendImpl; // http://crbug.com/74623 |
+ friend class disk_cache::InFlightIO; // http://crbug.com/74623 |
+ friend class ::TextInputClientMac; // http://crbug.com/121917 |
+ friend class ::MetricsService; // http://crbug.com/124954 |
+ |
+#ifndef NDEBUG |
+ static bool SetWaitAllowed(bool allowed); |
+#else |
+ static bool SetWaitAllowed(bool allowed) { return true; } |
+#endif |
+ |
+ // Constructing a ScopedAllowWait temporarily allows waiting on the current |
+ // thread. Doing this is almost always incorrect, which is why we limit who |
+ // can use this through friend. If you find yourself needing to use this, find |
+ // another way. Talk to jam or brettw. |
+ class BASE_EXPORT ScopedAllowWait { |
+ public: |
+ ScopedAllowWait() { previous_value_ = SetWaitAllowed(true); } |
+ ~ScopedAllowWait() { SetWaitAllowed(previous_value_); } |
+ private: |
+ // Whether singleton use is allowed when the ScopedAllowWait was |
+ // constructed. |
+ bool previous_value_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ScopedAllowWait); |
+ }; |
+ |
DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions); |
}; |