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" |
Danil Chapovalov | 02fddf6 | 2018-02-12 12:41:16 +0100 | [diff] [blame] | 32 | #include "rtc_base/criticalsection.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" |
| 37 | #include "rtc_base/refcount.h" |
| 38 | #include "rtc_base/refcountedobject.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 39 | #include "rtc_base/timeutils.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); |
| 193 | void PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 194 | std::unique_ptr<QueuedTask> reply, |
| 195 | TaskQueue::Impl* reply_queue); |
| 196 | |
| 197 | void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds); |
| 198 | |
| 199 | void RunPendingTasks(); |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 200 | |
| 201 | private: |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 202 | static void ThreadMain(void* context); |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 203 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 204 | class WorkerThread : public PlatformThread { |
| 205 | public: |
| 206 | WorkerThread(ThreadRunFunction func, |
| 207 | void* obj, |
| 208 | const char* thread_name, |
| 209 | ThreadPriority priority) |
| 210 | : PlatformThread(func, obj, thread_name, priority) {} |
| 211 | |
| 212 | bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data) { |
| 213 | return PlatformThread::QueueAPC(apc_function, data); |
| 214 | } |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 217 | class ThreadState { |
| 218 | public: |
| 219 | explicit ThreadState(HANDLE in_queue) : in_queue_(in_queue) {} |
| 220 | ~ThreadState() {} |
| 221 | |
| 222 | void RunThreadMain(); |
| 223 | |
| 224 | private: |
| 225 | bool ProcessQueuedMessages(); |
| 226 | void RunDueTasks(); |
| 227 | void ScheduleNextTimer(); |
| 228 | void CancelTimers(); |
| 229 | |
| 230 | // Since priority_queue<> by defult orders items in terms of |
| 231 | // largest->smallest, using std::less<>, and we want smallest->largest, |
| 232 | // we would like to use std::greater<> here. Alas it's only available in |
| 233 | // C++14 and later, so we roll our own compare template that that relies on |
| 234 | // operator<(). |
| 235 | template <typename T> |
| 236 | struct greater { |
| 237 | bool operator()(const T& l, const T& r) { return l > r; } |
| 238 | }; |
| 239 | |
| 240 | MultimediaTimer timer_; |
| 241 | std::priority_queue<DelayedTaskInfo, |
| 242 | std::vector<DelayedTaskInfo>, |
| 243 | greater<DelayedTaskInfo>> |
| 244 | timer_tasks_; |
| 245 | UINT_PTR timer_id_ = 0; |
| 246 | HANDLE in_queue_; |
| 247 | }; |
| 248 | |
| 249 | TaskQueue* const queue_; |
| 250 | WorkerThread thread_; |
| 251 | rtc::CriticalSection pending_lock_; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 252 | std::queue<std::unique_ptr<QueuedTask>> pending_ |
| 253 | RTC_GUARDED_BY(pending_lock_); |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 254 | HANDLE in_queue_; |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 255 | }; |
| 256 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 257 | TaskQueue::Impl::Impl(const char* queue_name, |
| 258 | TaskQueue* queue, |
| 259 | Priority priority) |
| 260 | : queue_(queue), |
| 261 | thread_(&TaskQueue::Impl::ThreadMain, |
tommi | c9bb791 | 2017-02-24 10:42:14 -0800 | [diff] [blame] | 262 | this, |
| 263 | queue_name, |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 264 | TaskQueuePriorityToThreadPriority(priority)), |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 265 | in_queue_(::CreateEvent(nullptr, true, false, nullptr)) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 266 | RTC_DCHECK(queue_name); |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 267 | RTC_DCHECK(in_queue_); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 268 | thread_.Start(); |
| 269 | Event event(false, false); |
| 270 | ThreadStartupData startup = {&event, this}; |
| 271 | RTC_CHECK(thread_.QueueAPC(&InitializeQueueThread, |
| 272 | reinterpret_cast<ULONG_PTR>(&startup))); |
| 273 | event.Wait(Event::kForever); |
| 274 | } |
| 275 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 276 | TaskQueue::Impl::~Impl() { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 277 | RTC_DCHECK(!IsCurrent()); |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 278 | while (!::PostThreadMessage(thread_.GetThreadRef(), WM_QUIT, 0, 0)) { |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 279 | RTC_CHECK_EQ(ERROR_NOT_ENOUGH_QUOTA, ::GetLastError()); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 280 | Sleep(1); |
| 281 | } |
| 282 | thread_.Stop(); |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 283 | ::CloseHandle(in_queue_); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // static |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 287 | TaskQueue::Impl* TaskQueue::Impl::Current() { |
| 288 | return static_cast<TaskQueue::Impl*>(::TlsGetValue(GetQueuePtrTls())); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 289 | } |
| 290 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 291 | // static |
| 292 | TaskQueue* TaskQueue::Impl::CurrentQueue() { |
| 293 | TaskQueue::Impl* current = Current(); |
| 294 | return current ? current->queue_ : nullptr; |
| 295 | } |
| 296 | |
| 297 | bool TaskQueue::Impl::IsCurrent() const { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 298 | return IsThreadRefEqual(thread_.GetThreadRef(), CurrentThreadRef()); |
| 299 | } |
| 300 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 301 | void TaskQueue::Impl::PostTask(std::unique_ptr<QueuedTask> task) { |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 302 | rtc::CritScope lock(&pending_lock_); |
| 303 | pending_.push(std::move(task)); |
| 304 | ::SetEvent(in_queue_); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 305 | } |
| 306 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 307 | void TaskQueue::Impl::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 308 | uint32_t milliseconds) { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 309 | if (!milliseconds) { |
| 310 | PostTask(std::move(task)); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | // TODO(tommi): Avoid this allocation. It is currently here since |
| 315 | // the timestamp stored in the task info object, is a 64bit timestamp |
| 316 | // and WPARAM is 32bits in 32bit builds. Otherwise, we could pass the |
| 317 | // task pointer and timestamp as LPARAM and WPARAM. |
| 318 | auto* task_info = new DelayedTaskInfo(milliseconds, std::move(task)); |
| 319 | if (!::PostThreadMessage(thread_.GetThreadRef(), WM_QUEUE_DELAYED_TASK, 0, |
| 320 | reinterpret_cast<LPARAM>(task_info))) { |
| 321 | delete task_info; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 325 | void TaskQueue::Impl::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 326 | std::unique_ptr<QueuedTask> reply, |
| 327 | TaskQueue::Impl* reply_queue) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 328 | QueuedTask* task_ptr = task.release(); |
| 329 | QueuedTask* reply_task_ptr = reply.release(); |
| 330 | DWORD reply_thread_id = reply_queue->thread_.GetThreadRef(); |
| 331 | PostTask([task_ptr, reply_task_ptr, reply_thread_id]() { |
| 332 | if (task_ptr->Run()) |
| 333 | delete task_ptr; |
| 334 | // If the thread's message queue is full, we can't queue the task and will |
| 335 | // have to drop it (i.e. delete). |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 336 | if (!::PostThreadMessage(reply_thread_id, WM_RUN_TASK, 0, |
| 337 | reinterpret_cast<LPARAM>(reply_task_ptr))) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 338 | delete reply_task_ptr; |
| 339 | } |
| 340 | }); |
| 341 | } |
| 342 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 343 | void TaskQueue::Impl::RunPendingTasks() { |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 344 | while (true) { |
| 345 | std::unique_ptr<QueuedTask> task; |
| 346 | { |
| 347 | rtc::CritScope lock(&pending_lock_); |
| 348 | if (pending_.empty()) |
| 349 | break; |
| 350 | task = std::move(pending_.front()); |
| 351 | pending_.pop(); |
| 352 | } |
| 353 | |
| 354 | if (!task->Run()) |
| 355 | task.release(); |
| 356 | } |
| 357 | } |
| 358 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 359 | // static |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 360 | void TaskQueue::Impl::ThreadMain(void* context) { |
| 361 | ThreadState state(static_cast<TaskQueue::Impl*>(context)->in_queue_); |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 362 | state.RunThreadMain(); |
| 363 | } |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 364 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 365 | void TaskQueue::Impl::ThreadState::RunThreadMain() { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 366 | HANDLE handles[2] = {*timer_.event_for_wait(), in_queue_}; |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 367 | while (true) { |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 368 | // Make sure we do an alertable wait as that's required to allow APCs to run |
| 369 | // (e.g. required for InitializeQueueThread and stopping the thread in |
| 370 | // PlatformThread). |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 371 | DWORD result = ::MsgWaitForMultipleObjectsEx( |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 372 | arraysize(handles), handles, INFINITE, QS_ALLEVENTS, MWMO_ALERTABLE); |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 373 | RTC_CHECK_NE(WAIT_FAILED, result); |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 374 | if (result == (WAIT_OBJECT_0 + 2)) { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 375 | // There are messages in the message queue that need to be handled. |
| 376 | if (!ProcessQueuedMessages()) |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 377 | break; |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 380 | if (result == WAIT_OBJECT_0 || |
| 381 | (!timer_tasks_.empty() && |
| 382 | ::WaitForSingleObject(*timer_.event_for_wait(), 0) == WAIT_OBJECT_0)) { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 383 | // The multimedia timer was signaled. |
| 384 | timer_.Cancel(); |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 385 | RunDueTasks(); |
| 386 | ScheduleNextTimer(); |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | if (result == (WAIT_OBJECT_0 + 1)) { |
| 390 | ::ResetEvent(in_queue_); |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 391 | TaskQueue::Impl::Current()->RunPendingTasks(); |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 394 | } |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 395 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 396 | bool TaskQueue::Impl::ThreadState::ProcessQueuedMessages() { |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 397 | MSG msg = {}; |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 398 | // To protect against overly busy message queues, we limit the time |
| 399 | // we process tasks to a few milliseconds. If we don't do that, there's |
| 400 | // a chance that timer tasks won't ever run. |
| 401 | static const int kMaxTaskProcessingTimeMs = 500; |
| 402 | auto start = GetTick(); |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 403 | while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) && |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 404 | msg.message != WM_QUIT) { |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 405 | if (!msg.hwnd) { |
| 406 | switch (msg.message) { |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 407 | // TODO(tommi): Stop using this way of queueing tasks. |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 408 | case WM_RUN_TASK: { |
| 409 | QueuedTask* task = reinterpret_cast<QueuedTask*>(msg.lParam); |
| 410 | if (task->Run()) |
| 411 | delete task; |
| 412 | break; |
| 413 | } |
| 414 | case WM_QUEUE_DELAYED_TASK: { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 415 | std::unique_ptr<DelayedTaskInfo> info( |
| 416 | reinterpret_cast<DelayedTaskInfo*>(msg.lParam)); |
| 417 | bool need_to_schedule_timers = |
| 418 | timer_tasks_.empty() || |
| 419 | timer_tasks_.top().due_time() > info->due_time(); |
| 420 | timer_tasks_.emplace(std::move(*info.get())); |
| 421 | if (need_to_schedule_timers) { |
| 422 | CancelTimers(); |
| 423 | ScheduleNextTimer(); |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 424 | } |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 425 | break; |
| 426 | } |
| 427 | case WM_TIMER: { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 428 | RTC_DCHECK_EQ(timer_id_, msg.wParam); |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 429 | ::KillTimer(nullptr, msg.wParam); |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 430 | timer_id_ = 0; |
| 431 | RunDueTasks(); |
| 432 | ScheduleNextTimer(); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 433 | break; |
| 434 | } |
| 435 | default: |
| 436 | RTC_NOTREACHED(); |
| 437 | break; |
| 438 | } |
| 439 | } else { |
tommi | f9d9154 | 2017-02-17 02:47:11 -0800 | [diff] [blame] | 440 | ::TranslateMessage(&msg); |
| 441 | ::DispatchMessage(&msg); |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 442 | } |
tommi | 8372226 | 2017-03-15 04:36:29 -0700 | [diff] [blame] | 443 | |
| 444 | if (GetTick() > start + kMaxTaskProcessingTimeMs) |
| 445 | break; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 446 | } |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 447 | return msg.message != WM_QUIT; |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 448 | } |
tommi | b89257a | 2016-07-12 01:24:36 -0700 | [diff] [blame] | 449 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 450 | void TaskQueue::Impl::ThreadState::RunDueTasks() { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 451 | RTC_DCHECK(!timer_tasks_.empty()); |
| 452 | auto now = GetTick(); |
| 453 | do { |
| 454 | const auto& top = timer_tasks_.top(); |
| 455 | if (top.due_time() > now) |
| 456 | break; |
| 457 | top.Run(); |
| 458 | timer_tasks_.pop(); |
| 459 | } while (!timer_tasks_.empty()); |
| 460 | } |
| 461 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 462 | void TaskQueue::Impl::ThreadState::ScheduleNextTimer() { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 463 | RTC_DCHECK_EQ(timer_id_, 0); |
| 464 | if (timer_tasks_.empty()) |
| 465 | return; |
| 466 | |
| 467 | const auto& next_task = timer_tasks_.top(); |
| 468 | int64_t delay_ms = std::max(0ll, next_task.due_time() - GetTick()); |
| 469 | uint32_t milliseconds = rtc::dchecked_cast<uint32_t>(delay_ms); |
| 470 | if (!timer_.StartOneShotTimer(milliseconds)) |
| 471 | timer_id_ = ::SetTimer(nullptr, 0, milliseconds, nullptr); |
| 472 | } |
| 473 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 474 | void TaskQueue::Impl::ThreadState::CancelTimers() { |
tommi | 0b94215 | 2017-03-10 09:33:53 -0800 | [diff] [blame] | 475 | timer_.Cancel(); |
| 476 | if (timer_id_) { |
| 477 | ::KillTimer(nullptr, timer_id_); |
| 478 | timer_id_ = 0; |
| 479 | } |
| 480 | } |
| 481 | |
nisse | 341c8e4 | 2017-09-06 04:38:22 -0700 | [diff] [blame] | 482 | // Boilerplate for the PIMPL pattern. |
| 483 | TaskQueue::TaskQueue(const char* queue_name, Priority priority) |
| 484 | : impl_(new RefCountedObject<TaskQueue::Impl>(queue_name, this, priority)) { |
| 485 | } |
| 486 | |
| 487 | TaskQueue::~TaskQueue() {} |
| 488 | |
| 489 | // static |
| 490 | TaskQueue* TaskQueue::Current() { |
| 491 | return TaskQueue::Impl::CurrentQueue(); |
| 492 | } |
| 493 | |
| 494 | // Used for DCHECKing the current queue. |
| 495 | bool TaskQueue::IsCurrent() const { |
| 496 | return impl_->IsCurrent(); |
| 497 | } |
| 498 | |
| 499 | void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) { |
| 500 | return TaskQueue::impl_->PostTask(std::move(task)); |
| 501 | } |
| 502 | |
| 503 | void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 504 | std::unique_ptr<QueuedTask> reply, |
| 505 | TaskQueue* reply_queue) { |
| 506 | return TaskQueue::impl_->PostTaskAndReply(std::move(task), std::move(reply), |
| 507 | reply_queue->impl_.get()); |
| 508 | } |
| 509 | |
| 510 | void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 511 | std::unique_ptr<QueuedTask> reply) { |
| 512 | return TaskQueue::impl_->PostTaskAndReply(std::move(task), std::move(reply), |
| 513 | impl_.get()); |
| 514 | } |
| 515 | |
| 516 | void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 517 | uint32_t milliseconds) { |
| 518 | return TaskQueue::impl_->PostDelayedTask(std::move(task), milliseconds); |
| 519 | } |
| 520 | |
tommi | c06b133 | 2016-05-14 11:31:40 -0700 | [diff] [blame] | 521 | } // namespace rtc |