| 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" | 8 #include "base/basictypes.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| 11 | 11 |
| 12 // ThreadRestrictions helps protect threads that should not block from | 12 // Certain behavior is disallowed on certain threads. ThreadRestrictions helps |
| 13 // making blocking calls. It works like this: | 13 // enforce these rules. Examples of such rules: |
| 14 // |
| 15 // * Do not do blocking IO (makes the thread janky) |
| 16 // * Do not access Singleton/LazyInstance (may lead to shutdown crashes) |
| 17 // |
| 18 // Here's more about how the protection works: |
| 14 // | 19 // |
| 15 // 1) If a thread should not be allowed to make IO calls, mark it: | 20 // 1) If a thread should not be allowed to make IO calls, mark it: |
| 16 // base::ThreadRestrictions::SetIOAllowed(false); | 21 // base::ThreadRestrictions::SetIOAllowed(false); |
| 17 // By default, threads *are* allowed to make IO calls. | 22 // By default, threads *are* allowed to make IO calls. |
| 18 // In Chrome browser code, IO calls should be proxied to the File thread. | 23 // In Chrome browser code, IO calls should be proxied to the File thread. |
| 19 // | 24 // |
| 20 // 2) If a function makes a call that will go out to disk, check whether the | 25 // 2) If a function makes a call that will go out to disk, check whether the |
| 21 // current thread is allowed: | 26 // current thread is allowed: |
| 22 // base::ThreadRestrictions::AssertIOAllowed(); | 27 // base::ThreadRestrictions::AssertIOAllowed(); |
| 23 // | 28 // |
| (...skipping 14 matching lines...) Expand all Loading... |
| 38 public: | 43 public: |
| 39 ScopedAllowIO() { previous_value_ = SetIOAllowed(true); } | 44 ScopedAllowIO() { previous_value_ = SetIOAllowed(true); } |
| 40 ~ScopedAllowIO() { SetIOAllowed(previous_value_); } | 45 ~ScopedAllowIO() { SetIOAllowed(previous_value_); } |
| 41 private: | 46 private: |
| 42 // Whether IO is allowed when the ScopedAllowIO was constructed. | 47 // Whether IO is allowed when the ScopedAllowIO was constructed. |
| 43 bool previous_value_; | 48 bool previous_value_; |
| 44 | 49 |
| 45 DISALLOW_COPY_AND_ASSIGN(ScopedAllowIO); | 50 DISALLOW_COPY_AND_ASSIGN(ScopedAllowIO); |
| 46 }; | 51 }; |
| 47 | 52 |
| 53 // Constructing a ScopedAllowSingleton temporarily allows accessing for the |
| 54 // current thread. Doing this is almost always incorrect. |
| 55 class ScopedAllowSingleton { |
| 56 public: |
| 57 ScopedAllowSingleton() { previous_value_ = SetSingletonAllowed(true); } |
| 58 ~ScopedAllowSingleton() { SetSingletonAllowed(previous_value_); } |
| 59 private: |
| 60 // Whether singleton use is allowed when the ScopedAllowSingleton was |
| 61 // constructed. |
| 62 bool previous_value_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(ScopedAllowSingleton); |
| 65 }; |
| 66 |
| 48 #ifndef NDEBUG | 67 #ifndef NDEBUG |
| 49 // Set whether the current thread to make IO calls. | 68 // Set whether the current thread to make IO calls. |
| 50 // Threads start out in the *allowed* state. | 69 // Threads start out in the *allowed* state. |
| 51 // Returns the previous value. | 70 // Returns the previous value. |
| 52 static bool SetIOAllowed(bool allowed); | 71 static bool SetIOAllowed(bool allowed); |
| 53 | 72 |
| 54 // Check whether the current thread is allowed to make IO calls, | 73 // Check whether the current thread is allowed to make IO calls, |
| 55 // and DCHECK if not. See the block comment above the class for | 74 // and DCHECK if not. See the block comment above the class for |
| 56 // a discussion of where to add these checks. | 75 // a discussion of where to add these checks. |
| 57 static void AssertIOAllowed(); | 76 static void AssertIOAllowed(); |
| 77 |
| 78 // Set whether the current thread can use singletons. Returns the previous |
| 79 // value. |
| 80 static bool SetSingletonAllowed(bool allowed); |
| 81 |
| 82 // Check whether the current thread is allowed to use singletons (Singleton / |
| 83 // LazyInstance). DCHECKs if not. |
| 84 static void AssertSingletonAllowed(); |
| 58 #else | 85 #else |
| 59 // In Release builds, inline the empty definitions of these functions so | 86 // In Release builds, inline the empty definitions of these functions so |
| 60 // that they can be compiled out. | 87 // that they can be compiled out. |
| 61 static bool SetIOAllowed(bool allowed) { return true; } | 88 static bool SetIOAllowed(bool allowed) { return true; } |
| 62 static void AssertIOAllowed() {} | 89 static void AssertIOAllowed() {} |
| 90 static bool SetSingletonAllowed(bool allowed) { return true; } |
| 91 static void AssertSingletonAllowed() {} |
| 63 #endif | 92 #endif |
| 64 | 93 |
| 65 private: | 94 private: |
| 66 ThreadRestrictions(); // class for namespacing only | 95 DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions); |
| 67 }; | 96 }; |
| 68 | 97 |
| 69 } // namespace base | 98 } // namespace base |
| 70 | 99 |
| 71 #endif // BASE_THREAD_RESTRICTIONS_H_ | 100 #endif // BASE_THREAD_RESTRICTIONS_H_ |
| OLD | NEW |