| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "base/task_scheduler/task_scheduler.h" | 5 #include "base/task_scheduler/task_scheduler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/sys_info.h" | 10 #include "base/sys_info.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 background_blocking_worker_pool_params( | 31 background_blocking_worker_pool_params( |
| 32 background_blocking_worker_pool_params_in), | 32 background_blocking_worker_pool_params_in), |
| 33 foreground_worker_pool_params(foreground_worker_pool_params_in), | 33 foreground_worker_pool_params(foreground_worker_pool_params_in), |
| 34 foreground_blocking_worker_pool_params( | 34 foreground_blocking_worker_pool_params( |
| 35 foreground_blocking_worker_pool_params_in) {} | 35 foreground_blocking_worker_pool_params_in) {} |
| 36 | 36 |
| 37 TaskScheduler::InitParams::~InitParams() = default; | 37 TaskScheduler::InitParams::~InitParams() = default; |
| 38 | 38 |
| 39 #if !defined(OS_NACL) | 39 #if !defined(OS_NACL) |
| 40 // static | 40 // static |
| 41 void TaskScheduler::CreateAndSetSimpleTaskScheduler(const std::string& name) { | 41 void TaskScheduler::CreateAndSetSimpleTaskScheduler(StringPiece name) { |
| 42 using StandbyThreadPolicy = SchedulerWorkerPoolParams::StandbyThreadPolicy; | 42 using StandbyThreadPolicy = SchedulerWorkerPoolParams::StandbyThreadPolicy; |
| 43 | 43 |
| 44 // Values were chosen so that: | 44 // Values were chosen so that: |
| 45 // * There are few background threads. | 45 // * There are few background threads. |
| 46 // * Background threads never outnumber foreground threads. | 46 // * Background threads never outnumber foreground threads. |
| 47 // * The system is utilized maximally by foreground threads. | 47 // * The system is utilized maximally by foreground threads. |
| 48 const int num_cores = SysInfo::NumberOfProcessors(); | 48 const int num_cores = SysInfo::NumberOfProcessors(); |
| 49 constexpr int kBackgroundMaxThreads = 1; | 49 constexpr int kBackgroundMaxThreads = 1; |
| 50 constexpr int kBackgroundBlockingMaxThreads = 2; | 50 constexpr int kBackgroundBlockingMaxThreads = 2; |
| 51 const int kForegroundMaxThreads = std::max(1, num_cores); | 51 const int kForegroundMaxThreads = std::max(1, num_cores); |
| 52 const int kForegroundBlockingMaxThreads = std::max(2, num_cores); | 52 const int kForegroundBlockingMaxThreads = std::max(2, num_cores); |
| 53 | 53 |
| 54 constexpr TimeDelta kSuggestedReclaimTime = TimeDelta::FromSeconds(30); | 54 constexpr TimeDelta kSuggestedReclaimTime = TimeDelta::FromSeconds(30); |
| 55 | 55 |
| 56 CreateAndSetDefaultTaskScheduler( | 56 CreateAndSetDefaultTaskScheduler( |
| 57 name, {{StandbyThreadPolicy::LAZY, kBackgroundMaxThreads, | 57 name, {{StandbyThreadPolicy::LAZY, kBackgroundMaxThreads, |
| 58 kSuggestedReclaimTime}, | 58 kSuggestedReclaimTime}, |
| 59 {StandbyThreadPolicy::LAZY, kBackgroundBlockingMaxThreads, | 59 {StandbyThreadPolicy::LAZY, kBackgroundBlockingMaxThreads, |
| 60 kSuggestedReclaimTime}, | 60 kSuggestedReclaimTime}, |
| 61 {StandbyThreadPolicy::LAZY, kForegroundMaxThreads, | 61 {StandbyThreadPolicy::LAZY, kForegroundMaxThreads, |
| 62 kSuggestedReclaimTime}, | 62 kSuggestedReclaimTime}, |
| 63 {StandbyThreadPolicy::LAZY, kForegroundBlockingMaxThreads, | 63 {StandbyThreadPolicy::LAZY, kForegroundBlockingMaxThreads, |
| 64 kSuggestedReclaimTime}}); | 64 kSuggestedReclaimTime}}); |
| 65 } | 65 } |
| 66 #endif // !defined(OS_NACL) | 66 #endif // !defined(OS_NACL) |
| 67 | 67 |
| 68 void TaskScheduler::CreateAndSetDefaultTaskScheduler( | 68 void TaskScheduler::CreateAndSetDefaultTaskScheduler( |
| 69 const std::string& name, | 69 StringPiece name, |
| 70 const InitParams& init_params) { | 70 const InitParams& init_params) { |
| 71 SetInstance(internal::TaskSchedulerImpl::Create(name, init_params)); | 71 SetInstance(internal::TaskSchedulerImpl::Create(name, init_params)); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // static | 74 // static |
| 75 void TaskScheduler::SetInstance(std::unique_ptr<TaskScheduler> task_scheduler) { | 75 void TaskScheduler::SetInstance(std::unique_ptr<TaskScheduler> task_scheduler) { |
| 76 delete g_task_scheduler; | 76 delete g_task_scheduler; |
| 77 g_task_scheduler = task_scheduler.release(); | 77 g_task_scheduler = task_scheduler.release(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 // static | 80 // static |
| 81 TaskScheduler* TaskScheduler::GetInstance() { | 81 TaskScheduler* TaskScheduler::GetInstance() { |
| 82 return g_task_scheduler; | 82 return g_task_scheduler; |
| 83 } | 83 } |
| 84 | 84 |
| 85 } // namespace base | 85 } // namespace base |
| OLD | NEW |