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