Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef BASE_TASK_SCHEDULER_SEQUENCE_H_ | 5 #ifndef BASE_TASK_SCHEDULER_SEQUENCE_H_ |
| 6 #define BASE_TASK_SCHEDULER_SEQUENCE_H_ | 6 #define BASE_TASK_SCHEDULER_SEQUENCE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <queue> | 11 #include <queue> |
| 12 | 12 |
| 13 #include "base/base_export.h" | 13 #include "base/base_export.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/sequence_token.h" | 16 #include "base/sequence_token.h" |
| 17 #include "base/task_scheduler/scheduler_lock.h" | 17 #include "base/task_scheduler/scheduler_lock.h" |
| 18 #include "base/task_scheduler/sequence_sort_key.h" | 18 #include "base/task_scheduler/sequence_sort_key.h" |
| 19 #include "base/task_scheduler/task.h" | 19 #include "base/task_scheduler/task.h" |
| 20 #include "base/task_scheduler/task_traits.h" | 20 #include "base/task_scheduler/task_traits.h" |
| 21 | 21 |
| 22 namespace base { | 22 namespace base { |
| 23 namespace internal { | 23 namespace internal { |
| 24 | 24 |
| 25 // A sequence holds tasks that must be executed in posting order. | 25 // A Sequence holds tasks that must be executed in posting order. |
|
robliao
2016/10/07 20:57:11
A Sequence holds slots each containing up to a sin
fdoray
2016/10/11 12:29:19
Done.
| |
| 26 // | |
| 27 // PushTask() adds a task in a new slot at the end of the Sequence. TakeTask() | |
|
robliao
2016/10/07 20:57:12
This duplicates the method comments below. I would
fdoray
2016/10/11 12:29:19
Done.
| |
| 28 // transfers ownership of the task in the front slot of the Sequence to the | |
| 29 // caller, leaving the slot empty. Pop() removes the empty slot in front of the | |
| 30 // Sequence. | |
| 31 // | |
| 32 // In comments below, an "empty Sequence" is a Sequence with no slot. | |
| 26 // | 33 // |
| 27 // Note: there is a known refcounted-ownership cycle in the Scheduler | 34 // Note: there is a known refcounted-ownership cycle in the Scheduler |
| 28 // architecture: Sequence -> Task -> TaskRunner -> Sequence -> ... | 35 // architecture: Sequence -> Task -> TaskRunner -> Sequence -> ... |
| 29 // This is okay so long as the other owners of Sequence (PriorityQueue and | 36 // This is okay so long as the other owners of Sequence (PriorityQueue and |
| 30 // SchedulerWorker in alternation and | 37 // SchedulerWorker in alternation and |
| 31 // SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::GetWork() | 38 // SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::GetWork() |
| 32 // temporarily) keep running it (and taking Tasks from it as a result). A | 39 // temporarily) keep running it (and taking Tasks from it as a result). A |
| 33 // dangling reference cycle would only occur should they release their reference | 40 // dangling reference cycle would only occur should they release their reference |
| 34 // to it while it's not empty. In other words, it is only correct for them to | 41 // to it while it's not empty. In other words, it is only correct for them to |
| 35 // release it after PopTask() returns false to indicate it was made empty by | 42 // release it after PopTask() returns false to indicate it was made empty by |
| 36 // that call (in which case the next PushTask() will return true to indicate to | 43 // that call (in which case the next PushTask() will return true to indicate to |
| 37 // the caller that the Sequence should be re-enqueued for execution). | 44 // the caller that the Sequence should be re-enqueued for execution). |
| 38 // | 45 // |
| 39 // This class is thread-safe. | 46 // This class is thread-safe. |
| 40 class BASE_EXPORT Sequence : public RefCountedThreadSafe<Sequence> { | 47 class BASE_EXPORT Sequence : public RefCountedThreadSafe<Sequence> { |
| 41 public: | 48 public: |
| 42 Sequence(); | 49 Sequence(); |
| 43 | 50 |
| 44 // Adds |task| at the end of the sequence's queue. Returns true if the | 51 // Adds |task| in a new slot at the end of the Sequence. Returns true if the |
| 45 // sequence was empty before this operation. | 52 // Sequence was empty before this operation. |
| 46 bool PushTask(std::unique_ptr<Task> task); | 53 bool PushTask(std::unique_ptr<Task> task); |
| 47 | 54 |
| 48 // Returns the task in front of the sequence's queue, if any. | 55 // Transfers ownership of the Task in the front slot of the Sequence to the |
| 49 const Task* PeekTask() const; | 56 // caller. The front slot of the Sequence will be empty until Pop() is |
|
robliao
2016/10/07 20:57:12
Might be clearer to say the "front slot of the Seq
fdoray
2016/10/11 12:29:19
Done.
| |
| 57 // invoked. Cannot be called on an empty Sequence or a Sequence whose front | |
| 58 // slot is already empty. | |
| 59 std::unique_ptr<Task> TakeTask(); | |
| 50 | 60 |
| 51 // Removes the task in front of the sequence's queue. Returns true if the | 61 // Returns the TaskTraits of the Task in front of the Sequence. Cannot be |
| 52 // sequence is empty after this operation. Cannot be called on an empty | 62 // called on an empty Sequence or on a Sequence whose front slot is empty. |
| 53 // sequence. | 63 TaskTraits PeekTaskTraits() const; |
| 54 bool PopTask(); | |
| 55 | 64 |
| 56 // Returns a SequenceSortKey representing the priority of the sequence. Cannot | 65 // Removes the front slot of the Sequence. The front slot must have been |
| 57 // be called on an empty sequence. | 66 // emptied by TakeTask() before this is called. Cannot be called on an empty |
| 67 // Sequence. Returns true if the Sequence is empty after this operation. | |
| 68 bool Pop(); | |
| 69 | |
| 70 // Returns a SequenceSortKey representing the priority of the Sequence. Cannot | |
| 71 // be called on an empty Sequence. | |
| 58 SequenceSortKey GetSortKey() const; | 72 SequenceSortKey GetSortKey() const; |
| 59 | 73 |
| 60 // Returns a token that uniquely identifies this Sequence. | 74 // Returns a token that uniquely identifies this Sequence. |
| 61 const SequenceToken& token() const { return token_; } | 75 const SequenceToken& token() const { return token_; } |
| 62 | 76 |
| 63 private: | 77 private: |
| 64 friend class RefCountedThreadSafe<Sequence>; | 78 friend class RefCountedThreadSafe<Sequence>; |
| 65 ~Sequence(); | 79 ~Sequence(); |
| 66 | 80 |
| 67 const SequenceToken token_ = SequenceToken::Create(); | 81 const SequenceToken token_ = SequenceToken::Create(); |
| 68 | 82 |
| 69 // Synchronizes access to all members. | 83 // Synchronizes access to all members. |
| 70 mutable SchedulerLock lock_; | 84 mutable SchedulerLock lock_; |
| 71 | 85 |
| 72 // Queue of tasks to execute. | 86 // Queue of tasks to execute. |
| 73 std::queue<std::unique_ptr<Task>> queue_; | 87 std::queue<std::unique_ptr<Task>> queue_; |
| 74 | 88 |
| 75 // Number of tasks contained in the sequence for each priority. | 89 // Number of tasks contained in the Sequence for each priority. |
| 76 size_t num_tasks_per_priority_[static_cast<int>(TaskPriority::HIGHEST) + 1] = | 90 size_t num_tasks_per_priority_[static_cast<int>(TaskPriority::HIGHEST) + 1] = |
| 77 {}; | 91 {}; |
| 78 | 92 |
| 79 DISALLOW_COPY_AND_ASSIGN(Sequence); | 93 DISALLOW_COPY_AND_ASSIGN(Sequence); |
| 80 }; | 94 }; |
| 81 | 95 |
| 82 } // namespace internal | 96 } // namespace internal |
| 83 } // namespace base | 97 } // namespace base |
| 84 | 98 |
| 85 #endif // BASE_TASK_SCHEDULER_SEQUENCE_H_ | 99 #endif // BASE_TASK_SCHEDULER_SEQUENCE_H_ |
| OLD | NEW |