blob: 00a8c79827573b7fde17d894879763430bce5c3c [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
kjellandere96c45b2017-06-30 10:45:21 -070011#include "webrtc/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
kjellandere96c45b2017-06-30 10:45:21 -070019#include "webrtc/rtc_base/arraysize.h"
20#include "webrtc/rtc_base/checks.h"
nisse341c8e42017-09-06 04:38:22 -070021#include "webrtc/rtc_base/event.h"
kjellandere96c45b2017-06-30 10:45:21 -070022#include "webrtc/rtc_base/logging.h"
nisse341c8e42017-09-06 04:38:22 -070023#include "webrtc/rtc_base/platform_thread.h"
24#include "webrtc/rtc_base/refcount.h"
25#include "webrtc/rtc_base/refcountedobject.h"
kjellandere96c45b2017-06-30 10:45:21 -070026#include "webrtc/rtc_base/safe_conversions.h"
27#include "webrtc/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_;
239 std::queue<std::unique_ptr<QueuedTask>> pending_ GUARDED_BY(pending_lock_);
tommi83722262017-03-15 04:36:29 -0700240 HANDLE in_queue_;
tommi0b942152017-03-10 09:33:53 -0800241};
242
nisse341c8e42017-09-06 04:38:22 -0700243TaskQueue::Impl::Impl(const char* queue_name,
244 TaskQueue* queue,
245 Priority priority)
246 : queue_(queue),
247 thread_(&TaskQueue::Impl::ThreadMain,
tommic9bb7912017-02-24 10:42:14 -0800248 this,
249 queue_name,
tommi83722262017-03-15 04:36:29 -0700250 TaskQueuePriorityToThreadPriority(priority)),
nisse341c8e42017-09-06 04:38:22 -0700251 in_queue_(::CreateEvent(nullptr, true, false, nullptr)) {
tommic06b1332016-05-14 11:31:40 -0700252 RTC_DCHECK(queue_name);
tommi83722262017-03-15 04:36:29 -0700253 RTC_DCHECK(in_queue_);
tommic06b1332016-05-14 11:31:40 -0700254 thread_.Start();
255 Event event(false, false);
256 ThreadStartupData startup = {&event, this};
257 RTC_CHECK(thread_.QueueAPC(&InitializeQueueThread,
258 reinterpret_cast<ULONG_PTR>(&startup)));
259 event.Wait(Event::kForever);
260}
261
nisse341c8e42017-09-06 04:38:22 -0700262TaskQueue::Impl::~Impl() {
tommic06b1332016-05-14 11:31:40 -0700263 RTC_DCHECK(!IsCurrent());
tommif9d91542017-02-17 02:47:11 -0800264 while (!::PostThreadMessage(thread_.GetThreadRef(), WM_QUIT, 0, 0)) {
kwiberg352444f2016-11-28 15:58:53 -0800265 RTC_CHECK_EQ(ERROR_NOT_ENOUGH_QUOTA, ::GetLastError());
tommic06b1332016-05-14 11:31:40 -0700266 Sleep(1);
267 }
268 thread_.Stop();
tommi83722262017-03-15 04:36:29 -0700269 ::CloseHandle(in_queue_);
tommic06b1332016-05-14 11:31:40 -0700270}
271
272// static
nisse341c8e42017-09-06 04:38:22 -0700273TaskQueue::Impl* TaskQueue::Impl::Current() {
274 return static_cast<TaskQueue::Impl*>(::TlsGetValue(GetQueuePtrTls()));
tommic06b1332016-05-14 11:31:40 -0700275}
276
nisse341c8e42017-09-06 04:38:22 -0700277// static
278TaskQueue* TaskQueue::Impl::CurrentQueue() {
279 TaskQueue::Impl* current = Current();
280 return current ? current->queue_ : nullptr;
281}
282
283bool TaskQueue::Impl::IsCurrent() const {
tommic06b1332016-05-14 11:31:40 -0700284 return IsThreadRefEqual(thread_.GetThreadRef(), CurrentThreadRef());
285}
286
nisse341c8e42017-09-06 04:38:22 -0700287void TaskQueue::Impl::PostTask(std::unique_ptr<QueuedTask> task) {
tommi83722262017-03-15 04:36:29 -0700288 rtc::CritScope lock(&pending_lock_);
289 pending_.push(std::move(task));
290 ::SetEvent(in_queue_);
tommic06b1332016-05-14 11:31:40 -0700291}
292
nisse341c8e42017-09-06 04:38:22 -0700293void TaskQueue::Impl::PostDelayedTask(std::unique_ptr<QueuedTask> task,
294 uint32_t milliseconds) {
tommi0b942152017-03-10 09:33:53 -0800295 if (!milliseconds) {
296 PostTask(std::move(task));
297 return;
298 }
299
300 // TODO(tommi): Avoid this allocation. It is currently here since
301 // the timestamp stored in the task info object, is a 64bit timestamp
302 // and WPARAM is 32bits in 32bit builds. Otherwise, we could pass the
303 // task pointer and timestamp as LPARAM and WPARAM.
304 auto* task_info = new DelayedTaskInfo(milliseconds, std::move(task));
305 if (!::PostThreadMessage(thread_.GetThreadRef(), WM_QUEUE_DELAYED_TASK, 0,
306 reinterpret_cast<LPARAM>(task_info))) {
307 delete task_info;
tommic06b1332016-05-14 11:31:40 -0700308 }
309}
310
nisse341c8e42017-09-06 04:38:22 -0700311void TaskQueue::Impl::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
312 std::unique_ptr<QueuedTask> reply,
313 TaskQueue::Impl* reply_queue) {
tommic06b1332016-05-14 11:31:40 -0700314 QueuedTask* task_ptr = task.release();
315 QueuedTask* reply_task_ptr = reply.release();
316 DWORD reply_thread_id = reply_queue->thread_.GetThreadRef();
317 PostTask([task_ptr, reply_task_ptr, reply_thread_id]() {
318 if (task_ptr->Run())
319 delete task_ptr;
320 // If the thread's message queue is full, we can't queue the task and will
321 // have to drop it (i.e. delete).
tommif9d91542017-02-17 02:47:11 -0800322 if (!::PostThreadMessage(reply_thread_id, WM_RUN_TASK, 0,
323 reinterpret_cast<LPARAM>(reply_task_ptr))) {
tommic06b1332016-05-14 11:31:40 -0700324 delete reply_task_ptr;
325 }
326 });
327}
328
nisse341c8e42017-09-06 04:38:22 -0700329void TaskQueue::Impl::RunPendingTasks() {
tommi83722262017-03-15 04:36:29 -0700330 while (true) {
331 std::unique_ptr<QueuedTask> task;
332 {
333 rtc::CritScope lock(&pending_lock_);
334 if (pending_.empty())
335 break;
336 task = std::move(pending_.front());
337 pending_.pop();
338 }
339
340 if (!task->Run())
341 task.release();
342 }
343}
344
tommic06b1332016-05-14 11:31:40 -0700345// static
nisse341c8e42017-09-06 04:38:22 -0700346void TaskQueue::Impl::ThreadMain(void* context) {
347 ThreadState state(static_cast<TaskQueue::Impl*>(context)->in_queue_);
tommi0b942152017-03-10 09:33:53 -0800348 state.RunThreadMain();
349}
tommif9d91542017-02-17 02:47:11 -0800350
nisse341c8e42017-09-06 04:38:22 -0700351void TaskQueue::Impl::ThreadState::RunThreadMain() {
tommi83722262017-03-15 04:36:29 -0700352 HANDLE handles[2] = { *timer_.event_for_wait(), in_queue_ };
tommib89257a2016-07-12 01:24:36 -0700353 while (true) {
tommif9d91542017-02-17 02:47:11 -0800354 // Make sure we do an alertable wait as that's required to allow APCs to run
355 // (e.g. required for InitializeQueueThread and stopping the thread in
356 // PlatformThread).
tommi0b942152017-03-10 09:33:53 -0800357 DWORD result = ::MsgWaitForMultipleObjectsEx(
tommi83722262017-03-15 04:36:29 -0700358 arraysize(handles), handles, INFINITE, QS_ALLEVENTS, MWMO_ALERTABLE);
tommib89257a2016-07-12 01:24:36 -0700359 RTC_CHECK_NE(WAIT_FAILED, result);
tommi83722262017-03-15 04:36:29 -0700360 if (result == (WAIT_OBJECT_0 + 2)) {
tommi0b942152017-03-10 09:33:53 -0800361 // There are messages in the message queue that need to be handled.
362 if (!ProcessQueuedMessages())
tommib89257a2016-07-12 01:24:36 -0700363 break;
tommi83722262017-03-15 04:36:29 -0700364 }
365
366 if (result == WAIT_OBJECT_0 || (!timer_tasks_.empty() &&
367 ::WaitForSingleObject(*timer_.event_for_wait(), 0) == WAIT_OBJECT_0)) {
tommi0b942152017-03-10 09:33:53 -0800368 // The multimedia timer was signaled.
369 timer_.Cancel();
tommi0b942152017-03-10 09:33:53 -0800370 RunDueTasks();
371 ScheduleNextTimer();
tommi83722262017-03-15 04:36:29 -0700372 }
373
374 if (result == (WAIT_OBJECT_0 + 1)) {
375 ::ResetEvent(in_queue_);
nisse341c8e42017-09-06 04:38:22 -0700376 TaskQueue::Impl::Current()->RunPendingTasks();
tommib89257a2016-07-12 01:24:36 -0700377 }
378 }
tommib89257a2016-07-12 01:24:36 -0700379}
tommic06b1332016-05-14 11:31:40 -0700380
nisse341c8e42017-09-06 04:38:22 -0700381bool TaskQueue::Impl::ThreadState::ProcessQueuedMessages() {
tommib89257a2016-07-12 01:24:36 -0700382 MSG msg = {};
tommi83722262017-03-15 04:36:29 -0700383 // To protect against overly busy message queues, we limit the time
384 // we process tasks to a few milliseconds. If we don't do that, there's
385 // a chance that timer tasks won't ever run.
386 static const int kMaxTaskProcessingTimeMs = 500;
387 auto start = GetTick();
tommif9d91542017-02-17 02:47:11 -0800388 while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) &&
tommib89257a2016-07-12 01:24:36 -0700389 msg.message != WM_QUIT) {
tommic06b1332016-05-14 11:31:40 -0700390 if (!msg.hwnd) {
391 switch (msg.message) {
tommi83722262017-03-15 04:36:29 -0700392 // TODO(tommi): Stop using this way of queueing tasks.
tommic06b1332016-05-14 11:31:40 -0700393 case WM_RUN_TASK: {
394 QueuedTask* task = reinterpret_cast<QueuedTask*>(msg.lParam);
395 if (task->Run())
396 delete task;
397 break;
398 }
399 case WM_QUEUE_DELAYED_TASK: {
tommi0b942152017-03-10 09:33:53 -0800400 std::unique_ptr<DelayedTaskInfo> info(
401 reinterpret_cast<DelayedTaskInfo*>(msg.lParam));
402 bool need_to_schedule_timers =
403 timer_tasks_.empty() ||
404 timer_tasks_.top().due_time() > info->due_time();
405 timer_tasks_.emplace(std::move(*info.get()));
406 if (need_to_schedule_timers) {
407 CancelTimers();
408 ScheduleNextTimer();
tommif9d91542017-02-17 02:47:11 -0800409 }
tommic06b1332016-05-14 11:31:40 -0700410 break;
411 }
412 case WM_TIMER: {
tommi0b942152017-03-10 09:33:53 -0800413 RTC_DCHECK_EQ(timer_id_, msg.wParam);
tommif9d91542017-02-17 02:47:11 -0800414 ::KillTimer(nullptr, msg.wParam);
tommi0b942152017-03-10 09:33:53 -0800415 timer_id_ = 0;
416 RunDueTasks();
417 ScheduleNextTimer();
tommic06b1332016-05-14 11:31:40 -0700418 break;
419 }
420 default:
421 RTC_NOTREACHED();
422 break;
423 }
424 } else {
tommif9d91542017-02-17 02:47:11 -0800425 ::TranslateMessage(&msg);
426 ::DispatchMessage(&msg);
tommic06b1332016-05-14 11:31:40 -0700427 }
tommi83722262017-03-15 04:36:29 -0700428
429 if (GetTick() > start + kMaxTaskProcessingTimeMs)
430 break;
tommic06b1332016-05-14 11:31:40 -0700431 }
tommib89257a2016-07-12 01:24:36 -0700432 return msg.message != WM_QUIT;
tommic06b1332016-05-14 11:31:40 -0700433}
tommib89257a2016-07-12 01:24:36 -0700434
nisse341c8e42017-09-06 04:38:22 -0700435void TaskQueue::Impl::ThreadState::RunDueTasks() {
tommi0b942152017-03-10 09:33:53 -0800436 RTC_DCHECK(!timer_tasks_.empty());
437 auto now = GetTick();
438 do {
439 const auto& top = timer_tasks_.top();
440 if (top.due_time() > now)
441 break;
442 top.Run();
443 timer_tasks_.pop();
444 } while (!timer_tasks_.empty());
445}
446
nisse341c8e42017-09-06 04:38:22 -0700447void TaskQueue::Impl::ThreadState::ScheduleNextTimer() {
tommi0b942152017-03-10 09:33:53 -0800448 RTC_DCHECK_EQ(timer_id_, 0);
449 if (timer_tasks_.empty())
450 return;
451
452 const auto& next_task = timer_tasks_.top();
453 int64_t delay_ms = std::max(0ll, next_task.due_time() - GetTick());
454 uint32_t milliseconds = rtc::dchecked_cast<uint32_t>(delay_ms);
455 if (!timer_.StartOneShotTimer(milliseconds))
456 timer_id_ = ::SetTimer(nullptr, 0, milliseconds, nullptr);
457}
458
nisse341c8e42017-09-06 04:38:22 -0700459void TaskQueue::Impl::ThreadState::CancelTimers() {
tommi0b942152017-03-10 09:33:53 -0800460 timer_.Cancel();
461 if (timer_id_) {
462 ::KillTimer(nullptr, timer_id_);
463 timer_id_ = 0;
464 }
465}
466
nisse341c8e42017-09-06 04:38:22 -0700467// Boilerplate for the PIMPL pattern.
468TaskQueue::TaskQueue(const char* queue_name, Priority priority)
469 : impl_(new RefCountedObject<TaskQueue::Impl>(queue_name, this, priority)) {
470}
471
472TaskQueue::~TaskQueue() {}
473
474// static
475TaskQueue* TaskQueue::Current() {
476 return TaskQueue::Impl::CurrentQueue();
477}
478
479// Used for DCHECKing the current queue.
480bool TaskQueue::IsCurrent() const {
481 return impl_->IsCurrent();
482}
483
484void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) {
485 return TaskQueue::impl_->PostTask(std::move(task));
486}
487
488void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
489 std::unique_ptr<QueuedTask> reply,
490 TaskQueue* reply_queue) {
491 return TaskQueue::impl_->PostTaskAndReply(std::move(task), std::move(reply),
492 reply_queue->impl_.get());
493}
494
495void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task,
496 std::unique_ptr<QueuedTask> reply) {
497 return TaskQueue::impl_->PostTaskAndReply(std::move(task), std::move(reply),
498 impl_.get());
499}
500
501void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task,
502 uint32_t milliseconds) {
503 return TaskQueue::impl_->PostDelayedTask(std::move(task), milliseconds);
504}
505
tommic06b1332016-05-14 11:31:40 -0700506} // namespace rtc