| 6 | #define BASE_TASK_POST_JOB_H_ |
| 7 |
| 8 | #include "base/base_export.h" |
| 9 | #include "base/callback.h" |
| 10 | #include "base/location.h" |
| 11 | #include "base/logging.h" |
| 12 | #include "base/macros.h" |
| 13 | #include "base/memory/ref_counted.h" |
| 14 | #include "base/task/task_traits.h" |
| 15 | #include "base/time/time.h" |
| 16 |
| 17 | namespace base { |
| 18 | namespace internal { |
| 19 | class JobTaskSource; |
| 20 | class PooledTaskRunnerDelegate; |
| 21 | } |
| 22 |
| 23 | // Delegate that's passed to Job's worker task, providing an entry point to |
| 24 | // communicate with the scheduler. |
| 25 | class BASE_EXPORT JobDelegate { |
| 26 | public: |
| 27 | // A JobDelegate is instantiated for each worker task that is run. |
| 28 | // |task_source| is the task source whose worker task is running with this |
| 29 | // delegate and |pooled_task_runner_delegate| is used by ShouldYield() to |
| 30 | // check whether the pool wants this worker task to yield (null if this worker |
| 31 | // should never yield -- e.g. when the main thread is a worker). |
| 32 | JobDelegate(internal::JobTaskSource* task_source, |
| 33 | internal::PooledTaskRunnerDelegate* pooled_task_runner_delegate); |
| 34 | ~JobDelegate(); |
| 35 |
| 36 | // Returns true if this thread should return from the worker task on the |
| 37 | // current thread ASAP. Workers should periodically invoke ShouldYield (or |
| 38 | // YieldIfNeeded()) as often as is reasonable. |
| 39 | bool ShouldYield(); |
| 40 |
| 41 | // If ShouldYield(), this will pause the current thread (allowing it to be |
| 42 | // replaced in the pool); no-ops otherwise. If it pauses, it will resume and |
| 43 | // return from this call whenever higher priority work completes. |
| 44 | // Prefer ShouldYield() over this (only use YieldIfNeeded() when unwinding |
| 45 | // the stack is not possible). |
| 46 | void YieldIfNeeded(); |
| 47 |
| 48 | // Notifies the scheduler that max concurrency was increased, and the number |
| 49 | // of worker should be adjusted accordingly. See PostJob() for more details. |
| 50 | void NotifyConcurrencyIncrease(); |
| 51 |
| 52 | private: |
| 53 | // Verifies that either max concurrency is lower or equal to |
| 54 | // |expected_max_concurrency|, or there is an increase version update |
| 55 | // triggered by NotifyConcurrencyIncrease(). |
| 56 | void AssertExpectedConcurrency(size_t expected_max_concurrency); |
| 57 |
| 58 | internal::JobTaskSource* const task_source_; |
| 59 | internal::PooledTaskRunnerDelegate* const pooled_task_runner_delegate_; |
| 60 |
| 61 | #if DCHECK_IS_ON() |
| 62 | // Used in AssertExpectedConcurrency(), see that method's impl for details. |
| 63 | // Value of max concurrency recorded before running the worker task. |
| 64 | size_t recorded_max_concurrency_; |
| 65 | // Value of the increase version recorded before running the worker task. |
| 66 | size_t recorded_increase_version_; |
| 67 | // Value returned by the last call to ShouldYield(). |
| 68 | bool last_should_yield_ = false; |
| 69 | #endif |
| 70 |
| 71 | DISALLOW_COPY_AND_ASSIGN(JobDelegate); |
| 72 | }; |
| 73 |
| 74 | // Handle returned when posting a Job. Provides methods to control execution of |
| 75 | // the posted Job. |
| 76 | class BASE_EXPORT JobHandle { |
| 77 | public: |
| 78 | JobHandle(); |
| 79 | // A job must either be joined, canceled or detached before the JobHandle is |
| 80 | // destroyed. |
| 81 | ~JobHandle(); |
| 82 |
| 83 | JobHandle(JobHandle&&); |
| 84 | JobHandle& operator=(JobHandle&&); |
| 85 |
| 86 | // Returns true if associated with a Job. |
| 87 | explicit operator bool() const { return task_source_ != nullptr; } |
| 88 |
| 89 | // Update this Job's priority. |
| 90 | void UpdatePriority(TaskPriority new_priority); |
| 91 |
| 92 | // Notifies the scheduler that max concurrency was increased, and the number |
| 93 | // of workers should be adjusted accordingly. See PostJob() for more details. |
| 94 | void NotifyConcurrencyIncrease(); |
| 95 |
| 96 | // Contributes to the job on this thread. Doesn't return until all tasks have |
| 97 | // completed and max concurrency becomes 0. This also promotes this Job's |
| 98 | // priority to be at least as high as the calling thread's priority. |
| 99 | void Join(); |
| 100 |
| 101 | // Forces all existing workers to yield ASAP. Waits until they have all |
| 102 | // returned from the Job's callback before returning. |
| 103 | void Cancel(); |
| 104 |
| 105 | // Forces all existing workers to yield ASAP but doesn’t wait for them. |
| 106 | // Warning, this is dangerous if the Job's callback is bound to or has access |
| 107 | // to state which may be deleted after this call. |
| 108 | void CancelAndDetach(); |
| 109 |
| 110 | // Can be invoked before ~JobHandle() to avoid waiting on the job completing. |
| 111 | void Detach(); |
| 112 |
| 113 | private: |
| 114 | friend class internal::JobTaskSource; |
| 115 |
| 116 | explicit JobHandle(scoped_refptr<internal::JobTaskSource> task_source); |
| 117 |
| 118 | scoped_refptr<internal::JobTaskSource> task_source_; |
| 119 |
| 120 | DISALLOW_COPY_AND_ASSIGN(JobHandle); |
| 121 | }; |
| 122 |
| 123 | // Posts a repeating |worker_task| with specific |traits| to run in parallel on |
| 124 | // base::ThreadPool. |
| 125 | // Returns a JobHandle associated with the Job, which can be joined, canceled or |
| 126 | // detached. |
| 127 | // To avoid scheduling overhead, |worker_task| should do as much work as |
| 128 | // possible in a loop when invoked, and JobDelegate::ShouldYield() should be |
| 129 | // periodically invoked to conditionally exit and let the scheduler prioritize |
| 130 | // work. |
| 131 | // |
| 132 | // A canonical implementation of |worker_task| looks like: |
| 133 | // void WorkerTask(JobDelegate* job_delegate) { |
| 134 | // while (!job_delegate->ShouldYield()) { |
| 135 | // auto work_item = worker_queue.TakeWorkItem(); // Smallest unit of work. |
| 136 | // if (!work_item) |
| 137 | // return: |
| 138 | // ProcessWork(work_item); |
| 139 | // } |
| 140 | // } |
| 141 | // |
| 142 | // |max_concurrency_callback| controls the maximum number of threads calling |
| 143 | // |worker_task| concurrently. |worker_task| is only invoked if the number of |
| 144 | // threads previously running |worker_task| was less than the value returned by |
| 145 | // |max_concurrency_callback|. In general, |max_concurrency_callback| should |
| 146 | // return the latest number of incomplete work items (smallest unit of work) |
| 147 | // left to processed. JobHandle/JobDelegate::NotifyConcurrencyIncrease() *must* |
| 148 | // be invoked shortly after |max_concurrency_callback| starts returning a value |
| 149 | // larger than previously returned values. This usually happens when new work |
| 150 | // items are added and the API user wants additional threads to invoke |
| 151 | // |worker_task| concurrently. The callbacks may be called concurrently on any |
| 152 | // thread until the job is complete. If the job handle is detached, the |
| 153 | // callbacks may still be called, so they must not access global state that |
| 154 | // could be destroyed. |
| 155 | // |
| 156 | // |traits| requirements: |
| 157 | // - base::ThreadPolicy must be specified if the priority of the task runner |
| 158 | // will ever be increased from BEST_EFFORT. |
| 159 | JobHandle BASE_EXPORT |
| 160 | PostJob(const Location& from_here, |
| 161 | const TaskTraits& traits, |
| 162 | RepeatingCallback<void(JobDelegate*)> worker_task, |
| 163 | RepeatingCallback<size_t()> max_concurrency_callback); |
| 164 |
| 165 | } // namespace base |
| 166 |
| 167 | #endif // BASE_TASK_POST_JOB_H_ |
| 168 |