Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(460)

Side by Side Diff: base/threading/thread_restrictions.h

Issue 10151009: Disallow UI/IO thread blocking on any other thread. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_THREADING_THREAD_RESTRICTIONS_H_ 5 #ifndef BASE_THREADING_THREAD_RESTRICTIONS_H_
6 #define BASE_THREADING_THREAD_RESTRICTIONS_H_ 6 #define BASE_THREADING_THREAD_RESTRICTIONS_H_
7 7
8 #include "base/base_export.h" 8 #include "base/base_export.h"
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 10
11 class MetricsService;
12 class RenderWidgetHelper;
13 class TextInputClientMac;
14 namespace chrome_browser_net {
15 class Predictor;
16 }
17 namespace disk_cache {
18 class BackendImpl;
19 class InFlightIO;
20 }
21
11 namespace base { 22 namespace base {
12 23
24 class Thread;
25
13 // Certain behavior is disallowed on certain threads. ThreadRestrictions helps 26 // Certain behavior is disallowed on certain threads. ThreadRestrictions helps
14 // enforce these rules. Examples of such rules: 27 // enforce these rules. Examples of such rules:
15 // 28 //
16 // * Do not do blocking IO (makes the thread janky) 29 // * Do not do blocking IO (makes the thread janky)
17 // * Do not access Singleton/LazyInstance (may lead to shutdown crashes) 30 // * Do not access Singleton/LazyInstance (may lead to shutdown crashes)
18 // 31 //
19 // Here's more about how the protection works: 32 // Here's more about how the protection works:
20 // 33 //
21 // 1) If a thread should not be allowed to make IO calls, mark it: 34 // 1) If a thread should not be allowed to make IO calls, mark it:
22 // base::ThreadRestrictions::SetIOAllowed(false); 35 // base::ThreadRestrictions::SetIOAllowed(false);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 // a discussion of where to add these checks. 89 // a discussion of where to add these checks.
77 static void AssertIOAllowed(); 90 static void AssertIOAllowed();
78 91
79 // Set whether the current thread can use singletons. Returns the previous 92 // Set whether the current thread can use singletons. Returns the previous
80 // value. 93 // value.
81 static bool SetSingletonAllowed(bool allowed); 94 static bool SetSingletonAllowed(bool allowed);
82 95
83 // Check whether the current thread is allowed to use singletons (Singleton / 96 // Check whether the current thread is allowed to use singletons (Singleton /
84 // LazyInstance). DCHECKs if not. 97 // LazyInstance). DCHECKs if not.
85 static void AssertSingletonAllowed(); 98 static void AssertSingletonAllowed();
99
100 // Disable waiting on the current thread. Threads start out in the *allowed*
101 // state. Returns the previous value.
102 static void DisallowWaiting();
103
104 // Check whether the current thread is allowed to wait, and DCHECK if not.
105 static void AssertWaitAllowed();
86 #else 106 #else
87 // In Release builds, inline the empty definitions of these functions so 107 // In Release builds, inline the empty definitions of these functions so
88 // that they can be compiled out. 108 // that they can be compiled out.
89 static bool SetIOAllowed(bool allowed) { return true; } 109 static bool SetIOAllowed(bool allowed) { return true; }
90 static void AssertIOAllowed() {} 110 static void AssertIOAllowed() {}
91 static bool SetSingletonAllowed(bool allowed) { return true; } 111 static bool SetSingletonAllowed(bool allowed) { return true; }
92 static void AssertSingletonAllowed() {} 112 static void AssertSingletonAllowed() {}
113 static void DisallowWaiting() {}
114 static void AssertWaitAllowed() {}
93 #endif 115 #endif
94 116
95 private: 117 private:
118 // DO NOT ADD ANY OTHER FRIEND STATEMENTS.
119 friend class ::RenderWidgetHelper; // This is allowed, see documentation.
120 friend class Thread; // This is allowed.
121 friend class chrome_browser_net::Predictor; // http://crbug.com/78451
122 friend class disk_cache::BackendImpl; // http://crbug.com/74623
123 friend class disk_cache::InFlightIO; // http://crbug.com/74623
124 friend class ::TextInputClientMac; // http://crbug.com/121917
125 friend class ::MetricsService; // http://crbug.com/124954
126
127 #ifndef NDEBUG
128 static bool SetWaitAllowed(bool allowed);
129 #else
130 static bool SetWaitAllowed(bool allowed) { return true; }
131 #endif
132
133 // Constructing a ScopedAllowWait temporarily allows waiting on the current
134 // thread. Doing this is almost always incorrect, which is why we limit who
135 // can use this through friend. If you find yourself needing to use this, find
136 // another way. Talk to jam or brettw.
137 class BASE_EXPORT ScopedAllowWait {
138 public:
139 ScopedAllowWait() { previous_value_ = SetWaitAllowed(true); }
140 ~ScopedAllowWait() { SetWaitAllowed(previous_value_); }
141 private:
142 // Whether singleton use is allowed when the ScopedAllowWait was
143 // constructed.
144 bool previous_value_;
145
146 DISALLOW_COPY_AND_ASSIGN(ScopedAllowWait);
147 };
148
96 DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions); 149 DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions);
97 }; 150 };
98 151
99 } // namespace base 152 } // namespace base
100 153
101 #endif // BASE_THREADING_THREAD_RESTRICTIONS_H_ 154 #endif // BASE_THREADING_THREAD_RESTRICTIONS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698