[Task] Add a second heap handle for delayed priority queue

Bug: 1127430
Change-Id: I96cb57dafc4378ac4bd4d4c7addcbf148e7fb83b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3846596
Commit-Queue: Abdias Dagbekpo <[email protected]>
Reviewed-by: Etienne Pierre-Doray <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1037927}
diff --git a/base/task/thread_pool/priority_queue.cc b/base/task/thread_pool/priority_queue.cc
index 729f836..bce20965 100644
--- a/base/task/thread_pool/priority_queue.cc
+++ b/base/task/thread_pool/priority_queue.cc
@@ -41,7 +41,7 @@
   // call.
   RegisteredTaskSource take_task_source() {
     DCHECK(task_source_);
-    task_source_->ClearHeapHandle();
+    task_source_->ClearImmediateHeapHandle();
     return std::move(task_source_);
   }
 
@@ -54,7 +54,7 @@
   // Required by IntrusiveHeap.
   void SetHeapHandle(const HeapHandle& handle) {
     DCHECK(task_source_);
-    task_source_->SetHeapHandle(handle);
+    task_source_->SetImmediateHeapHandle(handle);
   }
 
   // Required by IntrusiveHeap.
@@ -62,13 +62,13 @@
     // Ensure |task_source_| is not nullptr, which may be the case if
     // take_task_source() was called before this.
     if (task_source_)
-      task_source_->ClearHeapHandle();
+      task_source_->ClearImmediateHeapHandle();
   }
 
   // Required by IntrusiveHeap.
   HeapHandle GetHeapHandle() const {
     if (task_source_)
-      return task_source_->GetHeapHandle();
+      return task_source_->GetImmediateHeapHandle();
     return HeapHandle::Invalid();
   }
 
@@ -140,7 +140,7 @@
   if (IsEmpty())
     return nullptr;
 
-  const HeapHandle heap_handle = task_source.heap_handle();
+  const HeapHandle heap_handle = task_source.immediate_heap_handle();
   if (!heap_handle.IsValid())
     return nullptr;
 
@@ -162,7 +162,7 @@
   if (IsEmpty())
     return;
 
-  const HeapHandle heap_handle = task_source.heap_handle();
+  const HeapHandle heap_handle = task_source.immediate_heap_handle();
   if (!heap_handle.IsValid())
     return;
 
diff --git a/base/task/thread_pool/task_source.cc b/base/task/thread_pool/task_source.cc
index 41dd50a..7fd56922 100644
--- a/base/task/thread_pool/task_source.cc
+++ b/base/task/thread_pool/task_source.cc
@@ -38,12 +38,20 @@
                                      std::memory_order_relaxed);
 }
 
-void TaskSource::SetHeapHandle(const HeapHandle& handle) {
-  heap_handle_ = handle;
+void TaskSource::SetImmediateHeapHandle(const HeapHandle& handle) {
+  immediate_pq_heap_handle_ = handle;
 }
 
-void TaskSource::ClearHeapHandle() {
-  heap_handle_ = HeapHandle();
+void TaskSource::ClearImmediateHeapHandle() {
+  immediate_pq_heap_handle_ = HeapHandle();
+}
+
+void TaskSource::SetDelayedHeapHandle(const HeapHandle& handle) {
+  delayed_pq_heap_handle_ = handle;
+}
+
+void TaskSource::ClearDelayedHeapHandle() {
+  delayed_pq_heap_handle_ = HeapHandle();
 }
 
 TaskSource::TaskSource(const TaskTraits& traits,
diff --git a/base/task/thread_pool/task_source.h b/base/task/thread_pool/task_source.h
index 7be8157..70f9b00 100644
--- a/base/task/thread_pool/task_source.h
+++ b/base/task/thread_pool/task_source.h
@@ -153,12 +153,21 @@
   // TaskSource.
   virtual TimeTicks GetDelayedSortKey() const = 0;
 
-  // Support for IntrusiveHeap.
-  void SetHeapHandle(const HeapHandle& handle);
-  void ClearHeapHandle();
-  HeapHandle GetHeapHandle() const { return heap_handle_; }
+  // Support for IntrusiveHeap in ThreadGroup::PriorityQueue.
+  void SetImmediateHeapHandle(const HeapHandle& handle);
+  void ClearImmediateHeapHandle();
+  HeapHandle GetImmediateHeapHandle() const {
+    return immediate_pq_heap_handle_;
+  }
 
-  HeapHandle heap_handle() const { return heap_handle_; }
+  HeapHandle immediate_heap_handle() const { return immediate_pq_heap_handle_; }
+
+  // Support for IntrusiveHeap in ThreadGroup::DelayedPriorityQueue.
+  void SetDelayedHeapHandle(const HeapHandle& handle);
+  void ClearDelayedHeapHandle();
+  HeapHandle GetDelayedHeapHandle() const { return delayed_pq_heap_handle_; }
+
+  HeapHandle delayed_heap_handle() const { return delayed_pq_heap_handle_; }
 
   // Returns the shutdown behavior of all Tasks in the TaskSource. Can be
   // accessed without a Transaction because it is never mutated.
@@ -219,7 +228,11 @@
 
   // The TaskSource's position in its current PriorityQueue. Access is protected
   // by the PriorityQueue's lock.
-  HeapHandle heap_handle_;
+  HeapHandle immediate_pq_heap_handle_;
+
+  // The TaskSource's position in its current DelayedPriorityQueue. Access is
+  // protected by the DelayedPriorityQueue's lock.
+  HeapHandle delayed_pq_heap_handle_;
 
   // A pointer to the TaskRunner that posts to this TaskSource, if any. The
   // derived class is responsible for calling AddRef() when a TaskSource from
diff --git a/base/task/thread_pool/thread_group.cc b/base/task/thread_pool/thread_group.cc
index a16db32..f731e1d 100644
--- a/base/task/thread_pool/thread_group.cc
+++ b/base/task/thread_pool/thread_group.cc
@@ -170,7 +170,8 @@
     // Another worker that was running a task from this task source may have
     // reenqueued it already, in which case its heap_handle will be valid. It
     // shouldn't be queued twice so the task source registration is released.
-    if (transaction_with_task_source.task_source->heap_handle().IsValid()) {
+    if (transaction_with_task_source.task_source->immediate_heap_handle()
+            .IsValid()) {
       workers_executor->ScheduleReleaseTaskSource(
           std::move(transaction_with_task_source.task_source));
     } else {
@@ -248,7 +249,8 @@
   DCHECK_EQ(delegate_->GetThreadGroupForTraits(
                 transaction_with_task_source.transaction.traits()),
             this);
-  if (transaction_with_task_source.task_source->heap_handle().IsValid()) {
+  if (transaction_with_task_source.task_source->immediate_heap_handle()
+          .IsValid()) {
     // If the task source changed group, it is possible that multiple concurrent
     // workers try to enqueue it. Only the first enqueue should succeed.
     executor->ScheduleReleaseTaskSource(