Added llvm into exceptions as we can't add README.chromium into 3rd party repository
[chromium-blink-merge.git] / base / threading / thread_restrictions.h
blob7c46fd2bdb4c91a19d892b3dfb9f4d0affda6600
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
14 #else
15 #define ENABLE_THREAD_RESTRICTIONS 0
16 #endif
18 class BrowserProcessImpl;
19 class HistogramSynchronizer;
20 class NativeBackendKWallet;
21 class ScopedAllowWaitForLegacyWebViewApi;
23 namespace cc {
24 class CompletionEvent;
26 namespace chromeos {
27 class BlockingMethodCaller;
28 namespace system {
29 class StatisticsProviderImpl;
32 namespace chrome_browser_net {
33 class Predictor;
35 namespace content {
36 class BrowserGpuChannelHostFactory;
37 class BrowserGpuMemoryBufferManager;
38 class BrowserShutdownProfileDumper;
39 class BrowserTestBase;
40 class GpuChannelHost;
41 class NestedMessagePumpAndroid;
42 class RenderWidgetResizeHelper;
43 class ScopedAllowWaitForAndroidLayoutTests;
44 class TextInputClientMac;
46 namespace dbus {
47 class Bus;
49 namespace disk_cache {
50 class BackendImpl;
51 class InFlightIO;
53 namespace mojo {
54 namespace common {
55 class WatcherThreadManager;
58 namespace net {
59 namespace internal {
60 class AddressTrackerLinux;
64 namespace remoting {
65 class AutoThread;
68 namespace base {
70 namespace android {
71 class JavaHandlerThread;
74 class SequencedWorkerPool;
75 class SimpleThread;
76 class Thread;
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 {
105 public:
106 // Constructing a ScopedAllowIO temporarily allows IO for the current
107 // thread. Doing this is almost certainly always incorrect.
108 class BASE_EXPORT ScopedAllowIO {
109 public:
110 ScopedAllowIO() { previous_value_ = SetIOAllowed(true); }
111 ~ScopedAllowIO() { SetIOAllowed(previous_value_); }
112 private:
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 {
122 public:
123 ScopedAllowSingleton() { previous_value_ = SetSingletonAllowed(true); }
124 ~ScopedAllowSingleton() { SetSingletonAllowed(previous_value_); }
125 private:
126 // Whether singleton use is allowed when the ScopedAllowSingleton was
127 // constructed.
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
145 // value.
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();
158 #else
159 // Inline the empty definitions of these functions so that they can be
160 // compiled out.
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() {}
167 #endif
169 private:
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;
185 friend class Thread;
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
195 friend class
196 content::BrowserGpuChannelHostFactory; // http://crbug.com/125248
197 friend class
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);
211 #else
212 static bool SetWaitAllowed(bool allowed) { return true; }
213 #endif
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 {
220 public:
221 ScopedAllowWait() { previous_value_ = SetWaitAllowed(true); }
222 ~ScopedAllowWait() { SetWaitAllowed(previous_value_); }
223 private:
224 // Whether singleton use is allowed when the ScopedAllowWait was
225 // constructed.
226 bool previous_value_;
228 DISALLOW_COPY_AND_ASSIGN(ScopedAllowWait);
231 DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions);
234 } // namespace base
236 #endif // BASE_THREADING_THREAD_RESTRICTIONS_H_