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

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

Issue 2399213005: TaskScheduler: Change Sequence::PeekTask to Sequence::TakeTask. (Closed)
Patch Set: self-review 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
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 tasks that must be executed in posting order.
26 // 26 //
27 // Note: there is a known refcounted-ownership cycle in the Scheduler 27 // Note: there is a known refcounted-ownership cycle in the Scheduler
28 // architecture: Sequence -> Task -> TaskRunner -> Sequence -> ... 28 // architecture: Sequence -> Task -> TaskRunner -> Sequence -> ...
29 // This is okay so long as the other owners of Sequence (PriorityQueue and 29 // This is okay so long as the other owners of Sequence (PriorityQueue and
30 // SchedulerWorker in alternation and 30 // SchedulerWorker in alternation and
31 // SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::GetWork() 31 // SchedulerWorkerPoolImpl::SchedulerWorkerDelegateImpl::GetWork()
32 // temporarily) keep running it (and taking Tasks from it as a result). A 32 // 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 33 // 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 34 // 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 35 // 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 36 // 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). 37 // the caller that the Sequence should be re-enqueued for execution).
38 // 38 //
39 // This class is thread-safe. 39 // This class is thread-safe.
40 class BASE_EXPORT Sequence : public RefCountedThreadSafe<Sequence> { 40 class BASE_EXPORT Sequence : public RefCountedThreadSafe<Sequence> {
41 public: 41 public:
42 Sequence(); 42 Sequence();
43 43
44 // Adds |task| at the end of the sequence's queue. Returns true if the 44 // Adds |task| in a new slot at the end of the Sequence. Returns true if the
45 // sequence was empty before this operation. 45 // Sequence contained no slot before this operation.
gab 2016/10/07 19:51:08 I still prefer to refer to the Sequence as being "
fdoray 2016/10/07 20:30:30 Done.
46 bool PushTask(std::unique_ptr<Task> task); 46 bool PushTask(std::unique_ptr<Task> task);
47 47
48 // Returns the task in front of the sequence's queue, if any. 48 // Transfers ownership of the Task in the front slot of the Sequence to the
49 const Task* PeekTask() const; 49 // caller. If this returns nullptr, the front slot of the Sequence was empty
50 // or the Sequence contained no slot before this operation. If this returns a
51 // Task, the front slot of the Sequence is empty immediately after this
gab 2016/10/07 19:51:08 , the front slot of the Sequence will be empty unt
fdoray 2016/10/07 20:30:30 Done.
52 // operation.
53 std::unique_ptr<Task> TakeTask();
50 54
51 // Removes the task in front of the sequence's queue. Returns true if the 55 // 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 56 // called on an empty Sequence or on a Sequence whose front slot is empty.
53 // sequence. 57 TaskTraits GetFrontTaskTraits() const;
gab 2016/10/07 19:51:08 PeekTaskTraits()?
fdoray 2016/10/07 20:30:30 Done.
54 bool PopTask();
55 58
56 // Returns a SequenceSortKey representing the priority of the sequence. Cannot 59 // Removes the slot in front of the Sequence. The front slot must have been
57 // be called on an empty sequence. 60 // emptied by TakeTask() before this is called. Cannot be called on a Sequence
61 // that contains no slot. Returns true if the Sequence contains no slot
62 // immediately after this operation.
63 bool RemoveFrontSlot();
gab 2016/10/07 19:51:08 I prefer Pop() here, documentation can highlight w
fdoray 2016/10/07 20:30:30 Done.
64
65 // Returns a SequenceSortKey representing the priority of the Sequence. Cannot
66 // be called on an empty Sequence.
58 SequenceSortKey GetSortKey() const; 67 SequenceSortKey GetSortKey() const;
59 68
60 // Returns a token that uniquely identifies this Sequence. 69 // Returns a token that uniquely identifies this Sequence.
61 const SequenceToken& token() const { return token_; } 70 const SequenceToken& token() const { return token_; }
62 71
63 private: 72 private:
64 friend class RefCountedThreadSafe<Sequence>; 73 friend class RefCountedThreadSafe<Sequence>;
65 ~Sequence(); 74 ~Sequence();
66 75
67 const SequenceToken token_ = SequenceToken::Create(); 76 const SequenceToken token_ = SequenceToken::Create();
68 77
69 // Synchronizes access to all members. 78 // Synchronizes access to all members.
70 mutable SchedulerLock lock_; 79 mutable SchedulerLock lock_;
71 80
72 // Queue of tasks to execute. 81 // Queue of tasks to execute.
73 std::queue<std::unique_ptr<Task>> queue_; 82 std::queue<std::unique_ptr<Task>> queue_;
74 83
75 // Number of tasks contained in the sequence for each priority. 84 // Number of tasks contained in the Sequence for each priority.
76 size_t num_tasks_per_priority_[static_cast<int>(TaskPriority::HIGHEST) + 1] = 85 size_t num_tasks_per_priority_[static_cast<int>(TaskPriority::HIGHEST) + 1] =
77 {}; 86 {};
78 87
79 DISALLOW_COPY_AND_ASSIGN(Sequence); 88 DISALLOW_COPY_AND_ASSIGN(Sequence);
80 }; 89 };
81 90
82 } // namespace internal 91 } // namespace internal
83 } // namespace base 92 } // namespace base
84 93
85 #endif // BASE_TASK_SCHEDULER_SEQUENCE_H_ 94 #endif // BASE_TASK_SCHEDULER_SEQUENCE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698