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