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 | |
kjellander | e96c45b | 2017-06-30 10:45:21 -0700 | [diff] [blame] | 11 | #include "webrtc/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 | |
kjellander | e96c45b | 2017-06-30 10:45:21 -0700 | [diff] [blame] | 19 | #include "webrtc/rtc_base/arraysize.h" |
| 20 | #include "webrtc/rtc_base/checks.h" |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 21 | #include "webrtc/rtc_base/event.h" |
kjellander | e96c45b | 2017-06-30 10:45:21 -0700 | [diff] [blame] | 22 | #include "webrtc/rtc_base/logging.h" |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 23 | #include "webrtc/rtc_base/platform_thread.h" |
| 24 | #include "webrtc/rtc_base/refcount.h" |
| 25 | #include "webrtc/rtc_base/refcountedobject.h" |
kjellander | e96c45b | 2017-06-30 10:45:21 -0700 | [diff] [blame] | 26 | #include "webrtc/rtc_base/safe_conversions.h" |
| 27 | #include "webrtc/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_; |
| 239 | std::queue<std::unique_ptr<QueuedTask>> pending_ GUARDED_BY(pending_lock_); |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 240 | HANDLE in_queue_; |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 241 | }; |
| 242 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 243 | TaskQueue::Impl::Impl(const char* queue_name, |
| 244 | TaskQueue* queue, |
| 245 | Priority priority) |
| 246 | : queue_(queue), |
| 247 | thread_(&TaskQueue::Impl::ThreadMain, |
tommi | c9bb791 | 2017-02-24 10:42:14 -0800 | [diff] [blame] | 248 | this, |
| 249 | queue_name, |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 250 | TaskQueuePriorityToThreadPriority(priority)), |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 251 | in_queue_(::CreateEvent(nullptr, true, false, nullptr)) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 252 | RTC_DCHECK(queue_name); |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 253 | RTC_DCHECK(in_queue_); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 254 | thread_.Start(); |
| 255 | Event event(false, false); |
| 256 | ThreadStartupData startup = {&event, this}; |
| 257 | RTC_CHECK(thread_.QueueAPC(&InitializeQueueThread, |
| 258 | reinterpret_cast<ULONG_PTR>(&startup))); |
| 259 | event.Wait(Event::kForever); |
| 260 | } |
| 261 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 262 | TaskQueue::Impl::~Impl() { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 263 | RTC_DCHECK(!IsCurrent()); |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 264 | while (!::PostThreadMessage(thread_.GetThreadRef(), WM_QUIT, 0, 0)) { |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 265 | RTC_CHECK_EQ(ERROR_NOT_ENOUGH_QUOTA, ::GetLastError()); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 266 | Sleep(1); |
| 267 | } |
| 268 | thread_.Stop(); |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 269 | ::CloseHandle(in_queue_); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // static |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 273 | TaskQueue::Impl* TaskQueue::Impl::Current() { |
| 274 | return static_cast<TaskQueue::Impl*>(::TlsGetValue(GetQueuePtrTls())); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 275 | } |
| 276 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 277 | // static |
| 278 | TaskQueue* TaskQueue::Impl::CurrentQueue() { |
| 279 | TaskQueue::Impl* current = Current(); |
| 280 | return current ? current->queue_ : nullptr; |
| 281 | } |
| 282 | |
| 283 | bool TaskQueue::Impl::IsCurrent() const { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 284 | return IsThreadRefEqual(thread_.GetThreadRef(), CurrentThreadRef()); |
| 285 | } |
| 286 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 287 | void TaskQueue::Impl::PostTask(std::unique_ptr<QueuedTask> task) { |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 288 | rtc::CritScope lock(&pending_lock_); |
| 289 | pending_.push(std::move(task)); |
| 290 | ::SetEvent(in_queue_); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 291 | } |
| 292 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 293 | void TaskQueue::Impl::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 294 | uint32_t milliseconds) { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 295 | if (!milliseconds) { |
| 296 | PostTask(std::move(task)); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | // TODO(tommi): Avoid this allocation. It is currently here since |
| 301 | // the timestamp stored in the task info object, is a 64bit timestamp |
| 302 | // and WPARAM is 32bits in 32bit builds. Otherwise, we could pass the |
| 303 | // task pointer and timestamp as LPARAM and WPARAM. |
| 304 | auto* task_info = new DelayedTaskInfo(milliseconds, std::move(task)); |
| 305 | if (!::PostThreadMessage(thread_.GetThreadRef(), WM_QUEUE_DELAYED_TASK, 0, |
| 306 | reinterpret_cast<LPARAM>(task_info))) { |
| 307 | delete task_info; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 311 | void TaskQueue::Impl::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 312 | std::unique_ptr<QueuedTask> reply, |
| 313 | TaskQueue::Impl* reply_queue) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 314 | QueuedTask* task_ptr = task.release(); |
| 315 | QueuedTask* reply_task_ptr = reply.release(); |
| 316 | DWORD reply_thread_id = reply_queue->thread_.GetThreadRef(); |
| 317 | PostTask([task_ptr, reply_task_ptr, reply_thread_id]() { |
| 318 | if (task_ptr->Run()) |
| 319 | delete task_ptr; |
| 320 | // If the thread's message queue is full, we can't queue the task and will |
| 321 | // have to drop it (i.e. delete). |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 322 | if (!::PostThreadMessage(reply_thread_id, WM_RUN_TASK, 0, |
| 323 | reinterpret_cast<LPARAM>(reply_task_ptr))) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 324 | delete reply_task_ptr; |
| 325 | } |
| 326 | }); |
| 327 | } |
| 328 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 329 | void TaskQueue::Impl::RunPendingTasks() { |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 330 | while (true) { |
| 331 | std::unique_ptr<QueuedTask> task; |
| 332 | { |
| 333 | rtc::CritScope lock(&pending_lock_); |
| 334 | if (pending_.empty()) |
| 335 | break; |
| 336 | task = std::move(pending_.front()); |
| 337 | pending_.pop(); |
| 338 | } |
| 339 | |
| 340 | if (!task->Run()) |
| 341 | task.release(); |
| 342 | } |
| 343 | } |
| 344 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 345 | // static |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 346 | void TaskQueue::Impl::ThreadMain(void* context) { |
| 347 | ThreadState state(static_cast<TaskQueue::Impl*>(context)->in_queue_); |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 348 | state.RunThreadMain(); |
| 349 | } |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 350 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 351 | void TaskQueue::Impl::ThreadState::RunThreadMain() { |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 352 | HANDLE handles[2] = { *timer_.event_for_wait(), in_queue_ }; |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 353 | while (true) { |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 354 | // Make sure we do an alertable wait as that's required to allow APCs to run |
| 355 | // (e.g. required for InitializeQueueThread and stopping the thread in |
| 356 | // PlatformThread). |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 357 | DWORD result = ::MsgWaitForMultipleObjectsEx( |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 358 | arraysize(handles), handles, INFINITE, QS_ALLEVENTS, MWMO_ALERTABLE); |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 359 | RTC_CHECK_NE(WAIT_FAILED, result); |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 360 | if (result == (WAIT_OBJECT_0 + 2)) { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 361 | // There are messages in the message queue that need to be handled. |
| 362 | if (!ProcessQueuedMessages()) |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 363 | break; |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | if (result == WAIT_OBJECT_0 || (!timer_tasks_.empty() && |
| 367 | ::WaitForSingleObject(*timer_.event_for_wait(), 0) == WAIT_OBJECT_0)) { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 368 | // The multimedia timer was signaled. |
| 369 | timer_.Cancel(); |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 370 | RunDueTasks(); |
| 371 | ScheduleNextTimer(); |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | if (result == (WAIT_OBJECT_0 + 1)) { |
| 375 | ::ResetEvent(in_queue_); |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 376 | TaskQueue::Impl::Current()->RunPendingTasks(); |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 379 | } |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 380 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 381 | bool TaskQueue::Impl::ThreadState::ProcessQueuedMessages() { |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 382 | MSG msg = {}; |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 383 | // To protect against overly busy message queues, we limit the time |
| 384 | // we process tasks to a few milliseconds. If we don't do that, there's |
| 385 | // a chance that timer tasks won't ever run. |
| 386 | static const int kMaxTaskProcessingTimeMs = 500; |
| 387 | auto start = GetTick(); |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 388 | while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) && |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 389 | msg.message != WM_QUIT) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 390 | if (!msg.hwnd) { |
| 391 | switch (msg.message) { |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 392 | // TODO(tommi): Stop using this way of queueing tasks. |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 393 | case WM_RUN_TASK: { |
| 394 | QueuedTask* task = reinterpret_cast<QueuedTask*>(msg.lParam); |
| 395 | if (task->Run()) |
| 396 | delete task; |
| 397 | break; |
| 398 | } |
| 399 | case WM_QUEUE_DELAYED_TASK: { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 400 | std::unique_ptr<DelayedTaskInfo> info( |
| 401 | reinterpret_cast<DelayedTaskInfo*>(msg.lParam)); |
| 402 | bool need_to_schedule_timers = |
| 403 | timer_tasks_.empty() || |
| 404 | timer_tasks_.top().due_time() > info->due_time(); |
| 405 | timer_tasks_.emplace(std::move(*info.get())); |
| 406 | if (need_to_schedule_timers) { |
| 407 | CancelTimers(); |
| 408 | ScheduleNextTimer(); |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 409 | } |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 410 | break; |
| 411 | } |
| 412 | case WM_TIMER: { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 413 | RTC_DCHECK_EQ(timer_id_, msg.wParam); |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 414 | ::KillTimer(nullptr, msg.wParam); |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 415 | timer_id_ = 0; |
| 416 | RunDueTasks(); |
| 417 | ScheduleNextTimer(); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 418 | break; |
| 419 | } |
| 420 | default: |
| 421 | RTC_NOTREACHED(); |
| 422 | break; |
| 423 | } |
| 424 | } else { |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 425 | ::TranslateMessage(&msg); |
| 426 | ::DispatchMessage(&msg); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 427 | } |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 428 | |
| 429 | if (GetTick() > start + kMaxTaskProcessingTimeMs) |
| 430 | break; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 431 | } |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 432 | return msg.message != WM_QUIT; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 433 | } |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 434 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 435 | void TaskQueue::Impl::ThreadState::RunDueTasks() { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 436 | RTC_DCHECK(!timer_tasks_.empty()); |
| 437 | auto now = GetTick(); |
| 438 | do { |
| 439 | const auto& top = timer_tasks_.top(); |
| 440 | if (top.due_time() > now) |
| 441 | break; |
| 442 | top.Run(); |
| 443 | timer_tasks_.pop(); |
| 444 | } while (!timer_tasks_.empty()); |
| 445 | } |
| 446 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 447 | void TaskQueue::Impl::ThreadState::ScheduleNextTimer() { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 448 | RTC_DCHECK_EQ(timer_id_, 0); |
| 449 | if (timer_tasks_.empty()) |
| 450 | return; |
| 451 | |
| 452 | const auto& next_task = timer_tasks_.top(); |
| 453 | int64_t delay_ms = std::max(0ll, next_task.due_time() - GetTick()); |
| 454 | uint32_t milliseconds = rtc::dchecked_cast<uint32_t>(delay_ms); |
| 455 | if (!timer_.StartOneShotTimer(milliseconds)) |
| 456 | timer_id_ = ::SetTimer(nullptr, 0, milliseconds, nullptr); |
| 457 | } |
| 458 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 459 | void TaskQueue::Impl::ThreadState::CancelTimers() { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 460 | timer_.Cancel(); |
| 461 | if (timer_id_) { |
| 462 | ::KillTimer(nullptr, timer_id_); |
| 463 | timer_id_ = 0; |
| 464 | } |
| 465 | } |
| 466 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame^] | 467 | // Boilerplate for the PIMPL pattern. |
| 468 | TaskQueue::TaskQueue(const char* queue_name, Priority priority) |
| 469 | : impl_(new RefCountedObject<TaskQueue::Impl>(queue_name, this, priority)) { |
| 470 | } |
| 471 | |
| 472 | TaskQueue::~TaskQueue() {} |
| 473 | |
| 474 | // static |
| 475 | TaskQueue* TaskQueue::Current() { |
| 476 | return TaskQueue::Impl::CurrentQueue(); |
| 477 | } |
| 478 | |
| 479 | // Used for DCHECKing the current queue. |
| 480 | bool TaskQueue::IsCurrent() const { |
| 481 | return impl_->IsCurrent(); |
| 482 | } |
| 483 | |
| 484 | void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) { |
| 485 | return TaskQueue::impl_->PostTask(std::move(task)); |
| 486 | } |
| 487 | |
| 488 | void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 489 | std::unique_ptr<QueuedTask> reply, |
| 490 | TaskQueue* reply_queue) { |
| 491 | return TaskQueue::impl_->PostTaskAndReply(std::move(task), std::move(reply), |
| 492 | reply_queue->impl_.get()); |
| 493 | } |
| 494 | |
| 495 | void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 496 | std::unique_ptr<QueuedTask> reply) { |
| 497 | return TaskQueue::impl_->PostTaskAndReply(std::move(task), std::move(reply), |
| 498 | impl_.get()); |
| 499 | } |
| 500 | |
| 501 | void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 502 | uint32_t milliseconds) { |
| 503 | return TaskQueue::impl_->PostDelayedTask(std::move(task), milliseconds); |
| 504 | } |
| 505 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 506 | } // namespace rtc |