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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <list> | 9 #include <list> |
10 #include <map> | 10 #include <map> |
11 #include <memory> | 11 #include <memory> |
12 #include <set> | 12 #include <set> |
13 #include <unordered_map> | |
13 #include <utility> | 14 #include <utility> |
14 #include <vector> | 15 #include <vector> |
15 | 16 |
16 #include "base/atomic_sequence_num.h" | 17 #include "base/atomic_sequence_num.h" |
17 #include "base/callback.h" | 18 #include "base/callback.h" |
18 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
19 #include "base/critical_closure.h" | 20 #include "base/critical_closure.h" |
20 #include "base/lazy_instance.h" | 21 #include "base/lazy_instance.h" |
21 #include "base/logging.h" | 22 #include "base/logging.h" |
22 #include "base/macros.h" | 23 #include "base/macros.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
45 #endif | 46 #endif |
46 | 47 |
47 #if !defined(OS_NACL) | 48 #if !defined(OS_NACL) |
48 #include "base/metrics/histogram.h" | 49 #include "base/metrics/histogram.h" |
49 #endif | 50 #endif |
50 | 51 |
51 namespace base { | 52 namespace base { |
52 | 53 |
53 namespace { | 54 namespace { |
54 | 55 |
55 // An enum representing the state of all pools. Any given process should only | 56 // An enum representing the state of all pools. Any given non-test process |
56 // ever transition from NONE_ACTIVE to the active states, transitions between | 57 // should only ever transition from NONE_ACTIVE to the active states, |
57 // actives states are unexpected. The REDIRECTED_TO_TASK_SCHEDULER transition | 58 // transitions between actives states are unexpected. The |
58 // occurs when RedirectSequencedWorkerPoolsToTaskSchedulerForProcess() is called | 59 // REDIRECTED_TO_TASK_SCHEDULER transition occurs when |
59 // and the WORKER_CREATED transition occurs when a Worker needs to be created | 60 // RedirectToTaskSchedulerForProcess() is called and the WORKER_CREATED |
60 // because the first task was posted and the state is still NONE_ACTIVE. | 61 // transition occurs when a Worker needs to be created because the first task |
62 // was posted and the state is still NONE_ACTIVE. In a test process, | |
63 // ResetRedirectToTaskSchedulerForProcessForTesting() causes a transition to | |
64 // the NONE_ACTIVE state. | |
61 // TODO(gab): Remove this if http://crbug.com/622400 fails (SequencedWorkerPool | 65 // TODO(gab): Remove this if http://crbug.com/622400 fails (SequencedWorkerPool |
62 // will be phased out completely otherwise). | 66 // will be phased out completely otherwise). |
63 enum class AllPoolsState { | 67 enum class AllPoolsState { |
64 NONE_ACTIVE, | 68 NONE_ACTIVE, |
65 WORKER_CREATED, | 69 WORKER_CREATED, |
66 REDIRECTED_TO_TASK_SCHEDULER, | 70 REDIRECTED_TO_TASK_SCHEDULER, |
67 } g_all_pools_state = AllPoolsState::NONE_ACTIVE; | 71 } g_all_pools_state = AllPoolsState::NONE_ACTIVE; |
68 | 72 |
69 struct SequencedTask : public TrackingInfo { | 73 struct SequencedTask : public TrackingInfo { |
70 SequencedTask() | 74 SequencedTask() |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
363 }; | 367 }; |
364 | 368 |
365 enum CleanupState { | 369 enum CleanupState { |
366 CLEANUP_REQUESTED, | 370 CLEANUP_REQUESTED, |
367 CLEANUP_STARTING, | 371 CLEANUP_STARTING, |
368 CLEANUP_RUNNING, | 372 CLEANUP_RUNNING, |
369 CLEANUP_FINISHING, | 373 CLEANUP_FINISHING, |
370 CLEANUP_DONE, | 374 CLEANUP_DONE, |
371 }; | 375 }; |
372 | 376 |
377 struct TaskRunnerAndShutdownBehavior { | |
378 scoped_refptr<TaskRunner> task_runner; | |
379 TaskShutdownBehavior shutdown_behavior; | |
380 }; | |
381 | |
382 bool RunsTasksOnCurrentThreadAssertSynchronized() const; | |
383 | |
373 // Helper used by PostTask() to complete the work when redirection is on. | 384 // Helper used by PostTask() to complete the work when redirection is on. |
374 // Coalesce upon resolution of http://crbug.com/622400. | 385 // Coalesce upon resolution of http://crbug.com/622400. |
375 void PostTaskToTaskScheduler(const SequencedTask& sequenced); | 386 bool PostTaskToTaskScheduler(const SequencedTask& sequenced, |
387 const TimeDelta& delay); | |
376 | 388 |
377 // Called from within the lock, this converts the given token name into a | 389 // Called from within the lock, this converts the given token name into a |
378 // token ID, creating a new one if necessary. | 390 // token ID, creating a new one if necessary. |
379 int LockedGetNamedTokenID(const std::string& name); | 391 int LockedGetNamedTokenID(const std::string& name); |
380 | 392 |
381 // Called from within the lock, this returns the next sequence task number. | 393 // Called from within the lock, this returns the next sequence task number. |
382 int64_t LockedGetNextSequenceTaskNumber(); | 394 int64_t LockedGetNextSequenceTaskNumber(); |
383 | 395 |
384 // Gets new task. There are 3 cases depending on the return value: | 396 // Gets new task. There are 3 cases depending on the return value: |
385 // | 397 // |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
524 | 536 |
525 // Members below are used for the experimental redirection to TaskScheduler. | 537 // Members below are used for the experimental redirection to TaskScheduler. |
526 // TODO(gab): Remove these if http://crbug.com/622400 fails | 538 // TODO(gab): Remove these if http://crbug.com/622400 fails |
527 // (SequencedWorkerPool will be phased out completely otherwise). | 539 // (SequencedWorkerPool will be phased out completely otherwise). |
528 | 540 |
529 // The TaskPriority to be used for SequencedWorkerPool tasks redirected to the | 541 // The TaskPriority to be used for SequencedWorkerPool tasks redirected to the |
530 // TaskScheduler as an experiment (unused otherwise). | 542 // TaskScheduler as an experiment (unused otherwise). |
531 const base::TaskPriority task_priority_; | 543 const base::TaskPriority task_priority_; |
532 | 544 |
533 // A map of SequenceToken IDs to TaskScheduler TaskRunners used to redirect | 545 // A map of SequenceToken IDs to TaskScheduler TaskRunners used to redirect |
534 // SequencedWorkerPool usage to the TaskScheduler. | 546 // sequenced tasks to the TaskScheduler. |
535 std::map<int, scoped_refptr<TaskRunner>> sequenced_task_runner_map_; | 547 std::unordered_map<int, TaskRunnerAndShutdownBehavior> |
548 sequenced_task_runner_map_; | |
549 | |
550 // TaskScheduler TaskRunners to redirect unsequenced tasks to the | |
551 // TaskScheduler. | |
552 std::unordered_map<TaskShutdownBehavior, scoped_refptr<TaskRunner>> | |
553 unsequenced_task_runners_; | |
536 | 554 |
537 // A dummy TaskRunner obtained from TaskScheduler with the same TaskTraits as | 555 // A dummy TaskRunner obtained from TaskScheduler with the same TaskTraits as |
538 // used by this SequencedWorkerPool to query for RunsTasksOnCurrentThread(). | 556 // used by this SequencedWorkerPool to query for RunsTasksOnCurrentThread(). |
539 // Mutable so it can be lazily instantiated from RunsTasksOnCurrentThread(). | 557 // Mutable so it can be lazily instantiated from RunsTasksOnCurrentThread(). |
540 mutable scoped_refptr<TaskRunner> runs_tasks_on_verifier_; | 558 mutable scoped_refptr<TaskRunner> runs_tasks_on_verifier_; |
541 | 559 |
542 DISALLOW_COPY_AND_ASSIGN(Inner); | 560 DISALLOW_COPY_AND_ASSIGN(Inner); |
543 }; | 561 }; |
544 | 562 |
545 // Worker definitions --------------------------------------------------------- | 563 // Worker definitions --------------------------------------------------------- |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
660 DCHECK(delay.is_zero() || shutdown_behavior == SKIP_ON_SHUTDOWN); | 678 DCHECK(delay.is_zero() || shutdown_behavior == SKIP_ON_SHUTDOWN); |
661 SequencedTask sequenced(from_here); | 679 SequencedTask sequenced(from_here); |
662 sequenced.sequence_token_id = sequence_token.id_; | 680 sequenced.sequence_token_id = sequence_token.id_; |
663 sequenced.shutdown_behavior = shutdown_behavior; | 681 sequenced.shutdown_behavior = shutdown_behavior; |
664 sequenced.posted_from = from_here; | 682 sequenced.posted_from = from_here; |
665 sequenced.task = | 683 sequenced.task = |
666 shutdown_behavior == BLOCK_SHUTDOWN ? | 684 shutdown_behavior == BLOCK_SHUTDOWN ? |
667 base::MakeCriticalClosure(task) : task; | 685 base::MakeCriticalClosure(task) : task; |
668 sequenced.time_to_run = TimeTicks::Now() + delay; | 686 sequenced.time_to_run = TimeTicks::Now() + delay; |
669 | 687 |
688 bool task_successfully_posted = true; | |
689 | |
670 int create_thread_id = 0; | 690 int create_thread_id = 0; |
671 { | 691 { |
672 AutoLock lock(lock_); | 692 AutoLock lock(lock_); |
673 | 693 |
674 if (shutdown_called_) { | 694 if (shutdown_called_ && |
695 g_all_pools_state != AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) { | |
675 // Don't allow a new task to be posted if it doesn't block shutdown. | 696 // Don't allow a new task to be posted if it doesn't block shutdown. |
676 if (shutdown_behavior != BLOCK_SHUTDOWN) | 697 if (shutdown_behavior != BLOCK_SHUTDOWN) |
677 return false; | 698 return false; |
678 | 699 |
679 // If the current thread is running a task, and that task doesn't block | 700 // If the current thread is running a task, and that task doesn't block |
680 // shutdown, then it shouldn't be allowed to post any more tasks. | 701 // shutdown, then it shouldn't be allowed to post any more tasks. |
681 ThreadMap::const_iterator found = | 702 ThreadMap::const_iterator found = |
682 threads_.find(PlatformThread::CurrentId()); | 703 threads_.find(PlatformThread::CurrentId()); |
683 if (found != threads_.end() && found->second->is_processing_task() && | 704 if (found != threads_.end() && found->second->is_processing_task() && |
684 found->second->task_shutdown_behavior() != BLOCK_SHUTDOWN) { | 705 found->second->task_shutdown_behavior() != BLOCK_SHUTDOWN) { |
(...skipping 15 matching lines...) Expand all Loading... | |
700 TRACE_ID_MANGLE(GetTaskTraceID(sequenced, static_cast<void*>(this))), | 721 TRACE_ID_MANGLE(GetTaskTraceID(sequenced, static_cast<void*>(this))), |
701 TRACE_EVENT_FLAG_FLOW_OUT); | 722 TRACE_EVENT_FLAG_FLOW_OUT); |
702 | 723 |
703 sequenced.sequence_task_number = LockedGetNextSequenceTaskNumber(); | 724 sequenced.sequence_task_number = LockedGetNextSequenceTaskNumber(); |
704 | 725 |
705 // Now that we have the lock, apply the named token rules. | 726 // Now that we have the lock, apply the named token rules. |
706 if (optional_token_name) | 727 if (optional_token_name) |
707 sequenced.sequence_token_id = LockedGetNamedTokenID(*optional_token_name); | 728 sequenced.sequence_token_id = LockedGetNamedTokenID(*optional_token_name); |
708 | 729 |
709 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) { | 730 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) { |
710 PostTaskToTaskScheduler(sequenced); | 731 task_successfully_posted = PostTaskToTaskScheduler(sequenced, delay); |
711 } else { | 732 } else { |
712 pending_tasks_.insert(sequenced); | 733 pending_tasks_.insert(sequenced); |
713 | 734 |
714 if (sequenced.shutdown_behavior == BLOCK_SHUTDOWN) | 735 if (sequenced.shutdown_behavior == BLOCK_SHUTDOWN) |
715 blocking_shutdown_pending_task_count_++; | 736 blocking_shutdown_pending_task_count_++; |
716 | 737 |
717 create_thread_id = PrepareToStartAdditionalThreadIfHelpful(); | 738 create_thread_id = PrepareToStartAdditionalThreadIfHelpful(); |
718 } | 739 } |
719 } | 740 } |
720 | 741 |
(...skipping 13 matching lines...) Expand all Loading... | |
734 // intended for one of them at runtime, confirm exclusive usage here. | 755 // intended for one of them at runtime, confirm exclusive usage here. |
735 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) { | 756 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) { |
736 DCHECK(pending_tasks_.empty()); | 757 DCHECK(pending_tasks_.empty()); |
737 DCHECK_EQ(0, create_thread_id); | 758 DCHECK_EQ(0, create_thread_id); |
738 } else { | 759 } else { |
739 DCHECK(sequenced_task_runner_map_.empty()); | 760 DCHECK(sequenced_task_runner_map_.empty()); |
740 } | 761 } |
741 } | 762 } |
742 #endif // DCHECK_IS_ON() | 763 #endif // DCHECK_IS_ON() |
743 | 764 |
744 return true; | 765 return task_successfully_posted; |
745 } | 766 } |
746 | 767 |
747 void SequencedWorkerPool::Inner::PostTaskToTaskScheduler( | 768 bool SequencedWorkerPool::Inner::RunsTasksOnCurrentThreadAssertSynchronized() |
748 const SequencedTask& sequenced) { | 769 const { |
770 lock_.AssertAcquired(); | |
771 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) { | |
772 // TODO(fdoray): Add a special case for single-threaded pools. | |
773 if (!runs_tasks_on_verifier_) { | |
774 runs_tasks_on_verifier_ = CreateTaskRunnerWithTraits( | |
775 TaskTraits().WithFileIO().WithPriority(task_priority_), | |
776 ExecutionMode::PARALLEL); | |
777 } | |
778 return runs_tasks_on_verifier_->RunsTasksOnCurrentThread(); | |
779 } else { | |
robliao
2016/08/29 23:28:50
Nit: Remove else
fdoray
2016/08/30 17:20:49
Done.
| |
780 return ContainsKey(threads_, PlatformThread::CurrentId()); | |
781 } | |
782 } | |
783 | |
784 bool SequencedWorkerPool::Inner::PostTaskToTaskScheduler( | |
785 const SequencedTask& sequenced, | |
786 const TimeDelta& delay) { | |
749 DCHECK_EQ(AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER, g_all_pools_state); | 787 DCHECK_EQ(AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER, g_all_pools_state); |
750 | 788 |
751 lock_.AssertAcquired(); | 789 lock_.AssertAcquired(); |
752 | 790 |
753 // Confirm that the TaskScheduler's shutdown behaviors use the same | 791 // Confirm that the TaskScheduler's shutdown behaviors use the same |
754 // underlying values as SequencedWorkerPool. | 792 // underlying values as SequencedWorkerPool. |
755 static_assert( | 793 static_assert( |
756 static_cast<int>(TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) == | 794 static_cast<int>(TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN) == |
757 static_cast<int>(CONTINUE_ON_SHUTDOWN), | 795 static_cast<int>(CONTINUE_ON_SHUTDOWN), |
758 "TaskShutdownBehavior and WorkerShutdown enum mismatch for " | 796 "TaskShutdownBehavior and WorkerShutdown enum mismatch for " |
759 "CONTINUE_ON_SHUTDOWN."); | 797 "CONTINUE_ON_SHUTDOWN."); |
760 static_assert(static_cast<int>(TaskShutdownBehavior::SKIP_ON_SHUTDOWN) == | 798 static_assert(static_cast<int>(TaskShutdownBehavior::SKIP_ON_SHUTDOWN) == |
761 static_cast<int>(SKIP_ON_SHUTDOWN), | 799 static_cast<int>(SKIP_ON_SHUTDOWN), |
762 "TaskShutdownBehavior and WorkerShutdown enum mismatch for " | 800 "TaskShutdownBehavior and WorkerShutdown enum mismatch for " |
763 "SKIP_ON_SHUTDOWN."); | 801 "SKIP_ON_SHUTDOWN."); |
764 static_assert(static_cast<int>(TaskShutdownBehavior::BLOCK_SHUTDOWN) == | 802 static_assert(static_cast<int>(TaskShutdownBehavior::BLOCK_SHUTDOWN) == |
765 static_cast<int>(BLOCK_SHUTDOWN), | 803 static_cast<int>(BLOCK_SHUTDOWN), |
766 "TaskShutdownBehavior and WorkerShutdown enum mismatch for " | 804 "TaskShutdownBehavior and WorkerShutdown enum mismatch for " |
767 "BLOCK_SHUTDOWN."); | 805 "BLOCK_SHUTDOWN."); |
768 | 806 |
769 const TaskShutdownBehavior task_shutdown_behavior = | 807 const TaskShutdownBehavior task_shutdown_behavior = |
770 static_cast<TaskShutdownBehavior>(sequenced.shutdown_behavior); | 808 static_cast<TaskShutdownBehavior>(sequenced.shutdown_behavior); |
771 const TaskTraits pool_traits = | 809 const TaskTraits pool_traits = |
772 TaskTraits() | 810 TaskTraits() |
773 .WithFileIO() | 811 .WithFileIO() |
774 .WithPriority(task_priority_) | 812 .WithPriority(task_priority_) |
775 .WithShutdownBehavior(task_shutdown_behavior); | 813 .WithShutdownBehavior(task_shutdown_behavior); |
776 | 814 |
777 // Find or create the TaskScheduler TaskRunner to redirect this task to if | 815 // Find or create the TaskScheduler TaskRunner to redirect this task to. |
778 // it is posted to a specific sequence. | 816 scoped_refptr<TaskRunner> task_runner; |
779 scoped_refptr<TaskRunner>* sequenced_task_runner = nullptr; | |
780 if (sequenced.sequence_token_id) { | 817 if (sequenced.sequence_token_id) { |
781 sequenced_task_runner = | 818 TaskRunnerAndShutdownBehavior& task_runner_and_shutdown_behavior = |
782 &sequenced_task_runner_map_[sequenced.sequence_token_id]; | 819 sequenced_task_runner_map_[sequenced.sequence_token_id]; |
783 if (!*sequenced_task_runner) { | 820 if (!task_runner_and_shutdown_behavior.task_runner) { |
784 const ExecutionMode execution_mode = | 821 const ExecutionMode execution_mode = |
785 max_threads_ == 1U ? ExecutionMode::SINGLE_THREADED | 822 max_threads_ == 1U ? ExecutionMode::SINGLE_THREADED |
786 : ExecutionMode::SEQUENCED; | 823 : ExecutionMode::SEQUENCED; |
787 *sequenced_task_runner = | 824 task_runner_and_shutdown_behavior.task_runner = |
825 CreateTaskRunnerWithTraits(pool_traits, execution_mode); | |
826 task_runner_and_shutdown_behavior.shutdown_behavior = | |
827 task_shutdown_behavior; | |
828 } | |
829 | |
830 // Posting tasks to the same sequence with different shutdown behaviors | |
831 // isn't supported by the TaskScheduler. | |
832 DCHECK_EQ(task_shutdown_behavior, | |
833 task_runner_and_shutdown_behavior.shutdown_behavior); | |
834 | |
835 task_runner = task_runner_and_shutdown_behavior.task_runner; | |
836 } else { | |
837 scoped_refptr<TaskRunner>& task_runner_for_shutdown_behavior_ref = | |
838 unsequenced_task_runners_[task_shutdown_behavior]; | |
839 if (!task_runner_for_shutdown_behavior_ref) { | |
840 const ExecutionMode execution_mode = max_threads_ == 1U | |
841 ? ExecutionMode::SINGLE_THREADED | |
842 : ExecutionMode::PARALLEL; | |
843 task_runner_for_shutdown_behavior_ref = | |
788 CreateTaskRunnerWithTraits(pool_traits, execution_mode); | 844 CreateTaskRunnerWithTraits(pool_traits, execution_mode); |
789 } | 845 } |
846 | |
847 task_runner = task_runner_for_shutdown_behavior_ref; | |
790 } | 848 } |
791 | 849 |
792 if (sequenced_task_runner) { | 850 // Single-threaded pools can legitimatelly assume thread affinity. Disallow |
robliao
2016/08/29 23:28:50
Nit: s/legitimatelly/legitimately/
fdoray
2016/08/30 17:20:49
Done.
| |
793 (*sequenced_task_runner) | 851 // posting tasks with different sequence tokens or shutdown behaviors in such |
794 ->PostTask(sequenced.posted_from, sequenced.task); | 852 // pools since the TaskScheduler can't force them to run on the same thread. |
795 } else { | 853 DCHECK(max_threads_ > 1 || |
796 // PostTaskWithTraits() posts a task with PARALLEL semantics. There are | 854 (unsequenced_task_runners_.size() + |
robliao
2016/08/29 23:28:50
A caller could plausibly call GetTaskRunnerWithShu
fdoray
2016/08/30 17:20:49
No, we shouldn't disallow that. Done.
| |
797 // however a few pools that use only one thread and therefore can currently | 855 sequenced_task_runner_map_.size()) == 1); |
798 // legitimatelly assume thread affinity despite using SequencedWorkerPool. | 856 |
799 // Such pools typically only give access to their TaskRunner which will be | 857 return task_runner->PostDelayedTask(sequenced.posted_from, sequenced.task, |
800 // SINGLE_THREADED per nature of the pool having only one thread but this | 858 delay); |
801 // DCHECK ensures no such pools use SequencedWorkerPool::PostTask() | |
802 // directly. | |
803 DCHECK_GT(max_threads_, 1U); | |
804 base::PostTaskWithTraits(sequenced.posted_from, pool_traits, | |
805 sequenced.task); | |
806 } | |
807 } | 859 } |
808 | 860 |
809 bool SequencedWorkerPool::Inner::RunsTasksOnCurrentThread() const { | 861 bool SequencedWorkerPool::Inner::RunsTasksOnCurrentThread() const { |
810 AutoLock lock(lock_); | 862 AutoLock lock(lock_); |
811 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) { | 863 return RunsTasksOnCurrentThreadAssertSynchronized(); |
812 if (!runs_tasks_on_verifier_) { | |
813 runs_tasks_on_verifier_ = CreateTaskRunnerWithTraits( | |
814 TaskTraits().WithFileIO().WithPriority(task_priority_), | |
815 ExecutionMode::PARALLEL); | |
816 } | |
817 return runs_tasks_on_verifier_->RunsTasksOnCurrentThread(); | |
818 } else { | |
819 return ContainsKey(threads_, PlatformThread::CurrentId()); | |
820 } | |
821 } | 864 } |
822 | 865 |
823 bool SequencedWorkerPool::Inner::IsRunningSequenceOnCurrentThread( | 866 bool SequencedWorkerPool::Inner::IsRunningSequenceOnCurrentThread( |
824 SequenceToken sequence_token) const { | 867 SequenceToken sequence_token) const { |
825 AutoLock lock(lock_); | 868 AutoLock lock(lock_); |
826 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) { | 869 if (g_all_pools_state == AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER) { |
870 if (!sequence_token.IsValid()) | |
871 return RunsTasksOnCurrentThreadAssertSynchronized(); | |
872 | |
827 // TODO(gab): This currently only verifies that the current thread is a | 873 // TODO(gab): This currently only verifies that the current thread is a |
828 // thread on which a task bound to |sequence_token| *could* run, but it | 874 // thread on which a task bound to |sequence_token| *could* run, but it |
829 // doesn't verify that the current is *currently running* a task bound to | 875 // doesn't verify that the current is *currently running* a task bound to |
830 // |sequence_token|. | 876 // |sequence_token|. |
831 const auto sequenced_task_runner_it = | 877 const auto sequenced_task_runner_it = |
832 sequenced_task_runner_map_.find(sequence_token.id_); | 878 sequenced_task_runner_map_.find(sequence_token.id_); |
833 return sequenced_task_runner_it != sequenced_task_runner_map_.end() && | 879 return sequenced_task_runner_it != sequenced_task_runner_map_.end() && |
834 sequenced_task_runner_it->second->RunsTasksOnCurrentThread(); | 880 sequenced_task_runner_it->second.task_runner |
881 ->RunsTasksOnCurrentThread(); | |
835 } else { | 882 } else { |
836 ThreadMap::const_iterator found = | 883 ThreadMap::const_iterator found = |
837 threads_.find(PlatformThread::CurrentId()); | 884 threads_.find(PlatformThread::CurrentId()); |
838 if (found == threads_.end()) | 885 if (found == threads_.end()) |
839 return false; | 886 return false; |
840 return found->second->is_processing_task() && | 887 return found->second->is_processing_task() && |
841 sequence_token.Equals(found->second->task_sequence_token()); | 888 sequence_token.Equals(found->second->task_sequence_token()); |
842 } | 889 } |
843 } | 890 } |
844 | 891 |
(...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1371 scoped_refptr<SequencedWorkerPool> | 1418 scoped_refptr<SequencedWorkerPool> |
1372 SequencedWorkerPool::GetWorkerPoolForCurrentThread() { | 1419 SequencedWorkerPool::GetWorkerPoolForCurrentThread() { |
1373 Worker* worker = Worker::GetForCurrentThread(); | 1420 Worker* worker = Worker::GetForCurrentThread(); |
1374 if (!worker) | 1421 if (!worker) |
1375 return nullptr; | 1422 return nullptr; |
1376 | 1423 |
1377 return worker->worker_pool(); | 1424 return worker->worker_pool(); |
1378 } | 1425 } |
1379 | 1426 |
1380 // static | 1427 // static |
1381 void SequencedWorkerPool:: | 1428 void SequencedWorkerPool::RedirectToTaskSchedulerForProcess() { |
1382 RedirectSequencedWorkerPoolsToTaskSchedulerForProcess() { | |
1383 DCHECK(TaskScheduler::GetInstance()); | 1429 DCHECK(TaskScheduler::GetInstance()); |
1384 // Hitting this DCHECK indicates that a task was posted to a | 1430 // Hitting this DCHECK indicates that a task was posted to a |
1385 // SequencedWorkerPool before the TaskScheduler was initialized and | 1431 // SequencedWorkerPool before the TaskScheduler was initialized and |
1386 // redirected, posting task to SequencedWorkerPools needs to at least be | 1432 // redirected, posting task to SequencedWorkerPools needs to at least be |
1387 // delayed until after that point. | 1433 // delayed until after that point. |
1388 DCHECK_EQ(AllPoolsState::NONE_ACTIVE, g_all_pools_state); | 1434 DCHECK_EQ(AllPoolsState::NONE_ACTIVE, g_all_pools_state); |
1389 g_all_pools_state = AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER; | 1435 g_all_pools_state = AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER; |
1390 } | 1436 } |
1391 | 1437 |
1438 // static | |
1439 void SequencedWorkerPool::ResetRedirectToTaskSchedulerForProcessForTesting() { | |
1440 g_all_pools_state = AllPoolsState::NONE_ACTIVE; | |
1441 } | |
1442 | |
1392 SequencedWorkerPool::SequencedWorkerPool(size_t max_threads, | 1443 SequencedWorkerPool::SequencedWorkerPool(size_t max_threads, |
1393 const std::string& thread_name_prefix, | 1444 const std::string& thread_name_prefix, |
1394 base::TaskPriority task_priority) | 1445 base::TaskPriority task_priority) |
1395 : constructor_task_runner_(ThreadTaskRunnerHandle::Get()), | 1446 : constructor_task_runner_(ThreadTaskRunnerHandle::Get()), |
1396 inner_(new Inner(this, | 1447 inner_(new Inner(this, |
1397 max_threads, | 1448 max_threads, |
1398 thread_name_prefix, | 1449 thread_name_prefix, |
1399 task_priority, | 1450 task_priority, |
1400 NULL)) {} | 1451 NULL)) {} |
1401 | 1452 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1521 bool SequencedWorkerPool::RunsTasksOnCurrentThread() const { | 1572 bool SequencedWorkerPool::RunsTasksOnCurrentThread() const { |
1522 return inner_->RunsTasksOnCurrentThread(); | 1573 return inner_->RunsTasksOnCurrentThread(); |
1523 } | 1574 } |
1524 | 1575 |
1525 bool SequencedWorkerPool::IsRunningSequenceOnCurrentThread( | 1576 bool SequencedWorkerPool::IsRunningSequenceOnCurrentThread( |
1526 SequenceToken sequence_token) const { | 1577 SequenceToken sequence_token) const { |
1527 return inner_->IsRunningSequenceOnCurrentThread(sequence_token); | 1578 return inner_->IsRunningSequenceOnCurrentThread(sequence_token); |
1528 } | 1579 } |
1529 | 1580 |
1530 void SequencedWorkerPool::FlushForTesting() { | 1581 void SequencedWorkerPool::FlushForTesting() { |
1582 DCHECK_NE(AllPoolsState::REDIRECTED_TO_TASK_SCHEDULER, g_all_pools_state); | |
1531 inner_->CleanupForTesting(); | 1583 inner_->CleanupForTesting(); |
1532 } | 1584 } |
1533 | 1585 |
1534 void SequencedWorkerPool::SignalHasWorkForTesting() { | 1586 void SequencedWorkerPool::SignalHasWorkForTesting() { |
1535 inner_->SignalHasWorkForTesting(); | 1587 inner_->SignalHasWorkForTesting(); |
1536 } | 1588 } |
1537 | 1589 |
1538 void SequencedWorkerPool::Shutdown(int max_new_blocking_tasks_after_shutdown) { | 1590 void SequencedWorkerPool::Shutdown(int max_new_blocking_tasks_after_shutdown) { |
1539 DCHECK(constructor_task_runner_->BelongsToCurrentThread()); | 1591 DCHECK(constructor_task_runner_->BelongsToCurrentThread()); |
1540 inner_->Shutdown(max_new_blocking_tasks_after_shutdown); | 1592 inner_->Shutdown(max_new_blocking_tasks_after_shutdown); |
1541 } | 1593 } |
1542 | 1594 |
1543 bool SequencedWorkerPool::IsShutdownInProgress() { | 1595 bool SequencedWorkerPool::IsShutdownInProgress() { |
1544 return inner_->IsShutdownInProgress(); | 1596 return inner_->IsShutdownInProgress(); |
1545 } | 1597 } |
1546 | 1598 |
1547 } // namespace base | 1599 } // namespace base |
OLD | NEW |