Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_THREADING_THREAD_RESTRICTIONS_H_ |
michael@0 | 6 | #define BASE_THREADING_THREAD_RESTRICTIONS_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include "base/base_export.h" |
michael@0 | 9 | #include "base/basictypes.h" |
michael@0 | 10 | |
michael@0 | 11 | // See comment at top of thread_checker.h |
michael@0 | 12 | #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) |
michael@0 | 13 | #define ENABLE_THREAD_RESTRICTIONS 1 |
michael@0 | 14 | #else |
michael@0 | 15 | #define ENABLE_THREAD_RESTRICTIONS 0 |
michael@0 | 16 | #endif |
michael@0 | 17 | |
michael@0 | 18 | class AcceleratedPresenter; |
michael@0 | 19 | class BrowserProcessImpl; |
michael@0 | 20 | class HistogramSynchronizer; |
michael@0 | 21 | class MetricsService; |
michael@0 | 22 | class NativeBackendKWallet; |
michael@0 | 23 | class ScopedAllowWaitForLegacyWebViewApi; |
michael@0 | 24 | class TestingAutomationProvider; |
michael@0 | 25 | |
michael@0 | 26 | namespace browser_sync { |
michael@0 | 27 | class NonFrontendDataTypeController; |
michael@0 | 28 | class UIModelWorker; |
michael@0 | 29 | } |
michael@0 | 30 | namespace cc { |
michael@0 | 31 | class CompletionEvent; |
michael@0 | 32 | } |
michael@0 | 33 | namespace chromeos { |
michael@0 | 34 | class AudioMixerAlsa; |
michael@0 | 35 | class BlockingMethodCaller; |
michael@0 | 36 | namespace system { |
michael@0 | 37 | class StatisticsProviderImpl; |
michael@0 | 38 | } |
michael@0 | 39 | } |
michael@0 | 40 | namespace chrome_browser_net { |
michael@0 | 41 | class Predictor; |
michael@0 | 42 | } |
michael@0 | 43 | namespace content { |
michael@0 | 44 | class BrowserGpuChannelHostFactory; |
michael@0 | 45 | class BrowserShutdownProfileDumper; |
michael@0 | 46 | class BrowserTestBase; |
michael@0 | 47 | class GLHelper; |
michael@0 | 48 | class GpuChannelHost; |
michael@0 | 49 | class NestedMessagePumpAndroid; |
michael@0 | 50 | class RenderWidgetHelper; |
michael@0 | 51 | class ScopedAllowWaitForAndroidLayoutTests; |
michael@0 | 52 | class TextInputClientMac; |
michael@0 | 53 | } |
michael@0 | 54 | namespace dbus { |
michael@0 | 55 | class Bus; |
michael@0 | 56 | } |
michael@0 | 57 | namespace disk_cache { |
michael@0 | 58 | class BackendImpl; |
michael@0 | 59 | class InFlightIO; |
michael@0 | 60 | } |
michael@0 | 61 | namespace media { |
michael@0 | 62 | class AudioOutputController; |
michael@0 | 63 | } |
michael@0 | 64 | namespace net { |
michael@0 | 65 | class FileStreamPosix; |
michael@0 | 66 | class FileStreamWin; |
michael@0 | 67 | namespace internal { |
michael@0 | 68 | class AddressTrackerLinux; |
michael@0 | 69 | } |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | namespace remoting { |
michael@0 | 73 | class AutoThread; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | namespace base { |
michael@0 | 77 | |
michael@0 | 78 | namespace android { |
michael@0 | 79 | class JavaHandlerThread; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | class SequencedWorkerPool; |
michael@0 | 83 | class SimpleThread; |
michael@0 | 84 | class Thread; |
michael@0 | 85 | class ThreadTestHelper; |
michael@0 | 86 | |
michael@0 | 87 | // Certain behavior is disallowed on certain threads. ThreadRestrictions helps |
michael@0 | 88 | // enforce these rules. Examples of such rules: |
michael@0 | 89 | // |
michael@0 | 90 | // * Do not do blocking IO (makes the thread janky) |
michael@0 | 91 | // * Do not access Singleton/LazyInstance (may lead to shutdown crashes) |
michael@0 | 92 | // |
michael@0 | 93 | // Here's more about how the protection works: |
michael@0 | 94 | // |
michael@0 | 95 | // 1) If a thread should not be allowed to make IO calls, mark it: |
michael@0 | 96 | // base::ThreadRestrictions::SetIOAllowed(false); |
michael@0 | 97 | // By default, threads *are* allowed to make IO calls. |
michael@0 | 98 | // In Chrome browser code, IO calls should be proxied to the File thread. |
michael@0 | 99 | // |
michael@0 | 100 | // 2) If a function makes a call that will go out to disk, check whether the |
michael@0 | 101 | // current thread is allowed: |
michael@0 | 102 | // base::ThreadRestrictions::AssertIOAllowed(); |
michael@0 | 103 | // |
michael@0 | 104 | // |
michael@0 | 105 | // Style tip: where should you put AssertIOAllowed checks? It's best |
michael@0 | 106 | // if you put them as close to the disk access as possible, at the |
michael@0 | 107 | // lowest level. This rule is simple to follow and helps catch all |
michael@0 | 108 | // callers. For example, if your function GoDoSomeBlockingDiskCall() |
michael@0 | 109 | // only calls other functions in Chrome and not fopen(), you should go |
michael@0 | 110 | // add the AssertIOAllowed checks in the helper functions. |
michael@0 | 111 | |
michael@0 | 112 | class BASE_EXPORT ThreadRestrictions { |
michael@0 | 113 | public: |
michael@0 | 114 | // Constructing a ScopedAllowIO temporarily allows IO for the current |
michael@0 | 115 | // thread. Doing this is almost certainly always incorrect. |
michael@0 | 116 | class BASE_EXPORT ScopedAllowIO { |
michael@0 | 117 | public: |
michael@0 | 118 | ScopedAllowIO() { previous_value_ = SetIOAllowed(true); } |
michael@0 | 119 | ~ScopedAllowIO() { SetIOAllowed(previous_value_); } |
michael@0 | 120 | private: |
michael@0 | 121 | // Whether IO is allowed when the ScopedAllowIO was constructed. |
michael@0 | 122 | bool previous_value_; |
michael@0 | 123 | |
michael@0 | 124 | DISALLOW_COPY_AND_ASSIGN(ScopedAllowIO); |
michael@0 | 125 | }; |
michael@0 | 126 | |
michael@0 | 127 | // Constructing a ScopedAllowSingleton temporarily allows accessing for the |
michael@0 | 128 | // current thread. Doing this is almost always incorrect. |
michael@0 | 129 | class BASE_EXPORT ScopedAllowSingleton { |
michael@0 | 130 | public: |
michael@0 | 131 | ScopedAllowSingleton() { previous_value_ = SetSingletonAllowed(true); } |
michael@0 | 132 | ~ScopedAllowSingleton() { SetSingletonAllowed(previous_value_); } |
michael@0 | 133 | private: |
michael@0 | 134 | // Whether singleton use is allowed when the ScopedAllowSingleton was |
michael@0 | 135 | // constructed. |
michael@0 | 136 | bool previous_value_; |
michael@0 | 137 | |
michael@0 | 138 | DISALLOW_COPY_AND_ASSIGN(ScopedAllowSingleton); |
michael@0 | 139 | }; |
michael@0 | 140 | |
michael@0 | 141 | #if ENABLE_THREAD_RESTRICTIONS |
michael@0 | 142 | // Set whether the current thread to make IO calls. |
michael@0 | 143 | // Threads start out in the *allowed* state. |
michael@0 | 144 | // Returns the previous value. |
michael@0 | 145 | static bool SetIOAllowed(bool allowed); |
michael@0 | 146 | |
michael@0 | 147 | // Check whether the current thread is allowed to make IO calls, |
michael@0 | 148 | // and DCHECK if not. See the block comment above the class for |
michael@0 | 149 | // a discussion of where to add these checks. |
michael@0 | 150 | static void AssertIOAllowed(); |
michael@0 | 151 | |
michael@0 | 152 | // Set whether the current thread can use singletons. Returns the previous |
michael@0 | 153 | // value. |
michael@0 | 154 | static bool SetSingletonAllowed(bool allowed); |
michael@0 | 155 | |
michael@0 | 156 | // Check whether the current thread is allowed to use singletons (Singleton / |
michael@0 | 157 | // LazyInstance). DCHECKs if not. |
michael@0 | 158 | static void AssertSingletonAllowed(); |
michael@0 | 159 | |
michael@0 | 160 | // Disable waiting on the current thread. Threads start out in the *allowed* |
michael@0 | 161 | // state. Returns the previous value. |
michael@0 | 162 | static void DisallowWaiting(); |
michael@0 | 163 | |
michael@0 | 164 | // Check whether the current thread is allowed to wait, and DCHECK if not. |
michael@0 | 165 | static void AssertWaitAllowed(); |
michael@0 | 166 | #else |
michael@0 | 167 | // Inline the empty definitions of these functions so that they can be |
michael@0 | 168 | // compiled out. |
michael@0 | 169 | static bool SetIOAllowed(bool allowed) { return true; } |
michael@0 | 170 | static void AssertIOAllowed() {} |
michael@0 | 171 | static bool SetSingletonAllowed(bool allowed) { return true; } |
michael@0 | 172 | static void AssertSingletonAllowed() {} |
michael@0 | 173 | static void DisallowWaiting() {} |
michael@0 | 174 | static void AssertWaitAllowed() {} |
michael@0 | 175 | #endif |
michael@0 | 176 | |
michael@0 | 177 | private: |
michael@0 | 178 | // DO NOT ADD ANY OTHER FRIEND STATEMENTS, talk to jam or brettw first. |
michael@0 | 179 | // BEGIN ALLOWED USAGE. |
michael@0 | 180 | friend class content::BrowserShutdownProfileDumper; |
michael@0 | 181 | friend class content::BrowserTestBase; |
michael@0 | 182 | friend class content::NestedMessagePumpAndroid; |
michael@0 | 183 | friend class content::RenderWidgetHelper; |
michael@0 | 184 | friend class content::ScopedAllowWaitForAndroidLayoutTests; |
michael@0 | 185 | friend class ::HistogramSynchronizer; |
michael@0 | 186 | friend class ::ScopedAllowWaitForLegacyWebViewApi; |
michael@0 | 187 | friend class ::TestingAutomationProvider; |
michael@0 | 188 | friend class cc::CompletionEvent; |
michael@0 | 189 | friend class remoting::AutoThread; |
michael@0 | 190 | friend class MessagePumpDefault; |
michael@0 | 191 | friend class SequencedWorkerPool; |
michael@0 | 192 | friend class SimpleThread; |
michael@0 | 193 | friend class Thread; |
michael@0 | 194 | friend class ThreadTestHelper; |
michael@0 | 195 | friend class PlatformThread; |
michael@0 | 196 | friend class android::JavaHandlerThread; |
michael@0 | 197 | |
michael@0 | 198 | // END ALLOWED USAGE. |
michael@0 | 199 | // BEGIN USAGE THAT NEEDS TO BE FIXED. |
michael@0 | 200 | friend class ::chromeos::AudioMixerAlsa; // http://crbug.com/125206 |
michael@0 | 201 | friend class ::chromeos::BlockingMethodCaller; // http://crbug.com/125360 |
michael@0 | 202 | friend class ::chromeos::system::StatisticsProviderImpl; // http://crbug.com/125385 |
michael@0 | 203 | friend class browser_sync::NonFrontendDataTypeController; // http://crbug.com/19757 |
michael@0 | 204 | friend class browser_sync::UIModelWorker; // http://crbug.com/19757 |
michael@0 | 205 | friend class chrome_browser_net::Predictor; // http://crbug.com/78451 |
michael@0 | 206 | friend class |
michael@0 | 207 | content::BrowserGpuChannelHostFactory; // http://crbug.com/125248 |
michael@0 | 208 | friend class content::GLHelper; // http://crbug.com/125415 |
michael@0 | 209 | friend class content::GpuChannelHost; // http://crbug.com/125264 |
michael@0 | 210 | friend class content::TextInputClientMac; // http://crbug.com/121917 |
michael@0 | 211 | friend class dbus::Bus; // http://crbug.com/125222 |
michael@0 | 212 | friend class disk_cache::BackendImpl; // http://crbug.com/74623 |
michael@0 | 213 | friend class disk_cache::InFlightIO; // http://crbug.com/74623 |
michael@0 | 214 | friend class media::AudioOutputController; // http://crbug.com/120973 |
michael@0 | 215 | friend class net::FileStreamPosix; // http://crbug.com/115067 |
michael@0 | 216 | friend class net::FileStreamWin; // http://crbug.com/115067 |
michael@0 | 217 | friend class net::internal::AddressTrackerLinux; // http://crbug.com/125097 |
michael@0 | 218 | friend class ::AcceleratedPresenter; // http://crbug.com/125391 |
michael@0 | 219 | friend class ::BrowserProcessImpl; // http://crbug.com/125207 |
michael@0 | 220 | friend class ::MetricsService; // http://crbug.com/124954 |
michael@0 | 221 | friend class ::NativeBackendKWallet; // http://crbug.com/125331 |
michael@0 | 222 | // END USAGE THAT NEEDS TO BE FIXED. |
michael@0 | 223 | |
michael@0 | 224 | #if ENABLE_THREAD_RESTRICTIONS |
michael@0 | 225 | static bool SetWaitAllowed(bool allowed); |
michael@0 | 226 | #else |
michael@0 | 227 | static bool SetWaitAllowed(bool allowed) { return true; } |
michael@0 | 228 | #endif |
michael@0 | 229 | |
michael@0 | 230 | // Constructing a ScopedAllowWait temporarily allows waiting on the current |
michael@0 | 231 | // thread. Doing this is almost always incorrect, which is why we limit who |
michael@0 | 232 | // can use this through friend. If you find yourself needing to use this, find |
michael@0 | 233 | // another way. Talk to jam or brettw. |
michael@0 | 234 | class BASE_EXPORT ScopedAllowWait { |
michael@0 | 235 | public: |
michael@0 | 236 | ScopedAllowWait() { previous_value_ = SetWaitAllowed(true); } |
michael@0 | 237 | ~ScopedAllowWait() { SetWaitAllowed(previous_value_); } |
michael@0 | 238 | private: |
michael@0 | 239 | // Whether singleton use is allowed when the ScopedAllowWait was |
michael@0 | 240 | // constructed. |
michael@0 | 241 | bool previous_value_; |
michael@0 | 242 | |
michael@0 | 243 | DISALLOW_COPY_AND_ASSIGN(ScopedAllowWait); |
michael@0 | 244 | }; |
michael@0 | 245 | |
michael@0 | 246 | DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions); |
michael@0 | 247 | }; |
michael@0 | 248 | |
michael@0 | 249 | } // namespace base |
michael@0 | 250 | |
michael@0 | 251 | #endif // BASE_THREADING_THREAD_RESTRICTIONS_H_ |