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