| 6 |
| 7 | #include "base/task/scoped_set_task_priority_for_current_thread.h" |
| 8 | #include "base/task/thread_pool/job_task_source.h" |
| 9 | #include "base/task/thread_pool/pooled_task_runner_delegate.h" |
| 10 | #include "base/task/thread_pool/thread_pool_impl.h" |
| 11 | #include "base/task/thread_pool/thread_pool_instance.h" |
| 12 |
| 13 | namespace base { |
| 14 |
| 15 | JobDelegate::JobDelegate( |
| 16 | internal::JobTaskSource* task_source, |
| 17 | internal::PooledTaskRunnerDelegate* pooled_task_runner_delegate) |
| 18 | : task_source_(task_source), |
| 19 | pooled_task_runner_delegate_(pooled_task_runner_delegate) { |
| 20 | DCHECK(task_source_); |
| 21 | #if DCHECK_IS_ON() |
| 22 | recorded_increase_version_ = task_source_->GetConcurrencyIncreaseVersion(); |
| 23 | // Record max concurrency before running the worker task. |
| 24 | recorded_max_concurrency_ = task_source_->GetMaxConcurrency(); |
| 25 | #endif // DCHECK_IS_ON() |
| 26 | } |
| 27 |
| 28 | JobDelegate::~JobDelegate() { |
| 29 | #if DCHECK_IS_ON() |
| 30 | // When ShouldYield() returns false, the worker task is expected to do |
| 31 | // work before returning. |
| 32 | size_t expected_max_concurrency = recorded_max_concurrency_; |
| 33 | if (!last_should_yield_ && expected_max_concurrency > 0) |
| 34 | --expected_max_concurrency; |
| 35 | AssertExpectedConcurrency(expected_max_concurrency); |
| 36 | #endif // DCHECK_IS_ON() |
| 37 | } |
| 38 |
| 39 | bool JobDelegate::ShouldYield() { |
| 40 | #if DCHECK_IS_ON() |
| 41 | // ShouldYield() shouldn't be called again after returning true. |
| 42 | DCHECK(!last_should_yield_); |
| 43 | #endif // DCHECK_IS_ON() |
| 44 | const bool should_yield = |
| 45 | task_source_->ShouldYield() || |
| 46 | (pooled_task_runner_delegate_ && |
| 47 | pooled_task_runner_delegate_->ShouldYield(task_source_)); |
| 48 |
| 49 | #if DCHECK_IS_ON() |
| 50 | last_should_yield_ = should_yield; |
| 51 | #endif // DCHECK_IS_ON() |
| 52 | return should_yield; |
| 53 | } |
| 54 |
| 55 | void JobDelegate::YieldIfNeeded() { |
| 56 | // TODO(crbug.com/839091): Implement this. |
| 57 | } |
| 58 |
| 59 | void JobDelegate::NotifyConcurrencyIncrease() { |
| 60 | task_source_->NotifyConcurrencyIncrease(); |
| 61 | } |
| 62 |
| 63 | void JobDelegate::AssertExpectedConcurrency(size_t expected_max_concurrency) { |
| 64 | // In dcheck builds, verify that max concurrency falls in one of the following |
| 65 | // cases: |
| 66 | // 1) max concurrency behaves normally and is below or equals the expected |
| 67 | // value. |
| 68 | // 2) max concurrency increased above the expected value, which implies |
| 69 | // there are new work items that the associated worker task didn't see and |
| 70 | // NotifyConcurrencyIncrease() should be called to adjust the number of |
| 71 | // worker. |
| 72 | // a) NotifyConcurrencyIncrease() was already called and the recorded |
| 73 | // concurrency version is out of date, i.e. less than the actual version. |
| 74 | // b) NotifyConcurrencyIncrease() has not yet been called, in which case the |
| 75 | // function waits for an imminent increase of the concurrency version, |
| 76 | // or for max concurrency to decrease below or equal the expected value. |
| 77 | // This prevent ill-formed GetMaxConcurrency() implementations that: |
| 78 | // - Don't decrease with the number of remaining work items. |
| 79 | // - Don't return an up-to-date value. |
| 80 | #if DCHECK_IS_ON() |
| 81 | // Case 1: |
| 82 | if (task_source_->GetMaxConcurrency() <= expected_max_concurrency) |
| 83 | return; |
| 84 |
| 85 | // Case 2a: |
| 86 | const size_t actual_version = task_source_->GetConcurrencyIncreaseVersion(); |
| 87 | DCHECK_LE(recorded_increase_version_, actual_version); |
| 88 | if (recorded_increase_version_ < actual_version) |
| 89 | return; |
| 90 |
| 91 | // Case 2b: |
| 92 | const bool updated = task_source_->WaitForConcurrencyIncreaseUpdate( |
| 93 | recorded_increase_version_); |
| 94 | DCHECK(updated || |
| 95 | task_source_->GetMaxConcurrency() <= expected_max_concurrency) |
| 96 | << "Value returned by |max_concurrency_callback| is expected to " |
| 97 | "decrease, unless NotifyConcurrencyIncrease() is called."; |
| 98 |
| 99 | recorded_increase_version_ = task_source_->GetConcurrencyIncreaseVersion(); |
| 100 | recorded_max_concurrency_ = task_source_->GetMaxConcurrency(); |
| 101 | #endif // DCHECK_IS_ON() |
| 102 | } |
| 103 |
| 104 | JobHandle::JobHandle() = default; |
| 105 |
| 106 | JobHandle::JobHandle(scoped_refptr<internal::JobTaskSource> task_source) |
| 107 | : task_source_(std::move(task_source)) {} |
| 108 |
| 109 | JobHandle::~JobHandle() { |
| 110 | DCHECK(!task_source_) |
| 111 | << "The Job must be cancelled, detached or joined before its " |
| 112 | "JobHandle is destroyed."; |
| 113 | } |
| 114 |
| 115 | JobHandle::JobHandle(JobHandle&&) = default; |
| 116 |
| 117 | JobHandle& JobHandle::operator=(JobHandle&& other) { |
| 118 | DCHECK(!task_source_) |
| 119 | << "The Job must be cancelled, detached or joined before its " |
| 120 | "JobHandle is re-assigned."; |
| 121 | task_source_ = std::move(other.task_source_); |
| 122 | return *this; |
| 123 | } |
| 124 |
| 125 | void JobHandle::UpdatePriority(TaskPriority new_priority) { |
| 126 | task_source_->delegate()->UpdatePriority(task_source_, new_priority); |
| 127 | } |
| 128 |
| 129 | void JobHandle::NotifyConcurrencyIncrease() { |
| 130 | task_source_->NotifyConcurrencyIncrease(); |
| 131 | } |
| 132 |
| 133 | void JobHandle::Join() { |
| 134 | DCHECK_GE(internal::GetTaskPriorityForCurrentThread(), |
| 135 | task_source_->priority_racy()) |
| 136 | << "Join may not be called on Job with higher priority than the current " |
| 137 | "thread."; |
| 138 | UpdatePriority(internal::GetTaskPriorityForCurrentThread()); |
| 139 | bool must_run = task_source_->WillJoin(); |
| 140 | while (must_run) |
| 141 | must_run = task_source_->RunJoinTask(); |
| 142 | // Remove |task_source_| from the ThreadPool to prevent access to |
| 143 | // |max_concurrency_callback| after Join(). |
| 144 | task_source_->delegate()->RemoveJobTaskSource(task_source_); |
| 145 | task_source_ = nullptr; |
| 146 | } |
| 147 |
| 148 | void JobHandle::Cancel() { |
| 149 | task_source_->Cancel(); |
| 150 | Join(); |
| 151 | } |
| 152 |
| 153 | void JobHandle::CancelAndDetach() { |
| 154 | task_source_->Cancel(); |
| 155 | Detach(); |
| 156 | } |
| 157 |
| 158 | void JobHandle::Detach() { |
| 159 | DCHECK(task_source_); |
| 160 | task_source_ = nullptr; |
| 161 | } |
| 162 |
| 163 | JobHandle PostJob(const Location& from_here, |
| 164 | const TaskTraits& traits, |
| 165 | RepeatingCallback<void(JobDelegate*)> worker_task, |
| 166 | RepeatingCallback<size_t()> max_concurrency_callback) { |
| 167 | DCHECK(ThreadPoolInstance::Get()) |
| 168 | << "Ref. Prerequisite section of post_task.h.\n\n" |
| 169 | "Hint: if this is in a unit test, you're likely merely missing a " |
| 170 | "base::test::TaskEnvironment member in your fixture.\n"; |
| 171 | // ThreadPool is implicitly the destination for PostJob(). Extension traits |
| 172 | // cannot be used. |
| 173 | DCHECK_EQ(traits.extension_id(), |
| 174 | TaskTraitsExtensionStorage::kInvalidExtensionId); |
| 175 |
| 176 | auto task_source = base::MakeRefCounted<internal::JobTaskSource>( |
| 177 | from_here, traits, std::move(worker_task), |
| 178 | std::move(max_concurrency_callback), |
| 179 | static_cast<internal::ThreadPoolImpl*>(ThreadPoolInstance::Get())); |
| 180 | const bool queued = |
| 181 | static_cast<internal::ThreadPoolImpl*>(ThreadPoolInstance::Get()) |
| 182 | ->EnqueueJobTaskSource(task_source); |
| 183 | if (queued) |
| 184 | return internal::JobTaskSource::CreateJobHandle(std::move(task_source)); |
| 185 | return JobHandle(); |
| 186 | } |
| 187 |
| 188 | } // namespace base |
| 189 |