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