Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Leszek Swirski | 2987cc8 | 2018-11-05 15:19:28 | [diff] [blame] | 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/task/task_features.h" |
| 6 | |
Hans-Filip Elo | a7745f4 | 2022-10-18 18:41:22 | [diff] [blame] | 7 | #include <atomic> |
| 8 | |
David Sanders | 6e70994 | 2022-04-05 06:49:26 | [diff] [blame] | 9 | #include "base/base_export.h" |
Leszek Swirski | 2987cc8 | 2018-11-05 15:19:28 | [diff] [blame] | 10 | #include "base/feature_list.h" |
Jiahe Zhang | ce46f4cb | 2022-11-22 03:12:02 | [diff] [blame] | 11 | #include "base/threading/platform_thread.h" |
Leszek Swirski | 2987cc8 | 2018-11-05 15:19:28 | [diff] [blame] | 12 | |
| 13 | namespace base { |
| 14 | |
Gabriel Charette | dfad599 | 2022-10-28 17:57:17 | [diff] [blame] | 15 | // Note to implementers: thread pool code using task features must absolutely |
| 16 | // not invoke FeatureList::IsEnabled outside of the main thread. Doing so |
| 17 | // causes data races between worker threads and ~FeatureList when tests end |
| 18 | // (crbug.com/1344573). A reliable moment to query and cache the feature state |
| 19 | // is on ThreadPoolImpl::Start (and thus also on the first WorkerThread::Start, |
| 20 | // not the later ones) as this is invoked from the main thread after |
| 21 | // initializing the FeatureList. If caching the feature state in a static, you |
| 22 | // must be aware that all tests sharing a process will have the same state, |
| 23 | // regardless of future ScopedFeatureList instances. |
| 24 | |
Zhibo Wang | f8950ab6 | 2022-12-09 05:33:42 | [diff] [blame] | 25 | BASE_FEATURE(kUseUtilityThreadGroup, |
| 26 | "UseUtilityThreadGroup", |
| 27 | base::FEATURE_DISABLED_BY_DEFAULT); |
| 28 | |
Daniel Cheng | 0fff5c23 | 2022-09-21 17:43:34 | [diff] [blame] | 29 | BASE_FEATURE(kNoWorkerThreadReclaim, |
| 30 | "NoWorkerThreadReclaim", |
Etienne Pierre-doray | b01338b | 2023-03-13 15:44:53 | [diff] [blame] | 31 | base::FEATURE_ENABLED_BY_DEFAULT); |
Francois Doray | a20b6df2 | 2019-06-27 15:04:19 | [diff] [blame] | 32 | |
Patrick Monette | 6497eae | 2022-08-09 17:59:58 | [diff] [blame] | 33 | // static |
Daniel Cheng | 0fff5c23 | 2022-09-21 17:43:34 | [diff] [blame] | 34 | BASE_FEATURE(kNoWakeUpsForCanceledTasks, |
| 35 | "NoWakeUpsForCanceledTasks", |
François Doray | d2722e84 | 2022-12-13 00:19:40 | [diff] [blame] | 36 | FEATURE_ENABLED_BY_DEFAULT); |
Patrick Monette | 6497eae | 2022-08-09 17:59:58 | [diff] [blame] | 37 | |
Daniel Cheng | 0fff5c23 | 2022-09-21 17:43:34 | [diff] [blame] | 38 | BASE_FEATURE(kRemoveCanceledTasksInTaskQueue, |
| 39 | "RemoveCanceledTasksInTaskQueue2", |
François Doray | d2722e84 | 2022-12-13 00:19:40 | [diff] [blame] | 40 | base::FEATURE_ENABLED_BY_DEFAULT); |
Patrick Monette | eaaa238 | 2021-12-08 17:06:59 | [diff] [blame] | 41 | |
Gabriel Charette | dfad599 | 2022-10-28 17:57:17 | [diff] [blame] | 42 | BASE_FEATURE(kDelayFirstWorkerWake, |
| 43 | "DelayFirstWorkerWake", |
| 44 | base::FEATURE_DISABLED_BY_DEFAULT); |
| 45 | |
Daniel Cheng | 0fff5c23 | 2022-09-21 17:43:34 | [diff] [blame] | 46 | BASE_FEATURE(kAddTaskLeewayFeature, |
| 47 | "AddTaskLeeway", |
| 48 | base::FEATURE_ENABLED_BY_DEFAULT); |
Etienne Pierre-doray | a879273 | 2022-02-05 02:27:25 | [diff] [blame] | 49 | |
| 50 | const base::FeatureParam<TimeDelta> kTaskLeewayParam{&kAddTaskLeewayFeature, |
Jiahe Zhang | 07cf3313 | 2022-08-09 09:42:49 | [diff] [blame] | 51 | "leeway", kDefaultLeeway}; |
Etienne Pierre-doray | a879273 | 2022-02-05 02:27:25 | [diff] [blame] | 52 | |
Etienne Pierre-doray | cea9909 | 2023-07-20 20:31:14 | [diff] [blame] | 53 | BASE_FEATURE(kAlignWakeUps, "AlignWakeUps", base::FEATURE_DISABLED_BY_DEFAULT); |
Etienne Pierre-doray | 048472eb | 2022-02-07 16:24:48 | [diff] [blame] | 54 | |
Etienne Pierre-doray | 1f2b04e | 2023-09-05 20:23:56 | [diff] [blame^] | 55 | BASE_FEATURE(kTimerSlackMac, |
| 56 | "TimerSlackMac", |
| 57 | base::FEATURE_DISABLED_BY_DEFAULT); |
| 58 | |
Daniel Cheng | 0fff5c23 | 2022-09-21 17:43:34 | [diff] [blame] | 59 | BASE_FEATURE(kExplicitHighResolutionTimerWin, |
| 60 | "ExplicitHighResolutionTimerWin", |
Etienne Pierre-doray | d5cdda49 | 2023-07-05 18:20:14 | [diff] [blame] | 61 | base::FEATURE_ENABLED_BY_DEFAULT); |
Etienne Pierre-doray | ae2c15a | 2022-04-29 18:50:59 | [diff] [blame] | 62 | |
Daniel Cheng | 0fff5c23 | 2022-09-21 17:43:34 | [diff] [blame] | 63 | BASE_FEATURE(kRunTasksByBatches, |
| 64 | "RunTasksByBatches", |
| 65 | base::FEATURE_DISABLED_BY_DEFAULT); |
Edgar Arriaga | d096f40 | 2023-05-02 15:39:24 | [diff] [blame] | 66 | BASE_FEATURE(kThreadPoolCap, |
| 67 | "ThreadPoolCap", |
| 68 | base::FEATURE_DISABLED_BY_DEFAULT); |
| 69 | |
| 70 | const base::FeatureParam<int> kThreadPoolCapRestrictedCount{ |
| 71 | &kThreadPoolCap, "restricted_count", 3}; |
Alex Attar | 9fcc01e | 2022-05-20 14:01:27 | [diff] [blame] | 72 | |
Jiahe Zhang | 07cf3313 | 2022-08-09 09:42:49 | [diff] [blame] | 73 | // Leeway value applied to delayed tasks. An atomic is used here because the |
| 74 | // value is queried from multiple threads. |
| 75 | std::atomic<TimeDelta> g_task_leeway{kDefaultLeeway}; |
| 76 | |
| 77 | BASE_EXPORT void InitializeTaskLeeway() { |
| 78 | g_task_leeway.store(kTaskLeewayParam.Get(), std::memory_order_relaxed); |
| 79 | } |
| 80 | |
Jiahe Zhang | ce46f4cb | 2022-11-22 03:12:02 | [diff] [blame] | 81 | BASE_EXPORT TimeDelta GetTaskLeewayForCurrentThread() { |
| 82 | // For some threads, there might be a override of the leeway, so check it |
| 83 | // first. |
| 84 | auto leeway_override = PlatformThread::GetThreadLeewayOverride(); |
| 85 | if (leeway_override.has_value()) |
| 86 | return leeway_override.value(); |
| 87 | return g_task_leeway.load(std::memory_order_relaxed); |
| 88 | } |
| 89 | |
| 90 | BASE_EXPORT TimeDelta GetDefaultTaskLeeway() { |
Jiahe Zhang | 07cf3313 | 2022-08-09 09:42:49 | [diff] [blame] | 91 | return g_task_leeway.load(std::memory_order_relaxed); |
| 92 | } |
| 93 | |
Etienne Pierre-doray | e9957c9c | 2023-03-15 20:54:16 | [diff] [blame] | 94 | BASE_FEATURE(kMaxDelayedStarvationTasks, |
| 95 | "MaxDelayedStarvationTasks", |
| 96 | base::FEATURE_ENABLED_BY_DEFAULT); |
| 97 | |
| 98 | const base::FeatureParam<int> kMaxDelayedStarvationTasksParam{ |
| 99 | &kMaxDelayedStarvationTasks, "count", 3}; |
| 100 | |
Francois Doray | 5b44e33f | 2018-11-09 22:08:13 | [diff] [blame] | 101 | } // namespace base |