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