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
;
27 class BlockingMethodCaller
;
29 class StatisticsProviderImpl
;
32 namespace chrome_browser_net
{
36 class BrowserGpuChannelHostFactory
;
37 class BrowserGpuMemoryBufferManager
;
38 class BrowserShutdownProfileDumper
;
39 class BrowserTestBase
;
41 class NestedMessagePumpAndroid
;
42 class RenderWidgetResizeHelper
;
43 class ScopedAllowWaitForAndroidLayoutTests
;
44 class TextInputClientMac
;
49 namespace disk_cache
{
55 class WatcherThreadManager
;
60 class AddressTrackerLinux
;
71 class JavaHandlerThread
;
74 class SequencedWorkerPool
;
77 class ThreadTestHelper
;
79 // Certain behavior is disallowed on certain threads. ThreadRestrictions helps
80 // enforce these rules. Examples of such rules:
82 // * Do not do blocking IO (makes the thread janky)
83 // * Do not access Singleton/LazyInstance (may lead to shutdown crashes)
85 // Here's more about how the protection works:
87 // 1) If a thread should not be allowed to make IO calls, mark it:
88 // base::ThreadRestrictions::SetIOAllowed(false);
89 // By default, threads *are* allowed to make IO calls.
90 // In Chrome browser code, IO calls should be proxied to the File thread.
92 // 2) If a function makes a call that will go out to disk, check whether the
93 // current thread is allowed:
94 // base::ThreadRestrictions::AssertIOAllowed();
97 // Style tip: where should you put AssertIOAllowed checks? It's best
98 // if you put them as close to the disk access as possible, at the
99 // lowest level. This rule is simple to follow and helps catch all
100 // callers. For example, if your function GoDoSomeBlockingDiskCall()
101 // only calls other functions in Chrome and not fopen(), you should go
102 // add the AssertIOAllowed checks in the helper functions.
104 class BASE_EXPORT ThreadRestrictions
{
106 // Constructing a ScopedAllowIO temporarily allows IO for the current
107 // thread. Doing this is almost certainly always incorrect.
108 class BASE_EXPORT ScopedAllowIO
{
110 ScopedAllowIO() { previous_value_
= SetIOAllowed(true); }
111 ~ScopedAllowIO() { SetIOAllowed(previous_value_
); }
113 // Whether IO is allowed when the ScopedAllowIO was constructed.
114 bool previous_value_
;
116 DISALLOW_COPY_AND_ASSIGN(ScopedAllowIO
);
119 // Constructing a ScopedAllowSingleton temporarily allows accessing for the
120 // current thread. Doing this is almost always incorrect.
121 class BASE_EXPORT ScopedAllowSingleton
{
123 ScopedAllowSingleton() { previous_value_
= SetSingletonAllowed(true); }
124 ~ScopedAllowSingleton() { SetSingletonAllowed(previous_value_
); }
126 // Whether singleton use is allowed when the ScopedAllowSingleton was
128 bool previous_value_
;
130 DISALLOW_COPY_AND_ASSIGN(ScopedAllowSingleton
);
133 #if ENABLE_THREAD_RESTRICTIONS
134 // Set whether the current thread to make IO calls.
135 // Threads start out in the *allowed* state.
136 // Returns the previous value.
137 static bool SetIOAllowed(bool allowed
);
139 // Check whether the current thread is allowed to make IO calls,
140 // and DCHECK if not. See the block comment above the class for
141 // a discussion of where to add these checks.
142 static void AssertIOAllowed();
144 // Set whether the current thread can use singletons. Returns the previous
146 static bool SetSingletonAllowed(bool allowed
);
148 // Check whether the current thread is allowed to use singletons (Singleton /
149 // LazyInstance). DCHECKs if not.
150 static void AssertSingletonAllowed();
152 // Disable waiting on the current thread. Threads start out in the *allowed*
153 // state. Returns the previous value.
154 static void DisallowWaiting();
156 // Check whether the current thread is allowed to wait, and DCHECK if not.
157 static void AssertWaitAllowed();
159 // Inline the empty definitions of these functions so that they can be
161 static bool SetIOAllowed(bool allowed
) { return true; }
162 static void AssertIOAllowed() {}
163 static bool SetSingletonAllowed(bool allowed
) { return true; }
164 static void AssertSingletonAllowed() {}
165 static void DisallowWaiting() {}
166 static void AssertWaitAllowed() {}
170 // DO NOT ADD ANY OTHER FRIEND STATEMENTS, talk to jam or brettw first.
171 // BEGIN ALLOWED USAGE.
172 friend class content::BrowserShutdownProfileDumper
;
173 friend class content::BrowserTestBase
;
174 friend class content::NestedMessagePumpAndroid
;
175 friend class content::RenderWidgetResizeHelper
;
176 friend class content::ScopedAllowWaitForAndroidLayoutTests
;
177 friend class ::HistogramSynchronizer
;
178 friend class ::ScopedAllowWaitForLegacyWebViewApi
;
179 friend class cc::CompletionEvent
;
180 friend class mojo::common::WatcherThreadManager
;
181 friend class remoting::AutoThread
;
182 friend class MessagePumpDefault
;
183 friend class SequencedWorkerPool
;
184 friend class SimpleThread
;
186 friend class ThreadTestHelper
;
187 friend class PlatformThread
;
188 friend class android::JavaHandlerThread
;
190 // END ALLOWED USAGE.
191 // BEGIN USAGE THAT NEEDS TO BE FIXED.
192 friend class ::chromeos::BlockingMethodCaller
; // http://crbug.com/125360
193 friend class ::chromeos::system::StatisticsProviderImpl
; // http://crbug.com/125385
194 friend class chrome_browser_net::Predictor
; // http://crbug.com/78451
196 content::BrowserGpuChannelHostFactory
; // http://crbug.com/125248
198 content::BrowserGpuMemoryBufferManager
; // http://crbug.com/420368
199 friend class content::GpuChannelHost
; // http://crbug.com/125264
200 friend class content::TextInputClientMac
; // http://crbug.com/121917
201 friend class dbus::Bus
; // http://crbug.com/125222
202 friend class disk_cache::BackendImpl
; // http://crbug.com/74623
203 friend class disk_cache::InFlightIO
; // http://crbug.com/74623
204 friend class net::internal::AddressTrackerLinux
; // http://crbug.com/125097
205 friend class ::BrowserProcessImpl
; // http://crbug.com/125207
206 friend class ::NativeBackendKWallet
; // http://crbug.com/125331
207 // END USAGE THAT NEEDS TO BE FIXED.
209 #if ENABLE_THREAD_RESTRICTIONS
210 static bool SetWaitAllowed(bool allowed
);
212 static bool SetWaitAllowed(bool allowed
) { return true; }
215 // Constructing a ScopedAllowWait temporarily allows waiting on the current
216 // thread. Doing this is almost always incorrect, which is why we limit who
217 // can use this through friend. If you find yourself needing to use this, find
218 // another way. Talk to jam or brettw.
219 class BASE_EXPORT ScopedAllowWait
{
221 ScopedAllowWait() { previous_value_
= SetWaitAllowed(true); }
222 ~ScopedAllowWait() { SetWaitAllowed(previous_value_
); }
224 // Whether singleton use is allowed when the ScopedAllowWait was
226 bool previous_value_
;
228 DISALLOW_COPY_AND_ASSIGN(ScopedAllowWait
);
231 DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions
);
236 #endif // BASE_THREADING_THREAD_RESTRICTIONS_H_