1*635a8641SAndroid Build Coastguard Worker // Copyright 2016 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <memory> 9*635a8641SAndroid Build Coastguard Worker #include <vector> 10*635a8641SAndroid Build Coastguard Worker 11*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h" 12*635a8641SAndroid Build Coastguard Worker #include "base/callback.h" 13*635a8641SAndroid Build Coastguard Worker #include "base/gtest_prod_util.h" 14*635a8641SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h" 15*635a8641SAndroid Build Coastguard Worker #include "base/sequenced_task_runner.h" 16*635a8641SAndroid Build Coastguard Worker #include "base/single_thread_task_runner.h" 17*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_piece.h" 18*635a8641SAndroid Build Coastguard Worker #include "base/task_runner.h" 19*635a8641SAndroid Build Coastguard Worker #include "base/task_scheduler/scheduler_worker_pool_params.h" 20*635a8641SAndroid Build Coastguard Worker #include "base/task_scheduler/single_thread_task_runner_thread_mode.h" 21*635a8641SAndroid Build Coastguard Worker #include "base/task_scheduler/task_traits.h" 22*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h" 23*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h" 24*635a8641SAndroid Build Coastguard Worker 25*635a8641SAndroid Build Coastguard Worker namespace gin { 26*635a8641SAndroid Build Coastguard Worker class V8Platform; 27*635a8641SAndroid Build Coastguard Worker } 28*635a8641SAndroid Build Coastguard Worker 29*635a8641SAndroid Build Coastguard Worker namespace content { 30*635a8641SAndroid Build Coastguard Worker // Can't use the FRIEND_TEST_ALL_PREFIXES macro because the test is in a 31*635a8641SAndroid Build Coastguard Worker // different namespace. 32*635a8641SAndroid Build Coastguard Worker class BrowserMainLoopTest_CreateThreadsInSingleProcess_Test; 33*635a8641SAndroid Build Coastguard Worker } // namespace content 34*635a8641SAndroid Build Coastguard Worker 35*635a8641SAndroid Build Coastguard Worker namespace base { 36*635a8641SAndroid Build Coastguard Worker 37*635a8641SAndroid Build Coastguard Worker class HistogramBase; 38*635a8641SAndroid Build Coastguard Worker class Location; 39*635a8641SAndroid Build Coastguard Worker class SchedulerWorkerObserver; 40*635a8641SAndroid Build Coastguard Worker 41*635a8641SAndroid Build Coastguard Worker // Interface for a task scheduler and static methods to manage the instance used 42*635a8641SAndroid Build Coastguard Worker // by the post_task.h API. 43*635a8641SAndroid Build Coastguard Worker // 44*635a8641SAndroid Build Coastguard Worker // The task scheduler doesn't create threads until Start() is called. Tasks can 45*635a8641SAndroid Build Coastguard Worker // be posted at any time but will not run until after Start() is called. 46*635a8641SAndroid Build Coastguard Worker // 47*635a8641SAndroid Build Coastguard Worker // The instance methods of this class are thread-safe. 48*635a8641SAndroid Build Coastguard Worker // 49*635a8641SAndroid Build Coastguard Worker // Note: All base/task_scheduler users should go through post_task.h instead of 50*635a8641SAndroid Build Coastguard Worker // TaskScheduler except for the one callsite per process which manages the 51*635a8641SAndroid Build Coastguard Worker // process's instance. 52*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT TaskScheduler { 53*635a8641SAndroid Build Coastguard Worker public: 54*635a8641SAndroid Build Coastguard Worker struct BASE_EXPORT InitParams { 55*635a8641SAndroid Build Coastguard Worker enum class SharedWorkerPoolEnvironment { 56*635a8641SAndroid Build Coastguard Worker // Use the default environment (no environment). 57*635a8641SAndroid Build Coastguard Worker DEFAULT, 58*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN) 59*635a8641SAndroid Build Coastguard Worker // Place the worker in a COM MTA. 60*635a8641SAndroid Build Coastguard Worker COM_MTA, 61*635a8641SAndroid Build Coastguard Worker #endif // defined(OS_WIN) 62*635a8641SAndroid Build Coastguard Worker }; 63*635a8641SAndroid Build Coastguard Worker 64*635a8641SAndroid Build Coastguard Worker InitParams( 65*635a8641SAndroid Build Coastguard Worker const SchedulerWorkerPoolParams& background_worker_pool_params_in, 66*635a8641SAndroid Build Coastguard Worker const SchedulerWorkerPoolParams& 67*635a8641SAndroid Build Coastguard Worker background_blocking_worker_pool_params_in, 68*635a8641SAndroid Build Coastguard Worker const SchedulerWorkerPoolParams& foreground_worker_pool_params_in, 69*635a8641SAndroid Build Coastguard Worker const SchedulerWorkerPoolParams& 70*635a8641SAndroid Build Coastguard Worker foreground_blocking_worker_pool_params_in, 71*635a8641SAndroid Build Coastguard Worker SharedWorkerPoolEnvironment shared_worker_pool_environment_in = 72*635a8641SAndroid Build Coastguard Worker SharedWorkerPoolEnvironment::DEFAULT); 73*635a8641SAndroid Build Coastguard Worker ~InitParams(); 74*635a8641SAndroid Build Coastguard Worker 75*635a8641SAndroid Build Coastguard Worker SchedulerWorkerPoolParams background_worker_pool_params; 76*635a8641SAndroid Build Coastguard Worker SchedulerWorkerPoolParams background_blocking_worker_pool_params; 77*635a8641SAndroid Build Coastguard Worker SchedulerWorkerPoolParams foreground_worker_pool_params; 78*635a8641SAndroid Build Coastguard Worker SchedulerWorkerPoolParams foreground_blocking_worker_pool_params; 79*635a8641SAndroid Build Coastguard Worker SharedWorkerPoolEnvironment shared_worker_pool_environment; 80*635a8641SAndroid Build Coastguard Worker }; 81*635a8641SAndroid Build Coastguard Worker 82*635a8641SAndroid Build Coastguard Worker // Destroying a TaskScheduler is not allowed in production; it is always 83*635a8641SAndroid Build Coastguard Worker // leaked. In tests, it should only be destroyed after JoinForTesting() has 84*635a8641SAndroid Build Coastguard Worker // returned. 85*635a8641SAndroid Build Coastguard Worker virtual ~TaskScheduler() = default; 86*635a8641SAndroid Build Coastguard Worker 87*635a8641SAndroid Build Coastguard Worker // Allows the task scheduler to create threads and run tasks following the 88*635a8641SAndroid Build Coastguard Worker // |init_params| specification. 89*635a8641SAndroid Build Coastguard Worker // 90*635a8641SAndroid Build Coastguard Worker // If specified, |scheduler_worker_observer| will be notified when a worker 91*635a8641SAndroid Build Coastguard Worker // enters and exits its main function. It must not be destroyed before 92*635a8641SAndroid Build Coastguard Worker // JoinForTesting() has returned (must never be destroyed in production). 93*635a8641SAndroid Build Coastguard Worker // 94*635a8641SAndroid Build Coastguard Worker // CHECKs on failure. 95*635a8641SAndroid Build Coastguard Worker virtual void Start( 96*635a8641SAndroid Build Coastguard Worker const InitParams& init_params, 97*635a8641SAndroid Build Coastguard Worker SchedulerWorkerObserver* scheduler_worker_observer = nullptr) = 0; 98*635a8641SAndroid Build Coastguard Worker 99*635a8641SAndroid Build Coastguard Worker // Posts |task| with a |delay| and specific |traits|. |delay| can be zero. 100*635a8641SAndroid Build Coastguard Worker // For one off tasks that don't require a TaskRunner. 101*635a8641SAndroid Build Coastguard Worker virtual void PostDelayedTaskWithTraits(const Location& from_here, 102*635a8641SAndroid Build Coastguard Worker const TaskTraits& traits, 103*635a8641SAndroid Build Coastguard Worker OnceClosure task, 104*635a8641SAndroid Build Coastguard Worker TimeDelta delay) = 0; 105*635a8641SAndroid Build Coastguard Worker 106*635a8641SAndroid Build Coastguard Worker // Returns a TaskRunner whose PostTask invocations result in scheduling tasks 107*635a8641SAndroid Build Coastguard Worker // using |traits|. Tasks may run in any order and in parallel. 108*635a8641SAndroid Build Coastguard Worker virtual scoped_refptr<TaskRunner> CreateTaskRunnerWithTraits( 109*635a8641SAndroid Build Coastguard Worker const TaskTraits& traits) = 0; 110*635a8641SAndroid Build Coastguard Worker 111*635a8641SAndroid Build Coastguard Worker // Returns a SequencedTaskRunner whose PostTask invocations result in 112*635a8641SAndroid Build Coastguard Worker // scheduling tasks using |traits|. Tasks run one at a time in posting order. 113*635a8641SAndroid Build Coastguard Worker virtual scoped_refptr<SequencedTaskRunner> 114*635a8641SAndroid Build Coastguard Worker CreateSequencedTaskRunnerWithTraits(const TaskTraits& traits) = 0; 115*635a8641SAndroid Build Coastguard Worker 116*635a8641SAndroid Build Coastguard Worker // Returns a SingleThreadTaskRunner whose PostTask invocations result in 117*635a8641SAndroid Build Coastguard Worker // scheduling tasks using |traits|. Tasks run on a single thread in posting 118*635a8641SAndroid Build Coastguard Worker // order. 119*635a8641SAndroid Build Coastguard Worker virtual scoped_refptr<SingleThreadTaskRunner> 120*635a8641SAndroid Build Coastguard Worker CreateSingleThreadTaskRunnerWithTraits( 121*635a8641SAndroid Build Coastguard Worker const TaskTraits& traits, 122*635a8641SAndroid Build Coastguard Worker SingleThreadTaskRunnerThreadMode thread_mode) = 0; 123*635a8641SAndroid Build Coastguard Worker 124*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN) 125*635a8641SAndroid Build Coastguard Worker // Returns a SingleThreadTaskRunner whose PostTask invocations result in 126*635a8641SAndroid Build Coastguard Worker // scheduling tasks using |traits| in a COM Single-Threaded Apartment. Tasks 127*635a8641SAndroid Build Coastguard Worker // run in the same Single-Threaded Apartment in posting order for the returned 128*635a8641SAndroid Build Coastguard Worker // SingleThreadTaskRunner. There is not necessarily a one-to-one 129*635a8641SAndroid Build Coastguard Worker // correspondence between SingleThreadTaskRunners and Single-Threaded 130*635a8641SAndroid Build Coastguard Worker // Apartments. The implementation is free to share apartments or create new 131*635a8641SAndroid Build Coastguard Worker // apartments as necessary. In either case, care should be taken to make sure 132*635a8641SAndroid Build Coastguard Worker // COM pointers are not smuggled across apartments. 133*635a8641SAndroid Build Coastguard Worker virtual scoped_refptr<SingleThreadTaskRunner> 134*635a8641SAndroid Build Coastguard Worker CreateCOMSTATaskRunnerWithTraits( 135*635a8641SAndroid Build Coastguard Worker const TaskTraits& traits, 136*635a8641SAndroid Build Coastguard Worker SingleThreadTaskRunnerThreadMode thread_mode) = 0; 137*635a8641SAndroid Build Coastguard Worker #endif // defined(OS_WIN) 138*635a8641SAndroid Build Coastguard Worker 139*635a8641SAndroid Build Coastguard Worker // Returns a vector of all histograms available in this task scheduler. 140*635a8641SAndroid Build Coastguard Worker virtual std::vector<const HistogramBase*> GetHistograms() const = 0; 141*635a8641SAndroid Build Coastguard Worker 142*635a8641SAndroid Build Coastguard Worker // Synchronously shuts down the scheduler. Once this is called, only tasks 143*635a8641SAndroid Build Coastguard Worker // posted with the BLOCK_SHUTDOWN behavior will be run. When this returns: 144*635a8641SAndroid Build Coastguard Worker // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their 145*635a8641SAndroid Build Coastguard Worker // execution. 146*635a8641SAndroid Build Coastguard Worker // - All posted BLOCK_SHUTDOWN tasks have completed their execution. 147*635a8641SAndroid Build Coastguard Worker // - CONTINUE_ON_SHUTDOWN tasks might still be running. 148*635a8641SAndroid Build Coastguard Worker // Note that an implementation can keep threads and other resources alive to 149*635a8641SAndroid Build Coastguard Worker // support running CONTINUE_ON_SHUTDOWN after this returns. This can only be 150*635a8641SAndroid Build Coastguard Worker // called once. 151*635a8641SAndroid Build Coastguard Worker virtual void Shutdown() = 0; 152*635a8641SAndroid Build Coastguard Worker 153*635a8641SAndroid Build Coastguard Worker // Waits until there are no pending undelayed tasks. May be called in tests 154*635a8641SAndroid Build Coastguard Worker // to validate that a condition is met after all undelayed tasks have run. 155*635a8641SAndroid Build Coastguard Worker // 156*635a8641SAndroid Build Coastguard Worker // Does not wait for delayed tasks. Waits for undelayed tasks posted from 157*635a8641SAndroid Build Coastguard Worker // other threads during the call. Returns immediately when shutdown completes. 158*635a8641SAndroid Build Coastguard Worker virtual void FlushForTesting() = 0; 159*635a8641SAndroid Build Coastguard Worker 160*635a8641SAndroid Build Coastguard Worker // Returns and calls |flush_callback| when there are no incomplete undelayed 161*635a8641SAndroid Build Coastguard Worker // tasks. |flush_callback| may be called back on any thread and should not 162*635a8641SAndroid Build Coastguard Worker // perform a lot of work. May be used when additional work on the current 163*635a8641SAndroid Build Coastguard Worker // thread needs to be performed during a flush. Only one 164*635a8641SAndroid Build Coastguard Worker // FlushAsyncForTesting() may be pending at any given time. 165*635a8641SAndroid Build Coastguard Worker virtual void FlushAsyncForTesting(OnceClosure flush_callback) = 0; 166*635a8641SAndroid Build Coastguard Worker 167*635a8641SAndroid Build Coastguard Worker // Joins all threads. Tasks that are already running are allowed to complete 168*635a8641SAndroid Build Coastguard Worker // their execution. This can only be called once. Using this task scheduler 169*635a8641SAndroid Build Coastguard Worker // instance to create task runners or post tasks is not permitted during or 170*635a8641SAndroid Build Coastguard Worker // after this call. 171*635a8641SAndroid Build Coastguard Worker virtual void JoinForTesting() = 0; 172*635a8641SAndroid Build Coastguard Worker 173*635a8641SAndroid Build Coastguard Worker // CreateAndStartWithDefaultParams(), Create(), and SetInstance() register a 174*635a8641SAndroid Build Coastguard Worker // TaskScheduler to handle tasks posted through the post_task.h API for this 175*635a8641SAndroid Build Coastguard Worker // process. 176*635a8641SAndroid Build Coastguard Worker // 177*635a8641SAndroid Build Coastguard Worker // Processes that need to initialize TaskScheduler with custom params or that 178*635a8641SAndroid Build Coastguard Worker // need to allow tasks to be posted before the TaskScheduler creates its 179*635a8641SAndroid Build Coastguard Worker // threads should use Create() followed by Start(). Other processes can use 180*635a8641SAndroid Build Coastguard Worker // CreateAndStartWithDefaultParams(). 181*635a8641SAndroid Build Coastguard Worker // 182*635a8641SAndroid Build Coastguard Worker // A registered TaskScheduler is only deleted when a new TaskScheduler is 183*635a8641SAndroid Build Coastguard Worker // registered. The last registered TaskScheduler is leaked on shutdown. The 184*635a8641SAndroid Build Coastguard Worker // methods below must not be called when TaskRunners created by a previous 185*635a8641SAndroid Build Coastguard Worker // TaskScheduler are still alive. The methods are not thread-safe; proper 186*635a8641SAndroid Build Coastguard Worker // synchronization is required to use the post_task.h API after registering a 187*635a8641SAndroid Build Coastguard Worker // new TaskScheduler. 188*635a8641SAndroid Build Coastguard Worker 189*635a8641SAndroid Build Coastguard Worker #if !defined(OS_NACL) 190*635a8641SAndroid Build Coastguard Worker // Creates and starts a task scheduler using default params. |name| is used to 191*635a8641SAndroid Build Coastguard Worker // label histograms, it must not be empty. It should identify the component 192*635a8641SAndroid Build Coastguard Worker // that calls this. Start() is called by this method; it is invalid to call it 193*635a8641SAndroid Build Coastguard Worker // again afterwards. CHECKs on failure. For tests, prefer 194*635a8641SAndroid Build Coastguard Worker // base::test::ScopedTaskEnvironment (ensures isolation). 195*635a8641SAndroid Build Coastguard Worker static void CreateAndStartWithDefaultParams(StringPiece name); 196*635a8641SAndroid Build Coastguard Worker 197*635a8641SAndroid Build Coastguard Worker // Same as CreateAndStartWithDefaultParams() but allows callers to split the 198*635a8641SAndroid Build Coastguard Worker // Create() and StartWithDefaultParams() calls. 199*635a8641SAndroid Build Coastguard Worker void StartWithDefaultParams(); 200*635a8641SAndroid Build Coastguard Worker #endif // !defined(OS_NACL) 201*635a8641SAndroid Build Coastguard Worker 202*635a8641SAndroid Build Coastguard Worker // Creates a ready to start task scheduler. |name| is used to label 203*635a8641SAndroid Build Coastguard Worker // histograms, it must not be empty. It should identify the component that 204*635a8641SAndroid Build Coastguard Worker // creates the TaskScheduler. The task scheduler doesn't create threads until 205*635a8641SAndroid Build Coastguard Worker // Start() is called. Tasks can be posted at any time but will not run until 206*635a8641SAndroid Build Coastguard Worker // after Start() is called. For tests, prefer 207*635a8641SAndroid Build Coastguard Worker // base::test::ScopedTaskEnvironment (ensures isolation). 208*635a8641SAndroid Build Coastguard Worker static void Create(StringPiece name); 209*635a8641SAndroid Build Coastguard Worker 210*635a8641SAndroid Build Coastguard Worker // Registers |task_scheduler| to handle tasks posted through the post_task.h 211*635a8641SAndroid Build Coastguard Worker // API for this process. For tests, prefer base::test::ScopedTaskEnvironment 212*635a8641SAndroid Build Coastguard Worker // (ensures isolation). 213*635a8641SAndroid Build Coastguard Worker static void SetInstance(std::unique_ptr<TaskScheduler> task_scheduler); 214*635a8641SAndroid Build Coastguard Worker 215*635a8641SAndroid Build Coastguard Worker // Retrieve the TaskScheduler set via SetInstance() or 216*635a8641SAndroid Build Coastguard Worker // CreateAndSet(Simple|Default)TaskScheduler(). This should be used very 217*635a8641SAndroid Build Coastguard Worker // rarely; most users of TaskScheduler should use the post_task.h API. In 218*635a8641SAndroid Build Coastguard Worker // particular, refrain from doing 219*635a8641SAndroid Build Coastguard Worker // if (!TaskScheduler::GetInstance()) { 220*635a8641SAndroid Build Coastguard Worker // TaskScheduler::SetInstance(...); 221*635a8641SAndroid Build Coastguard Worker // base::PostTask(...); 222*635a8641SAndroid Build Coastguard Worker // } 223*635a8641SAndroid Build Coastguard Worker // instead make sure to SetInstance() early in one determinstic place in the 224*635a8641SAndroid Build Coastguard Worker // process' initialization phase. 225*635a8641SAndroid Build Coastguard Worker // In doubt, consult with //base/task_scheduler/OWNERS. 226*635a8641SAndroid Build Coastguard Worker static TaskScheduler* GetInstance(); 227*635a8641SAndroid Build Coastguard Worker 228*635a8641SAndroid Build Coastguard Worker private: 229*635a8641SAndroid Build Coastguard Worker friend class gin::V8Platform; 230*635a8641SAndroid Build Coastguard Worker friend class content::BrowserMainLoopTest_CreateThreadsInSingleProcess_Test; 231*635a8641SAndroid Build Coastguard Worker 232*635a8641SAndroid Build Coastguard Worker // Returns the maximum number of non-single-threaded non-blocked tasks posted 233*635a8641SAndroid Build Coastguard Worker // with |traits| that can run concurrently in this TaskScheduler. |traits| 234*635a8641SAndroid Build Coastguard Worker // can't contain TaskPriority::BACKGROUND. 235*635a8641SAndroid Build Coastguard Worker // 236*635a8641SAndroid Build Coastguard Worker // Do not use this method. To process n items, post n tasks that each process 237*635a8641SAndroid Build Coastguard Worker // 1 item rather than GetMaxConcurrentNonBlockedTasksWithTraitsDeprecated() 238*635a8641SAndroid Build Coastguard Worker // tasks that each process 239*635a8641SAndroid Build Coastguard Worker // n/GetMaxConcurrentNonBlockedTasksWithTraitsDeprecated() items. 240*635a8641SAndroid Build Coastguard Worker // 241*635a8641SAndroid Build Coastguard Worker // TODO(fdoray): Remove this method. https://crbug.com/687264 242*635a8641SAndroid Build Coastguard Worker virtual int GetMaxConcurrentNonBlockedTasksWithTraitsDeprecated( 243*635a8641SAndroid Build Coastguard Worker const TaskTraits& traits) const = 0; 244*635a8641SAndroid Build Coastguard Worker }; 245*635a8641SAndroid Build Coastguard Worker 246*635a8641SAndroid Build Coastguard Worker } // namespace base 247*635a8641SAndroid Build Coastguard Worker 248*635a8641SAndroid Build Coastguard Worker #endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ 249