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 #include "base/task_scheduler/scheduler_worker_pool_impl.h" | 5 #include "base/task_scheduler/scheduler_worker_pool_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
(...skipping 23 matching lines...) Expand all Loading... |
34 | 34 |
35 constexpr char kPoolNameSuffix[] = "Pool"; | 35 constexpr char kPoolNameSuffix[] = "Pool"; |
36 constexpr char kDetachDurationHistogramPrefix[] = | 36 constexpr char kDetachDurationHistogramPrefix[] = |
37 "TaskScheduler.DetachDuration."; | 37 "TaskScheduler.DetachDuration."; |
38 constexpr char kNumTasksBeforeDetachHistogramPrefix[] = | 38 constexpr char kNumTasksBeforeDetachHistogramPrefix[] = |
39 "TaskScheduler.NumTasksBeforeDetach."; | 39 "TaskScheduler.NumTasksBeforeDetach."; |
40 constexpr char kNumTasksBetweenWaitsHistogramPrefix[] = | 40 constexpr char kNumTasksBetweenWaitsHistogramPrefix[] = |
41 "TaskScheduler.NumTasksBetweenWaits."; | 41 "TaskScheduler.NumTasksBetweenWaits."; |
42 constexpr char kTaskLatencyHistogramPrefix[] = "TaskScheduler.TaskLatency."; | 42 constexpr char kTaskLatencyHistogramPrefix[] = "TaskScheduler.TaskLatency."; |
43 | 43 |
44 // SchedulerWorker that owns the current thread, if any. | |
45 LazyInstance<ThreadLocalPointer<const SchedulerWorker>>::Leaky | |
46 tls_current_worker = LAZY_INSTANCE_INITIALIZER; | |
47 | |
48 // SchedulerWorkerPool that owns the current thread, if any. | 44 // SchedulerWorkerPool that owns the current thread, if any. |
49 LazyInstance<ThreadLocalPointer<const SchedulerWorkerPool>>::Leaky | 45 LazyInstance<ThreadLocalPointer<const SchedulerWorkerPool>>::Leaky |
50 tls_current_worker_pool = LAZY_INSTANCE_INITIALIZER; | 46 tls_current_worker_pool = LAZY_INSTANCE_INITIALIZER; |
51 | 47 |
52 // A task runner that runs tasks with the PARALLEL ExecutionMode. | 48 // A task runner that runs tasks with the PARALLEL ExecutionMode. |
53 class SchedulerParallelTaskRunner : public TaskRunner { | 49 class SchedulerParallelTaskRunner : public TaskRunner { |
54 public: | 50 public: |
55 // Constructs a SchedulerParallelTaskRunner which can be used to post tasks so | 51 // Constructs a SchedulerParallelTaskRunner which can be used to post tasks so |
56 // long as |worker_pool| is alive. | 52 // long as |worker_pool| is alive. |
57 // TODO(robliao): Find a concrete way to manage |worker_pool|'s memory. | 53 // TODO(robliao): Find a concrete way to manage |worker_pool|'s memory. |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 } | 190 } |
195 | 191 |
196 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 192 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
197 const Closure& closure, | 193 const Closure& closure, |
198 base::TimeDelta delay) override { | 194 base::TimeDelta delay) override { |
199 // Tasks are never nested within the task scheduler. | 195 // Tasks are never nested within the task scheduler. |
200 return PostDelayedTask(from_here, closure, delay); | 196 return PostDelayedTask(from_here, closure, delay); |
201 } | 197 } |
202 | 198 |
203 bool RunsTasksOnCurrentThread() const override { | 199 bool RunsTasksOnCurrentThread() const override { |
204 return tls_current_worker.Get().Get() == worker_; | 200 // Even though this is a SingleThreadTaskRunner, test the actual sequence |
| 201 // instead of the assigned worker so that another task randomly assigned |
| 202 // to the same worker doesn't return true by happenstance. |
| 203 return sequence_->token() == SequenceToken::GetForCurrentThread(); |
205 } | 204 } |
206 | 205 |
207 private: | 206 private: |
208 ~SchedulerSingleThreadTaskRunner() override; | 207 ~SchedulerSingleThreadTaskRunner() override; |
209 | 208 |
210 // Sequence for all Tasks posted through this TaskRunner. | 209 // Sequence for all Tasks posted through this TaskRunner. |
211 const scoped_refptr<Sequence> sequence_ = new Sequence; | 210 const scoped_refptr<Sequence> sequence_ = new Sequence; |
212 | 211 |
213 const TaskTraits traits_; | 212 const TaskTraits traits_; |
214 SchedulerWorkerPool* const worker_pool_; | 213 SchedulerWorkerPool* const worker_pool_; |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 DCHECK_EQ(num_tasks_since_last_wait_, 0U); | 497 DCHECK_EQ(num_tasks_since_last_wait_, 0U); |
499 | 498 |
500 if (!last_detach_time_.is_null()) { | 499 if (!last_detach_time_.is_null()) { |
501 outer_->detach_duration_histogram_->AddTime(TimeTicks::Now() - | 500 outer_->detach_duration_histogram_->AddTime(TimeTicks::Now() - |
502 last_detach_time_); | 501 last_detach_time_); |
503 } | 502 } |
504 | 503 |
505 PlatformThread::SetName( | 504 PlatformThread::SetName( |
506 StringPrintf("TaskScheduler%sWorker%d", outer_->name_.c_str(), index_)); | 505 StringPrintf("TaskScheduler%sWorker%d", outer_->name_.c_str(), index_)); |
507 | 506 |
508 DCHECK(!tls_current_worker.Get().Get()); | |
509 DCHECK(!tls_current_worker_pool.Get().Get()); | 507 DCHECK(!tls_current_worker_pool.Get().Get()); |
510 tls_current_worker.Get().Set(worker); | |
511 tls_current_worker_pool.Get().Set(outer_); | 508 tls_current_worker_pool.Get().Set(outer_); |
512 | 509 |
513 // New threads haven't run GetWork() yet, so reset the |idle_start_time_|. | 510 // New threads haven't run GetWork() yet, so reset the |idle_start_time_|. |
514 idle_start_time_ = TimeTicks(); | 511 idle_start_time_ = TimeTicks(); |
515 | 512 |
516 ThreadRestrictions::SetIOAllowed( | 513 ThreadRestrictions::SetIOAllowed( |
517 outer_->io_restriction_ == | 514 outer_->io_restriction_ == |
518 SchedulerWorkerPoolParams::IORestriction::ALLOWED); | 515 SchedulerWorkerPoolParams::IORestriction::ALLOWED); |
519 } | 516 } |
520 | 517 |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
793 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); | 790 AutoSchedulerLock auto_lock(idle_workers_stack_lock_); |
794 idle_workers_stack_.Remove(worker); | 791 idle_workers_stack_.Remove(worker); |
795 } | 792 } |
796 | 793 |
797 bool SchedulerWorkerPoolImpl::CanWorkerDetachForTesting() { | 794 bool SchedulerWorkerPoolImpl::CanWorkerDetachForTesting() { |
798 return !worker_detachment_disallowed_.IsSet(); | 795 return !worker_detachment_disallowed_.IsSet(); |
799 } | 796 } |
800 | 797 |
801 } // namespace internal | 798 } // namespace internal |
802 } // namespace base | 799 } // namespace base |
OLD | NEW |