Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: base/task_scheduler/sequence.h

Issue 2399213005: TaskScheduler: Change Sequence::PeekTask to Sequence::TakeTask. (Closed)
Patch Set: CR robliao #5 Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/task_scheduler/scheduler_worker_unittest.cc ('k') | base/task_scheduler/sequence.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 slots each containing up to a single Task that must be
26 // executed in posting order.
27 //
28 // In comments below, an "empty Sequence" is a Sequence with no slot.
26 // 29 //
27 // Note: there is a known refcounted-ownership cycle in the Scheduler 30 // Note: there is a known refcounted-ownership cycle in the Scheduler
28 // architecture: Sequence -> Task -> TaskRunner -> Sequence -> ... 31 // architecture: Sequence -> Task -> TaskRunner -> Sequence -> ...
29 // This is okay so long as the other owners of Sequence (PriorityQueue and 32 // This is okay so long as the other owners of Sequence (PriorityQueue and
30 // SchedulerWorker in alternation and 33 // SchedulerWorker in alternation and
31 // SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::GetWork() 34 // SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::GetWork()
32 // temporarily) keep running it (and taking Tasks from it as a result). A 35 // 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 36 // 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 37 // 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 38 // 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 39 // 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). 40 // the caller that the Sequence should be re-enqueued for execution).
38 // 41 //
39 // This class is thread-safe. 42 // This class is thread-safe.
40 class BASE_EXPORT Sequence : public RefCountedThreadSafe<Sequence> { 43 class BASE_EXPORT Sequence : public RefCountedThreadSafe<Sequence> {
41 public: 44 public:
42 Sequence(); 45 Sequence();
43 46
44 // Adds |task| at the end of the sequence's queue. Returns true if the 47 // Adds |task| in a new slot at the end of the Sequence. Returns true if the
45 // sequence was empty before this operation. 48 // Sequence was empty before this operation.
46 bool PushTask(std::unique_ptr<Task> task); 49 bool PushTask(std::unique_ptr<Task> task);
47 50
48 // Returns the task in front of the sequence's queue, if any. 51 // Transfers ownership of the Task in the front slot of the Sequence to the
49 const Task* PeekTask() const; 52 // caller. The front slot of the Sequence will be nullptr and remain until
53 // Pop(). Cannot be called on an empty Sequence or a Sequence whose front slot
54 // is already nullptr.
55 std::unique_ptr<Task> TakeTask();
50 56
51 // Removes the task in front of the sequence's queue. Returns true if the 57 // 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 58 // called on an empty Sequence or on a Sequence whose front slot is empty.
53 // sequence. 59 TaskTraits PeekTaskTraits() const;
54 bool PopTask();
55 60
56 // Returns a SequenceSortKey representing the priority of the sequence. Cannot 61 // Removes the front slot of the Sequence. The front slot must have been
57 // be called on an empty sequence. 62 // emptied by TakeTask() before this is called. Cannot be called on an empty
63 // Sequence. Returns true if the Sequence is empty after this operation.
64 bool Pop();
65
66 // Returns a SequenceSortKey representing the priority of the Sequence. Cannot
67 // be called on an empty Sequence.
58 SequenceSortKey GetSortKey() const; 68 SequenceSortKey GetSortKey() const;
59 69
60 // Returns a token that uniquely identifies this Sequence. 70 // Returns a token that uniquely identifies this Sequence.
61 const SequenceToken& token() const { return token_; } 71 const SequenceToken& token() const { return token_; }
62 72
63 private: 73 private:
64 friend class RefCountedThreadSafe<Sequence>; 74 friend class RefCountedThreadSafe<Sequence>;
65 ~Sequence(); 75 ~Sequence();
66 76
67 const SequenceToken token_ = SequenceToken::Create(); 77 const SequenceToken token_ = SequenceToken::Create();
68 78
69 // Synchronizes access to all members. 79 // Synchronizes access to all members.
70 mutable SchedulerLock lock_; 80 mutable SchedulerLock lock_;
71 81
72 // Queue of tasks to execute. 82 // Queue of tasks to execute.
73 std::queue<std::unique_ptr<Task>> queue_; 83 std::queue<std::unique_ptr<Task>> queue_;
74 84
75 // Number of tasks contained in the sequence for each priority. 85 // Number of tasks contained in the Sequence for each priority.
76 size_t num_tasks_per_priority_[static_cast<int>(TaskPriority::HIGHEST) + 1] = 86 size_t num_tasks_per_priority_[static_cast<int>(TaskPriority::HIGHEST) + 1] =
77 {}; 87 {};
78 88
79 DISALLOW_COPY_AND_ASSIGN(Sequence); 89 DISALLOW_COPY_AND_ASSIGN(Sequence);
80 }; 90 };
81 91
82 } // namespace internal 92 } // namespace internal
83 } // namespace base 93 } // namespace base
84 94
85 #endif // BASE_TASK_SCHEDULER_SEQUENCE_H_ 95 #endif // BASE_TASK_SCHEDULER_SEQUENCE_H_
OLDNEW
« no previous file with comments | « base/task_scheduler/scheduler_worker_unittest.cc ('k') | base/task_scheduler/sequence.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698