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 BrowserProcessImpl
;
19 class HistogramSynchronizer
;
20 class NativeBackendKWallet
;
21 class ScopedAllowWaitForLegacyWebViewApi
;
24 class CompletionEvent
;
25 class TaskGraphRunner
;
28 class BlockingMethodCaller
;
30 class StatisticsProviderImpl
;
33 namespace chrome_browser_net
{
37 class BrowserGpuChannelHostFactory
;
38 class BrowserGpuMemoryBufferManager
;
39 class BrowserShutdownProfileDumper
;
40 class BrowserTestBase
;
42 class NestedMessagePumpAndroid
;
43 class RenderWidgetResizeHelper
;
44 class ScopedAllowWaitForAndroidLayoutTests
;
45 class TextInputClientMac
;
46 } // namespace content
50 namespace disk_cache
{
56 class WatcherThreadManager
;
61 class AddressTrackerLinux
;
72 class JavaHandlerThread
;
75 class SequencedWorkerPool
;
78 class ThreadTestHelper
;
80 // Certain behavior is disallowed on certain threads. ThreadRestrictions helps
81 // enforce these rules. Examples of such rules:
83 // * Do not do blocking IO (makes the thread janky)
84 // * Do not access Singleton/LazyInstance (may lead to shutdown crashes)
86 // Here's more about how the protection works:
88 // 1) If a thread should not be allowed to make IO calls, mark it:
89 // base::ThreadRestrictions::SetIOAllowed(false);
90 // By default, threads *are* allowed to make IO calls.
91 // In Chrome browser code, IO calls should be proxied to the File thread.
93 // 2) If a function makes a call that will go out to disk, check whether the
94 // current thread is allowed:
95 // base::ThreadRestrictions::AssertIOAllowed();
98 // Style tip: where should you put AssertIOAllowed checks? It's best
99 // if you put them as close to the disk access as possible, at the
100 // lowest level. This rule is simple to follow and helps catch all
101 // callers. For example, if your function GoDoSomeBlockingDiskCall()
102 // only calls other functions in Chrome and not fopen(), you should go
103 // add the AssertIOAllowed checks in the helper functions.
105 class BASE_EXPORT ThreadRestrictions
{
107 // Constructing a ScopedAllowIO temporarily allows IO for the current
108 // thread. Doing this is almost certainly always incorrect.
109 class BASE_EXPORT ScopedAllowIO
{
111 ScopedAllowIO() { previous_value_
= SetIOAllowed(true); }
112 ~ScopedAllowIO() { SetIOAllowed(previous_value_
); }
114 // Whether IO is allowed when the ScopedAllowIO was constructed.
115 bool previous_value_
;
117 DISALLOW_COPY_AND_ASSIGN(ScopedAllowIO
);
120 // Constructing a ScopedAllowSingleton temporarily allows accessing for the
121 // current thread. Doing this is almost always incorrect.
122 class BASE_EXPORT ScopedAllowSingleton
{
124 ScopedAllowSingleton() { previous_value_
= SetSingletonAllowed(true); }
125 ~ScopedAllowSingleton() { SetSingletonAllowed(previous_value_
); }
127 // Whether singleton use is allowed when the ScopedAllowSingleton was
129 bool previous_value_
;
131 DISALLOW_COPY_AND_ASSIGN(ScopedAllowSingleton
);
134 #if ENABLE_THREAD_RESTRICTIONS
135 // Set whether the current thread to make IO calls.
136 // Threads start out in the *allowed* state.
137 // Returns the previous value.
138 static bool SetIOAllowed(bool allowed
);
140 // Check whether the current thread is allowed to make IO calls,
141 // and DCHECK if not. See the block comment above the class for
142 // a discussion of where to add these checks.
143 static void AssertIOAllowed();
145 // Set whether the current thread can use singletons. Returns the previous
147 static bool SetSingletonAllowed(bool allowed
);
149 // Check whether the current thread is allowed to use singletons (Singleton /
150 // LazyInstance). DCHECKs if not.
151 static void AssertSingletonAllowed();
153 // Disable waiting on the current thread. Threads start out in the *allowed*
154 // state. Returns the previous value.
155 static void DisallowWaiting();
157 // Check whether the current thread is allowed to wait, and DCHECK if not.
158 static void AssertWaitAllowed();
160 // Inline the empty definitions of these functions so that they can be
162 static bool SetIOAllowed(bool allowed
) { return true; }
163 static void AssertIOAllowed() {}
164 static bool SetSingletonAllowed(bool allowed
) { return true; }
165 static void AssertSingletonAllowed() {}
166 static void DisallowWaiting() {}
167 static void AssertWaitAllowed() {}
171 // DO NOT ADD ANY OTHER FRIEND STATEMENTS, talk to jam or brettw first.
172 // BEGIN ALLOWED USAGE.
173 friend class content::BrowserShutdownProfileDumper
;
174 friend class content::BrowserTestBase
;
175 friend class content::NestedMessagePumpAndroid
;
176 friend class content::RenderWidgetResizeHelper
;
177 friend class content::ScopedAllowWaitForAndroidLayoutTests
;
178 friend class ::HistogramSynchronizer
;
179 friend class ::ScopedAllowWaitForLegacyWebViewApi
;
180 friend class cc::CompletionEvent
;
181 friend class cc::TaskGraphRunner
;
182 friend class mojo::common::WatcherThreadManager
;
183 friend class remoting::AutoThread
;
184 friend class MessagePumpDefault
;
185 friend class SequencedWorkerPool
;
186 friend class SimpleThread
;
188 friend class ThreadTestHelper
;
189 friend class PlatformThread
;
190 friend class android::JavaHandlerThread
;
192 // END ALLOWED USAGE.
193 // BEGIN USAGE THAT NEEDS TO BE FIXED.
194 friend class ::chromeos::BlockingMethodCaller
; // http://crbug.com/125360
195 friend class ::chromeos::system::StatisticsProviderImpl
; // http://crbug.com/125385
196 friend class chrome_browser_net::Predictor
; // http://crbug.com/78451
198 content::BrowserGpuChannelHostFactory
; // http://crbug.com/125248
200 content::BrowserGpuMemoryBufferManager
; // http://crbug.com/420368
201 friend class content::GpuChannelHost
; // http://crbug.com/125264
202 friend class content::TextInputClientMac
; // http://crbug.com/121917
203 friend class dbus::Bus
; // http://crbug.com/125222
204 friend class disk_cache::BackendImpl
; // http://crbug.com/74623
205 friend class disk_cache::InFlightIO
; // http://crbug.com/74623
206 friend class net::internal::AddressTrackerLinux
; // http://crbug.com/125097
207 friend class ::BrowserProcessImpl
; // http://crbug.com/125207
208 friend class ::NativeBackendKWallet
; // http://crbug.com/125331
209 // END USAGE THAT NEEDS TO BE FIXED.
211 #if ENABLE_THREAD_RESTRICTIONS
212 static bool SetWaitAllowed(bool allowed
);
214 static bool SetWaitAllowed(bool allowed
) { return true; }
217 // Constructing a ScopedAllowWait temporarily allows waiting on the current
218 // thread. Doing this is almost always incorrect, which is why we limit who
219 // can use this through friend. If you find yourself needing to use this, find
220 // another way. Talk to jam or brettw.
221 class BASE_EXPORT ScopedAllowWait
{
223 ScopedAllowWait() { previous_value_
= SetWaitAllowed(true); }
224 ~ScopedAllowWait() { SetWaitAllowed(previous_value_
); }
226 // Whether singleton use is allowed when the ScopedAllowWait was
228 bool previous_value_
;
230 DISALLOW_COPY_AND_ASSIGN(ScopedAllowWait
);
233 DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions
);
238 #endif // BASE_THREADING_THREAD_RESTRICTIONS_H_