OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_THREADING_THREAD_RESTRICTIONS_H_ | 5 #ifndef BASE_THREADING_THREAD_RESTRICTIONS_H_ |
6 #define BASE_THREADING_THREAD_RESTRICTIONS_H_ | 6 #define BASE_THREADING_THREAD_RESTRICTIONS_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 | 10 |
| 11 class MetricsService; |
| 12 class RenderWidgetHelper; |
| 13 class TestingAutomationProvider; |
| 14 class TextInputClientMac; |
| 15 namespace chrome_browser_net { |
| 16 class Predictor; |
| 17 } |
| 18 namespace disk_cache { |
| 19 class BackendImpl; |
| 20 class InFlightIO; |
| 21 } |
| 22 namespace media { |
| 23 class AudioOutputController; |
| 24 } |
| 25 namespace net { |
| 26 class FileStreamPosix; |
| 27 class FileStreamWin; |
| 28 class NetworkManagerApi; |
| 29 } |
| 30 |
11 namespace base { | 31 namespace base { |
12 | 32 |
| 33 class SequencedWorkerPool; |
| 34 class SimpleThread; |
| 35 class Thread; |
| 36 class ThreadTestHelper; |
| 37 |
13 // Certain behavior is disallowed on certain threads. ThreadRestrictions helps | 38 // Certain behavior is disallowed on certain threads. ThreadRestrictions helps |
14 // enforce these rules. Examples of such rules: | 39 // enforce these rules. Examples of such rules: |
15 // | 40 // |
16 // * Do not do blocking IO (makes the thread janky) | 41 // * Do not do blocking IO (makes the thread janky) |
17 // * Do not access Singleton/LazyInstance (may lead to shutdown crashes) | 42 // * Do not access Singleton/LazyInstance (may lead to shutdown crashes) |
18 // | 43 // |
19 // Here's more about how the protection works: | 44 // Here's more about how the protection works: |
20 // | 45 // |
21 // 1) If a thread should not be allowed to make IO calls, mark it: | 46 // 1) If a thread should not be allowed to make IO calls, mark it: |
22 // base::ThreadRestrictions::SetIOAllowed(false); | 47 // base::ThreadRestrictions::SetIOAllowed(false); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 // a discussion of where to add these checks. | 101 // a discussion of where to add these checks. |
77 static void AssertIOAllowed(); | 102 static void AssertIOAllowed(); |
78 | 103 |
79 // Set whether the current thread can use singletons. Returns the previous | 104 // Set whether the current thread can use singletons. Returns the previous |
80 // value. | 105 // value. |
81 static bool SetSingletonAllowed(bool allowed); | 106 static bool SetSingletonAllowed(bool allowed); |
82 | 107 |
83 // Check whether the current thread is allowed to use singletons (Singleton / | 108 // Check whether the current thread is allowed to use singletons (Singleton / |
84 // LazyInstance). DCHECKs if not. | 109 // LazyInstance). DCHECKs if not. |
85 static void AssertSingletonAllowed(); | 110 static void AssertSingletonAllowed(); |
| 111 |
| 112 // Disable waiting on the current thread. Threads start out in the *allowed* |
| 113 // state. Returns the previous value. |
| 114 static void DisallowWaiting(); |
| 115 |
| 116 // Check whether the current thread is allowed to wait, and DCHECK if not. |
| 117 static void AssertWaitAllowed(); |
86 #else | 118 #else |
87 // In Release builds, inline the empty definitions of these functions so | 119 // In Release builds, inline the empty definitions of these functions so |
88 // that they can be compiled out. | 120 // that they can be compiled out. |
89 static bool SetIOAllowed(bool allowed) { return true; } | 121 static bool SetIOAllowed(bool allowed) { return true; } |
90 static void AssertIOAllowed() {} | 122 static void AssertIOAllowed() {} |
91 static bool SetSingletonAllowed(bool allowed) { return true; } | 123 static bool SetSingletonAllowed(bool allowed) { return true; } |
92 static void AssertSingletonAllowed() {} | 124 static void AssertSingletonAllowed() {} |
| 125 static void DisallowWaiting() {} |
| 126 static void AssertWaitAllowed() {} |
93 #endif | 127 #endif |
94 | 128 |
95 private: | 129 private: |
| 130 // DO NOT ADD ANY OTHER FRIEND STATEMENTS, talk to jam or brettw first. |
| 131 // BEGIN ALLOWED USAGE. |
| 132 friend class ::RenderWidgetHelper; |
| 133 friend class ::TestingAutomationProvider; |
| 134 friend class SequencedWorkerPool; |
| 135 friend class SimpleThread; |
| 136 friend class Thread; |
| 137 friend class ThreadTestHelper; |
| 138 // END ALLOWED USAGE. |
| 139 // BEGIN USAGE THAT NEEDS TO BE FIXED. |
| 140 friend class chrome_browser_net::Predictor; // http://crbug.com/78451 |
| 141 friend class disk_cache::BackendImpl; // http://crbug.com/74623 |
| 142 friend class disk_cache::InFlightIO; // http://crbug.com/74623 |
| 143 friend class media::AudioOutputController; // http://crbug.com/120973 |
| 144 friend class net::FileStreamPosix; // http://crbug.com/74623 |
| 145 friend class net::FileStreamWin; // http://crbug.com/74623 |
| 146 friend class net::NetworkManagerApi; // http://crbug.com/125097 |
| 147 friend class ::TextInputClientMac; // http://crbug.com/121917 |
| 148 friend class ::MetricsService; // http://crbug.com/124954 |
| 149 // END USAGE THAT NEEDS TO BE FIXED. |
| 150 |
| 151 #ifndef NDEBUG |
| 152 static bool SetWaitAllowed(bool allowed); |
| 153 #else |
| 154 static bool SetWaitAllowed(bool allowed) { return true; } |
| 155 #endif |
| 156 |
| 157 // Constructing a ScopedAllowWait temporarily allows waiting on the current |
| 158 // thread. Doing this is almost always incorrect, which is why we limit who |
| 159 // can use this through friend. If you find yourself needing to use this, find |
| 160 // another way. Talk to jam or brettw. |
| 161 class BASE_EXPORT ScopedAllowWait { |
| 162 public: |
| 163 ScopedAllowWait() { previous_value_ = SetWaitAllowed(true); } |
| 164 ~ScopedAllowWait() { SetWaitAllowed(previous_value_); } |
| 165 private: |
| 166 // Whether singleton use is allowed when the ScopedAllowWait was |
| 167 // constructed. |
| 168 bool previous_value_; |
| 169 |
| 170 DISALLOW_COPY_AND_ASSIGN(ScopedAllowWait); |
| 171 }; |
| 172 |
96 DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions); | 173 DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions); |
97 }; | 174 }; |
98 | 175 |
99 } // namespace base | 176 } // namespace base |
100 | 177 |
101 #endif // BASE_THREADING_THREAD_RESTRICTIONS_H_ | 178 #endif // BASE_THREADING_THREAD_RESTRICTIONS_H_ |
OLD | NEW |