| 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_RTC_BASE_TASK_QUEUE_H_ | 11 #ifndef WEBRTC_RTC_BASE_TASK_QUEUE_H_ |
| 12 #define WEBRTC_RTC_BASE_TASK_QUEUE_H_ | 12 #define WEBRTC_RTC_BASE_TASK_QUEUE_H_ |
| 13 | 13 |
| 14 #include <list> | 14 #include <list> |
| 15 #include <memory> | 15 #include <memory> |
| 16 #include <queue> | 16 #include <queue> |
| 17 | 17 |
| 18 #if defined(WEBRTC_MAC) && !defined(WEBRTC_BUILD_LIBEVENT) | 18 #if defined(WEBRTC_MAC) |
| 19 #include <dispatch/dispatch.h> | 19 #include <dispatch/dispatch.h> |
| 20 #endif | 20 #endif |
| 21 | 21 |
| 22 #include "webrtc/rtc_base/constructormagic.h" | 22 #include "webrtc/rtc_base/constructormagic.h" |
| 23 #include "webrtc/rtc_base/criticalsection.h" | 23 #include "webrtc/rtc_base/criticalsection.h" |
| 24 | |
| 25 #if defined(WEBRTC_WIN) || defined(WEBRTC_BUILD_LIBEVENT) | |
| 26 #include "webrtc/rtc_base/platform_thread.h" | |
| 27 #endif | |
| 28 | |
| 29 #if defined(WEBRTC_BUILD_LIBEVENT) | |
| 30 #include "webrtc/rtc_base/refcountedobject.h" | |
| 31 #include "webrtc/rtc_base/scoped_ref_ptr.h" | 24 #include "webrtc/rtc_base/scoped_ref_ptr.h" |
| 32 | 25 |
| 33 struct event_base; | 26 #if defined(WEBRTC_WIN) |
| 34 struct event; | 27 #include "webrtc/rtc_base/platform_thread.h" |
| 35 #endif | 28 #endif |
| 36 | 29 |
| 37 namespace rtc { | 30 namespace rtc { |
| 38 | 31 |
| 32 class TaskQueueImpl; |
| 33 |
| 39 // Base interface for asynchronously executed tasks. | 34 // Base interface for asynchronously executed tasks. |
| 40 // The interface basically consists of a single function, Run(), that executes | 35 // 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. | 36 // on the target queue. For more details see the Run() method and TaskQueue. |
| 42 class QueuedTask { | 37 class QueuedTask { |
| 43 public: | 38 public: |
| 44 QueuedTask() {} | 39 QueuedTask() {} |
| 45 virtual ~QueuedTask() {} | 40 virtual ~QueuedTask() {} |
| 46 | 41 |
| 47 // Main routine that will run when the task is executed on the desired queue. | 42 // 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 | 43 // The task should return |true| to indicate that it should be deleted or |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 } | 230 } |
| 236 | 231 |
| 237 template <class Closure1, class Closure2> | 232 template <class Closure1, class Closure2> |
| 238 void PostTaskAndReply(const Closure1& task, const Closure2& reply) { | 233 void PostTaskAndReply(const Closure1& task, const Closure2& reply) { |
| 239 PostTaskAndReply( | 234 PostTaskAndReply( |
| 240 std::unique_ptr<QueuedTask>(new ClosureTask<Closure1>(task)), | 235 std::unique_ptr<QueuedTask>(new ClosureTask<Closure1>(task)), |
| 241 std::unique_ptr<QueuedTask>(new ClosureTask<Closure2>(reply))); | 236 std::unique_ptr<QueuedTask>(new ClosureTask<Closure2>(reply))); |
| 242 } | 237 } |
| 243 | 238 |
| 244 private: | 239 private: |
| 245 #if defined(WEBRTC_BUILD_LIBEVENT) | 240 #if defined(WEBRTC_MAC) |
| 246 static void ThreadMain(void* context); | |
| 247 static void OnWakeup(int socket, short flags, void* context); // NOLINT | |
| 248 static void RunTask(int fd, short flags, void* context); // NOLINT | |
| 249 static void RunTimer(int fd, short flags, void* context); // NOLINT | |
| 250 | |
| 251 class ReplyTaskOwner; | |
| 252 class PostAndReplyTask; | |
| 253 class SetTimerTask; | |
| 254 | |
| 255 typedef RefCountedObject<ReplyTaskOwner> ReplyTaskOwnerRef; | |
| 256 | |
| 257 void PrepareReplyTask(scoped_refptr<ReplyTaskOwnerRef> reply_task); | |
| 258 | |
| 259 struct QueueContext; | |
| 260 | |
| 261 int wakeup_pipe_in_ = -1; | |
| 262 int wakeup_pipe_out_ = -1; | |
| 263 event_base* event_base_; | |
| 264 std::unique_ptr<event> wakeup_event_; | |
| 265 PlatformThread thread_; | |
| 266 rtc::CriticalSection pending_lock_; | |
| 267 std::list<std::unique_ptr<QueuedTask>> pending_ GUARDED_BY(pending_lock_); | |
| 268 std::list<scoped_refptr<ReplyTaskOwnerRef>> pending_replies_ | |
| 269 GUARDED_BY(pending_lock_); | |
| 270 #elif defined(WEBRTC_MAC) | |
| 271 struct QueueContext; | 241 struct QueueContext; |
| 272 struct TaskContext; | 242 struct TaskContext; |
| 273 struct PostTaskAndReplyContext; | 243 struct PostTaskAndReplyContext; |
| 274 dispatch_queue_t queue_; | 244 dispatch_queue_t queue_; |
| 275 QueueContext* const context_; | 245 QueueContext* const context_; |
| 276 #elif defined(WEBRTC_WIN) | 246 #elif defined(WEBRTC_WIN) |
| 277 class ThreadState; | 247 class ThreadState; |
| 278 void RunPendingTasks(); | 248 void RunPendingTasks(); |
| 279 static void ThreadMain(void* context); | 249 static void ThreadMain(void* context); |
| 280 | 250 |
| 281 class WorkerThread : public PlatformThread { | 251 class WorkerThread : public PlatformThread { |
| 282 public: | 252 public: |
| 283 WorkerThread(ThreadRunFunction func, | 253 WorkerThread(ThreadRunFunction func, |
| 284 void* obj, | 254 void* obj, |
| 285 const char* thread_name, | 255 const char* thread_name, |
| 286 ThreadPriority priority) | 256 ThreadPriority priority) |
| 287 : PlatformThread(func, obj, thread_name, priority) {} | 257 : PlatformThread(func, obj, thread_name, priority) {} |
| 288 | 258 |
| 289 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data) { | 259 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data) { |
| 290 return PlatformThread::QueueAPC(apc_function, data); | 260 return PlatformThread::QueueAPC(apc_function, data); |
| 291 } | 261 } |
| 292 }; | 262 }; |
| 293 WorkerThread thread_; | 263 WorkerThread thread_; |
| 294 rtc::CriticalSection pending_lock_; | 264 rtc::CriticalSection pending_lock_; |
| 295 std::queue<std::unique_ptr<QueuedTask>> pending_ GUARDED_BY(pending_lock_); | 265 std::queue<std::unique_ptr<QueuedTask>> pending_ GUARDED_BY(pending_lock_); |
| 296 HANDLE in_queue_; | 266 HANDLE in_queue_; |
| 297 #else | 267 #else |
| 298 #error not supported. | 268 const scoped_refptr<TaskQueueImpl> impl_; |
| 299 #endif | 269 #endif |
| 300 | 270 |
| 301 RTC_DISALLOW_COPY_AND_ASSIGN(TaskQueue); | 271 RTC_DISALLOW_COPY_AND_ASSIGN(TaskQueue); |
| 302 }; | 272 }; |
| 303 | 273 |
| 304 } // namespace rtc | 274 } // namespace rtc |
| 305 | 275 |
| 306 #endif // WEBRTC_RTC_BASE_TASK_QUEUE_H_ | 276 #endif // WEBRTC_RTC_BASE_TASK_QUEUE_H_ |
| OLD | NEW |