OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_THREAD_RESTRICTIONS_H_ | 5 #ifndef BASE_THREAD_RESTRICTIONS_H_ |
6 #define BASE_THREAD_RESTRICTIONS_H_ | 6 #define BASE_THREAD_RESTRICTIONS_H_ |
7 | 7 |
8 namespace base { | 8 namespace base { |
9 | 9 |
10 // ThreadRestrictions helps protect threads that should not block from | 10 // ThreadRestrictions helps protect threads that should not block from |
11 // making blocking calls. It works like this: | 11 // making blocking calls. It works like this: |
12 // | 12 // |
13 // 1) If a thread should not be allowed to make IO calls, mark it: | 13 // 1) If a thread should not be allowed to make IO calls, mark it: |
14 // base::ThreadRestrictions::SetIOAllowed(false); | 14 // base::ThreadRestrictions::SetIOAllowed(false); |
15 // By default, threads *are* allowed to make IO calls. | 15 // By default, threads *are* allowed to make IO calls. |
16 // In Chrome browser code, IO calls should be proxied to the File thread. | 16 // In Chrome browser code, IO calls should be proxied to the File thread. |
17 // | 17 // |
18 // 2) If a function makes a call that will go out to disk, check whether the | 18 // 2) If a function makes a call that will go out to disk, check whether the |
19 // current thread is allowed: | 19 // current thread is allowed: |
20 // base::ThreadRestrictions::AssertIOAllowed(); | 20 // base::ThreadRestrictions::AssertIOAllowed(); |
21 // | 21 // |
22 // ThreadRestrictions does nothing in release builds; it is debug-only. | 22 // ThreadRestrictions does nothing in release builds; it is debug-only. |
23 // | 23 // |
| 24 // Style tip: where should you put AssertIOAllowed checks? It's best |
| 25 // if you put them as close to the disk access as possible, at the |
| 26 // lowest level. This rule is simple to follow and helps catch all |
| 27 // callers. For example, if your function GoDoSomeBlockingDiskCall() |
| 28 // only calls other functions in Chrome and not fopen(), you should go |
| 29 // add the AssertIOAllowed checks in the helper functions. |
| 30 |
24 class ThreadRestrictions { | 31 class ThreadRestrictions { |
25 public: | 32 public: |
| 33 // Constructing a ScopedAllowIO temporarily allows IO for the current |
| 34 // thread. Doing this is almost certainly always incorrect. This object |
| 35 // is available to temporarily work around bugs. |
| 36 class ScopedAllowIO { |
| 37 public: |
| 38 ScopedAllowIO() { previous_value_ = SetIOAllowed(true); } |
| 39 ~ScopedAllowIO() { SetIOAllowed(previous_value_); } |
| 40 private: |
| 41 // Whether IO is allowed when the ScopedAllowIO was constructed. |
| 42 bool previous_value_; |
| 43 }; |
| 44 |
26 // Set whether the current thread to make IO calls. | 45 // Set whether the current thread to make IO calls. |
27 // Threads start out in the *allowed* state. | 46 // Threads start out in the *allowed* state. |
28 static void SetIOAllowed(bool allowed); | 47 // Returns the previous value. |
| 48 static bool SetIOAllowed(bool allowed); |
29 | 49 |
30 // Check whether the current thread is allowed to make IO calls, | 50 // Check whether the current thread is allowed to make IO calls, |
31 // and DCHECK if not. | 51 // and DCHECK if not. See the block comment above the class for |
| 52 // a discussion of where to add these checks. |
32 static void AssertIOAllowed(); | 53 static void AssertIOAllowed(); |
33 | 54 |
34 private: | 55 private: |
35 ThreadRestrictions(); // class for namespacing only | 56 ThreadRestrictions(); // class for namespacing only |
36 }; | 57 }; |
37 | 58 |
38 // In Release builds, inline the empty definitions of these functions so | 59 // In Release builds, inline the empty definitions of these functions so |
39 // that they can be compiled out. | 60 // that they can be compiled out. |
40 #ifdef NDEBUG | 61 #ifdef NDEBUG |
41 void ThreadRestrictions::SetIOAllowed(bool allowed) {} | 62 bool ThreadRestrictions::SetIOAllowed(bool allowed) { return true; } |
42 void ThreadRestrictions::AssertIOAllowed() {} | 63 void ThreadRestrictions::AssertIOAllowed() {} |
43 #endif | 64 #endif |
44 | 65 |
45 } // namespace base | 66 } // namespace base |
46 | 67 |
47 #endif // BASE_THREAD_RESTRICTIONS_H_ | 68 #endif // BASE_THREAD_RESTRICTIONS_H_ |
OLD | NEW |