OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 } | 92 } |
93 | 93 |
94 PlatformThread::PlatformThread(ThreadRunFunction func, | 94 PlatformThread::PlatformThread(ThreadRunFunction func, |
95 void* obj, | 95 void* obj, |
96 const char* thread_name) | 96 const char* thread_name) |
97 : run_function_(func), | 97 : run_function_(func), |
98 obj_(obj), | 98 obj_(obj), |
99 name_(thread_name ? thread_name : "webrtc"), | 99 name_(thread_name ? thread_name : "webrtc"), |
100 #if defined(WEBRTC_WIN) | 100 #if defined(WEBRTC_WIN) |
101 stop_(false), | 101 stop_(false), |
102 thread_(NULL) { | 102 thread_(NULL), |
| 103 thread_id_(0) { |
103 #else | 104 #else |
104 stop_event_(false, false), | 105 stop_event_(false, false), |
105 thread_(0) { | 106 thread_(0) { |
106 #endif // defined(WEBRTC_WIN) | 107 #endif // defined(WEBRTC_WIN) |
107 RTC_DCHECK(func); | 108 RTC_DCHECK(func); |
108 RTC_DCHECK(name_.length() < 64); | 109 RTC_DCHECK(name_.length() < 64); |
109 } | 110 } |
110 | 111 |
111 PlatformThread::~PlatformThread() { | 112 PlatformThread::~PlatformThread() { |
112 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 113 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
113 #if defined(WEBRTC_WIN) | 114 #if defined(WEBRTC_WIN) |
114 RTC_DCHECK(!thread_); | 115 RTC_DCHECK(!thread_); |
| 116 RTC_DCHECK(!thread_id_); |
115 #endif // defined(WEBRTC_WIN) | 117 #endif // defined(WEBRTC_WIN) |
116 } | 118 } |
117 | 119 |
118 #if defined(WEBRTC_WIN) | 120 #if defined(WEBRTC_WIN) |
119 DWORD WINAPI PlatformThread::StartThread(void* param) { | 121 DWORD WINAPI PlatformThread::StartThread(void* param) { |
120 static_cast<PlatformThread*>(param)->Run(); | 122 static_cast<PlatformThread*>(param)->Run(); |
121 return 0; | 123 return 0; |
122 } | 124 } |
123 #else | 125 #else |
124 void* PlatformThread::StartThread(void* param) { | 126 void* PlatformThread::StartThread(void* param) { |
125 static_cast<PlatformThread*>(param)->Run(); | 127 static_cast<PlatformThread*>(param)->Run(); |
126 return 0; | 128 return 0; |
127 } | 129 } |
128 #endif // defined(WEBRTC_WIN) | 130 #endif // defined(WEBRTC_WIN) |
129 | 131 |
130 void PlatformThread::Start() { | 132 void PlatformThread::Start() { |
131 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 133 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
132 RTC_DCHECK(!thread_) << "Thread already started?"; | 134 RTC_DCHECK(!thread_) << "Thread already started?"; |
133 #if defined(WEBRTC_WIN) | 135 #if defined(WEBRTC_WIN) |
134 stop_ = false; | 136 stop_ = false; |
135 | 137 |
136 // See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION. | 138 // See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION. |
137 // Set the reserved stack stack size to 1M, which is the default on Windows | 139 // Set the reserved stack stack size to 1M, which is the default on Windows |
138 // and Linux. | 140 // and Linux. |
139 DWORD thread_id; | |
140 thread_ = ::CreateThread(NULL, 1024 * 1024, &StartThread, this, | 141 thread_ = ::CreateThread(NULL, 1024 * 1024, &StartThread, this, |
141 STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id); | 142 STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id_); |
142 RTC_CHECK(thread_) << "CreateThread failed"; | 143 RTC_CHECK(thread_) << "CreateThread failed"; |
| 144 RTC_DCHECK(thread_id_); |
143 #else | 145 #else |
144 ThreadAttributes attr; | 146 ThreadAttributes attr; |
145 // Set the stack stack size to 1M. | 147 // Set the stack stack size to 1M. |
146 pthread_attr_setstacksize(&attr, 1024 * 1024); | 148 pthread_attr_setstacksize(&attr, 1024 * 1024); |
147 RTC_CHECK_EQ(0, pthread_create(&thread_, &attr, &StartThread, this)); | 149 RTC_CHECK_EQ(0, pthread_create(&thread_, &attr, &StartThread, this)); |
148 #endif // defined(WEBRTC_WIN) | 150 #endif // defined(WEBRTC_WIN) |
149 } | 151 } |
150 | 152 |
151 bool PlatformThread::IsRunning() const { | 153 bool PlatformThread::IsRunning() const { |
152 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 154 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
153 #if defined(WEBRTC_WIN) | 155 #if defined(WEBRTC_WIN) |
154 return thread_ != nullptr; | 156 return thread_ != nullptr; |
155 #else | 157 #else |
156 return thread_ != 0; | 158 return thread_ != 0; |
157 #endif // defined(WEBRTC_WIN) | 159 #endif // defined(WEBRTC_WIN) |
158 } | 160 } |
159 | 161 |
| 162 PlatformThreadRef PlatformThread::GetThreadRef() const { |
| 163 #if defined(WEBRTC_WIN) |
| 164 return thread_id_; |
| 165 #else |
| 166 return thread_; |
| 167 #endif // defined(WEBRTC_WIN) |
| 168 } |
| 169 |
160 void PlatformThread::Stop() { | 170 void PlatformThread::Stop() { |
161 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 171 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
162 if (!IsRunning()) | 172 if (!IsRunning()) |
163 return; | 173 return; |
164 | 174 |
165 #if defined(WEBRTC_WIN) | 175 #if defined(WEBRTC_WIN) |
166 // Set stop_ to |true| on the worker thread. | 176 // Set stop_ to |true| on the worker thread. |
167 QueueUserAPC(&RaiseFlag, thread_, reinterpret_cast<ULONG_PTR>(&stop_)); | 177 bool queued = QueueAPC(&RaiseFlag, reinterpret_cast<ULONG_PTR>(&stop_)); |
| 178 // Queuing the APC can fail if the thread is being terminated. |
| 179 RTC_CHECK(queued || GetLastError() == ERROR_GEN_FAILURE); |
168 WaitForSingleObject(thread_, INFINITE); | 180 WaitForSingleObject(thread_, INFINITE); |
169 CloseHandle(thread_); | 181 CloseHandle(thread_); |
170 thread_ = nullptr; | 182 thread_ = nullptr; |
| 183 thread_id_ = 0; |
171 #else | 184 #else |
172 stop_event_.Set(); | 185 stop_event_.Set(); |
173 RTC_CHECK_EQ(0, pthread_join(thread_, nullptr)); | 186 RTC_CHECK_EQ(0, pthread_join(thread_, nullptr)); |
174 thread_ = 0; | 187 thread_ = 0; |
175 #endif // defined(WEBRTC_WIN) | 188 #endif // defined(WEBRTC_WIN) |
176 } | 189 } |
177 | 190 |
178 void PlatformThread::Run() { | 191 void PlatformThread::Run() { |
179 if (!name_.empty()) | 192 if (!name_.empty()) |
180 rtc::SetCurrentThreadName(name_.c_str()); | 193 rtc::SetCurrentThreadName(name_.c_str()); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 param.sched_priority = std::max(top_prio - 1, low_prio); | 253 param.sched_priority = std::max(top_prio - 1, low_prio); |
241 break; | 254 break; |
242 case kRealtimePriority: | 255 case kRealtimePriority: |
243 param.sched_priority = top_prio; | 256 param.sched_priority = top_prio; |
244 break; | 257 break; |
245 } | 258 } |
246 return pthread_setschedparam(thread_, policy, ¶m) == 0; | 259 return pthread_setschedparam(thread_, policy, ¶m) == 0; |
247 #endif // defined(WEBRTC_WIN) | 260 #endif // defined(WEBRTC_WIN) |
248 } | 261 } |
249 | 262 |
| 263 #if defined(WEBRTC_WIN) |
| 264 bool PlatformThread::QueueAPC(PAPCFUNC function, ULONG_PTR data) { |
| 265 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 266 RTC_DCHECK(IsRunning()); |
| 267 |
| 268 return QueueUserAPC(function, thread_, data) != FALSE; |
| 269 } |
| 270 #endif |
| 271 |
250 } // namespace rtc | 272 } // namespace rtc |
OLD | NEW |