tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 1 | /* |
| 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 Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 11 | #include "rtc_base/task_queue_win.h" |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 12 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 13 | // clang-format off |
| 14 | // clang formating would change include order. |
| 15 | |
Danil Chapovalov | 02fddf6 | 2018-02-12 11:41:16 | [diff] [blame] | 16 | // Include winsock2.h before including <windows.h> to maintain consistency with |
Niels Möller | b06b0a6 | 2018-05-25 08:05:34 | [diff] [blame] | 17 | // win32.h. To include win32.h directly, it must be broken out into its own |
| 18 | // build target. |
Danil Chapovalov | 02fddf6 | 2018-02-12 11:41:16 | [diff] [blame] | 19 | #include <winsock2.h> |
| 20 | #include <windows.h> |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 21 | #include <sal.h> // Must come after windows headers. |
Danil Chapovalov | 02fddf6 | 2018-02-12 11:41:16 | [diff] [blame] | 22 | #include <mmsystem.h> // Must come after windows headers. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 23 | // clang-format on |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 24 | #include <string.h> |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 25 | |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 26 | #include <algorithm> |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 27 | #include <memory> |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 28 | #include <queue> |
Danil Chapovalov | 6f09ae2 | 2017-10-12 12:39:25 | [diff] [blame] | 29 | #include <utility> |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 30 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 31 | #include "absl/strings/string_view.h" |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 32 | #include "absl/types/optional.h" |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 33 | #include "api/task_queue/queued_task.h" |
| 34 | #include "api/task_queue/task_queue_base.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 35 | #include "rtc_base/arraysize.h" |
| 36 | #include "rtc_base/checks.h" |
| 37 | #include "rtc_base/event.h" |
| 38 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 09:42:26 | [diff] [blame] | 39 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 40 | #include "rtc_base/platform_thread.h" |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 41 | #include "rtc_base/synchronization/mutex.h" |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 42 | #include "rtc_base/time_utils.h" |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 43 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 44 | namespace webrtc { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 45 | namespace { |
| 46 | #define WM_RUN_TASK WM_USER + 1 |
| 47 | #define WM_QUEUE_DELAYED_TASK WM_USER + 2 |
| 48 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 49 | void CALLBACK InitializeQueueThread(ULONG_PTR param) { |
| 50 | MSG msg; |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 51 | ::PeekMessage(&msg, nullptr, WM_USER, WM_USER, PM_NOREMOVE); |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 52 | rtc::Event* data = reinterpret_cast<rtc::Event*>(param); |
| 53 | data->Set(); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 54 | } |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 55 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 56 | rtc::ThreadPriority TaskQueuePriorityToThreadPriority( |
| 57 | TaskQueueFactory::Priority priority) { |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 58 | switch (priority) { |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 59 | case TaskQueueFactory::Priority::HIGH: |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 60 | return rtc::ThreadPriority::kRealtime; |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 61 | case TaskQueueFactory::Priority::LOW: |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 62 | return rtc::ThreadPriority::kLow; |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 63 | case TaskQueueFactory::Priority::NORMAL: |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 64 | return rtc::ThreadPriority::kNormal; |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 65 | } |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 66 | } |
tommi | 5bdee47 | 2017-03-03 13:20:12 | [diff] [blame] | 67 | |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 68 | int64_t GetTick() { |
tommi | 5bdee47 | 2017-03-03 13:20:12 | [diff] [blame] | 69 | static const UINT kPeriod = 1; |
| 70 | bool high_res = (timeBeginPeriod(kPeriod) == TIMERR_NOERROR); |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 71 | int64_t ret = rtc::TimeMillis(); |
tommi | 5bdee47 | 2017-03-03 13:20:12 | [diff] [blame] | 72 | if (high_res) |
| 73 | timeEndPeriod(kPeriod); |
| 74 | return ret; |
| 75 | } |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 76 | |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 77 | class DelayedTaskInfo { |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 78 | public: |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 79 | // Default ctor needed to support priority_queue::pop(). |
| 80 | DelayedTaskInfo() {} |
| 81 | DelayedTaskInfo(uint32_t milliseconds, std::unique_ptr<QueuedTask> task) |
| 82 | : due_time_(GetTick() + milliseconds), task_(std::move(task)) {} |
| 83 | DelayedTaskInfo(DelayedTaskInfo&&) = default; |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 84 | |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 85 | // Implement for priority_queue. |
| 86 | bool operator>(const DelayedTaskInfo& other) const { |
| 87 | return due_time_ > other.due_time_; |
| 88 | } |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 89 | |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 90 | // Required by priority_queue::pop(). |
| 91 | DelayedTaskInfo& operator=(DelayedTaskInfo&& other) = default; |
| 92 | |
| 93 | // See below for why this method is const. |
| 94 | void Run() const { |
| 95 | RTC_DCHECK(due_time_); |
| 96 | task_->Run() ? task_.reset() : static_cast<void>(task_.release()); |
| 97 | } |
| 98 | |
| 99 | int64_t due_time() const { return due_time_; } |
| 100 | |
| 101 | private: |
| 102 | int64_t due_time_ = 0; // Absolute timestamp in milliseconds. |
| 103 | |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 104 | // `task` needs to be mutable because std::priority_queue::top() returns |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 105 | // a const reference and a key in an ordered queue must not be changed. |
| 106 | // There are two basic workarounds, one using const_cast, which would also |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 107 | // make the key (`due_time`), non-const and the other is to make the non-key |
| 108 | // (`task`), mutable. |
| 109 | // Because of this, the `task` variable is made private and can only be |
| 110 | // mutated by calling the `Run()` method. |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 111 | mutable std::unique_ptr<QueuedTask> task_; |
| 112 | }; |
| 113 | |
| 114 | class MultimediaTimer { |
| 115 | public: |
tommi | 8372226 | 2017-03-15 11:36:29 | [diff] [blame] | 116 | // Note: We create an event that requires manual reset. |
| 117 | MultimediaTimer() : event_(::CreateEvent(nullptr, true, false, nullptr)) {} |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 118 | |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 119 | ~MultimediaTimer() { |
| 120 | Cancel(); |
| 121 | ::CloseHandle(event_); |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 122 | } |
| 123 | |
Artem Titov | 6cae2d5 | 2022-01-26 15:01:10 | [diff] [blame] | 124 | MultimediaTimer(const MultimediaTimer&) = delete; |
| 125 | MultimediaTimer& operator=(const MultimediaTimer&) = delete; |
| 126 | |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 127 | bool StartOneShotTimer(UINT delay_ms) { |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 128 | RTC_DCHECK_EQ(0, timer_id_); |
| 129 | RTC_DCHECK(event_ != nullptr); |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 130 | timer_id_ = |
| 131 | ::timeSetEvent(delay_ms, 0, reinterpret_cast<LPTIMECALLBACK>(event_), 0, |
| 132 | TIME_ONESHOT | TIME_CALLBACK_EVENT_SET); |
| 133 | return timer_id_ != 0; |
| 134 | } |
| 135 | |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 136 | void Cancel() { |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 137 | if (timer_id_) { |
| 138 | ::timeKillEvent(timer_id_); |
| 139 | timer_id_ = 0; |
| 140 | } |
Danil Chapovalov | fa73393 | 2020-01-13 11:56:13 | [diff] [blame] | 141 | // Now that timer is killed and not able to set the event, reset the event. |
| 142 | // Doing it in opposite order is racy because event may be set between |
| 143 | // event was reset and timer is killed leaving MultimediaTimer in surprising |
| 144 | // state where both event is set and timer is canceled. |
| 145 | ::ResetEvent(event_); |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 146 | } |
| 147 | |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 148 | HANDLE* event_for_wait() { return &event_; } |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 149 | |
| 150 | private: |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 151 | HANDLE event_ = nullptr; |
| 152 | MMRESULT timer_id_ = 0; |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 153 | }; |
| 154 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 155 | class TaskQueueWin : public TaskQueueBase { |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 156 | public: |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 157 | TaskQueueWin(absl::string_view queue_name, rtc::ThreadPriority priority); |
| 158 | ~TaskQueueWin() override = default; |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 159 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 160 | void Delete() override; |
| 161 | void PostTask(std::unique_ptr<QueuedTask> task) override; |
| 162 | void PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 163 | uint32_t milliseconds) override; |
nisse | 341c8e40 | 2017-09-06 11:38:22 | [diff] [blame] | 164 | |
| 165 | void RunPendingTasks(); |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 166 | |
| 167 | private: |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 168 | void RunThreadMain(); |
| 169 | bool ProcessQueuedMessages(); |
| 170 | void RunDueTasks(); |
| 171 | void ScheduleNextTimer(); |
| 172 | void CancelTimers(); |
nisse | 341c8e40 | 2017-09-06 11:38:22 | [diff] [blame] | 173 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 174 | // Since priority_queue<> by defult orders items in terms of |
| 175 | // largest->smallest, using std::less<>, and we want smallest->largest, |
| 176 | // we would like to use std::greater<> here. Alas it's only available in |
| 177 | // C++14 and later, so we roll our own compare template that that relies on |
| 178 | // operator<(). |
| 179 | template <typename T> |
| 180 | struct greater { |
| 181 | bool operator()(const T& l, const T& r) { return l > r; } |
nisse | 341c8e40 | 2017-09-06 11:38:22 | [diff] [blame] | 182 | }; |
| 183 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 184 | MultimediaTimer timer_; |
| 185 | std::priority_queue<DelayedTaskInfo, |
| 186 | std::vector<DelayedTaskInfo>, |
| 187 | greater<DelayedTaskInfo>> |
| 188 | timer_tasks_; |
| 189 | UINT_PTR timer_id_ = 0; |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 190 | rtc::PlatformThread thread_; |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 191 | Mutex pending_lock_; |
danilchap | a37de39 | 2017-09-09 11:17:22 | [diff] [blame] | 192 | std::queue<std::unique_ptr<QueuedTask>> pending_ |
| 193 | RTC_GUARDED_BY(pending_lock_); |
tommi | 8372226 | 2017-03-15 11:36:29 | [diff] [blame] | 194 | HANDLE in_queue_; |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 195 | }; |
| 196 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 197 | TaskQueueWin::TaskQueueWin(absl::string_view queue_name, |
| 198 | rtc::ThreadPriority priority) |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 199 | : in_queue_(::CreateEvent(nullptr, true, false, nullptr)) { |
tommi | 8372226 | 2017-03-15 11:36:29 | [diff] [blame] | 200 | RTC_DCHECK(in_queue_); |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 201 | thread_ = rtc::PlatformThread::SpawnJoinable( |
| 202 | [this] { RunThreadMain(); }, queue_name, |
| 203 | rtc::ThreadAttributes().SetPriority(priority)); |
| 204 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 205 | rtc::Event event(false, false); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 206 | RTC_CHECK(thread_.QueueAPC(&InitializeQueueThread, |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 207 | reinterpret_cast<ULONG_PTR>(&event))); |
| 208 | event.Wait(rtc::Event::kForever); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 209 | } |
| 210 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 211 | void TaskQueueWin::Delete() { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 212 | RTC_DCHECK(!IsCurrent()); |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 213 | RTC_CHECK(thread_.GetHandle() != absl::nullopt); |
| 214 | while ( |
| 215 | !::PostThreadMessage(GetThreadId(*thread_.GetHandle()), WM_QUIT, 0, 0)) { |
kwiberg | 352444f | 2016-11-28 23:58:53 | [diff] [blame] | 216 | RTC_CHECK_EQ(ERROR_NOT_ENOUGH_QUOTA, ::GetLastError()); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 217 | Sleep(1); |
| 218 | } |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 219 | thread_.Finalize(); |
tommi | 8372226 | 2017-03-15 11:36:29 | [diff] [blame] | 220 | ::CloseHandle(in_queue_); |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 221 | delete this; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 222 | } |
| 223 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 224 | void TaskQueueWin::PostTask(std::unique_ptr<QueuedTask> task) { |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 225 | MutexLock lock(&pending_lock_); |
tommi | 8372226 | 2017-03-15 11:36:29 | [diff] [blame] | 226 | pending_.push(std::move(task)); |
| 227 | ::SetEvent(in_queue_); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 228 | } |
| 229 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 230 | void TaskQueueWin::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 231 | uint32_t milliseconds) { |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 232 | if (!milliseconds) { |
| 233 | PostTask(std::move(task)); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | // TODO(tommi): Avoid this allocation. It is currently here since |
| 238 | // the timestamp stored in the task info object, is a 64bit timestamp |
| 239 | // and WPARAM is 32bits in 32bit builds. Otherwise, we could pass the |
| 240 | // task pointer and timestamp as LPARAM and WPARAM. |
| 241 | auto* task_info = new DelayedTaskInfo(milliseconds, std::move(task)); |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 242 | RTC_CHECK(thread_.GetHandle() != absl::nullopt); |
| 243 | if (!::PostThreadMessage(GetThreadId(*thread_.GetHandle()), |
| 244 | WM_QUEUE_DELAYED_TASK, 0, |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 245 | reinterpret_cast<LPARAM>(task_info))) { |
| 246 | delete task_info; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 250 | void TaskQueueWin::RunPendingTasks() { |
tommi | 8372226 | 2017-03-15 11:36:29 | [diff] [blame] | 251 | while (true) { |
| 252 | std::unique_ptr<QueuedTask> task; |
| 253 | { |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 254 | MutexLock lock(&pending_lock_); |
tommi | 8372226 | 2017-03-15 11:36:29 | [diff] [blame] | 255 | if (pending_.empty()) |
| 256 | break; |
| 257 | task = std::move(pending_.front()); |
| 258 | pending_.pop(); |
| 259 | } |
| 260 | |
| 261 | if (!task->Run()) |
| 262 | task.release(); |
| 263 | } |
| 264 | } |
| 265 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 266 | void TaskQueueWin::RunThreadMain() { |
| 267 | CurrentTaskQueueSetter set_current(this); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 268 | HANDLE handles[2] = {*timer_.event_for_wait(), in_queue_}; |
tommi | b89257a | 2016-07-12 08:24:36 | [diff] [blame] | 269 | while (true) { |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 270 | // Make sure we do an alertable wait as that's required to allow APCs to run |
| 271 | // (e.g. required for InitializeQueueThread and stopping the thread in |
| 272 | // PlatformThread). |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 273 | DWORD result = ::MsgWaitForMultipleObjectsEx( |
tommi | 8372226 | 2017-03-15 11:36:29 | [diff] [blame] | 274 | arraysize(handles), handles, INFINITE, QS_ALLEVENTS, MWMO_ALERTABLE); |
tommi | b89257a | 2016-07-12 08:24:36 | [diff] [blame] | 275 | RTC_CHECK_NE(WAIT_FAILED, result); |
tommi | 8372226 | 2017-03-15 11:36:29 | [diff] [blame] | 276 | if (result == (WAIT_OBJECT_0 + 2)) { |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 277 | // There are messages in the message queue that need to be handled. |
| 278 | if (!ProcessQueuedMessages()) |
tommi | b89257a | 2016-07-12 08:24:36 | [diff] [blame] | 279 | break; |
tommi | 8372226 | 2017-03-15 11:36:29 | [diff] [blame] | 280 | } |
| 281 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 282 | if (result == WAIT_OBJECT_0 || |
| 283 | (!timer_tasks_.empty() && |
| 284 | ::WaitForSingleObject(*timer_.event_for_wait(), 0) == WAIT_OBJECT_0)) { |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 285 | // The multimedia timer was signaled. |
| 286 | timer_.Cancel(); |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 287 | RunDueTasks(); |
| 288 | ScheduleNextTimer(); |
tommi | 8372226 | 2017-03-15 11:36:29 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | if (result == (WAIT_OBJECT_0 + 1)) { |
| 292 | ::ResetEvent(in_queue_); |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 293 | RunPendingTasks(); |
tommi | b89257a | 2016-07-12 08:24:36 | [diff] [blame] | 294 | } |
| 295 | } |
tommi | b89257a | 2016-07-12 08:24:36 | [diff] [blame] | 296 | } |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 297 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 298 | bool TaskQueueWin::ProcessQueuedMessages() { |
tommi | b89257a | 2016-07-12 08:24:36 | [diff] [blame] | 299 | MSG msg = {}; |
tommi | 8372226 | 2017-03-15 11:36:29 | [diff] [blame] | 300 | // To protect against overly busy message queues, we limit the time |
| 301 | // we process tasks to a few milliseconds. If we don't do that, there's |
| 302 | // a chance that timer tasks won't ever run. |
| 303 | static const int kMaxTaskProcessingTimeMs = 500; |
| 304 | auto start = GetTick(); |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 305 | while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) && |
tommi | b89257a | 2016-07-12 08:24:36 | [diff] [blame] | 306 | msg.message != WM_QUIT) { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 307 | if (!msg.hwnd) { |
| 308 | switch (msg.message) { |
tommi | 8372226 | 2017-03-15 11:36:29 | [diff] [blame] | 309 | // TODO(tommi): Stop using this way of queueing tasks. |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 310 | case WM_RUN_TASK: { |
| 311 | QueuedTask* task = reinterpret_cast<QueuedTask*>(msg.lParam); |
| 312 | if (task->Run()) |
| 313 | delete task; |
| 314 | break; |
| 315 | } |
| 316 | case WM_QUEUE_DELAYED_TASK: { |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 317 | std::unique_ptr<DelayedTaskInfo> info( |
| 318 | reinterpret_cast<DelayedTaskInfo*>(msg.lParam)); |
| 319 | bool need_to_schedule_timers = |
| 320 | timer_tasks_.empty() || |
| 321 | timer_tasks_.top().due_time() > info->due_time(); |
| 322 | timer_tasks_.emplace(std::move(*info.get())); |
| 323 | if (need_to_schedule_timers) { |
| 324 | CancelTimers(); |
| 325 | ScheduleNextTimer(); |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 326 | } |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 327 | break; |
| 328 | } |
| 329 | case WM_TIMER: { |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 330 | RTC_DCHECK_EQ(timer_id_, msg.wParam); |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 331 | ::KillTimer(nullptr, msg.wParam); |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 332 | timer_id_ = 0; |
| 333 | RunDueTasks(); |
| 334 | ScheduleNextTimer(); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | default: |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 338 | RTC_DCHECK_NOTREACHED(); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | } else { |
tommi | f9d9154 | 2017-02-17 10:47:11 | [diff] [blame] | 342 | ::TranslateMessage(&msg); |
| 343 | ::DispatchMessage(&msg); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 344 | } |
tommi | 8372226 | 2017-03-15 11:36:29 | [diff] [blame] | 345 | |
| 346 | if (GetTick() > start + kMaxTaskProcessingTimeMs) |
| 347 | break; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 348 | } |
tommi | b89257a | 2016-07-12 08:24:36 | [diff] [blame] | 349 | return msg.message != WM_QUIT; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 350 | } |
tommi | b89257a | 2016-07-12 08:24:36 | [diff] [blame] | 351 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 352 | void TaskQueueWin::RunDueTasks() { |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 353 | RTC_DCHECK(!timer_tasks_.empty()); |
| 354 | auto now = GetTick(); |
| 355 | do { |
| 356 | const auto& top = timer_tasks_.top(); |
| 357 | if (top.due_time() > now) |
| 358 | break; |
| 359 | top.Run(); |
| 360 | timer_tasks_.pop(); |
| 361 | } while (!timer_tasks_.empty()); |
| 362 | } |
| 363 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 364 | void TaskQueueWin::ScheduleNextTimer() { |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 365 | RTC_DCHECK_EQ(timer_id_, 0); |
| 366 | if (timer_tasks_.empty()) |
| 367 | return; |
| 368 | |
| 369 | const auto& next_task = timer_tasks_.top(); |
| 370 | int64_t delay_ms = std::max(0ll, next_task.due_time() - GetTick()); |
| 371 | uint32_t milliseconds = rtc::dchecked_cast<uint32_t>(delay_ms); |
| 372 | if (!timer_.StartOneShotTimer(milliseconds)) |
| 373 | timer_id_ = ::SetTimer(nullptr, 0, milliseconds, nullptr); |
| 374 | } |
| 375 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 376 | void TaskQueueWin::CancelTimers() { |
tommi | 0b94215 | 2017-03-10 17:33:53 | [diff] [blame] | 377 | timer_.Cancel(); |
| 378 | if (timer_id_) { |
| 379 | ::KillTimer(nullptr, timer_id_); |
| 380 | timer_id_ = 0; |
| 381 | } |
| 382 | } |
| 383 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 384 | class TaskQueueWinFactory : public TaskQueueFactory { |
| 385 | public: |
| 386 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue( |
| 387 | absl::string_view name, |
| 388 | Priority priority) const override { |
| 389 | return std::unique_ptr<TaskQueueBase, TaskQueueDeleter>( |
| 390 | new TaskQueueWin(name, TaskQueuePriorityToThreadPriority(priority))); |
| 391 | } |
| 392 | }; |
| 393 | |
| 394 | } // namespace |
| 395 | |
| 396 | std::unique_ptr<TaskQueueFactory> CreateTaskQueueWinFactory() { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 397 | return std::make_unique<TaskQueueWinFactory>(); |
nisse | 341c8e40 | 2017-09-06 11:38:22 | [diff] [blame] | 398 | } |
| 399 | |
Danil Chapovalov | 826f2e7 | 2019-02-20 17:13:09 | [diff] [blame] | 400 | } // namespace webrtc |