sequence.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_SEQUENCE_H_
  5. #define BASE_TASK_THREAD_POOL_SEQUENCE_H_
  6. #include <stddef.h>
  7. #include "base/base_export.h"
  8. #include "base/compiler_specific.h"
  9. #include "base/containers/queue.h"
  10. #include "base/macros.h"
  11. #include "base/optional.h"
  12. #include "base/sequence_token.h"
  13. #include "base/task/task_traits.h"
  14. #include "base/task/thread_pool/pooled_parallel_task_runner.h"
  15. #include "base/task/thread_pool/task.h"
  16. #include "base/task/thread_pool/task_source.h"
  17. #include "base/task/thread_pool/task_source_sort_key.h"
  18. #include "base/threading/sequence_local_storage_map.h"
  19. namespace base {
  20. namespace internal {
  21. // A Sequence holds slots each containing up to a single Task that must be
  22. // executed in posting order.
  23. //
  24. // In comments below, an "empty Sequence" is a Sequence with no slot.
  25. //
  26. // Note: there is a known refcounted-ownership cycle in the Scheduler
  27. // architecture: Sequence -> Task -> TaskRunner -> Sequence -> ...
  28. // This is okay so long as the other owners of Sequence (PriorityQueue and
  29. // WorkerThread in alternation and
  30. // ThreadGroupImpl::WorkerThreadDelegateImpl::GetWork()
  31. // temporarily) keep running it (and taking Tasks from it as a result). A
  32. // dangling reference cycle would only occur should they release their reference
  33. // to it while it's not empty. In other words, it is only correct for them to
  34. // release it after PopTask() returns false to indicate it was made empty by
  35. // that call (in which case the next PushTask() will return true to indicate to
  36. // the caller that the Sequence should be re-enqueued for execution).
  37. //
  38. // This class is thread-safe.
  39. class BASE_EXPORT Sequence : public TaskSource {
  40. public:
  41. // A Transaction can perform multiple operations atomically on a
  42. // Sequence. While a Transaction is alive, it is guaranteed that nothing
  43. // else will access the Sequence; the Sequence's lock is held for the
  44. // lifetime of the Transaction.
  45. class BASE_EXPORT Transaction : public TaskSource::Transaction {
  46. public:
  47. Transaction(Transaction&& other);
  48. ~Transaction();
  49. // Returns true if the sequence would need to be queued after receiving a
  50. // new Task.
  51. bool WillPushTask() const WARN_UNUSED_RESULT;
  52. // Adds |task| in a new slot at the end of the Sequence. This must only be
  53. // called after invoking WillPushTask().
  54. void PushTask(Task task);
  55. Sequence* sequence() const { return static_cast<Sequence*>(task_source()); }
  56. private:
  57. friend class Sequence;
  58. explicit Transaction(Sequence* sequence);
  59. DISALLOW_COPY_AND_ASSIGN(Transaction);
  60. };
  61. // |traits| is metadata that applies to all Tasks in the Sequence.
  62. // |task_runner| is a reference to the TaskRunner feeding this TaskSource.
  63. // |task_runner| can be nullptr only for tasks with no TaskRunner, in which
  64. // case |execution_mode| must be kParallel. Otherwise, |execution_mode| is the
  65. // execution mode of |task_runner|.
  66. Sequence(const TaskTraits& traits,
  67. TaskRunner* task_runner,
  68. TaskSourceExecutionMode execution_mode);
  69. // Begins a Transaction. This method cannot be called on a thread which has an
  70. // active Sequence::Transaction.
  71. Transaction BeginTransaction() WARN_UNUSED_RESULT;
  72. // TaskSource:
  73. ExecutionEnvironment GetExecutionEnvironment() override;
  74. size_t GetRemainingConcurrency() const override;
  75. TaskSourceSortKey GetSortKey() const override;
  76. // Returns a token that uniquely identifies this Sequence.
  77. const SequenceToken& token() const { return token_; }
  78. SequenceLocalStorageMap* sequence_local_storage() {
  79. return &sequence_local_storage_;
  80. }
  81. private:
  82. ~Sequence() override;
  83. // TaskSource:
  84. RunStatus WillRunTask() override;
  85. Task TakeTask(TaskSource::Transaction* transaction) override;
  86. Task Clear(TaskSource::Transaction* transaction) override;
  87. bool DidProcessTask(TaskSource::Transaction* transaction) override;
  88. // Releases reference to TaskRunner.
  89. void ReleaseTaskRunner();
  90. const SequenceToken token_ = SequenceToken::Create();
  91. // Queue of tasks to execute.
  92. base::queue<Task> queue_;
  93. std::atomic<TimeTicks> ready_time_{TimeTicks()};
  94. // True if a worker is currently associated with a Task from this Sequence.
  95. bool has_worker_ = false;
  96. // Holds data stored through the SequenceLocalStorageSlot API.
  97. SequenceLocalStorageMap sequence_local_storage_;
  98. DISALLOW_COPY_AND_ASSIGN(Sequence);
  99. };
  100. } // namespace internal
  101. } // namespace base
  102. #endif // BASE_TASK_THREAD_POOL_SEQUENCE_H_