| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ | 5 #ifndef BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ |
| 6 #define BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ | 6 #define BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 // post_task.h instead of TaskScheduler except for the one callsite per process | 31 // post_task.h instead of TaskScheduler except for the one callsite per process |
| 32 // which manages the process' instance. | 32 // which manages the process' instance. |
| 33 class BASE_EXPORT TaskScheduler { | 33 class BASE_EXPORT TaskScheduler { |
| 34 public: | 34 public: |
| 35 // Returns the index of the worker pool in which a task with |traits| should | 35 // Returns the index of the worker pool in which a task with |traits| should |
| 36 // run. This should be coded in a future-proof way: new traits should | 36 // run. This should be coded in a future-proof way: new traits should |
| 37 // gracefully map to a default pool. | 37 // gracefully map to a default pool. |
| 38 using WorkerPoolIndexForTraitsCallback = | 38 using WorkerPoolIndexForTraitsCallback = |
| 39 Callback<size_t(const TaskTraits& traits)>; | 39 Callback<size_t(const TaskTraits& traits)>; |
| 40 | 40 |
| 41 // Destroying a TaskScheduler is not allowed in production; it is always |
| 42 // leaked. In tests, it should only be destroyed after JoinForTesting() has |
| 43 // returned. |
| 41 virtual ~TaskScheduler() = default; | 44 virtual ~TaskScheduler() = default; |
| 42 | 45 |
| 43 // Posts |task| with a |delay| and specific |traits|. |delay| can be zero. | 46 // Posts |task| with a |delay| and specific |traits|. |delay| can be zero. |
| 44 // For one off tasks that don't require a TaskRunner. | 47 // For one off tasks that don't require a TaskRunner. |
| 45 virtual void PostDelayedTaskWithTraits( | 48 virtual void PostDelayedTaskWithTraits( |
| 46 const tracked_objects::Location& from_here, | 49 const tracked_objects::Location& from_here, |
| 47 const TaskTraits& traits, | 50 const TaskTraits& traits, |
| 48 const Closure& task, | 51 const Closure& task, |
| 49 TimeDelta delay) = 0; | 52 TimeDelta delay) = 0; |
| 50 | 53 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 78 // called once. | 81 // called once. |
| 79 virtual void Shutdown() = 0; | 82 virtual void Shutdown() = 0; |
| 80 | 83 |
| 81 // Waits until there are no pending undelayed tasks. May be called in tests | 84 // Waits until there are no pending undelayed tasks. May be called in tests |
| 82 // to validate that a condition is met after all undelayed tasks have run. | 85 // to validate that a condition is met after all undelayed tasks have run. |
| 83 // | 86 // |
| 84 // Does not wait for delayed tasks. Waits for undelayed tasks posted from | 87 // Does not wait for delayed tasks. Waits for undelayed tasks posted from |
| 85 // other threads during the call. Returns immediately when shutdown completes. | 88 // other threads during the call. Returns immediately when shutdown completes. |
| 86 virtual void FlushForTesting() = 0; | 89 virtual void FlushForTesting() = 0; |
| 87 | 90 |
| 91 // Joins all threads. Tasks that are already running are allowed to complete |
| 92 // their execution. This can only be called once. |
| 93 virtual void JoinForTesting() = 0; |
| 94 |
| 88 // CreateAndSetSimpleTaskScheduler(), CreateAndSetDefaultTaskScheduler(), and | 95 // CreateAndSetSimpleTaskScheduler(), CreateAndSetDefaultTaskScheduler(), and |
| 89 // SetInstance() register a TaskScheduler to handle tasks posted through the | 96 // SetInstance() register a TaskScheduler to handle tasks posted through the |
| 90 // post_task.h API for this process. The registered TaskScheduler will only be | 97 // post_task.h API for this process. The registered TaskScheduler will only be |
| 91 // deleted when a new TaskScheduler is registered and is leaked on shutdown. | 98 // deleted when a new TaskScheduler is registered and is leaked on shutdown. |
| 92 // The methods must not be called when TaskRunners created by the previous | 99 // The methods must not be called when TaskRunners created by the previous |
| 93 // TaskScheduler are still alive. The methods are not thread-safe; proper | 100 // TaskScheduler are still alive. The methods are not thread-safe; proper |
| 94 // synchronization is required to use the post_task.h API after registering a | 101 // synchronization is required to use the post_task.h API after registering a |
| 95 // new TaskScheduler. | 102 // new TaskScheduler. |
| 96 | 103 |
| 97 // Creates and sets a task scheduler with one worker pool that can have up to | 104 // Creates and sets a task scheduler with one worker pool that can have up to |
| (...skipping 15 matching lines...) Expand all Loading... |
| 113 | 120 |
| 114 // Retrieve the TaskScheduler set via CreateAndSetDefaultTaskScheduler() or | 121 // Retrieve the TaskScheduler set via CreateAndSetDefaultTaskScheduler() or |
| 115 // SetInstance(). This should be used very rarely; most users of TaskScheduler | 122 // SetInstance(). This should be used very rarely; most users of TaskScheduler |
| 116 // should use the post_task.h API. | 123 // should use the post_task.h API. |
| 117 static TaskScheduler* GetInstance(); | 124 static TaskScheduler* GetInstance(); |
| 118 }; | 125 }; |
| 119 | 126 |
| 120 } // namespace base | 127 } // namespace base |
| 121 | 128 |
| 122 #endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ | 129 #endif // BASE_TASK_SCHEDULER_TASK_SCHEDULER_H_ |
| OLD | NEW |