blob: bb8e522c71869037e926d5e6956fb53ff2916bea [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
Danil Chapovalov826f2e72019-02-20 18:13:09 +010011#include "rtc_base/task_queue_win.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>
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +020027#include <functional>
Mirko Bonadei317a1f02019-09-17 17:06:18 +020028#include <memory>
tommi0b942152017-03-10 09:33:53 -080029#include <queue>
Danil Chapovalov6f09ae22017-10-12 14:39:25 +020030#include <utility>
tommif9d91542017-02-17 02:47:11 -080031
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +020032#include "absl/functional/any_invocable.h"
Danil Chapovalov826f2e72019-02-20 18:13:09 +010033#include "absl/strings/string_view.h"
Markus Handellad5037b2021-05-07 15:02:36 +020034#include "absl/types/optional.h"
Danil Chapovalov826f2e72019-02-20 18:13:09 +010035#include "api/task_queue/task_queue_base.h"
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +020036#include "api/units/time_delta.h"
37#include "api/units/timestamp.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "rtc_base/arraysize.h"
39#include "rtc_base/checks.h"
40#include "rtc_base/event.h"
41#include "rtc_base/logging.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010042#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/platform_thread.h"
Markus Handell18523c32020-07-08 17:55:58 +020044#include "rtc_base/synchronization/mutex.h"
Markus Handellad5037b2021-05-07 15:02:36 +020045#include "rtc_base/time_utils.h"
tommic06b1332016-05-14 11:31:40 -070046
Danil Chapovalov826f2e72019-02-20 18:13:09 +010047namespace webrtc {
tommic06b1332016-05-14 11:31:40 -070048namespace {
tommic06b1332016-05-14 11:31:40 -070049#define WM_QUEUE_DELAYED_TASK WM_USER + 2
50
tommic06b1332016-05-14 11:31:40 -070051void CALLBACK InitializeQueueThread(ULONG_PTR param) {
52 MSG msg;
tommif9d91542017-02-17 02:47:11 -080053 ::PeekMessage(&msg, nullptr, WM_USER, WM_USER, PM_NOREMOVE);
Danil Chapovalov826f2e72019-02-20 18:13:09 +010054 rtc::Event* data = reinterpret_cast<rtc::Event*>(param);
55 data->Set();
tommic06b1332016-05-14 11:31:40 -070056}
tommic9bb7912017-02-24 10:42:14 -080057
Danil Chapovalov826f2e72019-02-20 18:13:09 +010058rtc::ThreadPriority TaskQueuePriorityToThreadPriority(
59 TaskQueueFactory::Priority priority) {
tommic9bb7912017-02-24 10:42:14 -080060 switch (priority) {
Danil Chapovalov826f2e72019-02-20 18:13:09 +010061 case TaskQueueFactory::Priority::HIGH:
Markus Handellad5037b2021-05-07 15:02:36 +020062 return rtc::ThreadPriority::kRealtime;
Danil Chapovalov826f2e72019-02-20 18:13:09 +010063 case TaskQueueFactory::Priority::LOW:
Markus Handellad5037b2021-05-07 15:02:36 +020064 return rtc::ThreadPriority::kLow;
Danil Chapovalov826f2e72019-02-20 18:13:09 +010065 case TaskQueueFactory::Priority::NORMAL:
Markus Handellad5037b2021-05-07 15:02:36 +020066 return rtc::ThreadPriority::kNormal;
tommic9bb7912017-02-24 10:42:14 -080067 }
tommic9bb7912017-02-24 10:42:14 -080068}
tommi5bdee472017-03-03 05:20:12 -080069
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +020070Timestamp CurrentTime() {
tommi5bdee472017-03-03 05:20:12 -080071 static const UINT kPeriod = 1;
72 bool high_res = (timeBeginPeriod(kPeriod) == TIMERR_NOERROR);
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +020073 Timestamp ret = Timestamp::Micros(rtc::TimeMicros());
tommi5bdee472017-03-03 05:20:12 -080074 if (high_res)
75 timeEndPeriod(kPeriod);
76 return ret;
77}
tommic06b1332016-05-14 11:31:40 -070078
tommi0b942152017-03-10 09:33:53 -080079class DelayedTaskInfo {
tommif9d91542017-02-17 02:47:11 -080080 public:
tommi0b942152017-03-10 09:33:53 -080081 // Default ctor needed to support priority_queue::pop().
82 DelayedTaskInfo() {}
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +020083 DelayedTaskInfo(TimeDelta delay, absl::AnyInvocable<void() &&> task)
84 : due_time_(CurrentTime() + delay), task_(std::move(task)) {}
tommi0b942152017-03-10 09:33:53 -080085 DelayedTaskInfo(DelayedTaskInfo&&) = default;
tommif9d91542017-02-17 02:47:11 -080086
tommi0b942152017-03-10 09:33:53 -080087 // Implement for priority_queue.
88 bool operator>(const DelayedTaskInfo& other) const {
89 return due_time_ > other.due_time_;
90 }
tommif9d91542017-02-17 02:47:11 -080091
tommi0b942152017-03-10 09:33:53 -080092 // Required by priority_queue::pop().
93 DelayedTaskInfo& operator=(DelayedTaskInfo&& other) = default;
94
95 // See below for why this method is const.
96 void Run() const {
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +020097 RTC_DCHECK(task_);
98 std::move(task_)();
tommi0b942152017-03-10 09:33:53 -080099 }
100
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200101 Timestamp due_time() const { return due_time_; }
tommi0b942152017-03-10 09:33:53 -0800102
103 private:
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200104 Timestamp due_time_ = Timestamp::Zero();
tommi0b942152017-03-10 09:33:53 -0800105
Artem Titov96e3b992021-07-26 16:03:14 +0200106 // `task` needs to be mutable because std::priority_queue::top() returns
tommi0b942152017-03-10 09:33:53 -0800107 // a const reference and a key in an ordered queue must not be changed.
108 // There are two basic workarounds, one using const_cast, which would also
Artem Titov96e3b992021-07-26 16:03:14 +0200109 // make the key (`due_time`), non-const and the other is to make the non-key
110 // (`task`), mutable.
111 // Because of this, the `task` variable is made private and can only be
112 // mutated by calling the `Run()` method.
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200113 mutable absl::AnyInvocable<void() &&> task_;
tommi0b942152017-03-10 09:33:53 -0800114};
115
116class MultimediaTimer {
117 public:
tommi83722262017-03-15 04:36:29 -0700118 // Note: We create an event that requires manual reset.
119 MultimediaTimer() : event_(::CreateEvent(nullptr, true, false, nullptr)) {}
tommif9d91542017-02-17 02:47:11 -0800120
tommi0b942152017-03-10 09:33:53 -0800121 ~MultimediaTimer() {
122 Cancel();
123 ::CloseHandle(event_);
tommif9d91542017-02-17 02:47:11 -0800124 }
125
Artem Titov6cae2d52022-01-26 15:01:10 +0000126 MultimediaTimer(const MultimediaTimer&) = delete;
127 MultimediaTimer& operator=(const MultimediaTimer&) = delete;
128
tommi0b942152017-03-10 09:33:53 -0800129 bool StartOneShotTimer(UINT delay_ms) {
tommif9d91542017-02-17 02:47:11 -0800130 RTC_DCHECK_EQ(0, timer_id_);
131 RTC_DCHECK(event_ != nullptr);
tommif9d91542017-02-17 02:47:11 -0800132 timer_id_ =
133 ::timeSetEvent(delay_ms, 0, reinterpret_cast<LPTIMECALLBACK>(event_), 0,
134 TIME_ONESHOT | TIME_CALLBACK_EVENT_SET);
135 return timer_id_ != 0;
136 }
137
tommi0b942152017-03-10 09:33:53 -0800138 void Cancel() {
tommif9d91542017-02-17 02:47:11 -0800139 if (timer_id_) {
140 ::timeKillEvent(timer_id_);
141 timer_id_ = 0;
142 }
Danil Chapovalovfa733932020-01-13 12:56:13 +0100143 // Now that timer is killed and not able to set the event, reset the event.
144 // Doing it in opposite order is racy because event may be set between
145 // event was reset and timer is killed leaving MultimediaTimer in surprising
146 // state where both event is set and timer is canceled.
147 ::ResetEvent(event_);
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
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100157class TaskQueueWin : public TaskQueueBase {
tommi0b942152017-03-10 09:33:53 -0800158 public:
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100159 TaskQueueWin(absl::string_view queue_name, rtc::ThreadPriority priority);
160 ~TaskQueueWin() override = default;
tommi0b942152017-03-10 09:33:53 -0800161
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100162 void Delete() override;
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200163 void PostTask(absl::AnyInvocable<void() &&> task) override;
164 void PostDelayedTask(absl::AnyInvocable<void() &&> task,
165 TimeDelta delay) override;
166 void PostDelayedHighPrecisionTask(absl::AnyInvocable<void() &&> task,
167 TimeDelta delay) override;
nisse341c8e42017-09-06 04:38:22 -0700168 void RunPendingTasks();
tommi0b942152017-03-10 09:33:53 -0800169
170 private:
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100171 void RunThreadMain();
172 bool ProcessQueuedMessages();
173 void RunDueTasks();
174 void ScheduleNextTimer();
175 void CancelTimers();
nisse341c8e42017-09-06 04:38:22 -0700176
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200177 MultimediaTimer timer_;
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100178 // Since priority_queue<> by defult orders items in terms of
179 // largest->smallest, using std::less<>, and we want smallest->largest,
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200180 // we would like to use std::greater<> here.
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100181 std::priority_queue<DelayedTaskInfo,
182 std::vector<DelayedTaskInfo>,
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200183 std::greater<DelayedTaskInfo>>
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100184 timer_tasks_;
185 UINT_PTR timer_id_ = 0;
Markus Handellad5037b2021-05-07 15:02:36 +0200186 rtc::PlatformThread thread_;
Markus Handell18523c32020-07-08 17:55:58 +0200187 Mutex pending_lock_;
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200188 std::queue<absl::AnyInvocable<void() &&>> pending_
danilchapa37de392017-09-09 04:17:22 -0700189 RTC_GUARDED_BY(pending_lock_);
tommi83722262017-03-15 04:36:29 -0700190 HANDLE in_queue_;
tommi0b942152017-03-10 09:33:53 -0800191};
192
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100193TaskQueueWin::TaskQueueWin(absl::string_view queue_name,
194 rtc::ThreadPriority priority)
Markus Handellad5037b2021-05-07 15:02:36 +0200195 : in_queue_(::CreateEvent(nullptr, true, false, nullptr)) {
tommi83722262017-03-15 04:36:29 -0700196 RTC_DCHECK(in_queue_);
Markus Handellad5037b2021-05-07 15:02:36 +0200197 thread_ = rtc::PlatformThread::SpawnJoinable(
198 [this] { RunThreadMain(); }, queue_name,
199 rtc::ThreadAttributes().SetPriority(priority));
200
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100201 rtc::Event event(false, false);
tommic06b1332016-05-14 11:31:40 -0700202 RTC_CHECK(thread_.QueueAPC(&InitializeQueueThread,
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100203 reinterpret_cast<ULONG_PTR>(&event)));
204 event.Wait(rtc::Event::kForever);
tommic06b1332016-05-14 11:31:40 -0700205}
206
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100207void TaskQueueWin::Delete() {
tommic06b1332016-05-14 11:31:40 -0700208 RTC_DCHECK(!IsCurrent());
Markus Handellad5037b2021-05-07 15:02:36 +0200209 RTC_CHECK(thread_.GetHandle() != absl::nullopt);
210 while (
211 !::PostThreadMessage(GetThreadId(*thread_.GetHandle()), WM_QUIT, 0, 0)) {
kwiberg352444f2016-11-28 15:58:53 -0800212 RTC_CHECK_EQ(ERROR_NOT_ENOUGH_QUOTA, ::GetLastError());
tommic06b1332016-05-14 11:31:40 -0700213 Sleep(1);
214 }
Markus Handellad5037b2021-05-07 15:02:36 +0200215 thread_.Finalize();
tommi83722262017-03-15 04:36:29 -0700216 ::CloseHandle(in_queue_);
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100217 delete this;
tommic06b1332016-05-14 11:31:40 -0700218}
219
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200220void TaskQueueWin::PostTask(absl::AnyInvocable<void() &&> task) {
Markus Handell18523c32020-07-08 17:55:58 +0200221 MutexLock lock(&pending_lock_);
tommi83722262017-03-15 04:36:29 -0700222 pending_.push(std::move(task));
223 ::SetEvent(in_queue_);
tommic06b1332016-05-14 11:31:40 -0700224}
225
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200226void TaskQueueWin::PostDelayedTask(absl::AnyInvocable<void() &&> task,
227 TimeDelta delay) {
228 if (delay <= TimeDelta::Zero()) {
tommi0b942152017-03-10 09:33:53 -0800229 PostTask(std::move(task));
230 return;
231 }
232
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200233 auto* task_info = new DelayedTaskInfo(delay, std::move(task));
Markus Handellad5037b2021-05-07 15:02:36 +0200234 RTC_CHECK(thread_.GetHandle() != absl::nullopt);
235 if (!::PostThreadMessage(GetThreadId(*thread_.GetHandle()),
236 WM_QUEUE_DELAYED_TASK, 0,
tommi0b942152017-03-10 09:33:53 -0800237 reinterpret_cast<LPARAM>(task_info))) {
238 delete task_info;
tommic06b1332016-05-14 11:31:40 -0700239 }
240}
241
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200242void TaskQueueWin::PostDelayedHighPrecisionTask(
243 absl::AnyInvocable<void() &&> task,
244 TimeDelta delay) {
245 PostDelayedTask(std::move(task), delay);
246}
247
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100248void TaskQueueWin::RunPendingTasks() {
tommi83722262017-03-15 04:36:29 -0700249 while (true) {
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200250 absl::AnyInvocable<void() &&> task;
tommi83722262017-03-15 04:36:29 -0700251 {
Markus Handell18523c32020-07-08 17:55:58 +0200252 MutexLock lock(&pending_lock_);
tommi83722262017-03-15 04:36:29 -0700253 if (pending_.empty())
254 break;
255 task = std::move(pending_.front());
256 pending_.pop();
257 }
258
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200259 std::move(task)();
tommi83722262017-03-15 04:36:29 -0700260 }
261}
262
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100263void TaskQueueWin::RunThreadMain() {
264 CurrentTaskQueueSetter set_current(this);
Yves Gerey665174f2018-06-19 15:03:05 +0200265 HANDLE handles[2] = {*timer_.event_for_wait(), in_queue_};
tommib89257a2016-07-12 01:24:36 -0700266 while (true) {
tommif9d91542017-02-17 02:47:11 -0800267 // Make sure we do an alertable wait as that's required to allow APCs to run
268 // (e.g. required for InitializeQueueThread and stopping the thread in
269 // PlatformThread).
tommi0b942152017-03-10 09:33:53 -0800270 DWORD result = ::MsgWaitForMultipleObjectsEx(
tommi83722262017-03-15 04:36:29 -0700271 arraysize(handles), handles, INFINITE, QS_ALLEVENTS, MWMO_ALERTABLE);
tommib89257a2016-07-12 01:24:36 -0700272 RTC_CHECK_NE(WAIT_FAILED, result);
tommi83722262017-03-15 04:36:29 -0700273 if (result == (WAIT_OBJECT_0 + 2)) {
tommi0b942152017-03-10 09:33:53 -0800274 // There are messages in the message queue that need to be handled.
275 if (!ProcessQueuedMessages())
tommib89257a2016-07-12 01:24:36 -0700276 break;
tommi83722262017-03-15 04:36:29 -0700277 }
278
Yves Gerey665174f2018-06-19 15:03:05 +0200279 if (result == WAIT_OBJECT_0 ||
280 (!timer_tasks_.empty() &&
281 ::WaitForSingleObject(*timer_.event_for_wait(), 0) == WAIT_OBJECT_0)) {
tommi0b942152017-03-10 09:33:53 -0800282 // The multimedia timer was signaled.
283 timer_.Cancel();
tommi0b942152017-03-10 09:33:53 -0800284 RunDueTasks();
285 ScheduleNextTimer();
tommi83722262017-03-15 04:36:29 -0700286 }
287
288 if (result == (WAIT_OBJECT_0 + 1)) {
289 ::ResetEvent(in_queue_);
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100290 RunPendingTasks();
tommib89257a2016-07-12 01:24:36 -0700291 }
292 }
tommib89257a2016-07-12 01:24:36 -0700293}
tommic06b1332016-05-14 11:31:40 -0700294
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100295bool TaskQueueWin::ProcessQueuedMessages() {
tommib89257a2016-07-12 01:24:36 -0700296 MSG msg = {};
tommi83722262017-03-15 04:36:29 -0700297 // To protect against overly busy message queues, we limit the time
298 // we process tasks to a few milliseconds. If we don't do that, there's
299 // a chance that timer tasks won't ever run.
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200300 static constexpr TimeDelta kMaxTaskProcessingTime = TimeDelta::Millis(500);
301 Timestamp start = CurrentTime();
tommif9d91542017-02-17 02:47:11 -0800302 while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) &&
tommib89257a2016-07-12 01:24:36 -0700303 msg.message != WM_QUIT) {
tommic06b1332016-05-14 11:31:40 -0700304 if (!msg.hwnd) {
305 switch (msg.message) {
tommic06b1332016-05-14 11:31:40 -0700306 case WM_QUEUE_DELAYED_TASK: {
tommi0b942152017-03-10 09:33:53 -0800307 std::unique_ptr<DelayedTaskInfo> info(
308 reinterpret_cast<DelayedTaskInfo*>(msg.lParam));
309 bool need_to_schedule_timers =
310 timer_tasks_.empty() ||
311 timer_tasks_.top().due_time() > info->due_time();
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200312 timer_tasks_.push(std::move(*info));
tommi0b942152017-03-10 09:33:53 -0800313 if (need_to_schedule_timers) {
314 CancelTimers();
315 ScheduleNextTimer();
tommif9d91542017-02-17 02:47:11 -0800316 }
tommic06b1332016-05-14 11:31:40 -0700317 break;
318 }
319 case WM_TIMER: {
tommi0b942152017-03-10 09:33:53 -0800320 RTC_DCHECK_EQ(timer_id_, msg.wParam);
tommif9d91542017-02-17 02:47:11 -0800321 ::KillTimer(nullptr, msg.wParam);
tommi0b942152017-03-10 09:33:53 -0800322 timer_id_ = 0;
323 RunDueTasks();
324 ScheduleNextTimer();
tommic06b1332016-05-14 11:31:40 -0700325 break;
326 }
327 default:
Artem Titovd3251962021-11-15 16:57:07 +0100328 RTC_DCHECK_NOTREACHED();
tommic06b1332016-05-14 11:31:40 -0700329 break;
330 }
331 } else {
tommif9d91542017-02-17 02:47:11 -0800332 ::TranslateMessage(&msg);
333 ::DispatchMessage(&msg);
tommic06b1332016-05-14 11:31:40 -0700334 }
tommi83722262017-03-15 04:36:29 -0700335
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200336 if (CurrentTime() > start + kMaxTaskProcessingTime)
tommi83722262017-03-15 04:36:29 -0700337 break;
tommic06b1332016-05-14 11:31:40 -0700338 }
tommib89257a2016-07-12 01:24:36 -0700339 return msg.message != WM_QUIT;
tommic06b1332016-05-14 11:31:40 -0700340}
tommib89257a2016-07-12 01:24:36 -0700341
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100342void TaskQueueWin::RunDueTasks() {
tommi0b942152017-03-10 09:33:53 -0800343 RTC_DCHECK(!timer_tasks_.empty());
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200344 Timestamp now = CurrentTime();
tommi0b942152017-03-10 09:33:53 -0800345 do {
346 const auto& top = timer_tasks_.top();
347 if (top.due_time() > now)
348 break;
349 top.Run();
350 timer_tasks_.pop();
351 } while (!timer_tasks_.empty());
352}
353
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100354void TaskQueueWin::ScheduleNextTimer() {
tommi0b942152017-03-10 09:33:53 -0800355 RTC_DCHECK_EQ(timer_id_, 0);
356 if (timer_tasks_.empty())
357 return;
358
359 const auto& next_task = timer_tasks_.top();
Danil Chapovalov3c06cfc2022-07-20 13:38:58 +0200360 TimeDelta delay =
361 std::max(TimeDelta::Zero(), next_task.due_time() - CurrentTime());
362 uint32_t milliseconds = delay.RoundUpTo(TimeDelta::Millis(1)).ms<uint32_t>();
tommi0b942152017-03-10 09:33:53 -0800363 if (!timer_.StartOneShotTimer(milliseconds))
364 timer_id_ = ::SetTimer(nullptr, 0, milliseconds, nullptr);
365}
366
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100367void TaskQueueWin::CancelTimers() {
tommi0b942152017-03-10 09:33:53 -0800368 timer_.Cancel();
369 if (timer_id_) {
370 ::KillTimer(nullptr, timer_id_);
371 timer_id_ = 0;
372 }
373}
374
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100375class TaskQueueWinFactory : public TaskQueueFactory {
376 public:
377 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue(
378 absl::string_view name,
379 Priority priority) const override {
380 return std::unique_ptr<TaskQueueBase, TaskQueueDeleter>(
381 new TaskQueueWin(name, TaskQueuePriorityToThreadPriority(priority)));
382 }
383};
384
385} // namespace
386
387std::unique_ptr<TaskQueueFactory> CreateTaskQueueWinFactory() {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200388 return std::make_unique<TaskQueueWinFactory>();
nisse341c8e42017-09-06 04:38:22 -0700389}
390
Danil Chapovalov826f2e72019-02-20 18:13:09 +0100391} // namespace webrtc