| 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 #if defined(WEBRTC_POSIX) && !defined(WEBRTC_MAC) | |
| 15 #define LIBEVENT_TASK_QUEUE | |
| 16 #endif | |
| 17 | |
| 18 #include <list> | |
| 19 #include <memory> | |
| 20 | |
| 21 #if defined(WEBRTC_MAC) | |
| 22 #include <dispatch/dispatch.h> | |
| 23 #endif | |
| 24 | |
| 25 #include "webrtc/base/constructormagic.h" | |
| 26 #include "webrtc/base/criticalsection.h" | |
| 27 | |
| 28 #if !defined(WEBRTC_MAC) | |
| 29 #include "webrtc/base/platform_thread.h" | |
| 30 #endif | |
| 31 | |
| 32 #if defined(LIBEVENT_TASK_QUEUE) | |
| 33 struct event_base; | |
| 34 struct event; | |
| 35 #endif | |
| 36 | |
| 37 namespace rtc { | |
| 38 | |
| 39 // Base interface for asynchronously executed tasks. | |
| 40 // The interface basically consists of a single function, Run(), that executes | |
| 41 // on the target queue. For more details see the Run() method and TaskQueue. | |
| 42 class QueuedTask { | |
| 43 public: | |
| 44 QueuedTask() {} | |
| 45 virtual ~QueuedTask() {} | |
| 46 | |
| 47 // Main routine that will run when the task is executed on the desired queue. | |
| 48 // The task should return |true| to indicate that it should be deleted or | |
| 49 // |false| to indicate that the queue should consider ownership of the task | |
| 50 // having been transferred. Returning |false| can be useful if a task has | |
| 51 // re-posted itself to a different queue or is otherwise being re-used. | |
| 52 virtual bool Run() = 0; | |
| 53 | |
| 54 private: | |
| 55 RTC_DISALLOW_COPY_AND_ASSIGN(QueuedTask); | |
| 56 }; | |
| 57 | |
| 58 // Simple implementation of QueuedTask for use with rtc::Bind and lambdas. | |
| 59 template <class Closure> | |
| 60 class ClosureTask : public QueuedTask { | |
| 61 public: | |
| 62 explicit ClosureTask(const Closure& closure) : closure_(closure) {} | |
| 63 | |
| 64 private: | |
| 65 bool Run() override { | |
| 66 closure_(); | |
| 67 return true; | |
| 68 } | |
| 69 | |
| 70 Closure closure_; | |
| 71 }; | |
| 72 | |
| 73 // Extends ClosureTask to also allow specifying cleanup code. | |
| 74 // This is useful when using lambdas if guaranteeing cleanup, even if a task | |
| 75 // was dropped (queue is too full), is required. | |
| 76 template <class Closure, class Cleanup> | |
| 77 class ClosureTaskWithCleanup : public ClosureTask<Closure> { | |
| 78 public: | |
| 79 ClosureTaskWithCleanup(const Closure& closure, Cleanup cleanup) | |
| 80 : ClosureTask<Closure>(closure), cleanup_(cleanup) {} | |
| 81 ~ClosureTaskWithCleanup() { cleanup_(); } | |
| 82 | |
| 83 private: | |
| 84 Cleanup cleanup_; | |
| 85 }; | |
| 86 | |
| 87 // Convenience function to construct closures that can be passed directly | |
| 88 // to methods that support std::unique_ptr<QueuedTask> but not template | |
| 89 // based parameters. | |
| 90 template <class Closure> | |
| 91 static std::unique_ptr<QueuedTask> NewClosure(const Closure& closure) { | |
| 92 return std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(closure)); | |
| 93 } | |
| 94 | |
| 95 template <class Closure, class Cleanup> | |
| 96 static std::unique_ptr<QueuedTask> NewClosure(const Closure& closure, | |
| 97 const Cleanup& cleanup) { | |
| 98 return std::unique_ptr<QueuedTask>( | |
| 99 new ClosureTaskWithCleanup<Closure, Cleanup>(closure, cleanup)); | |
| 100 } | |
| 101 | |
| 102 // Implements a task queue that asynchronously executes tasks in a way that | |
| 103 // guarantees that they're executed in FIFO order and that tasks never overlap. | |
| 104 // Tasks may always execute on the same worker thread and they may not. | |
| 105 // To DCHECK that tasks are executing on a known task queue, use IsCurrent(). | |
| 106 // | |
| 107 // Here are some usage examples: | |
| 108 // | |
| 109 // 1) Asynchronously running a lambda: | |
| 110 // | |
| 111 // class MyClass { | |
| 112 // ... | |
| 113 // TaskQueue queue_("MyQueue"); | |
| 114 // }; | |
| 115 // | |
| 116 // void MyClass::StartWork() { | |
| 117 // queue_.PostTask([]() { Work(); }); | |
| 118 // ... | |
| 119 // | |
| 120 // 2) Doing work asynchronously on a worker queue and providing a notification | |
| 121 // callback on the current queue, when the work has been done: | |
| 122 // | |
| 123 // void MyClass::StartWorkAndLetMeKnowWhenDone( | |
| 124 // std::unique_ptr<QueuedTask> callback) { | |
| 125 // DCHECK(TaskQueue::Current()) << "Need to be running on a queue"; | |
| 126 // queue_.PostTaskAndReply([]() { Work(); }, std::move(callback)); | |
| 127 // } | |
| 128 // ... | |
| 129 // my_class->StartWorkAndLetMeKnowWhenDone( | |
| 130 // NewClosure([]() { LOG(INFO) << "The work is done!";})); | |
| 131 // | |
| 132 // 3) Posting a custom task on a timer. The task posts itself again after | |
| 133 // every running: | |
| 134 // | |
| 135 // class TimerTask : public QueuedTask { | |
| 136 // public: | |
| 137 // TimerTask() {} | |
| 138 // private: | |
| 139 // bool Run() override { | |
| 140 // ++count_; | |
| 141 // TaskQueue::Current()->PostDelayedTask( | |
| 142 // std::unique_ptr<QueuedTask>(this), 1000); | |
| 143 // // Ownership has been transferred to the next occurance, | |
| 144 // // so return false to prevent from being deleted now. | |
| 145 // return false; | |
| 146 // } | |
| 147 // int count_ = 0; | |
| 148 // }; | |
| 149 // ... | |
| 150 // queue_.PostDelayedTask( | |
| 151 // std::unique_ptr<QueuedTask>(new TimerTask()), 1000); | |
| 152 // | |
| 153 // For more examples, see task_queue_unittests.cc. | |
| 154 // | |
| 155 // A note on destruction: | |
| 156 // | |
| 157 // When a TaskQueue is deleted, pending tasks will not be executed but they will | |
| 158 // be deleted. The deletion of tasks may happen asynchronously after the | |
| 159 // TaskQueue itself has been deleted or it may happen synchronously while the | |
| 160 // TaskQueue instance is being deleted. This may vary from one OS to the next | |
| 161 // so assumptions about lifetimes of pending tasks should not be made. | |
| 162 class TaskQueue { | |
| 163 public: | |
| 164 explicit TaskQueue(const char* queue_name); | |
| 165 // TODO(tommi): Implement move semantics? | |
| 166 ~TaskQueue(); | |
| 167 | |
| 168 static TaskQueue* Current(); | |
| 169 | |
| 170 // Used for DCHECKing the current queue. | |
| 171 static bool IsCurrent(const char* queue_name); | |
| 172 bool IsCurrent() const; | |
| 173 | |
| 174 // TODO(tommi): For better debuggability, implement FROM_HERE. | |
| 175 | |
| 176 // Ownership of the task is passed to PostTask. | |
| 177 void PostTask(std::unique_ptr<QueuedTask> task); | |
| 178 void PostTaskAndReply(std::unique_ptr<QueuedTask> task, | |
| 179 std::unique_ptr<QueuedTask> reply, | |
| 180 TaskQueue* reply_queue); | |
| 181 void PostTaskAndReply(std::unique_ptr<QueuedTask> task, | |
| 182 std::unique_ptr<QueuedTask> reply); | |
| 183 | |
| 184 void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds); | |
| 185 | |
| 186 template <class Closure> | |
| 187 void PostTask(const Closure& closure) { | |
| 188 PostTask(std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(closure))); | |
| 189 } | |
| 190 | |
| 191 template <class Closure> | |
| 192 void PostDelayedTask(const Closure& closure, uint32_t milliseconds) { | |
| 193 PostDelayedTask( | |
| 194 std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(closure)), | |
| 195 milliseconds); | |
| 196 } | |
| 197 | |
| 198 template <class Closure1, class Closure2> | |
| 199 void PostTaskAndReply(const Closure1& task, | |
| 200 const Closure2& reply, | |
| 201 TaskQueue* reply_queue) { | |
| 202 PostTaskAndReply( | |
| 203 std::unique_ptr<QueuedTask>(new ClosureTask<Closure1>(task)), | |
| 204 std::unique_ptr<QueuedTask>(new ClosureTask<Closure2>(reply)), | |
| 205 reply_queue); | |
| 206 } | |
| 207 | |
| 208 template <class Closure> | |
| 209 void PostTaskAndReply(std::unique_ptr<QueuedTask> task, | |
| 210 const Closure& reply) { | |
| 211 PostTaskAndReply(std::move(task), std::unique_ptr<QueuedTask>( | |
| 212 new ClosureTask<Closure>(reply))); | |
| 213 } | |
| 214 | |
| 215 template <class Closure> | |
| 216 void PostTaskAndReply(const Closure& task, | |
| 217 std::unique_ptr<QueuedTask> reply) { | |
| 218 PostTaskAndReply( | |
| 219 std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(task)), | |
| 220 std::move(reply)); | |
| 221 } | |
| 222 | |
| 223 template <class Closure1, class Closure2> | |
| 224 void PostTaskAndReply(const Closure1& task, const Closure2& reply) { | |
| 225 PostTaskAndReply( | |
| 226 std::unique_ptr<QueuedTask>(new ClosureTask<Closure1>(task)), | |
| 227 std::unique_ptr<QueuedTask>(new ClosureTask<Closure2>(reply))); | |
| 228 } | |
| 229 | |
| 230 private: | |
| 231 #if defined(LIBEVENT_TASK_QUEUE) | |
| 232 static bool ThreadMain(void* context); | |
| 233 static void OnWakeup(int socket, short flags, void* context); // NOLINT | |
| 234 static void RunTask(int fd, short flags, void* context); // NOLINT | |
| 235 static void RunTimer(int fd, short flags, void* context); // NOLINT | |
| 236 | |
| 237 class PostAndReplyTask; | |
| 238 class SetTimerTask; | |
| 239 | |
| 240 void PrepareReplyTask(PostAndReplyTask* reply_task); | |
| 241 void ReplyTaskDone(PostAndReplyTask* reply_task); | |
| 242 | |
| 243 struct QueueContext; | |
| 244 | |
| 245 int wakeup_pipe_in_ = -1; | |
| 246 int wakeup_pipe_out_ = -1; | |
| 247 event_base* event_base_; | |
| 248 std::unique_ptr<event> wakeup_event_; | |
| 249 PlatformThread thread_; | |
| 250 rtc::CriticalSection pending_lock_; | |
| 251 std::list<std::unique_ptr<QueuedTask>> pending_ GUARDED_BY(pending_lock_); | |
| 252 std::list<PostAndReplyTask*> pending_replies_ GUARDED_BY(pending_lock_); | |
| 253 #elif defined(WEBRTC_MAC) | |
| 254 struct QueueContext; | |
| 255 struct TaskContext; | |
| 256 struct PostTaskAndReplyContext; | |
| 257 dispatch_queue_t queue_; | |
| 258 QueueContext* const context_; | |
| 259 #elif defined(WEBRTC_WIN) | |
| 260 static bool ThreadMain(void* context); | |
| 261 | |
| 262 class WorkerThread : public PlatformThread { | |
| 263 public: | |
| 264 WorkerThread(ThreadRunFunction func, void* obj, const char* thread_name) | |
| 265 : PlatformThread(func, obj, thread_name) {} | |
| 266 | |
| 267 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data) { | |
| 268 return PlatformThread::QueueAPC(apc_function, data); | |
| 269 } | |
| 270 }; | |
| 271 WorkerThread thread_; | |
| 272 #else | |
| 273 #error not supported. | |
| 274 #endif | |
| 275 | |
| 276 RTC_DISALLOW_COPY_AND_ASSIGN(TaskQueue); | |
| 277 }; | |
| 278 | |
| 279 } // namespace rtc | |
| 280 | |
| 281 #endif // WEBRTC_BASE_TASK_QUEUE_H_ | |
| OLD | NEW |