OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #ifndef WEBRTC_BASE_TASK_QUEUE_H_ |
| 12 #define WEBRTC_BASE_TASK_QUEUE_H_ |
| 13 |
| 14 #include <memory> |
| 15 #include <stdint.h> |
| 16 |
| 17 #include "base/macros.h" |
| 18 #include "third_party/webrtc/base/thread_annotations.h" |
| 19 |
| 20 namespace rtc { |
| 21 |
| 22 // Base interface for asynchronously executed tasks. |
| 23 // The interface basically consists of a single function, Run(), that executes |
| 24 // on the target queue. For more details see the Run() method and TaskQueue. |
| 25 class QueuedTask { |
| 26 public: |
| 27 QueuedTask() {} |
| 28 virtual ~QueuedTask() {} |
| 29 |
| 30 // Main routine that will run when the task is executed on the desired queue. |
| 31 // The task should return |true| to indicate that it should be deleted or |
| 32 // |false| to indicate that the queue should consider ownership of the task |
| 33 // having been transferred. Returning |false| can be useful if a task has |
| 34 // re-posted itself to a different queue or is otherwise being re-used. |
| 35 virtual bool Run() = 0; |
| 36 |
| 37 private: |
| 38 DISALLOW_COPY_AND_ASSIGN(QueuedTask); |
| 39 }; |
| 40 |
| 41 // Simple implementation of QueuedTask for use with rtc::Bind and lambdas. |
| 42 template <class Closure> |
| 43 class ClosureTask : public QueuedTask { |
| 44 public: |
| 45 explicit ClosureTask(const Closure& closure) : closure_(closure) {} |
| 46 |
| 47 private: |
| 48 bool Run() override { |
| 49 closure_(); |
| 50 return true; |
| 51 } |
| 52 |
| 53 Closure closure_; |
| 54 }; |
| 55 |
| 56 // Extends ClosureTask to also allow specifying cleanup code. |
| 57 // This is useful when using lambdas if guaranteeing cleanup, even if a task |
| 58 // was dropped (queue is too full), is required. |
| 59 template <class Closure, class Cleanup> |
| 60 class ClosureTaskWithCleanup : public ClosureTask<Closure> { |
| 61 public: |
| 62 ClosureTaskWithCleanup(const Closure& closure, Cleanup cleanup) |
| 63 : ClosureTask<Closure>(closure), cleanup_(cleanup) {} |
| 64 ~ClosureTaskWithCleanup() { cleanup_(); } |
| 65 |
| 66 private: |
| 67 Cleanup cleanup_; |
| 68 }; |
| 69 |
| 70 // Convenience function to construct closures that can be passed directly |
| 71 // to methods that support std::unique_ptr<QueuedTask> but not template |
| 72 // based parameters. |
| 73 template <class Closure> |
| 74 static std::unique_ptr<QueuedTask> NewClosure(const Closure& closure) { |
| 75 return std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(closure)); |
| 76 } |
| 77 |
| 78 template <class Closure, class Cleanup> |
| 79 static std::unique_ptr<QueuedTask> NewClosure(const Closure& closure, |
| 80 const Cleanup& cleanup) { |
| 81 return std::unique_ptr<QueuedTask>( |
| 82 new ClosureTaskWithCleanup<Closure, Cleanup>(closure, cleanup)); |
| 83 } |
| 84 |
| 85 // Implements a task queue that asynchronously executes tasks in a way that |
| 86 // guarantees that they're executed in FIFO order and that tasks never overlap. |
| 87 // Tasks may always execute on the same worker thread and they may not. |
| 88 // To DCHECK that tasks are executing on a known task queue, use IsCurrent(). |
| 89 // |
| 90 // Here are some usage examples: |
| 91 // |
| 92 // 1) Asynchronously running a lambda: |
| 93 // |
| 94 // class MyClass { |
| 95 // ... |
| 96 // TaskQueue queue_("MyQueue"); |
| 97 // }; |
| 98 // |
| 99 // void MyClass::StartWork() { |
| 100 // queue_.PostTask([]() { Work(); }); |
| 101 // ... |
| 102 // |
| 103 // 2) Doing work asynchronously on a worker queue and providing a notification |
| 104 // callback on the current queue, when the work has been done: |
| 105 // |
| 106 // void MyClass::StartWorkAndLetMeKnowWhenDone( |
| 107 // std::unique_ptr<QueuedTask> callback) { |
| 108 // DCHECK(TaskQueue::Current()) << "Need to be running on a queue"; |
| 109 // queue_.PostTaskAndReply([]() { Work(); }, std::move(callback)); |
| 110 // } |
| 111 // ... |
| 112 // my_class->StartWorkAndLetMeKnowWhenDone( |
| 113 // NewClosure([]() { LOG(INFO) << "The work is done!";})); |
| 114 // |
| 115 // 3) Posting a custom task on a timer. The task posts itself again after |
| 116 // every running: |
| 117 // |
| 118 // class TimerTask : public QueuedTask { |
| 119 // public: |
| 120 // TimerTask() {} |
| 121 // private: |
| 122 // bool Run() override { |
| 123 // ++count_; |
| 124 // TaskQueue::Current()->PostDelayedTask( |
| 125 // std::unique_ptr<QueuedTask>(this), 1000); |
| 126 // // Ownership has been transferred to the next occurance, |
| 127 // // so return false to prevent from being deleted now. |
| 128 // return false; |
| 129 // } |
| 130 // int count_ = 0; |
| 131 // }; |
| 132 // ... |
| 133 // queue_.PostDelayedTask( |
| 134 // std::unique_ptr<QueuedTask>(new TimerTask()), 1000); |
| 135 // |
| 136 // For more examples, see task_queue_unittests.cc. |
| 137 // |
| 138 // A note on destruction: |
| 139 // |
| 140 // When a TaskQueue is deleted, pending tasks will not be executed but they will |
| 141 // be deleted. The deletion of tasks may happen asynchronously after the |
| 142 // TaskQueue itself has been deleted or it may happen synchronously while the |
| 143 // TaskQueue instance is being deleted. This may vary from one OS to the next |
| 144 // so assumptions about lifetimes of pending tasks should not be made. |
| 145 class LOCKABLE TaskQueue { |
| 146 public: |
| 147 explicit TaskQueue(const char* queue_name); |
| 148 // TODO(tommi): Implement move semantics? |
| 149 ~TaskQueue(); |
| 150 |
| 151 static TaskQueue* Current(); |
| 152 |
| 153 // Used for DCHECKing the current queue. |
| 154 static bool IsCurrent(const char* queue_name); |
| 155 bool IsCurrent() const; |
| 156 |
| 157 // TODO(tommi): For better debuggability, implement RTC_FROM_HERE. |
| 158 |
| 159 // Ownership of the task is passed to PostTask. |
| 160 void PostTask(std::unique_ptr<QueuedTask> task); |
| 161 void PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 162 std::unique_ptr<QueuedTask> reply, |
| 163 TaskQueue* reply_queue); |
| 164 void PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 165 std::unique_ptr<QueuedTask> reply); |
| 166 |
| 167 void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds); |
| 168 |
| 169 template <class Closure> |
| 170 void PostTask(const Closure& closure) { |
| 171 PostTask(std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(closure))); |
| 172 } |
| 173 |
| 174 template <class Closure> |
| 175 void PostDelayedTask(const Closure& closure, uint32_t milliseconds) { |
| 176 PostDelayedTask( |
| 177 std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(closure)), |
| 178 milliseconds); |
| 179 } |
| 180 |
| 181 template <class Closure1, class Closure2> |
| 182 void PostTaskAndReply(const Closure1& task, |
| 183 const Closure2& reply, |
| 184 TaskQueue* reply_queue) { |
| 185 PostTaskAndReply( |
| 186 std::unique_ptr<QueuedTask>(new ClosureTask<Closure1>(task)), |
| 187 std::unique_ptr<QueuedTask>(new ClosureTask<Closure2>(reply)), |
| 188 reply_queue); |
| 189 } |
| 190 |
| 191 template <class Closure> |
| 192 void PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 193 const Closure& reply) { |
| 194 PostTaskAndReply(std::move(task), std::unique_ptr<QueuedTask>( |
| 195 new ClosureTask<Closure>(reply))); |
| 196 } |
| 197 |
| 198 template <class Closure> |
| 199 void PostTaskAndReply(const Closure& task, |
| 200 std::unique_ptr<QueuedTask> reply) { |
| 201 PostTaskAndReply( |
| 202 std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(task)), |
| 203 std::move(reply)); |
| 204 } |
| 205 |
| 206 template <class Closure1, class Closure2> |
| 207 void PostTaskAndReply(const Closure1& task, const Closure2& reply) { |
| 208 PostTaskAndReply( |
| 209 std::unique_ptr<QueuedTask>(new ClosureTask<Closure1>(task)), |
| 210 std::unique_ptr<QueuedTask>(new ClosureTask<Closure2>(reply))); |
| 211 } |
| 212 |
| 213 private: |
| 214 class WorkerThread; |
| 215 |
| 216 std::unique_ptr<WorkerThread> thread_; |
| 217 DISALLOW_COPY_AND_ASSIGN(TaskQueue); |
| 218 }; |
| 219 |
| 220 } // namespace rtc |
| 221 |
| 222 #endif // WEBRTC_BASE_TASK_QUEUE_H_ |
OLD | NEW |