1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_THREADING_THREAD_RESTRICTIONS_H_
6 #define BASE_THREADING_THREAD_RESTRICTIONS_H_
8 #include "base/base_export.h"
9 #include "base/basictypes.h"
11 // See comment at top of thread_checker.h
12 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
13 #define ENABLE_THREAD_RESTRICTIONS 1
15 #define ENABLE_THREAD_RESTRICTIONS 0
18 class AcceleratedPresenter
;
19 class BrowserProcessImpl
;
20 class HistogramSynchronizer
;
21 class NativeBackendKWallet
;
22 class ScopedAllowWaitForLegacyWebViewApi
;
23 class TestingAutomationProvider
;
25 namespace browser_sync
{
26 class NonFrontendDataTypeController
;
30 class CompletionEvent
;
34 class BlockingMethodCaller
;
36 class StatisticsProviderImpl
;
39 namespace chrome_browser_net
{
43 class BrowserGpuChannelHostFactory
;
44 class BrowserShutdownProfileDumper
;
45 class BrowserTestBase
;
48 class NestedMessagePumpAndroid
;
49 class RenderWidgetResizeHelper
;
50 class ScopedAllowWaitForAndroidLayoutTests
;
51 class TextInputClientMac
;
56 namespace disk_cache
{
61 class AudioOutputController
;
68 class WatcherThreadManager
;
72 class FileStreamPosix
;
75 class AddressTrackerLinux
;
86 class JavaHandlerThread
;
89 class SequencedWorkerPool
;
92 class ThreadTestHelper
;
94 // Certain behavior is disallowed on certain threads. ThreadRestrictions helps
95 // enforce these rules. Examples of such rules:
97 // * Do not do blocking IO (makes the thread janky)
98 // * Do not access Singleton/LazyInstance (may lead to shutdown crashes)
100 // Here's more about how the protection works:
102 // 1) If a thread should not be allowed to make IO calls, mark it:
103 // base::ThreadRestrictions::SetIOAllowed(false);
104 // By default, threads *are* allowed to make IO calls.
105 // In Chrome browser code, IO calls should be proxied to the File thread.
107 // 2) If a function makes a call that will go out to disk, check whether the
108 // current thread is allowed:
109 // base::ThreadRestrictions::AssertIOAllowed();
112 // Style tip: where should you put AssertIOAllowed checks? It's best
113 // if you put them as close to the disk access as possible, at the
114 // lowest level. This rule is simple to follow and helps catch all
115 // callers. For example, if your function GoDoSomeBlockingDiskCall()
116 // only calls other functions in Chrome and not fopen(), you should go
117 // add the AssertIOAllowed checks in the helper functions.
119 class BASE_EXPORT ThreadRestrictions
{
121 // Constructing a ScopedAllowIO temporarily allows IO for the current
122 // thread. Doing this is almost certainly always incorrect.
123 class BASE_EXPORT ScopedAllowIO
{
125 ScopedAllowIO() { previous_value_
= SetIOAllowed(true); }
126 ~ScopedAllowIO() { SetIOAllowed(previous_value_
); }
128 // Whether IO is allowed when the ScopedAllowIO was constructed.
129 bool previous_value_
;
131 DISALLOW_COPY_AND_ASSIGN(ScopedAllowIO
);
134 // Constructing a ScopedAllowSingleton temporarily allows accessing for the
135 // current thread. Doing this is almost always incorrect.
136 class BASE_EXPORT ScopedAllowSingleton
{
138 ScopedAllowSingleton() { previous_value_
= SetSingletonAllowed(true); }
139 ~ScopedAllowSingleton() { SetSingletonAllowed(previous_value_
); }
141 // Whether singleton use is allowed when the ScopedAllowSingleton was
143 bool previous_value_
;
145 DISALLOW_COPY_AND_ASSIGN(ScopedAllowSingleton
);
148 #if ENABLE_THREAD_RESTRICTIONS
149 // Set whether the current thread to make IO calls.
150 // Threads start out in the *allowed* state.
151 // Returns the previous value.
152 static bool SetIOAllowed(bool allowed
);
154 // Check whether the current thread is allowed to make IO calls,
155 // and DCHECK if not. See the block comment above the class for
156 // a discussion of where to add these checks.
157 static void AssertIOAllowed();
159 // Set whether the current thread can use singletons. Returns the previous
161 static bool SetSingletonAllowed(bool allowed
);
163 // Check whether the current thread is allowed to use singletons (Singleton /
164 // LazyInstance). DCHECKs if not.
165 static void AssertSingletonAllowed();
167 // Disable waiting on the current thread. Threads start out in the *allowed*
168 // state. Returns the previous value.
169 static void DisallowWaiting();
171 // Check whether the current thread is allowed to wait, and DCHECK if not.
172 static void AssertWaitAllowed();
174 // Inline the empty definitions of these functions so that they can be
176 static bool SetIOAllowed(bool allowed
) { return true; }
177 static void AssertIOAllowed() {}
178 static bool SetSingletonAllowed(bool allowed
) { return true; }
179 static void AssertSingletonAllowed() {}
180 static void DisallowWaiting() {}
181 static void AssertWaitAllowed() {}
185 // DO NOT ADD ANY OTHER FRIEND STATEMENTS, talk to jam or brettw first.
186 // BEGIN ALLOWED USAGE.
187 friend class content::BrowserShutdownProfileDumper
;
188 friend class content::BrowserTestBase
;
189 friend class content::NestedMessagePumpAndroid
;
190 friend class content::RenderWidgetResizeHelper
;
191 friend class content::ScopedAllowWaitForAndroidLayoutTests
;
192 friend class ::HistogramSynchronizer
;
193 friend class ::ScopedAllowWaitForLegacyWebViewApi
;
194 friend class ::TestingAutomationProvider
;
195 friend class cc::CompletionEvent
;
196 friend class mojo::common::WatcherThreadManager
;
197 friend class remoting::AutoThread
;
198 friend class MessagePumpDefault
;
199 friend class SequencedWorkerPool
;
200 friend class SimpleThread
;
202 friend class ThreadTestHelper
;
203 friend class PlatformThread
;
204 friend class android::JavaHandlerThread
;
206 // END ALLOWED USAGE.
207 // BEGIN USAGE THAT NEEDS TO BE FIXED.
208 friend class ::chromeos::AudioMixerAlsa
; // http://crbug.com/125206
209 friend class ::chromeos::BlockingMethodCaller
; // http://crbug.com/125360
210 friend class ::chromeos::system::StatisticsProviderImpl
; // http://crbug.com/125385
211 friend class browser_sync::NonFrontendDataTypeController
; // http://crbug.com/19757
212 friend class browser_sync::UIModelWorker
; // http://crbug.com/19757
213 friend class chrome_browser_net::Predictor
; // http://crbug.com/78451
215 content::BrowserGpuChannelHostFactory
; // http://crbug.com/125248
216 friend class content::GLHelper
; // http://crbug.com/125415
217 friend class content::GpuChannelHost
; // http://crbug.com/125264
218 friend class content::TextInputClientMac
; // http://crbug.com/121917
219 friend class dbus::Bus
; // http://crbug.com/125222
220 friend class disk_cache::BackendImpl
; // http://crbug.com/74623
221 friend class disk_cache::InFlightIO
; // http://crbug.com/74623
222 friend class media::AudioOutputController
; // http://crbug.com/120973
223 friend class net::FileStreamPosix
; // http://crbug.com/115067
224 friend class net::FileStreamWin
; // http://crbug.com/115067
225 friend class net::internal::AddressTrackerLinux
; // http://crbug.com/125097
226 friend class ::AcceleratedPresenter
; // http://crbug.com/125391
227 friend class ::BrowserProcessImpl
; // http://crbug.com/125207
228 friend class metrics::MetricsService
; // http://crbug.com/124954
229 friend class ::NativeBackendKWallet
; // http://crbug.com/125331
230 // END USAGE THAT NEEDS TO BE FIXED.
232 #if ENABLE_THREAD_RESTRICTIONS
233 static bool SetWaitAllowed(bool allowed
);
235 static bool SetWaitAllowed(bool allowed
) { return true; }
238 // Constructing a ScopedAllowWait temporarily allows waiting on the current
239 // thread. Doing this is almost always incorrect, which is why we limit who
240 // can use this through friend. If you find yourself needing to use this, find
241 // another way. Talk to jam or brettw.
242 class BASE_EXPORT ScopedAllowWait
{
244 ScopedAllowWait() { previous_value_
= SetWaitAllowed(true); }
245 ~ScopedAllowWait() { SetWaitAllowed(previous_value_
); }
247 // Whether singleton use is allowed when the ScopedAllowWait was
249 bool previous_value_
;
251 DISALLOW_COPY_AND_ASSIGN(ScopedAllowWait
);
254 DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions
);
259 #endif // BASE_THREADING_THREAD_RESTRICTIONS_H_