blob: 082bf9d631d86656ee554f3b76cd4df5a0f942da [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>
Danil Chapovalov6f09ae22017-10-12 14:39:25 +020018#include <utility>
tommif9d91542017-02-17 02:47:11 -080019
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/arraysize.h"
21#include "rtc_base/checks.h"
22#include "rtc_base/event.h"
23#include "rtc_base/logging.h"
24#include "rtc_base/platform_thread.h"
25#include "rtc_base/refcount.h"
26#include "rtc_base/refcountedobject.h"
27#include "rtc_base/safe_conversions.h"
28#include "rtc_base/timeutils.h"
tommic06b1332016-05-14 11:31:40 -070029
30namespace rtc {
31namespace {
32#define WM_RUN_TASK WM_USER + 1
33#define WM_QUEUE_DELAYED_TASK WM_USER + 2
34
tommic9bb7912017-02-24 10:42:14 -080035using Priority = TaskQueue::Priority;
36
tommic06b1332016-05-14 11:31:40 -070037DWORD g_queue_ptr_tls = 0;
38
39BOOL CALLBACK InitializeTls(PINIT_ONCE init_once, void* param, void** context) {
40 g_queue_ptr_tls = TlsAlloc();
41 return TRUE;
42}
43
44DWORD GetQueuePtrTls() {
45 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
tommif9d91542017-02-17 02:47:11 -080046 ::InitOnceExecuteOnce(&init_once, InitializeTls, nullptr, nullptr);
tommic06b1332016-05-14 11:31:40 -070047 return g_queue_ptr_tls;
48}
49
50struct ThreadStartupData {
51 Event* started;
52 void* thread_context;
53};
54
55void CALLBACK InitializeQueueThread(ULONG_PTR param) {
56 MSG msg;
tommif9d91542017-02-17 02:47:11 -080057 ::PeekMessage(&msg, nullptr, WM_USER, WM_USER, PM_NOREMOVE);
tommic06b1332016-05-14 11:31:40 -070058 ThreadStartupData* data = reinterpret_cast<ThreadStartupData*>(param);
tommif9d91542017-02-17 02:47:11 -080059 ::TlsSetValue(GetQueuePtrTls(), data->thread_context);
tommic06b1332016-05-14 11:31:40 -070060 data->started->Set();
61}
tommic9bb7912017-02-24 10:42:14 -080062
63ThreadPriority TaskQueuePriorityToThreadPriority(Priority priority) {
64 switch (priority) {
65 case Priority::HIGH:
66 return kRealtimePriority;
67 case Priority::LOW:
68 return kLowPriority;
69 case Priority::NORMAL:
70 return kNormalPriority;
71 default:
72 RTC_NOTREACHED();
73 break;
74 }
75 return kNormalPriority;
76}
tommi5bdee472017-03-03 05:20:12 -080077
tommi0b942152017-03-10 09:33:53 -080078int64_t GetTick() {
tommi5bdee472017-03-03 05:20:12 -080079 static const UINT kPeriod = 1;
80 bool high_res = (timeBeginPeriod(kPeriod) == TIMERR_NOERROR);
tommi0b942152017-03-10 09:33:53 -080081 int64_t ret = TimeMillis();
tommi5bdee472017-03-03 05:20:12 -080082 if (high_res)
83 timeEndPeriod(kPeriod);
84 return ret;
85}
tommic06b1332016-05-14 11:31:40 -070086
tommi0b942152017-03-10 09:33:53 -080087class DelayedTaskInfo {
tommif9d91542017-02-17 02:47:11 -080088 public:
tommi0b942152017-03-10 09:33:53 -080089 // Default ctor needed to support priority_queue::pop().
90 DelayedTaskInfo() {}
91 DelayedTaskInfo(uint32_t milliseconds, std::unique_ptr<QueuedTask> task)
92 : due_time_(GetTick() + milliseconds), task_(std::move(task)) {}
93 DelayedTaskInfo(DelayedTaskInfo&&) = default;
tommif9d91542017-02-17 02:47:11 -080094
tommi0b942152017-03-10 09:33:53 -080095 // Implement for priority_queue.
96 bool operator>(const DelayedTaskInfo& other) const {
97 return due_time_ > other.due_time_;
98 }
tommif9d91542017-02-17 02:47:11 -080099
tommi0b942152017-03-10 09:33:53 -0800100 // Required by priority_queue::pop().
101 DelayedTaskInfo& operator=(DelayedTaskInfo&& other) = default;
102
103 // See below for why this method is const.
104 void Run() const {
105 RTC_DCHECK(due_time_);
106 task_->Run() ? task_.reset() : static_cast<void>(task_.release());
107 }
108
109 int64_t due_time() const { return due_time_; }
110
111 private:
112 int64_t due_time_ = 0; // Absolute timestamp in milliseconds.
113
114 // |task| needs to be mutable because std::priority_queue::top() returns
115 // a const reference and a key in an ordered queue must not be changed.
116 // There are two basic workarounds, one using const_cast, which would also
117 // make the key (|due_time|), non-const and the other is to make the non-key
118 // (|task|), mutable.
119 // Because of this, the |task| variable is made private and can only be
120 // mutated by calling the |Run()| method.
121 mutable std::unique_ptr<QueuedTask> task_;
122};
123
124class MultimediaTimer {
125 public:
tommi83722262017-03-15 04:36:29 -0700126 // Note: We create an event that requires manual reset.
127 MultimediaTimer() : event_(::CreateEvent(nullptr, true, false, nullptr)) {}
tommif9d91542017-02-17 02:47:11 -0800128
tommi0b942152017-03-10 09:33:53 -0800129 ~MultimediaTimer() {
130 Cancel();
131 ::CloseHandle(event_);
tommif9d91542017-02-17 02:47:11 -0800132 }
133
tommi0b942152017-03-10 09:33:53 -0800134 bool StartOneShotTimer(UINT delay_ms) {
tommif9d91542017-02-17 02:47:11 -0800135 RTC_DCHECK_EQ(0, timer_id_);
136 RTC_DCHECK(event_ != nullptr);
tommif9d91542017-02-17 02:47:11 -0800137 timer_id_ =
138 ::timeSetEvent(delay_ms, 0, reinterpret_cast<LPTIMECALLBACK>(event_), 0,
139 TIME_ONESHOT | TIME_CALLBACK_EVENT_SET);
140 return timer_id_ != 0;
141 }
142
tommi0b942152017-03-10 09:33:53 -0800143 void Cancel() {
tommi83722262017-03-15 04:36:29 -0700144 ::ResetEvent(event_);
tommif9d91542017-02-17 02:47:11 -0800145 if (timer_id_) {
146 ::timeKillEvent(timer_id_);
147 timer_id_ = 0;
148 }
tommif9d91542017-02-17 02:47:11 -0800149 }
150
tommi0b942152017-03-10 09:33:53 -0800151 HANDLE* event_for_wait() { return &event_; }
tommif9d91542017-02-17 02:47:11 -0800152
153 private:
tommif9d91542017-02-17 02:47:11 -0800154 HANDLE event_ = nullptr;
155 MMRESULT timer_id_ = 0;
tommif9d91542017-02-17 02:47:11 -0800156
157 RTC_DISALLOW_COPY_AND_ASSIGN(MultimediaTimer);
158};
159
tommi0b942152017-03-10 09:33:53 -0800160} // namespace
161
nisse341c8e42017-09-06 04:38:22 -0700162class TaskQueue::Impl : public RefCountInterface {
tommi0b942152017-03-10 09:33:53 -0800163 public:
nisse341c8e42017-09-06 04:38:22 -0700164 Impl(const char* queue_name, TaskQueue* queue, Priority priority);
165 ~Impl() override;
tommi0b942152017-03-10 09:33:53 -0800166
nisse341c8e42017-09-06 04:38:22 -0700167 static TaskQueue::Impl* Current();
168 static TaskQueue* CurrentQueue();
169
170 // Used for DCHECKing the current queue.
171 bool IsCurrent() const;
172
173 template <class Closure,
Danil Chapovalov6f09ae22017-10-12 14:39:25 +0200174 typename std::enable_if<!std::is_convertible<
175 Closure,
176 std::unique_ptr<QueuedTask>>::value>::type* = nullptr>
177 void PostTask(Closure&& closure) {
178 PostTask(NewClosure(std::forward<Closure>(closure)));
nisse341c8e42017-09-06 04:38:22 -0700179 }
180
181 void PostTask(std::unique_ptr<QueuedTask> task);
182 void PostTaskAndReply(std::unique_ptr<QueuedTask> task,
183 std::unique_ptr<QueuedTask> reply,
184 TaskQueue::Impl* reply_queue);
185
186 void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds);
187
188 void RunPendingTasks();
tommi0b942152017-03-10 09:33:53 -0800189
190 private:
nisse341c8e42017-09-06 04:38:22 -0700191 static void ThreadMain(void* context);
tommi0b942152017-03-10 09:33:53 -0800192
nisse341c8e42017-09-06 04:38:22 -0700193 class WorkerThread : public PlatformThread {
194 public:
195 WorkerThread(ThreadRunFunction func,
196 void* obj,
197 const char* thread_name,
198 ThreadPriority priority)
199 : PlatformThread(func, obj, thread_name, priority) {}
200
201 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data) {
202 return PlatformThread::QueueAPC(apc_function, data);
203 }
tommi0b942152017-03-10 09:33:53 -0800204 };
205
nisse341c8e42017-09-06 04:38:22 -0700206 class ThreadState {
207 public:
208 explicit ThreadState(HANDLE in_queue) : in_queue_(in_queue) {}
209 ~ThreadState() {}
210
211 void RunThreadMain();
212
213 private:
214 bool ProcessQueuedMessages();
215 void RunDueTasks();
216 void ScheduleNextTimer();
217 void CancelTimers();
218
219 // Since priority_queue<> by defult orders items in terms of
220 // largest->smallest, using std::less<>, and we want smallest->largest,
221 // we would like to use std::greater<> here. Alas it's only available in
222 // C++14 and later, so we roll our own compare template that that relies on
223 // operator<().
224 template <typename T>
225 struct greater {
226 bool operator()(const T& l, const T& r) { return l > r; }
227 };
228
229 MultimediaTimer timer_;
230 std::priority_queue<DelayedTaskInfo,
231 std::vector<DelayedTaskInfo>,
232 greater<DelayedTaskInfo>>
233 timer_tasks_;
234 UINT_PTR timer_id_ = 0;
235 HANDLE in_queue_;
236 };
237
238 TaskQueue* const queue_;
239 WorkerThread thread_;
240 rtc::CriticalSection pending_lock_;
danilchapa37de392017-09-09 04:17:22 -0700241 std::queue<std::unique_ptr<QueuedTask>> pending_
242 RTC_GUARDED_BY(pending_lock_);
tommi83722262017-03-15 04:36:29 -0700243 HANDLE in_queue_;
tommi0b942152017-03-10 09:33:53 -0800244};
245
nisse341c8e42017-09-06 04:38:22 -0700246TaskQueue::Impl::Impl(const char* queue_name,
247 TaskQueue* queue,
248 Priority priority)
249 : queue_(queue),
250 thread_(&TaskQueue::Impl::ThreadMain,
tommic9bb7912017-02-24 10:42:14 -0800251 this,
252 queue_name,
tommi83722262017-03-15 04:36:29 -0700253 TaskQueuePriorityToThreadPriority(priority)),
nisse341c8e42017-09-06 04:38:22 -0700254 in_queue_(::CreateEvent(nullptr, true, false, nullptr)) {
tommic06b1332016-05-14 11:31:40 -0700255 RTC_DCHECK(queue_name);
tommi83722262017-03-15 04:36:29 -0700256 RTC_DCHECK(in_queue_);
tommic06b1332016-05-14 11:31:40 -0700257 thread_.Start();
258 Event event(false, false);
259 ThreadStartupData startup = {&event, this};
260 RTC_CHECK(thread_.QueueAPC(&InitializeQueueThread,
261 reinterpret_cast<ULONG_PTR>(&startup)));
262 event.Wait(Event::kForever);
263}
264
nisse341c8e42017-09-06 04:38:22 -0700265TaskQueue::Impl::~Impl() {
tommic06b1332016-05-14 11:31:40 -0700266 RTC_DCHECK(!IsCurrent());
tommif9d91542017-02-17 02:47:11 -0800267 while (!::PostThreadMessage(thread_.GetThreadRef(), WM_QUIT, 0, 0)) {
kwiberg352444f2016-11-28 15:58:53 -0800268 RTC_CHECK_EQ(ERROR_NOT_ENOUGH_QUOTA, ::GetLastError());
tommic06b1332016-05-14 11:31:40 -0700269 Sleep(1);
270 }
271 thread_.Stop();
tommi83722262017-03-15 04:36:29 -0700272 ::CloseHandle(in_queue_);
tommic06b1332016-05-14 11:31:40 -0700273}
274
275// static
nisse341c8e42017-09-06 04:38:22 -0700276TaskQueue::Impl* TaskQueue::Impl::Current() {
277 return static_cast<TaskQueue::Impl*>(::TlsGetValue(GetQueuePtrTls()));
tommic06b1332016-05-14 11:31:40 -0700278}
279
nisse341c8e42017-09-06 04:38:22 -0700280// static
281TaskQueue* TaskQueue::Impl::CurrentQueue() {
282 TaskQueue::Impl* current = Current();
283 return current ? current->queue_ : nullptr;
284}
285
286bool TaskQueue::Impl::IsCurrent() const {
tommic06b1332016-05-14 11:31:40 -0700287 return IsThreadRefEqual(thread_.GetThreadRef(), CurrentThreadRef());
288}
289
nisse341c8e42017-09-06 04:38:22 -0700290void TaskQueue::Impl::PostTask(std::unique_ptr<QueuedTask> task) {
tommi83722262017-03-15 04:36:29 -0700291 rtc::CritScope lock(&pending_lock_);
292 pending_.push(std::move(task));
293 ::SetEvent(in_queue_);
tommic06b1332016-05-14 11:31:40 -0700294}
295
nisse341c8e42017-09-06 04:38:22 -0700296void TaskQueue::Impl::PostDelayedTask(std::unique_ptr<QueuedTask> task,
297 uint32_t milliseconds) {
tommi0b942152017-03-10 09:33:53 -0800298 if (!milliseconds) {
299 PostTask(std::move(task));
300 return;
301 }
302
303 // TODO(tommi): Avoid this allocation. It is currently here since
304 // the timestamp stored in the task info object, is a 64bit timestamp
305 // and WPARAM is 32bits in 32bit builds. Otherwise, we could pass the
306 // task pointer and timestamp as LPARAM and WPARAM.
307 auto* task_info = new DelayedTaskInfo(milliseconds, std::move(task));
308 if (!::PostThreadMessage(thread_.GetThreadRef(), WM_QUEUE_DELAYED_TASK, 0,
309 reinterpret_cast<LPARAM>(task_info))) {
310 delete task_info;
tommic06b1332016-05-14 11:31:40 -0700311 }
312}
313
nisse341c8e42017-09-06 04:38:22 -0700314void TaskQueue::Impl::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
315 std::unique_ptr<QueuedTask> reply,
316 TaskQueue::Impl* reply_queue) {
tommic06b1332016-05-14 11:31:40 -0700317 QueuedTask* task_ptr = task.release();
318 QueuedTask* reply_task_ptr = reply.release();
319 DWORD reply_thread_id = reply_queue->thread_.GetThreadRef();
320 PostTask([task_ptr, reply_task_ptr, reply_thread_id]() {
321 if (task_ptr->Run())
322 delete task_ptr;
323 // If the thread's message queue is full, we can't queue the task and will
324 // have to drop it (i.e. delete).
tommif9d91542017-02-17 02:47:11 -0800325 if (!::PostThreadMessage(reply_thread_id, WM_RUN_TASK, 0,
326 reinterpret_cast<LPARAM>(reply_task_ptr))) {
tommic06b1332016-05-14 11:31:40 -0700327 delete reply_task_ptr;
328 }
329 });
330}
331
nisse341c8e42017-09-06 04:38:22 -0700332void TaskQueue::Impl::RunPendingTasks() {
tommi83722262017-03-15 04:36:29 -0700333 while (true) {
334 std::unique_ptr<QueuedTask> task;
335 {
336 rtc::CritScope lock(&pending_lock_);
337 if (pending_.empty())
338 break;
339 task = std::move(pending_.front());
340 pending_.pop();
341 }
342
343 if (!task->Run())
344 task.release();
345 }
346}
347
tommic06b1332016-05-14 11:31:40 -0700348// static
nisse341c8e42017-09-06 04:38:22 -0700349void TaskQueue::Impl::ThreadMain(void* context) {
350 ThreadState state(static_cast<TaskQueue::Impl*>(context)->in_queue_);
tommi0b942152017-03-10 09:33:53 -0800351 state.RunThreadMain();
352}
tommif9d91542017-02-17 02:47:11 -0800353
nisse341c8e42017-09-06 04:38:22 -0700354void TaskQueue::Impl::ThreadState::RunThreadMain() {
tommi83722262017-03-15 04:36:29 -0700355 HANDLE handles[2] = { *timer_.event_for_wait(), in_queue_ };
tommib89257a2016-07-12 01:24:36 -0700356 while (true) {
tommif9d91542017-02-17 02:47:11 -0800357 // Make sure we do an alertable wait as that's required to allow APCs to run
358 // (e.g. required for InitializeQueueThread and stopping the thread in
359 // PlatformThread).
tommi0b942152017-03-10 09:33:53 -0800360 DWORD result = ::MsgWaitForMultipleObjectsEx(
tommi83722262017-03-15 04:36:29 -0700361 arraysize(handles), handles, INFINITE, QS_ALLEVENTS, MWMO_ALERTABLE);
tommib89257a2016-07-12 01:24:36 -0700362 RTC_CHECK_NE(WAIT_FAILED, result);
tommi83722262017-03-15 04:36:29 -0700363 if (result == (WAIT_OBJECT_0 + 2)) {
tommi0b942152017-03-10 09:33:53 -0800364 // There are messages in the message queue that need to be handled.
365 if (!ProcessQueuedMessages())
tommib89257a2016-07-12 01:24:36 -0700366 break;
tommi83722262017-03-15 04:36:29 -0700367 }
368
369 if (result == WAIT_OBJECT_0 || (!timer_tasks_.empty() &&
370 ::WaitForSingleObject(*timer_.event_for_wait(), 0) == WAIT_OBJECT_0)) {
tommi0b942152017-03-10 09:33:53 -0800371 // The multimedia timer was signaled.
372 timer_.Cancel();
tommi0b942152017-03-10 09:33:53 -0800373 RunDueTasks();
374 ScheduleNextTimer();
tommi83722262017-03-15 04:36:29 -0700375 }
376
377 if (result == (WAIT_OBJECT_0 + 1)) {
378 ::ResetEvent(in_queue_);
nisse341c8e42017-09-06 04:38:22 -0700379 TaskQueue::Impl::Current()->RunPendingTasks();
tommib89257a2016-07-12 01:24:36 -0700380 }
381 }
tommib89257a2016-07-12 01:24:36 -0700382}
tommic06b1332016-05-14 11:31:40 -0700383
nisse341c8e42017-09-06 04:38:22 -0700384bool TaskQueue::Impl::ThreadState::ProcessQueuedMessages() {
tommib89257a2016-07-12 01:24:36 -0700385 MSG msg = {};
tommi83722262017-03-15 04:36:29 -0700386 // To protect against overly busy message queues, we limit the time
387 // we process tasks to a few milliseconds. If we don't do that, there's
388 // a chance that timer tasks won't ever run.
389 static const int kMaxTaskProcessingTimeMs = 500;
390 auto start = GetTick();
tommif9d91542017-02-17 02:47:11 -0800391 while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) &&
tommib89257a2016-07-12 01:24:36 -0700392 msg.message != WM_QUIT) {
tommic06b1332016-05-14 11:31:40 -0700393 if (!msg.hwnd) {
394 switch (msg.message) {
tommi83722262017-03-15 04:36:29 -0700395 // TODO(tommi): Stop using this way of queueing tasks.
tommic06b1332016-05-14 11:31:40 -0700396 case WM_RUN_TASK: {
397 QueuedTask* task = reinterpret_cast<QueuedTask*>(msg.lParam);
398 if (task->Run())
399 delete task;
400 break;
401 }
402 case WM_QUEUE_DELAYED_TASK: {
tommi0b942152017-03-10 09:33:53 -0800403 std::unique_ptr<DelayedTaskInfo> info(
404 reinterpret_cast<DelayedTaskInfo*>(msg.lParam));
405 bool need_to_schedule_timers =
406 timer_tasks_.empty() ||
407 timer_tasks_.top().due_time() > info->due_time();
408 timer_tasks_.emplace(std::move(*info.get()));
409 if (need_to_schedule_timers) {
410 CancelTimers();
411 ScheduleNextTimer();
tommif9d91542017-02-17 02:47:11 -0800412 }
tommic06b1332016-05-14 11:31:40 -0700413 break;
414 }
415 case WM_TIMER: {
tommi0b942152017-03-10 09:33:53 -0800416 RTC_DCHECK_EQ(timer_id_, msg.wParam);
tommif9d91542017-02-17 02:47:11 -0800417 ::KillTimer(nullptr, msg.wParam);
tommi0b942152017-03-10 09:33:53 -0800418 timer_id_ = 0;
419 RunDueTasks();
420 ScheduleNextTimer();
tommic06b1332016-05-14 11:31:40 -0700421 break;
422 }
423 default:
424 RTC_NOTREACHED();
425 break;
426 }
427 } else {
tommif9d91542017-02-17 02:47:11 -0800428 ::TranslateMessage(&msg);
429 ::DispatchMessage(&msg);
tommic06b1332016-05-14 11:31:40 -0700430 }
tommi83722262017-03-15 04:36:29 -0700431
432 if (GetTick() > start + kMaxTaskProcessingTimeMs)
433 break;
tommic06b1332016-05-14 11:31:40 -0700434 }
tommib89257a2016-07-12 01:24:36 -0700435 return msg.message != WM_QUIT;
tommic06b1332016-05-14 11:31:40 -0700436}
tommib89257a2016-07-12 01:24:36 -0700437
nisse341c8e42017-09-06 04:38:22 -0700438void TaskQueue::Impl::ThreadState::RunDueTasks() {
tommi0b942152017-03-10 09:33:53 -0800439 RTC_DCHECK(!timer_tasks_.empty());
440 auto now = GetTick();
441 do {
442 const auto& top = timer_tasks_.top();
443 if (top.due_time() > now)
444 break;
445 top.Run();
446 timer_tasks_.pop();
447 } while (!timer_tasks_.empty());
448}
449
nisse341c8e42017-09-06 04:38:22 -0700450void TaskQueue::Impl::ThreadState::ScheduleNextTimer() {
tommi0b942152017-03-10 09:33:53 -0800451 RTC_DCHECK_EQ(timer_id_, 0);
452 if (timer_tasks_.empty())
453 return;
454
455 const auto& next_task = timer_tasks_.top();
456 int64_t delay_ms = std::max(0ll, next_task.due_time() - GetTick());
457 uint32_t milliseconds = rtc::dchecked_cast<uint32_t>(delay_ms);
458 if (!timer_.StartOneShotTimer(milliseconds))
459 timer_id_ = ::SetTimer(nullptr, 0, milliseconds, nullptr);
460}
461
nisse341c8e42017-09-06 04:38:22 -0700462void TaskQueue::Impl::ThreadState::CancelTimers() {
tommi0b942152017-03-10 09:33:53 -0800463 timer_.Cancel();
464 if (timer_id_) {
465 ::KillTimer(nullptr, timer_id_);
466 timer_id_ = 0;
467 }
468}
469
nisse341c8e42017-09-06 04:38:22 -0700470// Boilerplate for the PIMPL pattern.
471TaskQueue::TaskQueue(const char* queue_name, Priority priority)
472 : impl_(new RefCountedObject<TaskQueue::Impl>(queue_name, this, priority)) {
473}
474
475TaskQueue::~TaskQueue() {}
476
477// static
478TaskQueue* TaskQueue::Current() {
479 return TaskQueue::Impl::CurrentQueue();
480}
481
482// Used for DCHECKing the current queue.
483bool TaskQueue::IsCurrent() const {
484 return impl_->IsCurrent();
485}
486
487void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) {
488 return TaskQueue::impl_->PostTask(std::move(task));
489}
490
491void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
492 std::unique_ptr<QueuedTask> reply,
493 TaskQueue* reply_queue) {
494 return TaskQueue::impl_->PostTaskAndReply(std::move(task), std::move(reply),
495 reply_queue->impl_.get());
496}
497
498void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
499 std::unique_ptr<QueuedTask> reply) {
500 return TaskQueue::impl_->PostTaskAndReply(std::move(task), std::move(reply),
501 impl_.get());
502}
503
504void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task,
505 uint32_t milliseconds) {
506 return TaskQueue::impl_->PostDelayedTask(std::move(task), milliseconds);
507}
508
tommic06b1332016-05-14 11:31:40 -0700509} // namespace rtc