| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/threading/sequenced_worker_pool.h" | 5 #include "base/threading/sequenced_worker_pool.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "base/tracked_objects.h" | 28 #include "base/tracked_objects.h" |
| 29 | 29 |
| 30 #if defined(OS_MACOSX) | 30 #if defined(OS_MACOSX) |
| 31 #include "base/mac/scoped_nsautorelease_pool.h" | 31 #include "base/mac/scoped_nsautorelease_pool.h" |
| 32 #endif | 32 #endif |
| 33 | 33 |
| 34 namespace base { | 34 namespace base { |
| 35 | 35 |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 struct SequencedTask { | 38 struct SequencedTask : public TrackingInfo { |
| 39 SequencedTask() | 39 SequencedTask() |
| 40 : sequence_token_id(0), | 40 : sequence_token_id(0), |
| 41 shutdown_behavior(SequencedWorkerPool::BLOCK_SHUTDOWN) {} | 41 shutdown_behavior(SequencedWorkerPool::BLOCK_SHUTDOWN) {} |
| 42 | 42 |
| 43 explicit SequencedTask(const tracked_objects::Location& from_here) |
| 44 : base::TrackingInfo(from_here, TimeTicks()), |
| 45 sequence_token_id(0), |
| 46 shutdown_behavior(SequencedWorkerPool::BLOCK_SHUTDOWN) {} |
| 47 |
| 43 ~SequencedTask() {} | 48 ~SequencedTask() {} |
| 44 | 49 |
| 45 int sequence_token_id; | 50 int sequence_token_id; |
| 46 SequencedWorkerPool::WorkerShutdown shutdown_behavior; | 51 SequencedWorkerPool::WorkerShutdown shutdown_behavior; |
| 47 tracked_objects::Location location; | 52 tracked_objects::Location posted_from; |
| 48 Closure task; | 53 Closure task; |
| 49 }; | 54 }; |
| 50 | 55 |
| 51 // SequencedWorkerPoolTaskRunner --------------------------------------------- | 56 // SequencedWorkerPoolTaskRunner --------------------------------------------- |
| 52 // A TaskRunner which posts tasks to a SequencedWorkerPool with a | 57 // A TaskRunner which posts tasks to a SequencedWorkerPool with a |
| 53 // fixed ShutdownBehavior. | 58 // fixed ShutdownBehavior. |
| 54 // | 59 // |
| 55 // Note that this class is RefCountedThreadSafe (inherited from TaskRunner). | 60 // Note that this class is RefCountedThreadSafe (inherited from TaskRunner). |
| 56 class SequencedWorkerPoolTaskRunner : public TaskRunner { | 61 class SequencedWorkerPoolTaskRunner : public TaskRunner { |
| 57 public: | 62 public: |
| (...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 AutoLock lock(lock_); | 479 AutoLock lock(lock_); |
| 475 return SequenceToken(LockedGetNamedTokenID(name)); | 480 return SequenceToken(LockedGetNamedTokenID(name)); |
| 476 } | 481 } |
| 477 | 482 |
| 478 bool SequencedWorkerPool::Inner::PostTask( | 483 bool SequencedWorkerPool::Inner::PostTask( |
| 479 const std::string* optional_token_name, | 484 const std::string* optional_token_name, |
| 480 SequenceToken sequence_token, | 485 SequenceToken sequence_token, |
| 481 WorkerShutdown shutdown_behavior, | 486 WorkerShutdown shutdown_behavior, |
| 482 const tracked_objects::Location& from_here, | 487 const tracked_objects::Location& from_here, |
| 483 const Closure& task) { | 488 const Closure& task) { |
| 484 SequencedTask sequenced; | 489 SequencedTask sequenced(from_here); |
| 485 sequenced.sequence_token_id = sequence_token.id_; | 490 sequenced.sequence_token_id = sequence_token.id_; |
| 486 sequenced.shutdown_behavior = shutdown_behavior; | 491 sequenced.shutdown_behavior = shutdown_behavior; |
| 487 sequenced.location = from_here; | 492 sequenced.posted_from = from_here; |
| 488 sequenced.task = task; | 493 sequenced.task = task; |
| 489 | 494 |
| 490 int create_thread_id = 0; | 495 int create_thread_id = 0; |
| 491 { | 496 { |
| 492 AutoLock lock(lock_); | 497 AutoLock lock(lock_); |
| 493 if (shutdown_called_) | 498 if (shutdown_called_) |
| 494 return false; | 499 return false; |
| 495 | 500 |
| 496 // Now that we have the lock, apply the named token rules. | 501 // Now that we have the lock, apply the named token rules. |
| 497 if (optional_token_name) | 502 if (optional_token_name) |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 SignalHasWork(); | 612 SignalHasWork(); |
| 608 delete_these_outside_lock.clear(); | 613 delete_these_outside_lock.clear(); |
| 609 | 614 |
| 610 // Complete thread creation outside the lock if necessary. | 615 // Complete thread creation outside the lock if necessary. |
| 611 if (new_thread_id) | 616 if (new_thread_id) |
| 612 FinishStartingAdditionalThread(new_thread_id); | 617 FinishStartingAdditionalThread(new_thread_id); |
| 613 | 618 |
| 614 this_worker->set_running_sequence( | 619 this_worker->set_running_sequence( |
| 615 SequenceToken(task.sequence_token_id)); | 620 SequenceToken(task.sequence_token_id)); |
| 616 | 621 |
| 622 tracked_objects::TrackedTime start_time = |
| 623 tracked_objects::ThreadData::NowForStartOfRun(task.birth_tally); |
| 624 |
| 617 task.task.Run(); | 625 task.task.Run(); |
| 618 | 626 |
| 627 tracked_objects::ThreadData::TallyRunOnNamedThreadIfTracking(task, |
| 628 start_time, tracked_objects::ThreadData::NowForEndOfRun()); |
| 629 |
| 619 this_worker->set_running_sequence(SequenceToken()); | 630 this_worker->set_running_sequence(SequenceToken()); |
| 620 | 631 |
| 621 // Make sure our task is erased outside the lock for the same reason | 632 // Make sure our task is erased outside the lock for the same reason |
| 622 // we do this with delete_these_oustide_lock. | 633 // we do this with delete_these_oustide_lock. |
| 623 task.task = Closure(); | 634 task.task = Closure(); |
| 624 } | 635 } |
| 625 DidRunWorkerTask(task); // Must be done inside the lock. | 636 DidRunWorkerTask(task); // Must be done inside the lock. |
| 626 } else { | 637 } else { |
| 627 // When we're terminating and there's no more work, we can | 638 // When we're terminating and there's no more work, we can |
| 628 // shut down. You can't get more tasks posted once | 639 // shut down. You can't get more tasks posted once |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1001 void SequencedWorkerPool::SignalHasWorkForTesting() { | 1012 void SequencedWorkerPool::SignalHasWorkForTesting() { |
| 1002 inner_->SignalHasWorkForTesting(); | 1013 inner_->SignalHasWorkForTesting(); |
| 1003 } | 1014 } |
| 1004 | 1015 |
| 1005 void SequencedWorkerPool::Shutdown() { | 1016 void SequencedWorkerPool::Shutdown() { |
| 1006 DCHECK(constructor_message_loop_->BelongsToCurrentThread()); | 1017 DCHECK(constructor_message_loop_->BelongsToCurrentThread()); |
| 1007 inner_->Shutdown(); | 1018 inner_->Shutdown(); |
| 1008 } | 1019 } |
| 1009 | 1020 |
| 1010 } // namespace base | 1021 } // namespace base |
| OLD | NEW |