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 ScopedAllowWaitForDebugURL
;
46 class TextInputClientMac
;
47 } // namespace content
51 namespace disk_cache
{
57 class WatcherThreadManager
;
61 class NetworkChangeNotifierMac
;
63 class AddressTrackerLinux
;
74 class JavaHandlerThread
;
77 class SequencedWorkerPool
;
80 class ThreadTestHelper
;
82 // Certain behavior is disallowed on certain threads. ThreadRestrictions helps
83 // enforce these rules. Examples of such rules:
85 // * Do not do blocking IO (makes the thread janky)
86 // * Do not access Singleton/LazyInstance (may lead to shutdown crashes)
88 // Here's more about how the protection works:
90 // 1) If a thread should not be allowed to make IO calls, mark it:
91 // base::ThreadRestrictions::SetIOAllowed(false);
92 // By default, threads *are* allowed to make IO calls.
93 // In Chrome browser code, IO calls should be proxied to the File thread.
95 // 2) If a function makes a call that will go out to disk, check whether the
96 // current thread is allowed:
97 // base::ThreadRestrictions::AssertIOAllowed();
100 // Style tip: where should you put AssertIOAllowed checks? It's best
101 // if you put them as close to the disk access as possible, at the
102 // lowest level. This rule is simple to follow and helps catch all
103 // callers. For example, if your function GoDoSomeBlockingDiskCall()
104 // only calls other functions in Chrome and not fopen(), you should go
105 // add the AssertIOAllowed checks in the helper functions.
107 class BASE_EXPORT ThreadRestrictions
{
109 // Constructing a ScopedAllowIO temporarily allows IO for the current
110 // thread. Doing this is almost certainly always incorrect.
111 class BASE_EXPORT ScopedAllowIO
{
113 ScopedAllowIO() { previous_value_
= SetIOAllowed(true); }
114 ~ScopedAllowIO() { SetIOAllowed(previous_value_
); }
116 // Whether IO is allowed when the ScopedAllowIO was constructed.
117 bool previous_value_
;
119 DISALLOW_COPY_AND_ASSIGN(ScopedAllowIO
);
122 // Constructing a ScopedAllowSingleton temporarily allows accessing for the
123 // current thread. Doing this is almost always incorrect.
124 class BASE_EXPORT ScopedAllowSingleton
{
126 ScopedAllowSingleton() { previous_value_
= SetSingletonAllowed(true); }
127 ~ScopedAllowSingleton() { SetSingletonAllowed(previous_value_
); }
129 // Whether singleton use is allowed when the ScopedAllowSingleton was
131 bool previous_value_
;
133 DISALLOW_COPY_AND_ASSIGN(ScopedAllowSingleton
);
136 #if ENABLE_THREAD_RESTRICTIONS
137 // Set whether the current thread to make IO calls.
138 // Threads start out in the *allowed* state.
139 // Returns the previous value.
140 static bool SetIOAllowed(bool allowed
);
142 // Check whether the current thread is allowed to make IO calls,
143 // and DCHECK if not. See the block comment above the class for
144 // a discussion of where to add these checks.
145 static void AssertIOAllowed();
147 // Set whether the current thread can use singletons. Returns the previous
149 static bool SetSingletonAllowed(bool allowed
);
151 // Check whether the current thread is allowed to use singletons (Singleton /
152 // LazyInstance). DCHECKs if not.
153 static void AssertSingletonAllowed();
155 // Disable waiting on the current thread. Threads start out in the *allowed*
156 // state. Returns the previous value.
157 static void DisallowWaiting();
159 // Check whether the current thread is allowed to wait, and DCHECK if not.
160 static void AssertWaitAllowed();
162 // Inline the empty definitions of these functions so that they can be
164 static bool SetIOAllowed(bool allowed
) { return true; }
165 static void AssertIOAllowed() {}
166 static bool SetSingletonAllowed(bool allowed
) { return true; }
167 static void AssertSingletonAllowed() {}
168 static void DisallowWaiting() {}
169 static void AssertWaitAllowed() {}
173 // DO NOT ADD ANY OTHER FRIEND STATEMENTS, talk to jam or brettw first.
174 // BEGIN ALLOWED USAGE.
175 friend class content::BrowserShutdownProfileDumper
;
176 friend class content::BrowserTestBase
;
177 friend class content::NestedMessagePumpAndroid
;
178 friend class content::RenderWidgetResizeHelper
;
179 friend class content::ScopedAllowWaitForAndroidLayoutTests
;
180 friend class content::ScopedAllowWaitForDebugURL
;
181 friend class ::HistogramSynchronizer
;
182 friend class ::ScopedAllowWaitForLegacyWebViewApi
;
183 friend class cc::CompletionEvent
;
184 friend class cc::TaskGraphRunner
;
185 friend class mojo::common::WatcherThreadManager
;
186 friend class remoting::AutoThread
;
187 friend class MessagePumpDefault
;
188 friend class SequencedWorkerPool
;
189 friend class SimpleThread
;
191 friend class ThreadTestHelper
;
192 friend class PlatformThread
;
193 friend class android::JavaHandlerThread
;
195 // END ALLOWED USAGE.
196 // BEGIN USAGE THAT NEEDS TO BE FIXED.
197 friend class ::chromeos::BlockingMethodCaller
; // http://crbug.com/125360
198 friend class ::chromeos::system::StatisticsProviderImpl
; // http://crbug.com/125385
199 friend class chrome_browser_net::Predictor
; // http://crbug.com/78451
201 content::BrowserGpuChannelHostFactory
; // http://crbug.com/125248
203 content::BrowserGpuMemoryBufferManager
; // http://crbug.com/420368
204 friend class content::GpuChannelHost
; // http://crbug.com/125264
205 friend class content::TextInputClientMac
; // http://crbug.com/121917
206 friend class dbus::Bus
; // http://crbug.com/125222
207 friend class disk_cache::BackendImpl
; // http://crbug.com/74623
208 friend class disk_cache::InFlightIO
; // http://crbug.com/74623
209 friend class net::internal::AddressTrackerLinux
; // http://crbug.com/125097
210 friend class net::NetworkChangeNotifierMac
; // http://crbug.com/125097
211 friend class ::BrowserProcessImpl
; // http://crbug.com/125207
212 friend class ::NativeBackendKWallet
; // http://crbug.com/125331
213 // END USAGE THAT NEEDS TO BE FIXED.
215 #if ENABLE_THREAD_RESTRICTIONS
216 static bool SetWaitAllowed(bool allowed
);
218 static bool SetWaitAllowed(bool allowed
) { return true; }
221 // Constructing a ScopedAllowWait temporarily allows waiting on the current
222 // thread. Doing this is almost always incorrect, which is why we limit who
223 // can use this through friend. If you find yourself needing to use this, find
224 // another way. Talk to jam or brettw.
225 class BASE_EXPORT ScopedAllowWait
{
227 ScopedAllowWait() { previous_value_
= SetWaitAllowed(true); }
228 ~ScopedAllowWait() { SetWaitAllowed(previous_value_
); }
230 // Whether singleton use is allowed when the ScopedAllowWait was
232 bool previous_value_
;
234 DISALLOW_COPY_AND_ASSIGN(ScopedAllowWait
);
237 DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions
);
242 #endif // BASE_THREADING_THREAD_RESTRICTIONS_H_