| OLD | NEW |
| 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_api.h" | 8 #include "base/base_export.h" |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 | 10 |
| 11 namespace base { | 11 namespace base { |
| 12 | 12 |
| 13 // Certain behavior is disallowed on certain threads. ThreadRestrictions helps | 13 // Certain behavior is disallowed on certain threads. ThreadRestrictions helps |
| 14 // enforce these rules. Examples of such rules: | 14 // enforce these rules. Examples of such rules: |
| 15 // | 15 // |
| 16 // * Do not do blocking IO (makes the thread janky) | 16 // * Do not do blocking IO (makes the thread janky) |
| 17 // * Do not access Singleton/LazyInstance (may lead to shutdown crashes) | 17 // * Do not access Singleton/LazyInstance (may lead to shutdown crashes) |
| 18 // | 18 // |
| (...skipping 10 matching lines...) Expand all Loading... |
| 29 // | 29 // |
| 30 // ThreadRestrictions does nothing in release builds; it is debug-only. | 30 // ThreadRestrictions does nothing in release builds; it is debug-only. |
| 31 // | 31 // |
| 32 // Style tip: where should you put AssertIOAllowed checks? It's best | 32 // Style tip: where should you put AssertIOAllowed checks? It's best |
| 33 // if you put them as close to the disk access as possible, at the | 33 // if you put them as close to the disk access as possible, at the |
| 34 // lowest level. This rule is simple to follow and helps catch all | 34 // lowest level. This rule is simple to follow and helps catch all |
| 35 // callers. For example, if your function GoDoSomeBlockingDiskCall() | 35 // callers. For example, if your function GoDoSomeBlockingDiskCall() |
| 36 // only calls other functions in Chrome and not fopen(), you should go | 36 // only calls other functions in Chrome and not fopen(), you should go |
| 37 // add the AssertIOAllowed checks in the helper functions. | 37 // add the AssertIOAllowed checks in the helper functions. |
| 38 | 38 |
| 39 class BASE_API ThreadRestrictions { | 39 class BASE_EXPORT ThreadRestrictions { |
| 40 public: | 40 public: |
| 41 // Constructing a ScopedAllowIO temporarily allows IO for the current | 41 // Constructing a ScopedAllowIO temporarily allows IO for the current |
| 42 // thread. Doing this is almost certainly always incorrect. | 42 // thread. Doing this is almost certainly always incorrect. |
| 43 class BASE_API ScopedAllowIO { | 43 class BASE_EXPORT ScopedAllowIO { |
| 44 public: | 44 public: |
| 45 ScopedAllowIO() { previous_value_ = SetIOAllowed(true); } | 45 ScopedAllowIO() { previous_value_ = SetIOAllowed(true); } |
| 46 ~ScopedAllowIO() { SetIOAllowed(previous_value_); } | 46 ~ScopedAllowIO() { SetIOAllowed(previous_value_); } |
| 47 private: | 47 private: |
| 48 // Whether IO is allowed when the ScopedAllowIO was constructed. | 48 // Whether IO is allowed when the ScopedAllowIO was constructed. |
| 49 bool previous_value_; | 49 bool previous_value_; |
| 50 | 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(ScopedAllowIO); | 51 DISALLOW_COPY_AND_ASSIGN(ScopedAllowIO); |
| 52 }; | 52 }; |
| 53 | 53 |
| 54 // Constructing a ScopedAllowSingleton temporarily allows accessing for the | 54 // Constructing a ScopedAllowSingleton temporarily allows accessing for the |
| 55 // current thread. Doing this is almost always incorrect. | 55 // current thread. Doing this is almost always incorrect. |
| 56 class BASE_API ScopedAllowSingleton { | 56 class BASE_EXPORT ScopedAllowSingleton { |
| 57 public: | 57 public: |
| 58 ScopedAllowSingleton() { previous_value_ = SetSingletonAllowed(true); } | 58 ScopedAllowSingleton() { previous_value_ = SetSingletonAllowed(true); } |
| 59 ~ScopedAllowSingleton() { SetSingletonAllowed(previous_value_); } | 59 ~ScopedAllowSingleton() { SetSingletonAllowed(previous_value_); } |
| 60 private: | 60 private: |
| 61 // Whether singleton use is allowed when the ScopedAllowSingleton was | 61 // Whether singleton use is allowed when the ScopedAllowSingleton was |
| 62 // constructed. | 62 // constructed. |
| 63 bool previous_value_; | 63 bool previous_value_; |
| 64 | 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(ScopedAllowSingleton); | 65 DISALLOW_COPY_AND_ASSIGN(ScopedAllowSingleton); |
| 66 }; | 66 }; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 92 static void AssertSingletonAllowed() {} | 92 static void AssertSingletonAllowed() {} |
| 93 #endif | 93 #endif |
| 94 | 94 |
| 95 private: | 95 private: |
| 96 DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions); | 96 DISALLOW_IMPLICIT_CONSTRUCTORS(ThreadRestrictions); |
| 97 }; | 97 }; |
| 98 | 98 |
| 99 } // namespace base | 99 } // namespace base |
| 100 | 100 |
| 101 #endif // BASE_THREADING_THREAD_RESTRICTIONS_H_ | 101 #endif // BASE_THREADING_THREAD_RESTRICTIONS_H_ |
| OLD | NEW |