| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2011 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 | |
| 11 #include "webrtc/system_wrappers/source/thread_win.h" | |
| 12 | |
| 13 #include <process.h> | |
| 14 #include <stdio.h> | |
| 15 #include <windows.h> | |
| 16 | |
| 17 #include "webrtc/base/checks.h" | |
| 18 #include "webrtc/system_wrappers/include/trace.h" | |
| 19 | |
| 20 namespace webrtc { | |
| 21 namespace { | |
| 22 void CALLBACK RaiseFlag(ULONG_PTR param) { | |
| 23 *reinterpret_cast<bool*>(param) = true; | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 ThreadWindows::ThreadWindows(ThreadRunFunction func, void* obj, | |
| 28 const char* thread_name) | |
| 29 : run_function_(func), | |
| 30 obj_(obj), | |
| 31 stop_(false), | |
| 32 thread_(NULL), | |
| 33 name_(thread_name ? thread_name : "webrtc") { | |
| 34 RTC_DCHECK(func); | |
| 35 } | |
| 36 | |
| 37 ThreadWindows::~ThreadWindows() { | |
| 38 RTC_DCHECK(main_thread_.CalledOnValidThread()); | |
| 39 RTC_DCHECK(!thread_); | |
| 40 } | |
| 41 | |
| 42 // static | |
| 43 DWORD WINAPI ThreadWindows::StartThread(void* param) { | |
| 44 static_cast<ThreadWindows*>(param)->Run(); | |
| 45 return 0; | |
| 46 } | |
| 47 | |
| 48 bool ThreadWindows::Start() { | |
| 49 RTC_DCHECK(main_thread_.CalledOnValidThread()); | |
| 50 RTC_DCHECK(!thread_); | |
| 51 | |
| 52 stop_ = false; | |
| 53 | |
| 54 // See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION. | |
| 55 // Set the reserved stack stack size to 1M, which is the default on Windows | |
| 56 // and Linux. | |
| 57 DWORD thread_id; | |
| 58 thread_ = ::CreateThread(NULL, 1024 * 1024, &StartThread, this, | |
| 59 STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id); | |
| 60 if (!thread_ ) { | |
| 61 RTC_DCHECK(false) << "CreateThread failed"; | |
| 62 return false; | |
| 63 } | |
| 64 | |
| 65 return true; | |
| 66 } | |
| 67 | |
| 68 bool ThreadWindows::Stop() { | |
| 69 RTC_DCHECK(main_thread_.CalledOnValidThread()); | |
| 70 if (thread_) { | |
| 71 // Set stop_ to |true| on the worker thread. | |
| 72 QueueUserAPC(&RaiseFlag, thread_, reinterpret_cast<ULONG_PTR>(&stop_)); | |
| 73 WaitForSingleObject(thread_, INFINITE); | |
| 74 CloseHandle(thread_); | |
| 75 thread_ = nullptr; | |
| 76 } | |
| 77 | |
| 78 return true; | |
| 79 } | |
| 80 | |
| 81 bool ThreadWindows::SetPriority(ThreadPriority priority) { | |
| 82 RTC_DCHECK(main_thread_.CalledOnValidThread()); | |
| 83 return thread_ && SetThreadPriority(thread_, priority); | |
| 84 } | |
| 85 | |
| 86 void ThreadWindows::Run() { | |
| 87 if (!name_.empty()) | |
| 88 rtc::SetCurrentThreadName(name_.c_str()); | |
| 89 | |
| 90 do { | |
| 91 // The interface contract of Start/Stop is that for a successfull call to | |
| 92 // Start, there should be at least one call to the run function. So we | |
| 93 // call the function before checking |stop_|. | |
| 94 if (!run_function_(obj_)) | |
| 95 break; | |
| 96 // Alertable sleep to permit RaiseFlag to run and update |stop_|. | |
| 97 SleepEx(0, true); | |
| 98 } while (!stop_); | |
| 99 } | |
| 100 | |
| 101 } // namespace webrtc | |
| OLD | NEW |