blob: 75a0f9091f11e0b1d48e3bfed21b876163b7de60 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2018 The Chromium Authors
Leszek Swirski2987cc82018-11-05 15:19:282// 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 Eloa7745f42022-10-18 18:41:227#include <atomic>
8
David Sanders6e709942022-04-05 06:49:269#include "base/base_export.h"
Leszek Swirski2987cc82018-11-05 15:19:2810#include "base/feature_list.h"
Jiahe Zhangce46f4cb2022-11-22 03:12:0211#include "base/threading/platform_thread.h"
Olivier Li Shing Tat-Dupuisc42ff2e2023-10-13 15:43:0312#include "build/build_config.h"
Leszek Swirski2987cc82018-11-05 15:19:2813
14namespace base {
15
Gabriel Charettedfad5992022-10-28 17:57:1716// Note to implementers: thread pool code using task features must absolutely
17// not invoke FeatureList::IsEnabled outside of the main thread. Doing so
18// causes data races between worker threads and ~FeatureList when tests end
19// (crbug.com/1344573). A reliable moment to query and cache the feature state
20// is on ThreadPoolImpl::Start (and thus also on the first WorkerThread::Start,
21// not the later ones) as this is invoked from the main thread after
22// initializing the FeatureList. If caching the feature state in a static, you
23// must be aware that all tests sharing a process will have the same state,
24// regardless of future ScopedFeatureList instances.
25
Zhibo Wangf8950ab62022-12-09 05:33:4226BASE_FEATURE(kUseUtilityThreadGroup,
27 "UseUtilityThreadGroup",
28 base::FEATURE_DISABLED_BY_DEFAULT);
29
Daniel Cheng0fff5c232022-09-21 17:43:3430BASE_FEATURE(kNoWorkerThreadReclaim,
31 "NoWorkerThreadReclaim",
Etienne Pierre-dorayb01338b2023-03-13 15:44:5332 base::FEATURE_ENABLED_BY_DEFAULT);
Francois Doraya20b6df22019-06-27 15:04:1933
Patrick Monette6497eae2022-08-09 17:59:5834// static
Daniel Cheng0fff5c232022-09-21 17:43:3435BASE_FEATURE(kNoWakeUpsForCanceledTasks,
36 "NoWakeUpsForCanceledTasks",
François Dorayd2722e842022-12-13 00:19:4037 FEATURE_ENABLED_BY_DEFAULT);
Patrick Monette6497eae2022-08-09 17:59:5838
Daniel Cheng0fff5c232022-09-21 17:43:3439BASE_FEATURE(kRemoveCanceledTasksInTaskQueue,
40 "RemoveCanceledTasksInTaskQueue2",
François Dorayd2722e842022-12-13 00:19:4041 base::FEATURE_ENABLED_BY_DEFAULT);
Patrick Monetteeaaa2382021-12-08 17:06:5942
Gabriel Charettedfad5992022-10-28 17:57:1743BASE_FEATURE(kDelayFirstWorkerWake,
44 "DelayFirstWorkerWake",
45 base::FEATURE_DISABLED_BY_DEFAULT);
46
Daniel Cheng0fff5c232022-09-21 17:43:3447BASE_FEATURE(kAddTaskLeewayFeature,
48 "AddTaskLeeway",
49 base::FEATURE_ENABLED_BY_DEFAULT);
Etienne Pierre-doraya8792732022-02-05 02:27:2550
51const base::FeatureParam<TimeDelta> kTaskLeewayParam{&kAddTaskLeewayFeature,
Jiahe Zhang07cf33132022-08-09 09:42:4952 "leeway", kDefaultLeeway};
Etienne Pierre-doray77474c902023-09-27 20:23:0953const base::FeatureParam<TimeDelta> kMaxPreciseDelay{
54 &kAddTaskLeewayFeature, "max_precise_delay", kDefaultMaxPreciseDelay};
Etienne Pierre-doraya8792732022-02-05 02:27:2555
Etienne Pierre-doraycea99092023-07-20 20:31:1456BASE_FEATURE(kAlignWakeUps, "AlignWakeUps", base::FEATURE_DISABLED_BY_DEFAULT);
Etienne Pierre-doray048472eb2022-02-07 16:24:4857
Etienne Pierre-doray1f2b04e2023-09-05 20:23:5658BASE_FEATURE(kTimerSlackMac,
59 "TimerSlackMac",
60 base::FEATURE_DISABLED_BY_DEFAULT);
61
Daniel Cheng0fff5c232022-09-21 17:43:3462BASE_FEATURE(kExplicitHighResolutionTimerWin,
63 "ExplicitHighResolutionTimerWin",
Etienne Pierre-dorayd5cdda492023-07-05 18:20:1464 base::FEATURE_ENABLED_BY_DEFAULT);
Etienne Pierre-dorayae2c15a2022-04-29 18:50:5965
Daniel Cheng0fff5c232022-09-21 17:43:3466BASE_FEATURE(kRunTasksByBatches,
67 "RunTasksByBatches",
Olivier Li Shing Tat-Dupuisc42ff2e2023-10-13 15:43:0368#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_CHROMEOS)
69 base::FEATURE_ENABLED_BY_DEFAULT);
70#else
Daniel Cheng0fff5c232022-09-21 17:43:3471 base::FEATURE_DISABLED_BY_DEFAULT);
Olivier Li Shing Tat-Dupuisc42ff2e2023-10-13 15:43:0372#endif
73
Edgar Arriaga453bcb12023-09-12 23:09:0174BASE_FEATURE(kThreadPoolCap2,
75 "ThreadPoolCap2",
Edgar Arriagad096f402023-05-02 15:39:2476 base::FEATURE_DISABLED_BY_DEFAULT);
77
78const base::FeatureParam<int> kThreadPoolCapRestrictedCount{
Edgar Arriaga453bcb12023-09-12 23:09:0179 &kThreadPoolCap2, "restricted_count", 3};
Alex Attar9fcc01e2022-05-20 14:01:2780
Jiahe Zhang07cf33132022-08-09 09:42:4981// Leeway value applied to delayed tasks. An atomic is used here because the
82// value is queried from multiple threads.
83std::atomic<TimeDelta> g_task_leeway{kDefaultLeeway};
84
85BASE_EXPORT void InitializeTaskLeeway() {
86 g_task_leeway.store(kTaskLeewayParam.Get(), std::memory_order_relaxed);
87}
88
Jiahe Zhangce46f4cb2022-11-22 03:12:0289BASE_EXPORT TimeDelta GetTaskLeewayForCurrentThread() {
90 // For some threads, there might be a override of the leeway, so check it
91 // first.
92 auto leeway_override = PlatformThread::GetThreadLeewayOverride();
93 if (leeway_override.has_value())
94 return leeway_override.value();
95 return g_task_leeway.load(std::memory_order_relaxed);
96}
97
98BASE_EXPORT TimeDelta GetDefaultTaskLeeway() {
Jiahe Zhang07cf33132022-08-09 09:42:4999 return g_task_leeway.load(std::memory_order_relaxed);
100}
101
Etienne Pierre-doraye9957c9c2023-03-15 20:54:16102BASE_FEATURE(kMaxDelayedStarvationTasks,
103 "MaxDelayedStarvationTasks",
104 base::FEATURE_ENABLED_BY_DEFAULT);
105
106const base::FeatureParam<int> kMaxDelayedStarvationTasksParam{
107 &kMaxDelayedStarvationTasks, "count", 3};
108
Francois Doray5b44e33f2018-11-09 22:08:13109} // namespace base