OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_WORK_QUEUE_H_ | 5 #ifndef THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_WORK_QUEUE_H_ |
6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_WORK_QUEUE_H_ | 6 #define THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_WORK_QUEUE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <set> | 10 #include <set> |
11 | 11 |
12 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
13 #include "base/trace_event/trace_event_argument.h" | 13 #include "base/trace_event/trace_event_argument.h" |
14 #include "platform/scheduler/base/enqueue_order.h" | 14 #include "platform/scheduler/base/enqueue_order.h" |
15 #include "platform/scheduler/base/task_queue_impl.h" | 15 #include "platform/scheduler/base/task_queue_impl.h" |
16 | 16 |
17 namespace blink { | 17 namespace blink { |
18 namespace scheduler { | 18 namespace scheduler { |
19 namespace internal { | 19 namespace internal { |
20 class WorkQueueSets; | 20 class WorkQueueSets; |
21 | 21 |
22 // This class keeps track of immediate and delayed tasks which are due to run | 22 // This class keeps track of immediate and delayed tasks which are due to run |
23 // now. It interfaces deeply with WorkQueueSets which keeps track of which queue | 23 // now. It interfaces deeply with WorkQueueSets which keeps track of which queue |
24 // (with a given priority) contains the oldest task. | 24 // (with a given priority) contains the oldest task. |
| 25 // |
| 26 // If a fence is inserted, WorkQueue behaves normally up until |
| 27 // TakeTaskFromWorkQueue reaches or exceeds the fence. At that point it the |
| 28 // API subset used by WorkQueueSets pretends the WorkQueue is empty until the |
| 29 // fence is removed. This functionality is a primitive intended for use by |
| 30 // throttling mechanisms. |
25 class BLINK_PLATFORM_EXPORT WorkQueue { | 31 class BLINK_PLATFORM_EXPORT WorkQueue { |
26 public: | 32 public: |
27 WorkQueue(TaskQueueImpl* task_queue, | 33 WorkQueue(TaskQueueImpl* task_queue, |
28 const char* name, | 34 const char* name, |
29 TaskQueueImpl::Task::ComparatorFn queue_comparator); | 35 TaskQueueImpl::Task::ComparatorFn queue_comparator); |
30 ~WorkQueue(); | 36 ~WorkQueue(); |
31 | 37 |
32 // Associates this work queue with the given work queue sets. This must be | 38 // Associates this work queue with the given work queue sets. This must be |
33 // called before any tasks can be inserted into this work queue. | 39 // called before any tasks can be inserted into this work queue. |
34 void AssignToWorkQueueSets(WorkQueueSets* work_queue_sets); | 40 void AssignToWorkQueueSets(WorkQueueSets* work_queue_sets); |
35 | 41 |
36 // Assigns the current set index. | 42 // Assigns the current set index. |
37 void AssignSetIndex(size_t work_queue_set_index); | 43 void AssignSetIndex(size_t work_queue_set_index); |
38 | 44 |
39 void AsValueInto(base::trace_event::TracedValue* state) const; | 45 void AsValueInto(base::trace_event::TracedValue* state) const; |
40 | 46 |
41 // Clears the |work_queue_|. | 47 // Returns true if the |work_queue_| is empty. This method ignores any fences. |
42 void Clear(); | |
43 | |
44 // returns true if the |work_queue_| is empty. | |
45 bool Empty() const { return work_queue_.empty(); } | 48 bool Empty() const { return work_queue_.empty(); } |
46 | 49 |
47 // If the |work_queue_| isn't empty, |enqueue_order| gets set to the enqueue | 50 // If the |work_queue_| isn't empty and a fence hasn't been reached, |
48 // order of the front task and the function returns true. Otherwise the | 51 // |enqueue_order| gets set to the enqueue order of the front task and the |
49 // function returns false. | 52 // function returns true. Otherwise the function returns false. |
50 bool GetFrontTaskEnqueueOrder(EnqueueOrder* enqueue_order) const; | 53 bool GetFrontTaskEnqueueOrder(EnqueueOrder* enqueue_order) const; |
51 | 54 |
52 // Returns the first task in this queue or null if the queue is empty. | 55 // Returns the first task in this queue or null if the queue is empty. This |
| 56 // method ignores any fences. |
53 const TaskQueueImpl::Task* GetFrontTask() const; | 57 const TaskQueueImpl::Task* GetFrontTask() const; |
54 | 58 |
55 // Pushes the task onto the |work_queue_| and informs the WorkQueueSets if | 59 // Returns the first task in this queue or null if the queue is empty. This |
56 // the head changed. | 60 // method ignores any fences. |
| 61 const TaskQueueImpl::Task* GetBackTask() const; |
| 62 |
| 63 // Pushes the task onto the |work_queue_| and a fence hasn't been reached it |
| 64 // informs the WorkQueueSets if the head changed. |
57 void Push(TaskQueueImpl::Task task); | 65 void Push(TaskQueueImpl::Task task); |
58 | 66 |
59 // Removes a cancelled task from the |work_queue_|. Note |key| isn't required | 67 // Removes a cancelled task from the |work_queue_|. Note |key| isn't required |
60 // to be the original task posted, it can be a fake key constructed by | 68 // to be the original task posted, it can be a fake key constructed by |
61 // TaskQueueImpl::Task::CreateFakeTaskFromHandle. | 69 // TaskQueueImpl::Task::CreateFakeTaskFromHandle. |
62 bool CancelTask(const TaskQueueImpl::Task& key); | 70 bool CancelTask(const TaskQueueImpl::Task& key); |
63 | 71 |
64 // Returns true if |work_queue_| contains a task matching |key|. Note |key| | 72 // Returns true if |work_queue_| contains a task matching |key|. Note |key| |
65 // isn't required to be the original task posted, it can be a fake key | 73 // isn't required to be the original task posted, it can be a fake key |
66 // constructed by TaskQueueImpl::Task::CreateFakeTaskFromHandle. | 74 // constructed by TaskQueueImpl::Task::CreateFakeTaskFromHandle. |
67 bool IsTaskPending(const TaskQueueImpl::Task& key) const; | 75 bool IsTaskPending(const TaskQueueImpl::Task& key) const; |
68 | 76 |
69 // Swap the |work_queue_| with |incoming_queue| and informs the | 77 // Swap the |work_queue_| with |incoming_queue| and if a fence hasn't been |
70 // WorkQueueSets if the head changed. Assumes |task_queue_->any_thread_lock_| | 78 // reached it informs the WorkQueueSets if the head changed. Assumes |
71 // is locked. | 79 // |task_queue_->any_thread_lock_| is locked. |
72 void SwapLocked(TaskQueueImpl::ComparatorQueue& incoming_queue); | 80 void SwapLocked(TaskQueueImpl::ComparatorQueue& incoming_queue); |
73 | 81 |
74 size_t Size() const { return work_queue_.size(); } | 82 size_t Size() const { return work_queue_.size(); } |
75 | 83 |
76 // Pulls a task off the |work_queue_| and informs the WorkQueueSets. | 84 // Pulls a task off the |work_queue_| and informs the WorkQueueSets. If the |
| 85 // task removed had an enqueue order >= the current fence then WorkQueue |
| 86 // pretends to be empty as far as the WorkQueueSets is concrned. |
77 TaskQueueImpl::Task TakeTaskFromWorkQueue(); | 87 TaskQueueImpl::Task TakeTaskFromWorkQueue(); |
78 | 88 |
79 const char* name() const { return name_; } | 89 const char* name() const { return name_; } |
80 | 90 |
81 TaskQueueImpl* task_queue() const { return task_queue_; } | 91 TaskQueueImpl* task_queue() const { return task_queue_; } |
82 | 92 |
83 WorkQueueSets* work_queue_sets() const { return work_queue_sets_; } | 93 WorkQueueSets* work_queue_sets() const { return work_queue_sets_; } |
84 | 94 |
85 size_t work_queue_set_index() const { return work_queue_set_index_; } | 95 size_t work_queue_set_index() const { return work_queue_set_index_; } |
86 | 96 |
87 // Test support function. This should not be used in production code. | 97 // Test support function. This should not be used in production code. |
88 void PopTaskForTest(); | 98 void PopTaskForTest(); |
89 | 99 |
90 // Returns true if the front task in this queue has an older enqueue order | 100 // Returns true if the front task in this queue has an older enqueue order |
91 // than the front task of |other_queue|. Both queue are assumed to be | 101 // than the front task of |other_queue|. Both queue are assumed to be |
92 // non-empty. | 102 // non-empty. This method ignores any fences. |
93 bool ShouldRunBefore(const WorkQueue* other_queue) const; | 103 bool ShouldRunBefore(const WorkQueue* other_queue) const; |
94 | 104 |
| 105 // Submit a fence. When TakeTaskFromWorkQueue encounters a task whose |
| 106 // enqueue_order is >= |fence| then the WorkQueue will start pretending to be. |
| 107 // empty. |
| 108 // Inserting a fence may supersede a previous one and unblock some tasks. |
| 109 // Returns true if any tasks where unblocked, returns false otherwise. |
| 110 bool InsertFence(EnqueueOrder fence); |
| 111 |
| 112 // Removes any fences that where added and if WorkQueue was pretending to be |
| 113 // empty, then the real value is reported to WorkQueueSets. Returns true if |
| 114 // any tasks where unblocked. |
| 115 bool RemoveFence(); |
| 116 |
| 117 // Returns true if any tasks are blocked by the fence. Returns true if the |
| 118 // queue is empty and fence has been set (i.e. future tasks would be blocked). |
| 119 // Otherwise returns false. |
| 120 bool BlockedByFence() const; |
| 121 |
95 private: | 122 private: |
96 TaskQueueImpl::ComparatorQueue work_queue_; | 123 TaskQueueImpl::ComparatorQueue work_queue_; |
97 WorkQueueSets* work_queue_sets_; // NOT OWNED. | 124 WorkQueueSets* work_queue_sets_; // NOT OWNED. |
98 TaskQueueImpl* task_queue_; // NOT OWNED. | 125 TaskQueueImpl* task_queue_; // NOT OWNED. |
99 size_t work_queue_set_index_; | 126 size_t work_queue_set_index_; |
100 const char* name_; | 127 const char* name_; |
| 128 EnqueueOrder fence_; |
101 | 129 |
102 DISALLOW_COPY_AND_ASSIGN(WorkQueue); | 130 DISALLOW_COPY_AND_ASSIGN(WorkQueue); |
103 }; | 131 }; |
104 | 132 |
105 } // namespace internal | 133 } // namespace internal |
106 } // namespace scheduler | 134 } // namespace scheduler |
107 } // namespace blink | 135 } // namespace blink |
108 | 136 |
109 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_WORK_QUEUE_H_ | 137 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_WORK_QUEUE_H_ |
OLD | NEW |