Etienne Bergeron | 147591d | 2019-06-19 16:40:12 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_THREADING_SCOPED_THREAD_PRIORITY_H_ |
| 6 | #define BASE_THREADING_SCOPED_THREAD_PRIORITY_H_ |
| 7 | |
| 8 | #include "base/base_export.h" |
Francois Doray | 25315986 | 2019-12-11 05:43:55 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
Francois Doray | 25315986 | 2019-12-11 05:43:55 | [diff] [blame] | 10 | #include "base/location.h" |
| 11 | #include "base/macros.h" |
Etienne Bergeron | 147591d | 2019-06-19 16:40:12 | [diff] [blame] | 12 | #include "base/optional.h" |
| 13 | #include "build/build_config.h" |
| 14 | |
| 15 | namespace base { |
| 16 | |
| 17 | class Location; |
| 18 | enum class ThreadPriority : int; |
| 19 | |
Francois Doray | 25315986 | 2019-12-11 05:43:55 | [diff] [blame] | 20 | // INTERNAL_SCOPED_THREAD_PRIORITY_APPEND_LINE(name) produces an identifier by |
| 21 | // appending the current line number to |name|. This is used to avoid name |
| 22 | // collisions from variables defined inside a macro. |
| 23 | #define INTERNAL_SCOPED_THREAD_PRIORITY_CONCAT(a, b) a##b |
| 24 | // CONCAT1 provides extra level of indirection so that __LINE__ macro expands. |
| 25 | #define INTERNAL_SCOPED_THREAD_PRIORITY_CONCAT1(a, b) \ |
| 26 | INTERNAL_SCOPED_THREAD_PRIORITY_CONCAT(a, b) |
| 27 | #define INTERNAL_SCOPED_THREAD_PRIORITY_APPEND_LINE(name) \ |
| 28 | INTERNAL_SCOPED_THREAD_PRIORITY_CONCAT1(name, __LINE__) |
| 29 | |
| 30 | // All code that may load a DLL on a background thread must be surrounded by a |
| 31 | // scope that starts with this macro. |
| 32 | // |
| 33 | // Example: |
| 34 | // Foo(); |
| 35 | // { |
| 36 | // SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY(); |
| 37 | // LoadMyDll(); |
| 38 | // } |
| 39 | // Bar(); |
| 40 | // |
| 41 | // The macro raises the thread priority to NORMAL for the scope when first |
| 42 | // encountered. On Windows, loading a DLL on a background thread can lead to a |
| 43 | // priority inversion on the loader lock and cause huge janks. |
| 44 | #define SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY() \ |
| 45 | base::internal::ScopedMayLoadLibraryAtBackgroundPriority \ |
| 46 | INTERNAL_SCOPED_THREAD_PRIORITY_APPEND_LINE( \ |
| 47 | scoped_may_load_library_at_background_priority)(FROM_HERE); \ |
| 48 | { \ |
| 49 | /* Thread-safe static local variable initialization ensures that */ \ |
| 50 | /* OnScopeFirstEntered() is only invoked the first time that this is */ \ |
| 51 | /* encountered. */ \ |
| 52 | static bool INTERNAL_SCOPED_THREAD_PRIORITY_APPEND_LINE(invoke_once) = \ |
| 53 | INTERNAL_SCOPED_THREAD_PRIORITY_APPEND_LINE( \ |
| 54 | scoped_may_load_library_at_background_priority) \ |
| 55 | .OnScopeFirstEntered(); \ |
| 56 | ALLOW_UNUSED_LOCAL( \ |
| 57 | INTERNAL_SCOPED_THREAD_PRIORITY_APPEND_LINE(invoke_once)); \ |
| 58 | } |
| 59 | |
| 60 | namespace internal { |
| 61 | |
| 62 | class BASE_EXPORT ScopedMayLoadLibraryAtBackgroundPriority { |
Etienne Bergeron | 147591d | 2019-06-19 16:40:12 | [diff] [blame] | 63 | public: |
Francois Doray | 25315986 | 2019-12-11 05:43:55 | [diff] [blame] | 64 | explicit ScopedMayLoadLibraryAtBackgroundPriority(const Location& from_here); |
| 65 | ~ScopedMayLoadLibraryAtBackgroundPriority(); |
| 66 | |
| 67 | // The SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY() macro invokes this the |
| 68 | // first time that it is encountered. |
| 69 | bool OnScopeFirstEntered(); |
Etienne Bergeron | 147591d | 2019-06-19 16:40:12 | [diff] [blame] | 70 | |
| 71 | private: |
| 72 | #if defined(OS_WIN) |
Francois Doray | 25315986 | 2019-12-11 05:43:55 | [diff] [blame] | 73 | // The original priority when invoking OnScopeFirstEntered(). |
Etienne Bergeron | 147591d | 2019-06-19 16:40:12 | [diff] [blame] | 74 | base::Optional<ThreadPriority> original_thread_priority_; |
| 75 | #endif |
| 76 | |
Francois Doray | 25315986 | 2019-12-11 05:43:55 | [diff] [blame] | 77 | DISALLOW_COPY_AND_ASSIGN(ScopedMayLoadLibraryAtBackgroundPriority); |
Etienne Bergeron | 147591d | 2019-06-19 16:40:12 | [diff] [blame] | 78 | }; |
| 79 | |
Francois Doray | 25315986 | 2019-12-11 05:43:55 | [diff] [blame] | 80 | } // namespace internal |
| 81 | |
Etienne Bergeron | 147591d | 2019-06-19 16:40:12 | [diff] [blame] | 82 | } // namespace base |
| 83 | |
| 84 | #endif // BASE_THREADING_SCOPED_THREAD_PRIORITY_H_ |