blob: 1d069e6040693a75206016c8012c9216b2f9bb59 [file] [log] [blame]
tommic06b1332016-05-14 11:31:40 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/task_queue.h"
tommic06b1332016-05-14 11:31:40 -070012
tommif9d91542017-02-17 02:47:11 -080013#include <mmsystem.h>
tommic06b1332016-05-14 11:31:40 -070014#include <string.h>
tommic06b1332016-05-14 11:31:40 -070015
tommif9d91542017-02-17 02:47:11 -080016#include <algorithm>
tommi0b942152017-03-10 09:33:53 -080017#include <queue>
tommif9d91542017-02-17 02:47:11 -080018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/arraysize.h"
20#include "rtc_base/checks.h"
21#include "rtc_base/event.h"
22#include "rtc_base/logging.h"
23#include "rtc_base/platform_thread.h"
24#include "rtc_base/refcount.h"
25#include "rtc_base/refcountedobject.h"
26#include "rtc_base/safe_conversions.h"
27#include "rtc_base/timeutils.h"
tommic06b1332016-05-14 11:31:40 -070028
29namespace rtc {
30namespace {
31#define WM_RUN_TASK WM_USER + 1
32#define WM_QUEUE_DELAYED_TASK WM_USER + 2
33
tommic9bb7912017-02-24 10:42:14 -080034using Priority = TaskQueue::Priority;
35
tommic06b1332016-05-14 11:31:40 -070036DWORD g_queue_ptr_tls = 0;
37
38BOOL CALLBACK InitializeTls(PINIT_ONCE init_once, void* param, void** context) {
39 g_queue_ptr_tls = TlsAlloc();
40 return TRUE;
41}
42
43DWORD GetQueuePtrTls() {
44 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
tommif9d91542017-02-17 02:47:11 -080045 ::InitOnceExecuteOnce(&init_once, InitializeTls, nullptr, nullptr);
tommic06b1332016-05-14 11:31:40 -070046 return g_queue_ptr_tls;
47}
48
49struct ThreadStartupData {
50 Event* started;
51 void* thread_context;
52};
53
54void CALLBACK InitializeQueueThread(ULONG_PTR param) {
55 MSG msg;
tommif9d91542017-02-17 02:47:11 -080056 ::PeekMessage(&msg, nullptr, WM_USER, WM_USER, PM_NOREMOVE);
tommic06b1332016-05-14 11:31:40 -070057 ThreadStartupData* data = reinterpret_cast<ThreadStartupData*>(param);
tommif9d91542017-02-17 02:47:11 -080058 ::TlsSetValue(GetQueuePtrTls(), data->thread_context);
tommic06b1332016-05-14 11:31:40 -070059 data->started->Set();
60}
tommic9bb7912017-02-24 10:42:14 -080061
62ThreadPriority TaskQueuePriorityToThreadPriority(Priority priority) {
63 switch (priority) {
64 case Priority::HIGH:
65 return kRealtimePriority;
66 case Priority::LOW:
67 return kLowPriority;
68 case Priority::NORMAL:
69 return kNormalPriority;
70 default:
71 RTC_NOTREACHED();
72 break;
73 }
74 return kNormalPriority;
75}
tommi5bdee472017-03-03 05:20:12 -080076
tommi0b942152017-03-10 09:33:53 -080077int64_t GetTick() {
tommi5bdee472017-03-03 05:20:12 -080078 static const UINT kPeriod = 1;
79 bool high_res = (timeBeginPeriod(kPeriod) == TIMERR_NOERROR);
tommi0b942152017-03-10 09:33:53 -080080 int64_t ret = TimeMillis();
tommi5bdee472017-03-03 05:20:12 -080081 if (high_res)
82 timeEndPeriod(kPeriod);
83 return ret;
84}
tommic06b1332016-05-14 11:31:40 -070085
tommi0b942152017-03-10 09:33:53 -080086class DelayedTaskInfo {
tommif9d91542017-02-17 02:47:11 -080087 public:
tommi0b942152017-03-10 09:33:53 -080088 // Default ctor needed to support priority_queue::pop().
89 DelayedTaskInfo() {}
90 DelayedTaskInfo(uint32_t milliseconds, std::unique_ptr<QueuedTask> task)
91 : due_time_(GetTick() + milliseconds), task_(std::move(task)) {}
92 DelayedTaskInfo(DelayedTaskInfo&&) = default;
tommif9d91542017-02-17 02:47:11 -080093
tommi0b942152017-03-10 09:33:53 -080094 // Implement for priority_queue.
95 bool operator>(const DelayedTaskInfo& other) const {
96 return due_time_ > other.due_time_;
97 }
tommif9d91542017-02-17 02:47:11 -080098
tommi0b942152017-03-10 09:33:53 -080099 // Required by priority_queue::pop().
100 DelayedTaskInfo& operator=(DelayedTaskInfo&& other) = default;
101
102 // See below for why this method is const.
103 void Run() const {
104 RTC_DCHECK(due_time_);
105 task_->Run() ? task_.reset() : static_cast<void>(task_.release());
106 }
107
108 int64_t due_time() const { return due_time_; }
109
110 private:
111 int64_t due_time_ = 0; // Absolute timestamp in milliseconds.
112
113 // |task| needs to be mutable because std::priority_queue::top() returns
114 // a const reference and a key in an ordered queue must not be changed.
115 // There are two basic workarounds, one using const_cast, which would also
116 // make the key (|due_time|), non-const and the other is to make the non-key
117 // (|task|), mutable.
118 // Because of this, the |task| variable is made private and can only be
119 // mutated by calling the |Run()| method.
120 mutable std::unique_ptr<QueuedTask> task_;
121};
122
123class MultimediaTimer {
124 public:
tommi83722262017-03-15 04:36:29 -0700125 // Note: We create an event that requires manual reset.
126 MultimediaTimer() : event_(::CreateEvent(nullptr, true, false, nullptr)) {}
tommif9d91542017-02-17 02:47:11 -0800127
tommi0b942152017-03-10 09:33:53 -0800128 ~MultimediaTimer() {
129 Cancel();
130 ::CloseHandle(event_);
tommif9d91542017-02-17 02:47:11 -0800131 }
132
tommi0b942152017-03-10 09:33:53 -0800133 bool StartOneShotTimer(UINT delay_ms) {
tommif9d91542017-02-17 02:47:11 -0800134 RTC_DCHECK_EQ(0, timer_id_);
135 RTC_DCHECK(event_ != nullptr);
tommif9d91542017-02-17 02:47:11 -0800136 timer_id_ =
137 ::timeSetEvent(delay_ms, 0, reinterpret_cast<LPTIMECALLBACK>(event_), 0,
138 TIME_ONESHOT | TIME_CALLBACK_EVENT_SET);
139 return timer_id_ != 0;
140 }
141
tommi0b942152017-03-10 09:33:53 -0800142 void Cancel() {
tommi83722262017-03-15 04:36:29 -0700143 ::ResetEvent(event_);
tommif9d91542017-02-17 02:47:11 -0800144 if (timer_id_) {
145 ::timeKillEvent(timer_id_);
146 timer_id_ = 0;
147 }
tommif9d91542017-02-17 02:47:11 -0800148 }
149
tommi0b942152017-03-10 09:33:53 -0800150 HANDLE* event_for_wait() { return &event_; }
tommif9d91542017-02-17 02:47:11 -0800151
152 private:
tommif9d91542017-02-17 02:47:11 -0800153 HANDLE event_ = nullptr;
154 MMRESULT timer_id_ = 0;
tommif9d91542017-02-17 02:47:11 -0800155
156 RTC_DISALLOW_COPY_AND_ASSIGN(MultimediaTimer);
157};
158
tommi0b942152017-03-10 09:33:53 -0800159} // namespace
160
nisse341c8e42017-09-06 04:38:22 -0700161class TaskQueue::Impl : public RefCountInterface {
tommi0b942152017-03-10 09:33:53 -0800162 public:
nisse341c8e42017-09-06 04:38:22 -0700163 Impl(const char* queue_name, TaskQueue* queue, Priority priority);
164 ~Impl() override;
tommi0b942152017-03-10 09:33:53 -0800165
nisse341c8e42017-09-06 04:38:22 -0700166 static TaskQueue::Impl* Current();
167 static TaskQueue* CurrentQueue();
168
169 // Used for DCHECKing the current queue.
170 bool IsCurrent() const;
171
172 template <class Closure,
173 typename std::enable_if<
174 std::is_copy_constructible<Closure>::value>::type* = nullptr>
175 void PostTask(const Closure& closure) {
176 PostTask(std::unique_ptr<QueuedTask>(new ClosureTask<Closure>(closure)));
177 }
178
179 void PostTask(std::unique_ptr<QueuedTask> task);
180 void PostTaskAndReply(std::unique_ptr<QueuedTask> task,
181 std::unique_ptr<QueuedTask> reply,
182 TaskQueue::Impl* reply_queue);
183
184 void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds);
185
186 void RunPendingTasks();
tommi0b942152017-03-10 09:33:53 -0800187
188 private:
nisse341c8e42017-09-06 04:38:22 -0700189 static void ThreadMain(void* context);
tommi0b942152017-03-10 09:33:53 -0800190
nisse341c8e42017-09-06 04:38:22 -0700191 class WorkerThread : public PlatformThread {
192 public:
193 WorkerThread(ThreadRunFunction func,
194 void* obj,
195 const char* thread_name,
196 ThreadPriority priority)
197 : PlatformThread(func, obj, thread_name, priority) {}
198
199 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data) {
200 return PlatformThread::QueueAPC(apc_function, data);
201 }
tommi0b942152017-03-10 09:33:53 -0800202 };
203
nisse341c8e42017-09-06 04:38:22 -0700204 class ThreadState {
205 public:
206 explicit ThreadState(HANDLE in_queue) : in_queue_(in_queue) {}
207 ~ThreadState() {}
208
209 void RunThreadMain();
210
211 private:
212 bool ProcessQueuedMessages();
213 void RunDueTasks();
214 void ScheduleNextTimer();
215 void CancelTimers();
216
217 // Since priority_queue<> by defult orders items in terms of
218 // largest->smallest, using std::less<>, and we want smallest->largest,
219 // we would like to use std::greater<> here. Alas it's only available in
220 // C++14 and later, so we roll our own compare template that that relies on
221 // operator<().
222 template <typename T>
223 struct greater {
224 bool operator()(const T& l, const T& r) { return l > r; }
225 };
226
227 MultimediaTimer timer_;
228 std::priority_queue<DelayedTaskInfo,
229 std::vector<DelayedTaskInfo>,
230 greater<DelayedTaskInfo>>
231 timer_tasks_;
232 UINT_PTR timer_id_ = 0;
233 HANDLE in_queue_;
234 };
235
236 TaskQueue* const queue_;
237 WorkerThread thread_;
238 rtc::CriticalSection pending_lock_;
danilchapa37de392017-09-09 04:17:22 -0700239 std::queue<std::unique_ptr<QueuedTask>> pending_
240 RTC_GUARDED_BY(pending_lock_);
tommi83722262017-03-15 04:36:29 -0700241 HANDLE in_queue_;
tommi0b942152017-03-10 09:33:53 -0800242};
243
nisse341c8e42017-09-06 04:38:22 -0700244TaskQueue::Impl::Impl(const char* queue_name,
245 TaskQueue* queue,
246 Priority priority)
247 : queue_(queue),
248 thread_(&TaskQueue::Impl::ThreadMain,
tommic9bb7912017-02-24 10:42:14 -0800249 this,
250 queue_name,
tommi83722262017-03-15 04:36:29 -0700251 TaskQueuePriorityToThreadPriority(priority)),
nisse341c8e42017-09-06 04:38:22 -0700252 in_queue_(::CreateEvent(nullptr, true, false, nullptr)) {
tommic06b1332016-05-14 11:31:40 -0700253 RTC_DCHECK(queue_name);
tommi83722262017-03-15 04:36:29 -0700254 RTC_DCHECK(in_queue_);
tommic06b1332016-05-14 11:31:40 -0700255 thread_.Start();
256 Event event(false, false);
257 ThreadStartupData startup = {&event, this};
258 RTC_CHECK(thread_.QueueAPC(&InitializeQueueThread,
259 reinterpret_cast<ULONG_PTR>(&startup)));
260 event.Wait(Event::kForever);
261}
262
nisse341c8e42017-09-06 04:38:22 -0700263TaskQueue::Impl::~Impl() {
tommic06b1332016-05-14 11:31:40 -0700264 RTC_DCHECK(!IsCurrent());
tommif9d91542017-02-17 02:47:11 -0800265 while (!::PostThreadMessage(thread_.GetThreadRef(), WM_QUIT, 0, 0)) {
kwiberg352444f2016-11-28 15:58:53 -0800266 RTC_CHECK_EQ(ERROR_NOT_ENOUGH_QUOTA, ::GetLastError());
tommic06b1332016-05-14 11:31:40 -0700267 Sleep(1);
268 }
269 thread_.Stop();
tommi83722262017-03-15 04:36:29 -0700270 ::CloseHandle(in_queue_);
tommic06b1332016-05-14 11:31:40 -0700271}
272
273// static
nisse341c8e42017-09-06 04:38:22 -0700274TaskQueue::Impl* TaskQueue::Impl::Current() {
275 return static_cast<TaskQueue::Impl*>(::TlsGetValue(GetQueuePtrTls()));
tommic06b1332016-05-14 11:31:40 -0700276}
277
nisse341c8e42017-09-06 04:38:22 -0700278// static
279TaskQueue* TaskQueue::Impl::CurrentQueue() {
280 TaskQueue::Impl* current = Current();
281 return current ? current->queue_ : nullptr;
282}
283
284bool TaskQueue::Impl::IsCurrent() const {
tommic06b1332016-05-14 11:31:40 -0700285 return IsThreadRefEqual(thread_.GetThreadRef(), CurrentThreadRef());
286}
287
nisse341c8e42017-09-06 04:38:22 -0700288void TaskQueue::Impl::PostTask(std::unique_ptr<QueuedTask> task) {
tommi83722262017-03-15 04:36:29 -0700289 rtc::CritScope lock(&pending_lock_);
290 pending_.push(std::move(task));
291 ::SetEvent(in_queue_);
tommic06b1332016-05-14 11:31:40 -0700292}
293
nisse341c8e42017-09-06 04:38:22 -0700294void TaskQueue::Impl::PostDelayedTask(std::unique_ptr<QueuedTask> task,
295 uint32_t milliseconds) {
tommi0b942152017-03-10 09:33:53 -0800296 if (!milliseconds) {
297 PostTask(std::move(task));
298 return;
299 }
300
301 // TODO(tommi): Avoid this allocation. It is currently here since
302 // the timestamp stored in the task info object, is a 64bit timestamp
303 // and WPARAM is 32bits in 32bit builds. Otherwise, we could pass the
304 // task pointer and timestamp as LPARAM and WPARAM.
305 auto* task_info = new DelayedTaskInfo(milliseconds, std::move(task));
306 if (!::PostThreadMessage(thread_.GetThreadRef(), WM_QUEUE_DELAYED_TASK, 0,
307 reinterpret_cast<LPARAM>(task_info))) {
308 delete task_info;
tommic06b1332016-05-14 11:31:40 -0700309 }
310}
311
nisse341c8e42017-09-06 04:38:22 -0700312void TaskQueue::Impl::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
313 std::unique_ptr<QueuedTask> reply,
314 TaskQueue::Impl* reply_queue) {
tommic06b1332016-05-14 11:31:40 -0700315 QueuedTask* task_ptr = task.release();
316 QueuedTask* reply_task_ptr = reply.release();
317 DWORD reply_thread_id = reply_queue->thread_.GetThreadRef();
318 PostTask([task_ptr, reply_task_ptr, reply_thread_id]() {
319 if (task_ptr->Run())
320 delete task_ptr;
321 // If the thread's message queue is full, we can't queue the task and will
322 // have to drop it (i.e. delete).
tommif9d91542017-02-17 02:47:11 -0800323 if (!::PostThreadMessage(reply_thread_id, WM_RUN_TASK, 0,
324 reinterpret_cast<LPARAM>(reply_task_ptr))) {
tommic06b1332016-05-14 11:31:40 -0700325 delete reply_task_ptr;
326 }
327 });
328}
329
nisse341c8e42017-09-06 04:38:22 -0700330void TaskQueue::Impl::RunPendingTasks() {
tommi83722262017-03-15 04:36:29 -0700331 while (true) {
332 std::unique_ptr<QueuedTask> task;
333 {
334 rtc::CritScope lock(&pending_lock_);
335 if (pending_.empty())
336 break;
337 task = std::move(pending_.front());
338 pending_.pop();
339 }
340
341 if (!task->Run())
342 task.release();
343 }
344}
345
tommic06b1332016-05-14 11:31:40 -0700346// static
nisse341c8e42017-09-06 04:38:22 -0700347void TaskQueue::Impl::ThreadMain(void* context) {
348 ThreadState state(static_cast<TaskQueue::Impl*>(context)->in_queue_);
tommi0b942152017-03-10 09:33:53 -0800349 state.RunThreadMain();
350}
tommif9d91542017-02-17 02:47:11 -0800351
nisse341c8e42017-09-06 04:38:22 -0700352void TaskQueue::Impl::ThreadState::RunThreadMain() {
tommi83722262017-03-15 04:36:29 -0700353 HANDLE handles[2] = { *timer_.event_for_wait(), in_queue_ };
tommib89257a2016-07-12 01:24:36 -0700354 while (true) {
tommif9d91542017-02-17 02:47:11 -0800355 // Make sure we do an alertable wait as that's required to allow APCs to run
356 // (e.g. required for InitializeQueueThread and stopping the thread in
357 // PlatformThread).
tommi0b942152017-03-10 09:33:53 -0800358 DWORD result = ::MsgWaitForMultipleObjectsEx(
tommi83722262017-03-15 04:36:29 -0700359 arraysize(handles), handles, INFINITE, QS_ALLEVENTS, MWMO_ALERTABLE);
tommib89257a2016-07-12 01:24:36 -0700360 RTC_CHECK_NE(WAIT_FAILED, result);
tommi83722262017-03-15 04:36:29 -0700361 if (result == (WAIT_OBJECT_0 + 2)) {
tommi0b942152017-03-10 09:33:53 -0800362 // There are messages in the message queue that need to be handled.
363 if (!ProcessQueuedMessages())
tommib89257a2016-07-12 01:24:36 -0700364 break;
tommi83722262017-03-15 04:36:29 -0700365 }
366
367 if (result == WAIT_OBJECT_0 || (!timer_tasks_.empty() &&
368 ::WaitForSingleObject(*timer_.event_for_wait(), 0) == WAIT_OBJECT_0)) {
tommi0b942152017-03-10 09:33:53 -0800369 // The multimedia timer was signaled.
370 timer_.Cancel();
tommi0b942152017-03-10 09:33:53 -0800371 RunDueTasks();
372 ScheduleNextTimer();
tommi83722262017-03-15 04:36:29 -0700373 }
374
375 if (result == (WAIT_OBJECT_0 + 1)) {
376 ::ResetEvent(in_queue_);
nisse341c8e42017-09-06 04:38:22 -0700377 TaskQueue::Impl::Current()->RunPendingTasks();
tommib89257a2016-07-12 01:24:36 -0700378 }
379 }
tommib89257a2016-07-12 01:24:36 -0700380}
tommic06b1332016-05-14 11:31:40 -0700381
nisse341c8e42017-09-06 04:38:22 -0700382bool TaskQueue::Impl::ThreadState::ProcessQueuedMessages() {
tommib89257a2016-07-12 01:24:36 -0700383 MSG msg = {};
tommi83722262017-03-15 04:36:29 -0700384 // To protect against overly busy message queues, we limit the time
385 // we process tasks to a few milliseconds. If we don't do that, there's
386 // a chance that timer tasks won't ever run.
387 static const int kMaxTaskProcessingTimeMs = 500;
388 auto start = GetTick();
tommif9d91542017-02-17 02:47:11 -0800389 while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) &&
tommib89257a2016-07-12 01:24:36 -0700390 msg.message != WM_QUIT) {
tommic06b1332016-05-14 11:31:40 -0700391 if (!msg.hwnd) {
392 switch (msg.message) {
tommi83722262017-03-15 04:36:29 -0700393 // TODO(tommi): Stop using this way of queueing tasks.
tommic06b1332016-05-14 11:31:40 -0700394 case WM_RUN_TASK: {
395 QueuedTask* task = reinterpret_cast<QueuedTask*>(msg.lParam);
396 if (task->Run())
397 delete task;
398 break;
399 }
400 case WM_QUEUE_DELAYED_TASK: {
tommi0b942152017-03-10 09:33:53 -0800401 std::unique_ptr<DelayedTaskInfo> info(
402 reinterpret_cast<DelayedTaskInfo*>(msg.lParam));
403 bool need_to_schedule_timers =
404 timer_tasks_.empty() ||
405 timer_tasks_.top().due_time() > info->due_time();
406 timer_tasks_.emplace(std::move(*info.get()));
407 if (need_to_schedule_timers) {
408 CancelTimers();
409 ScheduleNextTimer();
tommif9d91542017-02-17 02:47:11 -0800410 }
tommic06b1332016-05-14 11:31:40 -0700411 break;
412 }
413 case WM_TIMER: {
tommi0b942152017-03-10 09:33:53 -0800414 RTC_DCHECK_EQ(timer_id_, msg.wParam);
tommif9d91542017-02-17 02:47:11 -0800415 ::KillTimer(nullptr, msg.wParam);
tommi0b942152017-03-10 09:33:53 -0800416 timer_id_ = 0;
417 RunDueTasks();
418 ScheduleNextTimer();
tommic06b1332016-05-14 11:31:40 -0700419 break;
420 }
421 default:
422 RTC_NOTREACHED();
423 break;
424 }
425 } else {
tommif9d91542017-02-17 02:47:11 -0800426 ::TranslateMessage(&msg);
427 ::DispatchMessage(&msg);
tommic06b1332016-05-14 11:31:40 -0700428 }
tommi83722262017-03-15 04:36:29 -0700429
430 if (GetTick() > start + kMaxTaskProcessingTimeMs)
431 break;
tommic06b1332016-05-14 11:31:40 -0700432 }
tommib89257a2016-07-12 01:24:36 -0700433 return msg.message != WM_QUIT;
tommic06b1332016-05-14 11:31:40 -0700434}
tommib89257a2016-07-12 01:24:36 -0700435
nisse341c8e42017-09-06 04:38:22 -0700436void TaskQueue::Impl::ThreadState::RunDueTasks() {
tommi0b942152017-03-10 09:33:53 -0800437 RTC_DCHECK(!timer_tasks_.empty());
438 auto now = GetTick();
439 do {
440 const auto& top = timer_tasks_.top();
441 if (top.due_time() > now)
442 break;
443 top.Run();
444 timer_tasks_.pop();
445 } while (!timer_tasks_.empty());
446}
447
nisse341c8e42017-09-06 04:38:22 -0700448void TaskQueue::Impl::ThreadState::ScheduleNextTimer() {
tommi0b942152017-03-10 09:33:53 -0800449 RTC_DCHECK_EQ(timer_id_, 0);
450 if (timer_tasks_.empty())
451 return;
452
453 const auto& next_task = timer_tasks_.top();
454 int64_t delay_ms = std::max(0ll, next_task.due_time() - GetTick());
455 uint32_t milliseconds = rtc::dchecked_cast<uint32_t>(delay_ms);
456 if (!timer_.StartOneShotTimer(milliseconds))
457 timer_id_ = ::SetTimer(nullptr, 0, milliseconds, nullptr);
458}
459
nisse341c8e42017-09-06 04:38:22 -0700460void TaskQueue::Impl::ThreadState::CancelTimers() {
tommi0b942152017-03-10 09:33:53 -0800461 timer_.Cancel();
462 if (timer_id_) {
463 ::KillTimer(nullptr, timer_id_);
464 timer_id_ = 0;
465 }
466}
467
nisse341c8e42017-09-06 04:38:22 -0700468// Boilerplate for the PIMPL pattern.
469TaskQueue::TaskQueue(const char* queue_name, Priority priority)
470 : impl_(new RefCountedObject<TaskQueue::Impl>(queue_name, this, priority)) {
471}
472
473TaskQueue::~TaskQueue() {}
474
475// static
476TaskQueue* TaskQueue::Current() {
477 return TaskQueue::Impl::CurrentQueue();
478}
479
480// Used for DCHECKing the current queue.
481bool TaskQueue::IsCurrent() const {
482 return impl_->IsCurrent();
483}
484
485void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) {
486 return TaskQueue::impl_->PostTask(std::move(task));
487}
488
489void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
490 std::unique_ptr<QueuedTask> reply,
491 TaskQueue* reply_queue) {
492 return TaskQueue::impl_->PostTaskAndReply(std::move(task), std::move(reply),
493 reply_queue->impl_.get());
494}
495
496void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
497 std::unique_ptr<QueuedTask> reply) {
498 return TaskQueue::impl_->PostTaskAndReply(std::move(task), std::move(reply),
499 impl_.get());
500}
501
502void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task,
503 uint32_t milliseconds) {
504 return TaskQueue::impl_->PostDelayedTask(std::move(task), milliseconds);
505}
506
tommic06b1332016-05-14 11:31:40 -0700507} // namespace rtc