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/task_scheduler/scheduler_lock.h" | 16 #include "base/task_scheduler/scheduler_lock.h" |
17 #include "base/task_scheduler/sequence_sort_key.h" | 17 #include "base/task_scheduler/sequence_sort_key.h" |
18 #include "base/task_scheduler/task.h" | 18 #include "base/task_scheduler/task.h" |
19 #include "base/task_scheduler/task_traits.h" | 19 #include "base/task_scheduler/task_traits.h" |
20 | 20 |
21 namespace base { | 21 namespace base { |
22 namespace internal { | 22 namespace internal { |
23 | 23 |
24 // A sequence holds tasks that must be executed in posting order. | 24 // A sequence holds tasks that must be executed in posting order. |
25 // | |
26 // Note: there is a known refcounted-ownership cycle in the Scheduler | |
27 // architecture: | |
28 // Sequence -> Task -> TaskRunner -> Sequence -> ... | |
29 // this is okay so long as the other owners of Sequence (PriorityQueue and | |
30 // SchedulerWorkerThread in alternance and | |
31 // SchedulerThreadPoolImpl::SchedulerWorkerThreadDelegateImpl::GetWork() | |
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 | |
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 | |
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). | |
fdoray
2016/04/28 14:37:52
Nice comment :)
gab
2016/04/28 15:42:39
Thanks :-)
| |
38 // | |
25 // This class is thread-safe. | 39 // This class is thread-safe. |
26 class BASE_EXPORT Sequence : public RefCountedThreadSafe<Sequence> { | 40 class BASE_EXPORT Sequence : public RefCountedThreadSafe<Sequence> { |
27 public: | 41 public: |
28 Sequence(); | 42 Sequence(); |
29 | 43 |
30 // Adds |task| at the end of the sequence's queue. Returns true if the | 44 // Adds |task| at the end of the sequence's queue. Returns true if the |
31 // sequence was empty before this operation. | 45 // sequence was empty before this operation. |
32 bool PushTask(std::unique_ptr<Task> task); | 46 bool PushTask(std::unique_ptr<Task> task); |
33 | 47 |
34 // Returns the task in front of the sequence's queue, if any. | 48 // Returns the task in front of the sequence's queue, if any. |
(...skipping 22 matching lines...) Expand all Loading... | |
57 size_t num_tasks_per_priority_[static_cast<int>(TaskPriority::HIGHEST) + 1] = | 71 size_t num_tasks_per_priority_[static_cast<int>(TaskPriority::HIGHEST) + 1] = |
58 {}; | 72 {}; |
59 | 73 |
60 DISALLOW_COPY_AND_ASSIGN(Sequence); | 74 DISALLOW_COPY_AND_ASSIGN(Sequence); |
61 }; | 75 }; |
62 | 76 |
63 } // namespace internal | 77 } // namespace internal |
64 } // namespace base | 78 } // namespace base |
65 | 79 |
66 #endif // BASE_TASK_SCHEDULER_SEQUENCE_H_ | 80 #endif // BASE_TASK_SCHEDULER_SEQUENCE_H_ |
OLD | NEW |