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 #include "base/threading/scoped_thread_priority.h"
6 
7 #include "base/location.h"
8 #include "base/threading/platform_thread.h"
9 #include "base/trace_event/trace_event.h"
10 
11 namespace base {
12 namespace internal {
13 
14 ScopedMayLoadLibraryAtBackgroundPriority::
ScopedMayLoadLibraryAtBackgroundPriority(const Location & from_here)15     ScopedMayLoadLibraryAtBackgroundPriority(const Location& from_here) {
16   TRACE_EVENT_BEGIN2("base", "ScopedMayLoadLibraryAtBackgroundPriority",
17                      "file_name", from_here.file_name(), "function_name",
18                      from_here.function_name());
19 }
20 
OnScopeFirstEntered()21 bool ScopedMayLoadLibraryAtBackgroundPriority::OnScopeFirstEntered() {
22 #if defined(OS_WIN)
23   const base::ThreadPriority priority =
24       PlatformThread::GetCurrentThreadPriority();
25   if (priority == base::ThreadPriority::BACKGROUND) {
26     original_thread_priority_ = priority;
27     PlatformThread::SetCurrentThreadPriority(base::ThreadPriority::NORMAL);
28 
29     TRACE_EVENT_BEGIN0(
30         "base",
31         "ScopedMayLoadLibraryAtBackgroundPriority : Priority Increased");
32   }
33 #endif  // OS_WIN
34 
35   return true;
36 }
37 
38 ScopedMayLoadLibraryAtBackgroundPriority::
~ScopedMayLoadLibraryAtBackgroundPriority()39     ~ScopedMayLoadLibraryAtBackgroundPriority() {
40   // Trace events must be closed in reverse order of opening so that they nest
41   // correctly.
42 #if defined(OS_WIN)
43   if (original_thread_priority_) {
44     TRACE_EVENT_END0(
45         "base",
46         "ScopedMayLoadLibraryAtBackgroundPriority : Priority Increased");
47     PlatformThread::SetCurrentThreadPriority(original_thread_priority_.value());
48   }
49 #endif  // OS_WIN
50   TRACE_EVENT_END0("base", "ScopedMayLoadLibraryAtBackgroundPriority");
51 }
52 
53 }  // namespace internal
54 }  // namespace base
55