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

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

Powered by Google App Engine
This is Rietveld 408576698