blob: 560055788fc491cbb0bed27f9561b6ec8fa61b0b [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
Francois Dorayee4a5532021-03-30 00:17:1725#if HAS_NATIVE_THREAD_POOL()
Daniel Cheng0fff5c232022-09-21 17:43:3426BASE_FEATURE(kUseNativeThreadPool,
27 "UseNativeThreadPool",
28 base::FEATURE_DISABLED_BY_DEFAULT);
29BASE_FEATURE(kUseBackgroundNativeThreadPool,
30 "UseBackgroundNativeThreadPool",
31 base::FEATURE_DISABLED_BY_DEFAULT);
Aditya Keerthia41eda52019-04-01 15:03:2532#endif
33
Daniel Cheng0fff5c232022-09-21 17:43:3434BASE_FEATURE(kNoWorkerThreadReclaim,
35 "NoWorkerThreadReclaim",
36 base::FEATURE_DISABLED_BY_DEFAULT);
Francois Doraya20b6df22019-06-27 15:04:1937
Patrick Monette6497eae2022-08-09 17:59:5838// static
Daniel Cheng0fff5c232022-09-21 17:43:3439BASE_FEATURE(kNoWakeUpsForCanceledTasks,
40 "NoWakeUpsForCanceledTasks",
41 FEATURE_DISABLED_BY_DEFAULT);
Patrick Monette6497eae2022-08-09 17:59:5842
Daniel Cheng0fff5c232022-09-21 17:43:3443BASE_FEATURE(kRemoveCanceledTasksInTaskQueue,
44 "RemoveCanceledTasksInTaskQueue2",
45 base::FEATURE_DISABLED_BY_DEFAULT);
Patrick Monetteeaaa2382021-12-08 17:06:5946
Daniel Cheng0fff5c232022-09-21 17:43:3447BASE_FEATURE(kAlwaysAbandonScheduledTask,
48 "AlwaysAbandonScheduledTask",
49 base::FEATURE_DISABLED_BY_DEFAULT);
Patrick Monette4efba9c2022-08-18 16:41:3250
Gabriel Charettedfad5992022-10-28 17:57:1751BASE_FEATURE(kDelayFirstWorkerWake,
52 "DelayFirstWorkerWake",
53 base::FEATURE_DISABLED_BY_DEFAULT);
54
Daniel Cheng0fff5c232022-09-21 17:43:3455BASE_FEATURE(kAddTaskLeewayFeature,
56 "AddTaskLeeway",
57 base::FEATURE_ENABLED_BY_DEFAULT);
Etienne Pierre-doraya8792732022-02-05 02:27:2558
59const base::FeatureParam<TimeDelta> kTaskLeewayParam{&kAddTaskLeewayFeature,
Jiahe Zhang07cf33132022-08-09 09:42:4960 "leeway", kDefaultLeeway};
Etienne Pierre-doraya8792732022-02-05 02:27:2561
Sergey Ulanov2523ae872022-09-29 19:20:4662BASE_FEATURE(kAlignWakeUps, "AlignWakeUps", base::FEATURE_ENABLED_BY_DEFAULT);
Etienne Pierre-doray048472eb2022-02-07 16:24:4863
Daniel Cheng0fff5c232022-09-21 17:43:3464BASE_FEATURE(kExplicitHighResolutionTimerWin,
65 "ExplicitHighResolutionTimerWin",
66 base::FEATURE_DISABLED_BY_DEFAULT);
Etienne Pierre-dorayae2c15a2022-04-29 18:50:5967
Daniel Cheng0fff5c232022-09-21 17:43:3468BASE_FEATURE(kRunTasksByBatches,
69 "RunTasksByBatches",
70 base::FEATURE_DISABLED_BY_DEFAULT);
Alex Attar9fcc01e2022-05-20 14:01:2771
Daniel Cheng0fff5c232022-09-21 17:43:3472BASE_FEATURE(kBrowserPeriodicYieldingToNative,
73 "BrowserPeriodicYieldingToNative",
74 base::FEATURE_DISABLED_BY_DEFAULT);
Stephen Nusko408b9a92022-09-15 10:03:5775
76const BASE_EXPORT base::FeatureParam<base::TimeDelta>
77 kBrowserPeriodicYieldingToNativeNormalInputAfterMsParam{
78 &kBrowserPeriodicYieldingToNative,
79 "yield_to_android_looper_after_ms_normal_input", base::Milliseconds(8)};
80
81const BASE_EXPORT base::FeatureParam<base::TimeDelta>
82 kBrowserPeriodicYieldingToNativeFlingInputAfterMsParam{
83 &kBrowserPeriodicYieldingToNative,
84 "yield_to_android_looper_after_ms_fling_input", base::Milliseconds(16)};
85
86const BASE_EXPORT base::FeatureParam<base::TimeDelta>
87 kBrowserPeriodicYieldingToNativeNoInputAfterMsParam{
88 &kBrowserPeriodicYieldingToNative,
89 "yield_to_android_looper_after_ms_no_input", base::Milliseconds(100)};
90
91const BASE_EXPORT base::FeatureParam<base::TimeDelta>
92 kBrowserPeriodicYieldingToNativeDelay{&kBrowserPeriodicYieldingToNative,
93 "non_delayed_looper_defer_for_ns",
94 base::Nanoseconds(500000)};
95
Jiahe Zhang07cf33132022-08-09 09:42:4996// Leeway value applied to delayed tasks. An atomic is used here because the
97// value is queried from multiple threads.
98std::atomic<TimeDelta> g_task_leeway{kDefaultLeeway};
99
100BASE_EXPORT void InitializeTaskLeeway() {
101 g_task_leeway.store(kTaskLeewayParam.Get(), std::memory_order_relaxed);
102}
103
Jiahe Zhangce46f4cb2022-11-22 03:12:02104BASE_EXPORT TimeDelta GetTaskLeewayForCurrentThread() {
105 // For some threads, there might be a override of the leeway, so check it
106 // first.
107 auto leeway_override = PlatformThread::GetThreadLeewayOverride();
108 if (leeway_override.has_value())
109 return leeway_override.value();
110 return g_task_leeway.load(std::memory_order_relaxed);
111}
112
113BASE_EXPORT TimeDelta GetDefaultTaskLeeway() {
Jiahe Zhang07cf33132022-08-09 09:42:49114 return g_task_leeway.load(std::memory_order_relaxed);
115}
116
Francois Doray5b44e33f2018-11-09 22:08:13117} // namespace base