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

Side by Side Diff: third_party/WebKit/Source/platform/scheduler/base/work_queue.h

Issue 2258713004: Make tasks cancellable inside the blink scheduler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Various comment nits addressed Created 4 years, 4 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 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
23 // now. It interfaces deeply with WorkQueueSets which keeps track of which queue
24 // (with a given priority) contains the oldest task.
22 class BLINK_PLATFORM_EXPORT WorkQueue { 25 class BLINK_PLATFORM_EXPORT WorkQueue {
23 public: 26 public:
24 WorkQueue(TaskQueueImpl* task_queue, const char* name); 27 WorkQueue(TaskQueueImpl* task_queue,
28 const char* name,
29 TaskQueueImpl::Task::ComparatorFn queue_comparator);
25 ~WorkQueue(); 30 ~WorkQueue();
26 31
27 // Associates this work queue with the given work queue sets. This must be 32 // Associates this work queue with the given work queue sets. This must be
28 // called before any tasks can be inserted into this work queue. 33 // called before any tasks can be inserted into this work queue.
29 void AssignToWorkQueueSets(WorkQueueSets* work_queue_sets); 34 void AssignToWorkQueueSets(WorkQueueSets* work_queue_sets);
30 35
31 // Assigns the current set index. 36 // Assigns the current set index.
32 void AssignSetIndex(size_t work_queue_set_index); 37 void AssignSetIndex(size_t work_queue_set_index);
33 38
34 void AsValueInto(base::trace_event::TracedValue* state) const; 39 void AsValueInto(base::trace_event::TracedValue* state) const;
35 40
36 // Clears the |work_queue_|. 41 // Clears the |work_queue_|.
37 void Clear(); 42 void Clear();
38 43
39 // returns true if the |work_queue_| is empty. 44 // returns true if the |work_queue_| is empty.
40 bool Empty() const { return work_queue_.empty(); } 45 bool Empty() const { return work_queue_.empty(); }
41 46
42 // If the |work_queue_| isn't empty, |enqueue_order| gets set to the enqueue 47 // If the |work_queue_| isn't empty, |enqueue_order| gets set to the enqueue
43 // order of the front task and the function returns true. Otherwise the 48 // order of the front task and the function returns true. Otherwise the
44 // function returns false. 49 // function returns false.
45 bool GetFrontTaskEnqueueOrder(EnqueueOrder* enqueue_order) const; 50 bool GetFrontTaskEnqueueOrder(EnqueueOrder* enqueue_order) const;
46 51
47 // Returns the first task in this queue or null if the queue is empty. 52 // Returns the first task in this queue or null if the queue is empty.
48 const TaskQueueImpl::Task* GetFrontTask() const; 53 const TaskQueueImpl::Task* GetFrontTask() const;
49 54
50 // Pushes the task onto the |work_queue_| and informs the WorkQueueSets if 55 // Pushes the task onto the |work_queue_| and informs the WorkQueueSets if
51 // the head changed. 56 // the head changed.
52 void Push(TaskQueueImpl::Task task); 57 void Push(TaskQueueImpl::Task task);
53 58
54 // Pushes the task onto the |work_queue_|, sets the |enqueue_order| and 59 // Removes a cancelled task from the |work_queue_|. Note |key| isn't required
55 // informs the WorkQueueSets if the head changed. 60 // to be the original task posted, it can be a fake key constructed by
56 void PushAndSetEnqueueOrder(TaskQueueImpl::Task task, 61 // TaskQueueImpl::Task::CreateFakeTaskFromHandle.
57 EnqueueOrder enqueue_order); 62 bool CancelTask(const TaskQueueImpl::Task& key);
63
64 // 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
66 // constructed by TaskQueueImpl::Task::CreateFakeTaskFromHandle.
67 bool IsTaskPending(const TaskQueueImpl::Task& key) const;
58 68
59 // Swap the |work_queue_| with |incoming_queue| and informs the 69 // Swap the |work_queue_| with |incoming_queue| and informs the
60 // WorkQueueSets if the head changed. Assumes |task_queue_->any_thread_lock_| 70 // WorkQueueSets if the head changed. Assumes |task_queue_->any_thread_lock_|
61 // is locked. 71 // is locked.
62 void SwapLocked(std::queue<TaskQueueImpl::Task>& incoming_queue); 72 void SwapLocked(TaskQueueImpl::ComparatorQueue& incoming_queue);
63 73
64 size_t Size() const { return work_queue_.size(); } 74 size_t Size() const { return work_queue_.size(); }
65 75
66 // Pulls a task off the |work_queue_| and informs the WorkQueueSets. 76 // Pulls a task off the |work_queue_| and informs the WorkQueueSets.
67 TaskQueueImpl::Task TakeTaskFromWorkQueue(); 77 TaskQueueImpl::Task TakeTaskFromWorkQueue();
68 78
69 const char* name() const { return name_; } 79 const char* name() const { return name_; }
70 80
71 TaskQueueImpl* task_queue() const { return task_queue_; } 81 TaskQueueImpl* task_queue() const { return task_queue_; }
72 82
73 WorkQueueSets* work_queue_sets() const { return work_queue_sets_; } 83 WorkQueueSets* work_queue_sets() const { return work_queue_sets_; }
74 84
75 size_t work_queue_set_index() const { return work_queue_set_index_; } 85 size_t work_queue_set_index() const { return work_queue_set_index_; }
76 86
77 // Test support function. This should not be used in production code. 87 // Test support function. This should not be used in production code.
78 void PopTaskForTest(); 88 void PopTaskForTest();
79 89
80 // Returns true if the front task in this queue has an older enqueue order 90 // Returns true if the front task in this queue has an older enqueue order
81 // than the front task of |other_queue|. Both queue are assumed to be 91 // than the front task of |other_queue|. Both queue are assumed to be
82 // non-empty. 92 // non-empty.
83 bool ShouldRunBefore(const WorkQueue* other_queue) const; 93 bool ShouldRunBefore(const WorkQueue* other_queue) const;
84 94
85 private: 95 private:
86 std::queue<TaskQueueImpl::Task> work_queue_; 96 TaskQueueImpl::ComparatorQueue work_queue_;
87 WorkQueueSets* work_queue_sets_; // NOT OWNED. 97 WorkQueueSets* work_queue_sets_; // NOT OWNED.
88 TaskQueueImpl* task_queue_; // NOT OWNED. 98 TaskQueueImpl* task_queue_; // NOT OWNED.
89 size_t work_queue_set_index_; 99 size_t work_queue_set_index_;
90 const char* name_; 100 const char* name_;
91 101
92 DISALLOW_COPY_AND_ASSIGN(WorkQueue); 102 DISALLOW_COPY_AND_ASSIGN(WorkQueue);
93 }; 103 };
94 104
95 } // namespace internal 105 } // namespace internal
96 } // namespace scheduler 106 } // namespace scheduler
97 } // namespace blink 107 } // namespace blink
98 108
99 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_WORK_QUEUE_H_ 109 #endif // THIRD_PARTY_WEBKIT_SOURCE_PLATFORM_SCHEDULER_BASE_WORK_QUEUE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698