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

Side by Side Diff: webrtc/base/task_queue.h

Issue 1919733002: New task queueing primitive for async tasks: TaskQueue. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Update docs and add support for cleanup lambdas Created 4 years, 7 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
(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 PostTask().
89 template <class Closure>
90 static std::unique_ptr<QueuedTask> NewClosure(const Closure& closure) {
91 return std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(closure));
92 }
93
94 template <class Closure, class Cleanup>
95 static std::unique_ptr<QueuedTask> NewClosure(const Closure& closure,
96 const Cleanup& cleanup) {
97 return std::unique_ptr<QueuedTask>(
98 new ClosureTaskWithCleanup<Closure, Cleanup>(closure, cleanup));
99 }
100
101 // Implements a task queue that asynchronously executes tasks in a way that
102 // guarantees that they're executed in FIFO order and that tasks never overlap.
103 // Tasks may always execute on the same worker thread and they may not.
104 // To DCHECK that tasks are executing on a known task queue, use IsCurrent().
105 //
106 // Here are some usage examples:
107 //
108 // 1) Asynchronously running a lambda:
109 //
110 // class MyClass {
111 // ...
112 // TaskQueue queue_("MyQueue");
113 // };
114 //
115 // void MyClass::StartWork() {
116 // queue_.PostTask([]() { Work(); });
117 // ...
118 //
119 // 2) Doing work asynchronously on a worker queue and providing a notification
120 // callback on the current queue, when the work has been done:
121 //
122 // void MyClass::StartWorkAndLetMeKnowWhenDone(
123 // std::unique_ptr<QueuedTask> callback) {
124 // DCHECK(TaskQueue::Current()) << "Need to be running on a queue";
125 // queue_.PostTaskAndReply([]() { Work(); }, std::move(callback));
126 // }
127 // ...
128 // my_class->StartWorkAndLetMeKnowWhenDone(
129 // NewClosure([]() { LOG(INFO) << "The work is done!";}));
130 //
131 // 3) Posting a custom task on a timer. The task posts itself again after
132 // every running:
133 //
134 // class TimerTask : public QueuedTask {
135 // public:
136 // TimerTask() {}
137 // private:
138 // bool Run() override {
139 // ++count_;
140 // TaskQueue::Current()->PostDelayedTask(
141 // std::unique_ptr<QueuedTask>(this), 1000);
142 // // Ownership has been transferred to the next occurance,
143 // // so return false to prevent from being deleted now.
144 // return false;
145 // }
146 // int count_ = 0;
147 // };
148 // ...
149 // queue_.PostDelayedTask(
150 // std::unique_ptr<QueuedTask>(new TimerTask()), 1000);
151 //
152 // For more examples, see task_queue_unittests.cc.
153 //
154 // A note on destruction:
155 //
156 // When a TaskQueue is deleted, pending tasks will not be executed but they will
157 // be deleted. The deletion of tasks may happen asynchronously after the
158 // TaskQueue itself has been deleted or it may happen synchronously while the
159 // TaskQueue instance is being deleted. This may vary from one OS to the next
160 // so assumptions about lifetimes of pending tasks should not be made.
161 class TaskQueue {
162 public:
163 explicit TaskQueue(const char* queue_name);
164 // TODO(tommi): Implement move semantics?
165 ~TaskQueue();
166
167 static TaskQueue* Current();
168
169 // Used for DCHECKing the current queue.
170 static bool IsCurrent(const char* queue_name);
171 bool IsCurrent() const;
172
173 // TODO(tommi): For better debuggability, implement FROM_HERE.
174
175 // Ownership of the task is passed to PostTask.
176 void PostTask(std::unique_ptr<QueuedTask> task);
177 void PostTaskAndReply(std::unique_ptr<QueuedTask> task,
178 std::unique_ptr<QueuedTask> reply,
179 TaskQueue* reply_queue);
180 void PostTaskAndReply(std::unique_ptr<QueuedTask> task,
181 std::unique_ptr<QueuedTask> reply);
182
183 void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds);
184
185 template <class Closure>
186 void PostTask(const Closure& closure) {
187 PostTask(std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(closure)));
188 }
189
190 template <class Closure>
191 void PostDelayedTask(const Closure& closure, uint32_t milliseconds) {
192 PostDelayedTask(
193 std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(closure)),
194 milliseconds);
195 }
196
197 template <class Closure1, class Closure2>
198 void PostTaskAndReply(const Closure1& task,
199 const Closure2& reply,
200 TaskQueue* reply_queue) {
201 PostTaskAndReply(
202 std::unique_ptr<QueuedTask>(new ClosureTask<Closure1>(task)),
203 std::unique_ptr<QueuedTask>(new ClosureTask<Closure2>(reply)),
204 reply_queue);
205 }
206
207 template <class Closure>
208 void PostTaskAndReply(std::unique_ptr<QueuedTask> task,
209 const Closure& reply) {
210 PostTaskAndReply(std::move(task), std::unique_ptr<QueuedTask>(
211 new ClosureTask<Closure>(reply)));
212 }
213
214 template <class Closure>
215 void PostTaskAndReply(const Closure& task,
216 std::unique_ptr<QueuedTask> reply) {
217 PostTaskAndReply(
218 std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(task)),
219 std::move(reply));
220 }
221
222 template <class Closure1, class Closure2>
223 void PostTaskAndReply(const Closure1& task, const Closure2& reply) {
224 PostTaskAndReply(
225 std::unique_ptr<QueuedTask>(new ClosureTask<Closure1>(task)),
226 std::unique_ptr<QueuedTask>(new ClosureTask<Closure2>(reply)));
227 }
228
229 private:
230 #if defined(LIBEVENT_TASK_QUEUE)
231 static bool ThreadMain(void* context);
232 static void OnWakeup(int socket, short flags, void* context); // NOLINT
233 static void RunTask(int fd, short flags, void* context); // NOLINT
234 static void RunTimer(int fd, short flags, void* context); // NOLINT
235
236 class PostAndReplyTask;
237 class SetTimerTask;
238
239 void PrepareReplyTask(PostAndReplyTask* reply_task);
240 void ReplyTaskDone(PostAndReplyTask* reply_task);
241
242 struct QueueContext;
243
244 int wakeup_pipe_in_ = -1;
245 int wakeup_pipe_out_ = -1;
246 event_base* event_base_;
247 std::unique_ptr<event> wakeup_event_;
248 PlatformThread thread_;
249 rtc::CriticalSection pending_lock_;
250 std::list<std::unique_ptr<QueuedTask>> pending_ GUARDED_BY(pending_lock_);
251 std::list<PostAndReplyTask*> pending_replies_ GUARDED_BY(pending_lock_);
252 #elif defined(WEBRTC_MAC)
253 struct QueueContext;
254 struct TaskContext;
255 struct PostTaskAndReplyContext;
256 dispatch_queue_t queue_;
257 QueueContext* const context_;
258 #elif defined(WEBRTC_WIN)
259 static bool ThreadMain(void* context);
260
261 class WorkerThread : public PlatformThread {
262 public:
263 WorkerThread(ThreadRunFunction func, void* obj, const char* thread_name)
264 : PlatformThread(func, obj, thread_name) {}
265
266 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data) {
267 return PlatformThread::QueueAPC(apc_function, data);
268 }
269 };
270 WorkerThread thread_;
271 #else
272 #error not supported.
273 #endif
274
275 RTC_DISALLOW_COPY_AND_ASSIGN(TaskQueue);
276 };
277
278 } // namespace rtc
279
280 #endif // WEBRTC_BASE_TASK_QUEUE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698