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