1*635a8641SAndroid Build Coastguard Worker // Copyright 2017 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #include "base/task_scheduler/environment_config.h" 6*635a8641SAndroid Build Coastguard Worker 7*635a8641SAndroid Build Coastguard Worker #include "base/synchronization/lock.h" 8*635a8641SAndroid Build Coastguard Worker #include "base/threading/platform_thread.h" 9*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h" 10*635a8641SAndroid Build Coastguard Worker 11*635a8641SAndroid Build Coastguard Worker namespace base { 12*635a8641SAndroid Build Coastguard Worker namespace internal { 13*635a8641SAndroid Build Coastguard Worker GetEnvironmentIndexForTraits(const TaskTraits & traits)14*635a8641SAndroid Build Coastguard Workersize_t GetEnvironmentIndexForTraits(const TaskTraits& traits) { 15*635a8641SAndroid Build Coastguard Worker const bool is_background = 16*635a8641SAndroid Build Coastguard Worker traits.priority() == base::TaskPriority::BACKGROUND; 17*635a8641SAndroid Build Coastguard Worker if (traits.may_block() || traits.with_base_sync_primitives()) 18*635a8641SAndroid Build Coastguard Worker return is_background ? BACKGROUND_BLOCKING : FOREGROUND_BLOCKING; 19*635a8641SAndroid Build Coastguard Worker return is_background ? BACKGROUND : FOREGROUND; 20*635a8641SAndroid Build Coastguard Worker } 21*635a8641SAndroid Build Coastguard Worker CanUseBackgroundPriorityForSchedulerWorker()22*635a8641SAndroid Build Coastguard Workerbool CanUseBackgroundPriorityForSchedulerWorker() { 23*635a8641SAndroid Build Coastguard Worker // When Lock doesn't handle multiple thread priorities, run all 24*635a8641SAndroid Build Coastguard Worker // SchedulerWorker with a normal priority to avoid priority inversion when a 25*635a8641SAndroid Build Coastguard Worker // thread running with a normal priority tries to acquire a lock held by a 26*635a8641SAndroid Build Coastguard Worker // thread running with a background priority. 27*635a8641SAndroid Build Coastguard Worker if (!Lock::HandlesMultipleThreadPriorities()) 28*635a8641SAndroid Build Coastguard Worker return false; 29*635a8641SAndroid Build Coastguard Worker 30*635a8641SAndroid Build Coastguard Worker #if !defined(OS_ANDROID) 31*635a8641SAndroid Build Coastguard Worker // When thread priority can't be increased, run all threads with a normal 32*635a8641SAndroid Build Coastguard Worker // priority to avoid priority inversions on shutdown (TaskScheduler increases 33*635a8641SAndroid Build Coastguard Worker // background threads priority to normal on shutdown while resolving remaining 34*635a8641SAndroid Build Coastguard Worker // shutdown blocking tasks). 35*635a8641SAndroid Build Coastguard Worker // 36*635a8641SAndroid Build Coastguard Worker // This is ignored on Android, because it doesn't have a clean shutdown phase. 37*635a8641SAndroid Build Coastguard Worker if (!PlatformThread::CanIncreaseCurrentThreadPriority()) 38*635a8641SAndroid Build Coastguard Worker return false; 39*635a8641SAndroid Build Coastguard Worker #endif // defined(OS_ANDROID) 40*635a8641SAndroid Build Coastguard Worker 41*635a8641SAndroid Build Coastguard Worker return true; 42*635a8641SAndroid Build Coastguard Worker } 43*635a8641SAndroid Build Coastguard Worker 44*635a8641SAndroid Build Coastguard Worker } // namespace internal 45*635a8641SAndroid Build Coastguard Worker } // namespace base 46