blob: b73aa95493ca61cd86c22943c0682b612d4915a3 [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
Yves Gerey665174f2018-06-19 15:03:05 +020013// clang-format off
14// clang formating would change include order.
15
Danil Chapovalov02fddf62018-02-12 12:41:16 +010016// Include winsock2.h before including <windows.h> to maintain consistency with
Niels Möllerb06b0a62018-05-25 10:05:34 +020017// win32.h. To include win32.h directly, it must be broken out into its own
18// build target.
Danil Chapovalov02fddf62018-02-12 12:41:16 +010019#include <winsock2.h>
20#include <windows.h>
Yves Gerey665174f2018-06-19 15:03:05 +020021#include <sal.h> // Must come after windows headers.
Danil Chapovalov02fddf62018-02-12 12:41:16 +010022#include <mmsystem.h> // Must come after windows headers.
Yves Gerey665174f2018-06-19 15:03:05 +020023// clang-format on
tommic06b1332016-05-14 11:31:40 -070024#include <string.h>
tommic06b1332016-05-14 11:31:40 -070025
tommif9d91542017-02-17 02:47:11 -080026#include <algorithm>
tommi0b942152017-03-10 09:33:53 -080027#include <queue>
Danil Chapovalov6f09ae22017-10-12 14:39:25 +020028#include <utility>
tommif9d91542017-02-17 02:47:11 -080029
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/arraysize.h"
31#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/event.h"
34#include "rtc_base/logging.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010035#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/platform_thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080037#include "rtc_base/ref_count.h"
38#include "rtc_base/ref_counted_object.h"
39#include "rtc_base/time_utils.h"
tommic06b1332016-05-14 11:31:40 -070040
41namespace rtc {
42namespace {
43#define WM_RUN_TASK WM_USER + 1
44#define WM_QUEUE_DELAYED_TASK WM_USER + 2
45
tommic9bb7912017-02-24 10:42:14 -080046using Priority = TaskQueue::Priority;
47
tommic06b1332016-05-14 11:31:40 -070048DWORD g_queue_ptr_tls = 0;
49
50BOOL CALLBACK InitializeTls(PINIT_ONCE init_once, void* param, void** context) {
51 g_queue_ptr_tls = TlsAlloc();
52 return TRUE;
53}
54
55DWORD GetQueuePtrTls() {
56 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
tommif9d91542017-02-17 02:47:11 -080057 ::InitOnceExecuteOnce(&init_once, InitializeTls, nullptr, nullptr);
tommic06b1332016-05-14 11:31:40 -070058 return g_queue_ptr_tls;
59}
60
61struct ThreadStartupData {
62 Event* started;
63 void* thread_context;
64};
65
66void CALLBACK InitializeQueueThread(ULONG_PTR param) {
67 MSG msg;
tommif9d91542017-02-17 02:47:11 -080068 ::PeekMessage(&msg, nullptr, WM_USER, WM_USER, PM_NOREMOVE);
tommic06b1332016-05-14 11:31:40 -070069 ThreadStartupData* data = reinterpret_cast<ThreadStartupData*>(param);
tommif9d91542017-02-17 02:47:11 -080070 ::TlsSetValue(GetQueuePtrTls(), data->thread_context);
tommic06b1332016-05-14 11:31:40 -070071 data->started->Set();
72}
tommic9bb7912017-02-24 10:42:14 -080073
74ThreadPriority TaskQueuePriorityToThreadPriority(Priority priority) {
75 switch (priority) {
76 case Priority::HIGH:
77 return kRealtimePriority;
78 case Priority::LOW:
79 return kLowPriority;
80 case Priority::NORMAL:
81 return kNormalPriority;
82 default:
83 RTC_NOTREACHED();
84 break;
85 }
86 return kNormalPriority;
87}
tommi5bdee472017-03-03 05:20:12 -080088
tommi0b942152017-03-10 09:33:53 -080089int64_t GetTick() {
tommi5bdee472017-03-03 05:20:12 -080090 static const UINT kPeriod = 1;
91 bool high_res = (timeBeginPeriod(kPeriod) == TIMERR_NOERROR);
tommi0b942152017-03-10 09:33:53 -080092 int64_t ret = TimeMillis();
tommi5bdee472017-03-03 05:20:12 -080093 if (high_res)
94 timeEndPeriod(kPeriod);
95 return ret;
96}
tommic06b1332016-05-14 11:31:40 -070097
tommi0b942152017-03-10 09:33:53 -080098class DelayedTaskInfo {
tommif9d91542017-02-17 02:47:11 -080099 public:
tommi0b942152017-03-10 09:33:53 -0800100 // Default ctor needed to support priority_queue::pop().
101 DelayedTaskInfo() {}
102 DelayedTaskInfo(uint32_t milliseconds, std::unique_ptr<QueuedTask> task)
103 : due_time_(GetTick() + milliseconds), task_(std::move(task)) {}
104 DelayedTaskInfo(DelayedTaskInfo&&) = default;
tommif9d91542017-02-17 02:47:11 -0800105
tommi0b942152017-03-10 09:33:53 -0800106 // Implement for priority_queue.
107 bool operator>(const DelayedTaskInfo& other) const {
108 return due_time_ > other.due_time_;
109 }
tommif9d91542017-02-17 02:47:11 -0800110
tommi0b942152017-03-10 09:33:53 -0800111 // Required by priority_queue::pop().
112 DelayedTaskInfo& operator=(DelayedTaskInfo&& other) = default;
113
114 // See below for why this method is const.
115 void Run() const {
116 RTC_DCHECK(due_time_);
117 task_->Run() ? task_.reset() : static_cast<void>(task_.release());
118 }
119
120 int64_t due_time() const { return due_time_; }
121
122 private:
123 int64_t due_time_ = 0; // Absolute timestamp in milliseconds.
124
125 // |task| needs to be mutable because std::priority_queue::top() returns
126 // a const reference and a key in an ordered queue must not be changed.
127 // There are two basic workarounds, one using const_cast, which would also
128 // make the key (|due_time|), non-const and the other is to make the non-key
129 // (|task|), mutable.
130 // Because of this, the |task| variable is made private and can only be
131 // mutated by calling the |Run()| method.
132 mutable std::unique_ptr<QueuedTask> task_;
133};
134
135class MultimediaTimer {
136 public:
tommi83722262017-03-15 04:36:29 -0700137 // Note: We create an event that requires manual reset.
138 MultimediaTimer() : event_(::CreateEvent(nullptr, true, false, nullptr)) {}
tommif9d91542017-02-17 02:47:11 -0800139
tommi0b942152017-03-10 09:33:53 -0800140 ~MultimediaTimer() {
141 Cancel();
142 ::CloseHandle(event_);
tommif9d91542017-02-17 02:47:11 -0800143 }
144
tommi0b942152017-03-10 09:33:53 -0800145 bool StartOneShotTimer(UINT delay_ms) {
tommif9d91542017-02-17 02:47:11 -0800146 RTC_DCHECK_EQ(0, timer_id_);
147 RTC_DCHECK(event_ != nullptr);
tommif9d91542017-02-17 02:47:11 -0800148 timer_id_ =
149 ::timeSetEvent(delay_ms, 0, reinterpret_cast<LPTIMECALLBACK>(event_), 0,
150 TIME_ONESHOT | TIME_CALLBACK_EVENT_SET);
151 return timer_id_ != 0;
152 }
153
tommi0b942152017-03-10 09:33:53 -0800154 void Cancel() {
tommi83722262017-03-15 04:36:29 -0700155 ::ResetEvent(event_);
tommif9d91542017-02-17 02:47:11 -0800156 if (timer_id_) {
157 ::timeKillEvent(timer_id_);
158 timer_id_ = 0;
159 }
tommif9d91542017-02-17 02:47:11 -0800160 }
161
tommi0b942152017-03-10 09:33:53 -0800162 HANDLE* event_for_wait() { return &event_; }
tommif9d91542017-02-17 02:47:11 -0800163
164 private:
tommif9d91542017-02-17 02:47:11 -0800165 HANDLE event_ = nullptr;
166 MMRESULT timer_id_ = 0;
tommif9d91542017-02-17 02:47:11 -0800167
168 RTC_DISALLOW_COPY_AND_ASSIGN(MultimediaTimer);
169};
170
tommi0b942152017-03-10 09:33:53 -0800171} // namespace
172
nisse341c8e42017-09-06 04:38:22 -0700173class TaskQueue::Impl : public RefCountInterface {
tommi0b942152017-03-10 09:33:53 -0800174 public:
nisse341c8e42017-09-06 04:38:22 -0700175 Impl(const char* queue_name, TaskQueue* queue, Priority priority);
176 ~Impl() override;
tommi0b942152017-03-10 09:33:53 -0800177
nisse341c8e42017-09-06 04:38:22 -0700178 static TaskQueue::Impl* Current();
179 static TaskQueue* CurrentQueue();
180
181 // Used for DCHECKing the current queue.
182 bool IsCurrent() const;
183
184 template <class Closure,
Danil Chapovalov6f09ae22017-10-12 14:39:25 +0200185 typename std::enable_if<!std::is_convertible<
186 Closure,
187 std::unique_ptr<QueuedTask>>::value>::type* = nullptr>
188 void PostTask(Closure&& closure) {
189 PostTask(NewClosure(std::forward<Closure>(closure)));
nisse341c8e42017-09-06 04:38:22 -0700190 }
191
192 void PostTask(std::unique_ptr<QueuedTask> task);
nisse341c8e42017-09-06 04:38:22 -0700193 void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds);
194
195 void RunPendingTasks();
tommi0b942152017-03-10 09:33:53 -0800196
197 private:
nisse341c8e42017-09-06 04:38:22 -0700198 static void ThreadMain(void* context);
tommi0b942152017-03-10 09:33:53 -0800199
nisse341c8e42017-09-06 04:38:22 -0700200 class WorkerThread : public PlatformThread {
201 public:
202 WorkerThread(ThreadRunFunction func,
203 void* obj,
204 const char* thread_name,
205 ThreadPriority priority)
206 : PlatformThread(func, obj, thread_name, priority) {}
207
208 bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data) {
209 return PlatformThread::QueueAPC(apc_function, data);
210 }
tommi0b942152017-03-10 09:33:53 -0800211 };
212
nisse341c8e42017-09-06 04:38:22 -0700213 class ThreadState {
214 public:
215 explicit ThreadState(HANDLE in_queue) : in_queue_(in_queue) {}
216 ~ThreadState() {}
217
218 void RunThreadMain();
219
220 private:
221 bool ProcessQueuedMessages();
222 void RunDueTasks();
223 void ScheduleNextTimer();
224 void CancelTimers();
225
226 // Since priority_queue<> by defult orders items in terms of
227 // largest->smallest, using std::less<>, and we want smallest->largest,
228 // we would like to use std::greater<> here. Alas it's only available in
229 // C++14 and later, so we roll our own compare template that that relies on
230 // operator<().
231 template <typename T>
232 struct greater {
233 bool operator()(const T& l, const T& r) { return l > r; }
234 };
235
236 MultimediaTimer timer_;
237 std::priority_queue<DelayedTaskInfo,
238 std::vector<DelayedTaskInfo>,
239 greater<DelayedTaskInfo>>
240 timer_tasks_;
241 UINT_PTR timer_id_ = 0;
242 HANDLE in_queue_;
243 };
244
245 TaskQueue* const queue_;
246 WorkerThread thread_;
247 rtc::CriticalSection pending_lock_;
danilchapa37de392017-09-09 04:17:22 -0700248 std::queue<std::unique_ptr<QueuedTask>> pending_
249 RTC_GUARDED_BY(pending_lock_);
tommi83722262017-03-15 04:36:29 -0700250 HANDLE in_queue_;
tommi0b942152017-03-10 09:33:53 -0800251};
252
nisse341c8e42017-09-06 04:38:22 -0700253TaskQueue::Impl::Impl(const char* queue_name,
254 TaskQueue* queue,
255 Priority priority)
256 : queue_(queue),
257 thread_(&TaskQueue::Impl::ThreadMain,
tommic9bb7912017-02-24 10:42:14 -0800258 this,
259 queue_name,
tommi83722262017-03-15 04:36:29 -0700260 TaskQueuePriorityToThreadPriority(priority)),
nisse341c8e42017-09-06 04:38:22 -0700261 in_queue_(::CreateEvent(nullptr, true, false, nullptr)) {
tommic06b1332016-05-14 11:31:40 -0700262 RTC_DCHECK(queue_name);
tommi83722262017-03-15 04:36:29 -0700263 RTC_DCHECK(in_queue_);
tommic06b1332016-05-14 11:31:40 -0700264 thread_.Start();
265 Event event(false, false);
266 ThreadStartupData startup = {&event, this};
267 RTC_CHECK(thread_.QueueAPC(&InitializeQueueThread,
268 reinterpret_cast<ULONG_PTR>(&startup)));
269 event.Wait(Event::kForever);
270}
271
nisse341c8e42017-09-06 04:38:22 -0700272TaskQueue::Impl::~Impl() {
tommic06b1332016-05-14 11:31:40 -0700273 RTC_DCHECK(!IsCurrent());
tommif9d91542017-02-17 02:47:11 -0800274 while (!::PostThreadMessage(thread_.GetThreadRef(), WM_QUIT, 0, 0)) {
kwiberg352444f2016-11-28 15:58:53 -0800275 RTC_CHECK_EQ(ERROR_NOT_ENOUGH_QUOTA, ::GetLastError());
tommic06b1332016-05-14 11:31:40 -0700276 Sleep(1);
277 }
278 thread_.Stop();
tommi83722262017-03-15 04:36:29 -0700279 ::CloseHandle(in_queue_);
tommic06b1332016-05-14 11:31:40 -0700280}
281
282// static
nisse341c8e42017-09-06 04:38:22 -0700283TaskQueue::Impl* TaskQueue::Impl::Current() {
284 return static_cast<TaskQueue::Impl*>(::TlsGetValue(GetQueuePtrTls()));
tommic06b1332016-05-14 11:31:40 -0700285}
286
nisse341c8e42017-09-06 04:38:22 -0700287// static
288TaskQueue* TaskQueue::Impl::CurrentQueue() {
289 TaskQueue::Impl* current = Current();
290 return current ? current->queue_ : nullptr;
291}
292
293bool TaskQueue::Impl::IsCurrent() const {
tommic06b1332016-05-14 11:31:40 -0700294 return IsThreadRefEqual(thread_.GetThreadRef(), CurrentThreadRef());
295}
296
nisse341c8e42017-09-06 04:38:22 -0700297void TaskQueue::Impl::PostTask(std::unique_ptr<QueuedTask> task) {
tommi83722262017-03-15 04:36:29 -0700298 rtc::CritScope lock(&pending_lock_);
299 pending_.push(std::move(task));
300 ::SetEvent(in_queue_);
tommic06b1332016-05-14 11:31:40 -0700301}
302
nisse341c8e42017-09-06 04:38:22 -0700303void TaskQueue::Impl::PostDelayedTask(std::unique_ptr<QueuedTask> task,
304 uint32_t milliseconds) {
tommi0b942152017-03-10 09:33:53 -0800305 if (!milliseconds) {
306 PostTask(std::move(task));
307 return;
308 }
309
310 // TODO(tommi): Avoid this allocation. It is currently here since
311 // the timestamp stored in the task info object, is a 64bit timestamp
312 // and WPARAM is 32bits in 32bit builds. Otherwise, we could pass the
313 // task pointer and timestamp as LPARAM and WPARAM.
314 auto* task_info = new DelayedTaskInfo(milliseconds, std::move(task));
315 if (!::PostThreadMessage(thread_.GetThreadRef(), WM_QUEUE_DELAYED_TASK, 0,
316 reinterpret_cast<LPARAM>(task_info))) {
317 delete task_info;
tommic06b1332016-05-14 11:31:40 -0700318 }
319}
320
nisse341c8e42017-09-06 04:38:22 -0700321void TaskQueue::Impl::RunPendingTasks() {
tommi83722262017-03-15 04:36:29 -0700322 while (true) {
323 std::unique_ptr<QueuedTask> task;
324 {
325 rtc::CritScope lock(&pending_lock_);
326 if (pending_.empty())
327 break;
328 task = std::move(pending_.front());
329 pending_.pop();
330 }
331
332 if (!task->Run())
333 task.release();
334 }
335}
336
tommic06b1332016-05-14 11:31:40 -0700337// static
nisse341c8e42017-09-06 04:38:22 -0700338void TaskQueue::Impl::ThreadMain(void* context) {
339 ThreadState state(static_cast<TaskQueue::Impl*>(context)->in_queue_);
tommi0b942152017-03-10 09:33:53 -0800340 state.RunThreadMain();
341}
tommif9d91542017-02-17 02:47:11 -0800342
nisse341c8e42017-09-06 04:38:22 -0700343void TaskQueue::Impl::ThreadState::RunThreadMain() {
Yves Gerey665174f2018-06-19 15:03:05 +0200344 HANDLE handles[2] = {*timer_.event_for_wait(), in_queue_};
tommib89257a2016-07-12 01:24:36 -0700345 while (true) {
tommif9d91542017-02-17 02:47:11 -0800346 // Make sure we do an alertable wait as that's required to allow APCs to run
347 // (e.g. required for InitializeQueueThread and stopping the thread in
348 // PlatformThread).
tommi0b942152017-03-10 09:33:53 -0800349 DWORD result = ::MsgWaitForMultipleObjectsEx(
tommi83722262017-03-15 04:36:29 -0700350 arraysize(handles), handles, INFINITE, QS_ALLEVENTS, MWMO_ALERTABLE);
tommib89257a2016-07-12 01:24:36 -0700351 RTC_CHECK_NE(WAIT_FAILED, result);
tommi83722262017-03-15 04:36:29 -0700352 if (result == (WAIT_OBJECT_0 + 2)) {
tommi0b942152017-03-10 09:33:53 -0800353 // There are messages in the message queue that need to be handled.
354 if (!ProcessQueuedMessages())
tommib89257a2016-07-12 01:24:36 -0700355 break;
tommi83722262017-03-15 04:36:29 -0700356 }
357
Yves Gerey665174f2018-06-19 15:03:05 +0200358 if (result == WAIT_OBJECT_0 ||
359 (!timer_tasks_.empty() &&
360 ::WaitForSingleObject(*timer_.event_for_wait(), 0) == WAIT_OBJECT_0)) {
tommi0b942152017-03-10 09:33:53 -0800361 // The multimedia timer was signaled.
362 timer_.Cancel();
tommi0b942152017-03-10 09:33:53 -0800363 RunDueTasks();
364 ScheduleNextTimer();
tommi83722262017-03-15 04:36:29 -0700365 }
366
367 if (result == (WAIT_OBJECT_0 + 1)) {
368 ::ResetEvent(in_queue_);
nisse341c8e42017-09-06 04:38:22 -0700369 TaskQueue::Impl::Current()->RunPendingTasks();
tommib89257a2016-07-12 01:24:36 -0700370 }
371 }
tommib89257a2016-07-12 01:24:36 -0700372}
tommic06b1332016-05-14 11:31:40 -0700373
nisse341c8e42017-09-06 04:38:22 -0700374bool TaskQueue::Impl::ThreadState::ProcessQueuedMessages() {
tommib89257a2016-07-12 01:24:36 -0700375 MSG msg = {};
tommi83722262017-03-15 04:36:29 -0700376 // To protect against overly busy message queues, we limit the time
377 // we process tasks to a few milliseconds. If we don't do that, there's
378 // a chance that timer tasks won't ever run.
379 static const int kMaxTaskProcessingTimeMs = 500;
380 auto start = GetTick();
tommif9d91542017-02-17 02:47:11 -0800381 while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) &&
tommib89257a2016-07-12 01:24:36 -0700382 msg.message != WM_QUIT) {
tommic06b1332016-05-14 11:31:40 -0700383 if (!msg.hwnd) {
384 switch (msg.message) {
tommi83722262017-03-15 04:36:29 -0700385 // TODO(tommi): Stop using this way of queueing tasks.
tommic06b1332016-05-14 11:31:40 -0700386 case WM_RUN_TASK: {
387 QueuedTask* task = reinterpret_cast<QueuedTask*>(msg.lParam);
388 if (task->Run())
389 delete task;
390 break;
391 }
392 case WM_QUEUE_DELAYED_TASK: {
tommi0b942152017-03-10 09:33:53 -0800393 std::unique_ptr<DelayedTaskInfo> info(
394 reinterpret_cast<DelayedTaskInfo*>(msg.lParam));
395 bool need_to_schedule_timers =
396 timer_tasks_.empty() ||
397 timer_tasks_.top().due_time() > info->due_time();
398 timer_tasks_.emplace(std::move(*info.get()));
399 if (need_to_schedule_timers) {
400 CancelTimers();
401 ScheduleNextTimer();
tommif9d91542017-02-17 02:47:11 -0800402 }
tommic06b1332016-05-14 11:31:40 -0700403 break;
404 }
405 case WM_TIMER: {
tommi0b942152017-03-10 09:33:53 -0800406 RTC_DCHECK_EQ(timer_id_, msg.wParam);
tommif9d91542017-02-17 02:47:11 -0800407 ::KillTimer(nullptr, msg.wParam);
tommi0b942152017-03-10 09:33:53 -0800408 timer_id_ = 0;
409 RunDueTasks();
410 ScheduleNextTimer();
tommic06b1332016-05-14 11:31:40 -0700411 break;
412 }
413 default:
414 RTC_NOTREACHED();
415 break;
416 }
417 } else {
tommif9d91542017-02-17 02:47:11 -0800418 ::TranslateMessage(&msg);
419 ::DispatchMessage(&msg);
tommic06b1332016-05-14 11:31:40 -0700420 }
tommi83722262017-03-15 04:36:29 -0700421
422 if (GetTick() > start + kMaxTaskProcessingTimeMs)
423 break;
tommic06b1332016-05-14 11:31:40 -0700424 }
tommib89257a2016-07-12 01:24:36 -0700425 return msg.message != WM_QUIT;
tommic06b1332016-05-14 11:31:40 -0700426}
tommib89257a2016-07-12 01:24:36 -0700427
nisse341c8e42017-09-06 04:38:22 -0700428void TaskQueue::Impl::ThreadState::RunDueTasks() {
tommi0b942152017-03-10 09:33:53 -0800429 RTC_DCHECK(!timer_tasks_.empty());
430 auto now = GetTick();
431 do {
432 const auto& top = timer_tasks_.top();
433 if (top.due_time() > now)
434 break;
435 top.Run();
436 timer_tasks_.pop();
437 } while (!timer_tasks_.empty());
438}
439
nisse341c8e42017-09-06 04:38:22 -0700440void TaskQueue::Impl::ThreadState::ScheduleNextTimer() {
tommi0b942152017-03-10 09:33:53 -0800441 RTC_DCHECK_EQ(timer_id_, 0);
442 if (timer_tasks_.empty())
443 return;
444
445 const auto& next_task = timer_tasks_.top();
446 int64_t delay_ms = std::max(0ll, next_task.due_time() - GetTick());
447 uint32_t milliseconds = rtc::dchecked_cast<uint32_t>(delay_ms);
448 if (!timer_.StartOneShotTimer(milliseconds))
449 timer_id_ = ::SetTimer(nullptr, 0, milliseconds, nullptr);
450}
451
nisse341c8e42017-09-06 04:38:22 -0700452void TaskQueue::Impl::ThreadState::CancelTimers() {
tommi0b942152017-03-10 09:33:53 -0800453 timer_.Cancel();
454 if (timer_id_) {
455 ::KillTimer(nullptr, timer_id_);
456 timer_id_ = 0;
457 }
458}
459
nisse341c8e42017-09-06 04:38:22 -0700460// Boilerplate for the PIMPL pattern.
461TaskQueue::TaskQueue(const char* queue_name, Priority priority)
462 : impl_(new RefCountedObject<TaskQueue::Impl>(queue_name, this, priority)) {
463}
464
465TaskQueue::~TaskQueue() {}
466
467// static
468TaskQueue* TaskQueue::Current() {
469 return TaskQueue::Impl::CurrentQueue();
470}
471
472// Used for DCHECKing the current queue.
473bool TaskQueue::IsCurrent() const {
474 return impl_->IsCurrent();
475}
476
477void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) {
478 return TaskQueue::impl_->PostTask(std::move(task));
479}
480
nisse341c8e42017-09-06 04:38:22 -0700481void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task,
482 uint32_t milliseconds) {
483 return TaskQueue::impl_->PostDelayedTask(std::move(task), milliseconds);
484}
485
tommic06b1332016-05-14 11:31:40 -0700486} // namespace rtc