priority_queue.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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
  3. // found in the LICENSE file.
  4. #ifndef BASE_TASK_THREAD_POOL_PRIORITY_QUEUE_H_
  5. #define BASE_TASK_THREAD_POOL_PRIORITY_QUEUE_H_
  6. #include <memory>
  7. #include "base/base_export.h"
  8. #include "base/macros.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/task/common/checked_lock.h"
  11. #include "base/task/common/intrusive_heap.h"
  12. #include "base/task/thread_pool/task_source.h"
  13. #include "base/task/thread_pool/task_source_sort_key.h"
  14. namespace base {
  15. namespace internal {
  16. // A PriorityQueue holds TaskSources of Tasks. This class is not thread-safe
  17. // (requires external synchronization).
  18. class BASE_EXPORT PriorityQueue {
  19. public:
  20. PriorityQueue();
  21. ~PriorityQueue();
  22. PriorityQueue& operator=(PriorityQueue&& other);
  23. // Inserts |task_source| in the PriorityQueue with |task_source_sort_key|.
  24. void Push(TransactionWithRegisteredTaskSource transaction_with_task_source);
  25. // Returns a reference to the TaskSourceSortKey representing the priority of
  26. // the highest pending task in this PriorityQueue. The reference becomes
  27. // invalid the next time that this PriorityQueue is modified.
  28. // Cannot be called on an empty PriorityQueue.
  29. const TaskSourceSortKey& PeekSortKey() const;
  30. // Returns a reference to the highest priority TaskSource in this
  31. // PriorityQueue. Cannot be called on an empty PriorityQueue. The returned
  32. // task source may be modified as long as its sort key isn't affected.
  33. RegisteredTaskSource& PeekTaskSource() const;
  34. // Removes and returns the highest priority TaskSource in this PriorityQueue.
  35. // Cannot be called on an empty PriorityQueue.
  36. RegisteredTaskSource PopTaskSource();
  37. // Removes |task_source| from the PriorityQueue. Returns a
  38. // RegisteredTaskSource which evaluates to true if successful, or false if
  39. // |task_source| is not currently in the PriorityQueue or the PriorityQueue is
  40. // empty.
  41. RegisteredTaskSource RemoveTaskSource(const TaskSource& task_source);
  42. // Updates the sort key of |task_source| to |sort_key|, reordering
  43. // |task_source| in the queue if necessary. No-ops if the TaskSource is not in
  44. // the PriorityQueue or the PriorityQueue is empty.
  45. void UpdateSortKey(const TaskSource& task_source, TaskSourceSortKey sort_key);
  46. // Returns true if the PriorityQueue is empty.
  47. bool IsEmpty() const;
  48. // Returns the number of TaskSources in the PriorityQueue.
  49. size_t Size() const;
  50. // Returns the number of TaskSources with |priority|.
  51. size_t GetNumTaskSourcesWithPriority(TaskPriority priority) const {
  52. return num_task_sources_per_priority_[static_cast<int>(priority)];
  53. }
  54. // Set the PriorityQueue to empty all its TaskSources of Tasks when it is
  55. // destroyed; needed to prevent memory leaks caused by a reference cycle
  56. // (TaskSource -> Task -> TaskRunner -> TaskSource...) during test teardown.
  57. void EnableFlushTaskSourcesOnDestroyForTesting();
  58. private:
  59. // A class combining a TaskSource and the TaskSourceSortKey that determines
  60. // its position in a PriorityQueue.
  61. class TaskSourceAndSortKey;
  62. using ContainerType = IntrusiveHeap<TaskSourceAndSortKey>;
  63. void DecrementNumTaskSourcesForPriority(TaskPriority priority);
  64. void IncrementNumTaskSourcesForPriority(TaskPriority priority);
  65. ContainerType container_;
  66. std::array<size_t, static_cast<int>(TaskPriority::HIGHEST) + 1>
  67. num_task_sources_per_priority_ = {};
  68. // Should only be enabled by EnableFlushTaskSourcesOnDestroyForTesting().
  69. bool is_flush_task_sources_on_destroy_enabled_ = false;
  70. DISALLOW_COPY_AND_ASSIGN(PriorityQueue);
  71. };
  72. } // namespace internal
  73. } // namespace base
  74. #endif // BASE_TASK_THREAD_POOL_PRIORITY_QUEUE_H_