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