0

Modify ScopedThreadMayLoadLibraryOnBackgroundThread to change priority only once

This CL is changing the behavior of the
ScopedThreadMayLoadLibraryOnBackgroundThread which is used to change the
thread priority on a background thread when a library can be loaded.

The previous implementation was boosting the priority for the scope of
the class for each execution. This is overkill since the library is only loaded
once.

The proposition is to keep track of the executed program counters and
to avoid to boost priority twice.

R=gab@chromium.org, fdoray@chromium.org

Bug: 973868
Change-Id: I8c2e9e18ac7e2b922437132051eb6702f83cefaf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1775730
Reviewed-by: Etienne Bergeron <etienneb@chromium.org>
Reviewed-by: François Doray <fdoray@chromium.org>
Reviewed-by: Gabriel Charette <gab@chromium.org>
Commit-Queue: Etienne Bergeron <etienneb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#691844}
This commit is contained in:
Etienne Bergeron
2019-08-29 22:55:27 +00:00
committed by Commit Bot
parent ef1ba3ebf8
commit f287bd9136

@ -4,12 +4,32 @@
#include "base/threading/scoped_thread_priority.h"
#include "base/containers/flat_set.h"
#include "base/location.h"
#include "base/no_destructor.h"
#include "base/synchronization/lock.h"
#include "base/threading/platform_thread.h"
#include "base/trace_event/trace_event.h"
namespace base {
namespace {
#if defined(OS_WIN)
// Flags the location |from_here| as executed. Returns true if the location
// was not previously executed.
bool ShouldBoostThreadPriorityForLocation(const Location& from_here) {
using ExecutedProgramCounterSet = base::flat_set<const void*>;
static base::NoDestructor<base::Lock> lock;
static base::NoDestructor<ExecutedProgramCounterSet> cache;
base::AutoLock auto_lock(*lock);
return cache.get()->insert(from_here.program_counter()).second;
}
#endif // OS_WIN
} // namespace
#if defined(OS_WIN)
// Enable the boost of thread priority when the code may load a library. The
// thread priority boost is required to avoid priority inversion on the loader
@ -28,6 +48,11 @@ ScopedThreadMayLoadLibraryOnBackgroundThread::
if (!base::FeatureList::IsEnabled(kBoostThreadPriorityOnLibraryLoading))
return;
// If the code at |from_here| has already been executed, do not boost the
// thread priority.
if (!ShouldBoostThreadPriorityForLocation(from_here))
return;
base::ThreadPriority priority = PlatformThread::GetCurrentThreadPriority();
if (priority == base::ThreadPriority::BACKGROUND) {
original_thread_priority_ = priority;