| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/base/task_queue.h" | 11 #include "webrtc/base/task_queue.h" |
| 12 | 12 |
| 13 #include <mmsystem.h> | 13 #include <mmsystem.h> |
| 14 #include <string.h> | 14 #include <string.h> |
| 15 | 15 |
| 16 #include <algorithm> | 16 #include <algorithm> |
| 17 | 17 |
| 18 #include "webrtc/base/arraysize.h" | 18 #include "webrtc/base/arraysize.h" |
| 19 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
| 20 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
| 21 | 21 |
| 22 namespace rtc { | 22 namespace rtc { |
| 23 namespace { | 23 namespace { |
| 24 #define WM_RUN_TASK WM_USER + 1 | 24 #define WM_RUN_TASK WM_USER + 1 |
| 25 #define WM_QUEUE_DELAYED_TASK WM_USER + 2 | 25 #define WM_QUEUE_DELAYED_TASK WM_USER + 2 |
| 26 | 26 |
| 27 using Priority = TaskQueue::Priority; |
| 28 |
| 27 DWORD g_queue_ptr_tls = 0; | 29 DWORD g_queue_ptr_tls = 0; |
| 28 | 30 |
| 29 BOOL CALLBACK InitializeTls(PINIT_ONCE init_once, void* param, void** context) { | 31 BOOL CALLBACK InitializeTls(PINIT_ONCE init_once, void* param, void** context) { |
| 30 g_queue_ptr_tls = TlsAlloc(); | 32 g_queue_ptr_tls = TlsAlloc(); |
| 31 return TRUE; | 33 return TRUE; |
| 32 } | 34 } |
| 33 | 35 |
| 34 DWORD GetQueuePtrTls() { | 36 DWORD GetQueuePtrTls() { |
| 35 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT; | 37 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT; |
| 36 ::InitOnceExecuteOnce(&init_once, InitializeTls, nullptr, nullptr); | 38 ::InitOnceExecuteOnce(&init_once, InitializeTls, nullptr, nullptr); |
| 37 return g_queue_ptr_tls; | 39 return g_queue_ptr_tls; |
| 38 } | 40 } |
| 39 | 41 |
| 40 struct ThreadStartupData { | 42 struct ThreadStartupData { |
| 41 Event* started; | 43 Event* started; |
| 42 void* thread_context; | 44 void* thread_context; |
| 43 }; | 45 }; |
| 44 | 46 |
| 45 void CALLBACK InitializeQueueThread(ULONG_PTR param) { | 47 void CALLBACK InitializeQueueThread(ULONG_PTR param) { |
| 46 MSG msg; | 48 MSG msg; |
| 47 ::PeekMessage(&msg, nullptr, WM_USER, WM_USER, PM_NOREMOVE); | 49 ::PeekMessage(&msg, nullptr, WM_USER, WM_USER, PM_NOREMOVE); |
| 48 ThreadStartupData* data = reinterpret_cast<ThreadStartupData*>(param); | 50 ThreadStartupData* data = reinterpret_cast<ThreadStartupData*>(param); |
| 49 ::TlsSetValue(GetQueuePtrTls(), data->thread_context); | 51 ::TlsSetValue(GetQueuePtrTls(), data->thread_context); |
| 50 data->started->Set(); | 52 data->started->Set(); |
| 51 } | 53 } |
| 54 |
| 55 ThreadPriority TaskQueuePriorityToThreadPriority(Priority priority) { |
| 56 switch (priority) { |
| 57 case Priority::HIGH: |
| 58 return kRealtimePriority; |
| 59 case Priority::LOW: |
| 60 return kLowPriority; |
| 61 case Priority::NORMAL: |
| 62 return kNormalPriority; |
| 63 default: |
| 64 RTC_NOTREACHED(); |
| 65 break; |
| 66 } |
| 67 return kNormalPriority; |
| 68 } |
| 52 } // namespace | 69 } // namespace |
| 53 | 70 |
| 54 class TaskQueue::MultimediaTimer { | 71 class TaskQueue::MultimediaTimer { |
| 55 public: | 72 public: |
| 56 // kMaxTimers defines the limit of how many MultimediaTimer instances should | 73 // kMaxTimers defines the limit of how many MultimediaTimer instances should |
| 57 // be created. | 74 // be created. |
| 58 // Background: The maximum number of supported handles for Wait functions, is | 75 // Background: The maximum number of supported handles for Wait functions, is |
| 59 // MAXIMUM_WAIT_OBJECTS - 1 (63). | 76 // MAXIMUM_WAIT_OBJECTS - 1 (63). |
| 60 // There are some ways to work around the limitation but as it turns out, the | 77 // There are some ways to work around the limitation but as it turns out, the |
| 61 // limit of concurrently active multimedia timers per process, is much lower, | 78 // limit of concurrently active multimedia timers per process, is much lower, |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 } | 155 } |
| 139 } | 156 } |
| 140 | 157 |
| 141 HANDLE event_ = nullptr; | 158 HANDLE event_ = nullptr; |
| 142 MMRESULT timer_id_ = 0; | 159 MMRESULT timer_id_ = 0; |
| 143 std::unique_ptr<QueuedTask> task_; | 160 std::unique_ptr<QueuedTask> task_; |
| 144 | 161 |
| 145 RTC_DISALLOW_COPY_AND_ASSIGN(MultimediaTimer); | 162 RTC_DISALLOW_COPY_AND_ASSIGN(MultimediaTimer); |
| 146 }; | 163 }; |
| 147 | 164 |
| 148 TaskQueue::TaskQueue(const char* queue_name) | 165 TaskQueue::TaskQueue(const char* queue_name, Priority priority /*= NORMAL*/) |
| 149 : thread_(&TaskQueue::ThreadMain, this, queue_name) { | 166 : thread_(&TaskQueue::ThreadMain, |
| 167 this, |
| 168 queue_name, |
| 169 TaskQueuePriorityToThreadPriority(priority)) { |
| 150 RTC_DCHECK(queue_name); | 170 RTC_DCHECK(queue_name); |
| 151 thread_.Start(); | 171 thread_.Start(); |
| 152 Event event(false, false); | 172 Event event(false, false); |
| 153 ThreadStartupData startup = {&event, this}; | 173 ThreadStartupData startup = {&event, this}; |
| 154 RTC_CHECK(thread_.QueueAPC(&InitializeQueueThread, | 174 RTC_CHECK(thread_.QueueAPC(&InitializeQueueThread, |
| 155 reinterpret_cast<ULONG_PTR>(&startup))); | 175 reinterpret_cast<ULONG_PTR>(&startup))); |
| 156 event.Wait(Event::kForever); | 176 event.Wait(Event::kForever); |
| 157 } | 177 } |
| 158 | 178 |
| 159 TaskQueue::~TaskQueue() { | 179 TaskQueue::~TaskQueue() { |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 } | 375 } |
| 356 } else { | 376 } else { |
| 357 ::TranslateMessage(&msg); | 377 ::TranslateMessage(&msg); |
| 358 ::DispatchMessage(&msg); | 378 ::DispatchMessage(&msg); |
| 359 } | 379 } |
| 360 } | 380 } |
| 361 return msg.message != WM_QUIT; | 381 return msg.message != WM_QUIT; |
| 362 } | 382 } |
| 363 | 383 |
| 364 } // namespace rtc | 384 } // namespace rtc |
| OLD | NEW |