| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_BASE_TASK_QUEUE_H_ | 11 #ifndef WEBRTC_BASE_TASK_QUEUE_H_ |
| 12 #define WEBRTC_BASE_TASK_QUEUE_H_ | 12 #define WEBRTC_BASE_TASK_QUEUE_H_ |
| 13 | 13 |
| 14 #include <list> | |
| 15 #include <memory> | 14 #include <memory> |
| 16 #include <unordered_map> | 15 #include <stdint.h> |
| 17 | 16 |
| 18 #if defined(WEBRTC_MAC) && !defined(WEBRTC_BUILD_LIBEVENT) | 17 #include "base/macros.h" |
| 19 #include <dispatch/dispatch.h> | 18 #include "third_party/webrtc/base/thread_annotations.h" |
| 20 #endif | |
| 21 | |
| 22 #include "third_party/webrtc/base/constructormagic.h" | |
| 23 #include "third_party/webrtc/base/criticalsection.h" | |
| 24 | |
| 25 #if defined(WEBRTC_WIN) || defined(WEBRTC_BUILD_LIBEVENT) | |
| 26 #include "third_party/webrtc/base/platform_thread.h" | |
| 27 #endif | |
| 28 | |
| 29 #if defined(WEBRTC_BUILD_LIBEVENT) | |
| 30 struct event_base; | |
| 31 struct event; | |
| 32 #endif | |
| 33 | 19 |
| 34 namespace rtc { | 20 namespace rtc { |
| 35 | 21 |
| 36 // Base interface for asynchronously executed tasks. | 22 // Base interface for asynchronously executed tasks. |
| 37 // The interface basically consists of a single function, Run(), that executes | 23 // The interface basically consists of a single function, Run(), that executes |
| 38 // on the target queue. For more details see the Run() method and TaskQueue. | 24 // on the target queue. For more details see the Run() method and TaskQueue. |
| 39 class QueuedTask { | 25 class QueuedTask { |
| 40 public: | 26 public: |
| 41 QueuedTask() {} | 27 QueuedTask() {} |
| 42 virtual ~QueuedTask() {} | 28 virtual ~QueuedTask() {} |
| 43 | 29 |
| 44 // Main routine that will run when the task is executed on the desired queue. | 30 // Main routine that will run when the task is executed on the desired queue. |
| 45 // The task should return |true| to indicate that it should be deleted or | 31 // The task should return |true| to indicate that it should be deleted or |
| 46 // |false| to indicate that the queue should consider ownership of the task | 32 // |false| to indicate that the queue should consider ownership of the task |
| 47 // having been transferred. Returning |false| can be useful if a task has | 33 // having been transferred. Returning |false| can be useful if a task has |
| 48 // re-posted itself to a different queue or is otherwise being re-used. | 34 // re-posted itself to a different queue or is otherwise being re-used. |
| 49 virtual bool Run() = 0; | 35 virtual bool Run() = 0; |
| 50 | 36 |
| 51 private: | 37 private: |
| 52 RTC_DISALLOW_COPY_AND_ASSIGN(QueuedTask); | 38 DISALLOW_COPY_AND_ASSIGN(QueuedTask); |
| 53 }; | 39 }; |
| 54 | 40 |
| 55 // Simple implementation of QueuedTask for use with rtc::Bind and lambdas. | 41 // Simple implementation of QueuedTask for use with rtc::Bind and lambdas. |
| 56 template <class Closure> | 42 template <class Closure> |
| 57 class ClosureTask : public QueuedTask { | 43 class ClosureTask : public QueuedTask { |
| 58 public: | 44 public: |
| 59 explicit ClosureTask(const Closure& closure) : closure_(closure) {} | 45 explicit ClosureTask(const Closure& closure) : closure_(closure) {} |
| 60 | 46 |
| 61 private: | 47 private: |
| 62 bool Run() override { | 48 bool Run() override { |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 } | 204 } |
| 219 | 205 |
| 220 template <class Closure1, class Closure2> | 206 template <class Closure1, class Closure2> |
| 221 void PostTaskAndReply(const Closure1& task, const Closure2& reply) { | 207 void PostTaskAndReply(const Closure1& task, const Closure2& reply) { |
| 222 PostTaskAndReply( | 208 PostTaskAndReply( |
| 223 std::unique_ptr<QueuedTask>(new ClosureTask<Closure1>(task)), | 209 std::unique_ptr<QueuedTask>(new ClosureTask<Closure1>(task)), |
| 224 std::unique_ptr<QueuedTask>(new ClosureTask<Closure2>(reply))); | 210 std::unique_ptr<QueuedTask>(new ClosureTask<Closure2>(reply))); |
| 225 } | 211 } |
| 226 | 212 |
| 227 private: | 213 private: |
| 228 #if defined(WEBRTC_BUILD_LIBEVENT) | 214 class WorkerThread; |
| 229 static bool ThreadMain(void* context); | |
| 230 static void OnWakeup(int socket, short flags, void* context); // NOLINT | |
| 231 static void RunTask(int fd, short flags, void* context); // NOLINT | |
| 232 static void RunTimer(int fd, short flags, void* context); // NOLINT | |
| 233 | 215 |
| 234 class PostAndReplyTask; | 216 std::unique_ptr<WorkerThread> thread_; |
| 235 class SetTimerTask; | 217 DISALLOW_COPY_AND_ASSIGN(TaskQueue); |
| 236 | |
| 237 void PrepareReplyTask(PostAndReplyTask* reply_task); | |
| 238 void ReplyTaskDone(PostAndReplyTask* reply_task); | |
| 239 | |
| 240 struct QueueContext; | |
| 241 | |
| 242 int wakeup_pipe_in_ = -1; | |
| 243 int wakeup_pipe_out_ = -1; | |
| 244 event_base* event_base_; | |
| 245 std::unique_ptr<event> wakeup_event_; | |
| 246 PlatformThread thread_; | |
| 247 rtc::CriticalSection pending_lock_; | |
| 248 std::list<std::unique_ptr<QueuedTask>> pending_ GUARDED_BY(pending_lock_); | |
| 249 std::list<PostAndReplyTask*> pending_replies_ GUARDED_BY(pending_lock_); | |
| 250 #elif defined(WEBRTC_MAC) | |
| 251 struct QueueContext; | |
| 252 struct TaskContext; | |
| 253 struct PostTaskAndReplyContext; | |
| 254 dispatch_queue_t queue_; | |
| 255 QueueContext* const context_; | |
| 256 #elif defined(WEBRTC_WIN) | |
| 257 typedef std::unordered_map<UINT_PTR, std::unique_ptr<QueuedTask>> | |
| 258 DelayedTasks; | |
| 259 static bool ThreadMain(void* context); | |
| 260 static bool ProcessQueuedMessages(DelayedTasks* delayed_tasks); | |
| 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 }; | 218 }; |
| 278 | 219 |
| 279 } // namespace rtc | 220 } // namespace rtc |
| 280 | 221 |
| 281 #endif // WEBRTC_BASE_TASK_QUEUE_H_ | 222 #endif // WEBRTC_BASE_TASK_QUEUE_H_ |
| OLD | NEW |