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