blob: 52701901ef70e47714a639e3eb6d73dcec90f7e3 [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"
Leszek Swirski2987cc82018-11-05 15:19:2812
13namespace base {
14
Gabriel Charettedfad5992022-10-28 17:57:1715// 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 Wangf8950ab62022-12-09 05:33:4225BASE_FEATURE(kUseUtilityThreadGroup,
26 "UseUtilityThreadGroup",
27 base::FEATURE_DISABLED_BY_DEFAULT);
28
Daniel Cheng0fff5c232022-09-21 17:43:3429BASE_FEATURE(kNoWorkerThreadReclaim,
30 "NoWorkerThreadReclaim",
Etienne Pierre-dorayb01338b2023-03-13 15:44:5331 base::FEATURE_ENABLED_BY_DEFAULT);
Francois Doraya20b6df22019-06-27 15:04:1932
Patrick Monette6497eae2022-08-09 17:59:5833// static
Daniel Cheng0fff5c232022-09-21 17:43:3434BASE_FEATURE(kNoWakeUpsForCanceledTasks,
35 "NoWakeUpsForCanceledTasks",
François Dorayd2722e842022-12-13 00:19:4036 FEATURE_ENABLED_BY_DEFAULT);
Patrick Monette6497eae2022-08-09 17:59:5837
Daniel Cheng0fff5c232022-09-21 17:43:3438BASE_FEATURE(kRemoveCanceledTasksInTaskQueue,
39 "RemoveCanceledTasksInTaskQueue2",
François Dorayd2722e842022-12-13 00:19:4040 base::FEATURE_ENABLED_BY_DEFAULT);
Patrick Monetteeaaa2382021-12-08 17:06:5941
Gabriel Charettedfad5992022-10-28 17:57:1742BASE_FEATURE(kDelayFirstWorkerWake,
43 "DelayFirstWorkerWake",
44 base::FEATURE_DISABLED_BY_DEFAULT);
45
Daniel Cheng0fff5c232022-09-21 17:43:3446BASE_FEATURE(kAddTaskLeewayFeature,
47 "AddTaskLeeway",
48 base::FEATURE_ENABLED_BY_DEFAULT);
Etienne Pierre-doraya8792732022-02-05 02:27:2549
50const base::FeatureParam<TimeDelta> kTaskLeewayParam{&kAddTaskLeewayFeature,
Jiahe Zhang07cf33132022-08-09 09:42:4951 "leeway", kDefaultLeeway};
Etienne Pierre-doraya8792732022-02-05 02:27:2552
Etienne Pierre-doraycea99092023-07-20 20:31:1453BASE_FEATURE(kAlignWakeUps, "AlignWakeUps", base::FEATURE_DISABLED_BY_DEFAULT);
Etienne Pierre-doray048472eb2022-02-07 16:24:4854
Etienne Pierre-doray1f2b04e2023-09-05 20:23:5655BASE_FEATURE(kTimerSlackMac,
56 "TimerSlackMac",
57 base::FEATURE_DISABLED_BY_DEFAULT);
58
Daniel Cheng0fff5c232022-09-21 17:43:3459BASE_FEATURE(kExplicitHighResolutionTimerWin,
60 "ExplicitHighResolutionTimerWin",
Etienne Pierre-dorayd5cdda492023-07-05 18:20:1461 base::FEATURE_ENABLED_BY_DEFAULT);
Etienne Pierre-dorayae2c15a2022-04-29 18:50:5962
Daniel Cheng0fff5c232022-09-21 17:43:3463BASE_FEATURE(kRunTasksByBatches,
64 "RunTasksByBatches",
65 base::FEATURE_DISABLED_BY_DEFAULT);
Edgar Arriagad096f402023-05-02 15:39:2466BASE_FEATURE(kThreadPoolCap,
67 "ThreadPoolCap",
68 base::FEATURE_DISABLED_BY_DEFAULT);
69
70const base::FeatureParam<int> kThreadPoolCapRestrictedCount{
71 &kThreadPoolCap, "restricted_count", 3};
Alex Attar9fcc01e2022-05-20 14:01:2772
Jiahe Zhang07cf33132022-08-09 09:42:4973// Leeway value applied to delayed tasks. An atomic is used here because the
74// value is queried from multiple threads.
75std::atomic<TimeDelta> g_task_leeway{kDefaultLeeway};
76
77BASE_EXPORT void InitializeTaskLeeway() {
78 g_task_leeway.store(kTaskLeewayParam.Get(), std::memory_order_relaxed);
79}
80
Jiahe Zhangce46f4cb2022-11-22 03:12:0281BASE_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
90BASE_EXPORT TimeDelta GetDefaultTaskLeeway() {
Jiahe Zhang07cf33132022-08-09 09:42:4991 return g_task_leeway.load(std::memory_order_relaxed);
92}
93
Etienne Pierre-doraye9957c9c2023-03-15 20:54:1694BASE_FEATURE(kMaxDelayedStarvationTasks,
95 "MaxDelayedStarvationTasks",
96 base::FEATURE_ENABLED_BY_DEFAULT);
97
98const base::FeatureParam<int> kMaxDelayedStarvationTasksParam{
99 &kMaxDelayedStarvationTasks, "count", 3};
100
Francois Doray5b44e33f2018-11-09 22:08:13101} // namespace base