• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium Authors
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 
7 #include <atomic>
8 
9 #include "base/base_export.h"
10 #include "base/feature_list.h"
11 #include "base/threading/platform_thread.h"
12 
13 namespace base {
14 
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 
25 BASE_FEATURE(kUseUtilityThreadGroup,
26              "UseUtilityThreadGroup",
27              base::FEATURE_DISABLED_BY_DEFAULT);
28 
29 BASE_FEATURE(kNoWorkerThreadReclaim,
30              "NoWorkerThreadReclaim",
31              base::FEATURE_ENABLED_BY_DEFAULT);
32 
33 // static
34 BASE_FEATURE(kNoWakeUpsForCanceledTasks,
35              "NoWakeUpsForCanceledTasks",
36              FEATURE_ENABLED_BY_DEFAULT);
37 
38 BASE_FEATURE(kRemoveCanceledTasksInTaskQueue,
39              "RemoveCanceledTasksInTaskQueue2",
40              base::FEATURE_ENABLED_BY_DEFAULT);
41 
42 BASE_FEATURE(kDelayFirstWorkerWake,
43              "DelayFirstWorkerWake",
44              base::FEATURE_DISABLED_BY_DEFAULT);
45 
46 BASE_FEATURE(kAddTaskLeewayFeature,
47              "AddTaskLeeway",
48              base::FEATURE_ENABLED_BY_DEFAULT);
49 
50 const base::FeatureParam<TimeDelta> kTaskLeewayParam{&kAddTaskLeewayFeature,
51                                                      "leeway", kDefaultLeeway};
52 
53 BASE_FEATURE(kAlignWakeUps, "AlignWakeUps", base::FEATURE_ENABLED_BY_DEFAULT);
54 
55 BASE_FEATURE(kExplicitHighResolutionTimerWin,
56              "ExplicitHighResolutionTimerWin",
57              base::FEATURE_DISABLED_BY_DEFAULT);
58 
59 BASE_FEATURE(kRunTasksByBatches,
60              "RunTasksByBatches",
61              base::FEATURE_DISABLED_BY_DEFAULT);
62 
63 // Leeway value applied to delayed tasks. An atomic is used here because the
64 // value is queried from multiple threads.
65 std::atomic<TimeDelta> g_task_leeway{kDefaultLeeway};
66 
InitializeTaskLeeway()67 BASE_EXPORT void InitializeTaskLeeway() {
68   g_task_leeway.store(kTaskLeewayParam.Get(), std::memory_order_relaxed);
69 }
70 
GetTaskLeewayForCurrentThread()71 BASE_EXPORT TimeDelta GetTaskLeewayForCurrentThread() {
72   // For some threads, there might be a override of the leeway, so check it
73   // first.
74   auto leeway_override = PlatformThread::GetThreadLeewayOverride();
75   if (leeway_override.has_value())
76     return leeway_override.value();
77   return g_task_leeway.load(std::memory_order_relaxed);
78 }
79 
GetDefaultTaskLeeway()80 BASE_EXPORT TimeDelta GetDefaultTaskLeeway() {
81   return g_task_leeway.load(std::memory_order_relaxed);
82 }
83 
84 BASE_FEATURE(kMaxDelayedStarvationTasks,
85              "MaxDelayedStarvationTasks",
86              base::FEATURE_ENABLED_BY_DEFAULT);
87 
88 const base::FeatureParam<int> kMaxDelayedStarvationTasksParam{
89     &kMaxDelayedStarvationTasks, "count", 3};
90 
91 }  // namespace base
92