OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 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 25 matching lines...) Expand all Loading... |
36 return &thread_manager; | 36 return &thread_manager; |
37 } | 37 } |
38 | 38 |
39 // static | 39 // static |
40 Thread* Thread::Current() { | 40 Thread* Thread::Current() { |
41 return ThreadManager::Instance()->CurrentThread(); | 41 return ThreadManager::Instance()->CurrentThread(); |
42 } | 42 } |
43 | 43 |
44 #if defined(WEBRTC_POSIX) | 44 #if defined(WEBRTC_POSIX) |
45 ThreadManager::ThreadManager() { | 45 ThreadManager::ThreadManager() { |
46 pthread_key_create(&key_, NULL); | 46 pthread_key_create(&key_, nullptr); |
47 #ifndef NO_MAIN_THREAD_WRAPPING | 47 #ifndef NO_MAIN_THREAD_WRAPPING |
48 WrapCurrentThread(); | 48 WrapCurrentThread(); |
49 #endif | 49 #endif |
50 #if defined(WEBRTC_MAC) | 50 #if defined(WEBRTC_MAC) |
51 // This is necessary to alert the cocoa runtime of the fact that | 51 // This is necessary to alert the cocoa runtime of the fact that |
52 // we are running in a multithreaded environment. | 52 // we are running in a multithreaded environment. |
53 InitCocoaMultiThreading(); | 53 InitCocoaMultiThreading(); |
54 #endif | 54 #endif |
55 } | 55 } |
56 | 56 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 return static_cast<Thread *>(TlsGetValue(key_)); | 91 return static_cast<Thread *>(TlsGetValue(key_)); |
92 } | 92 } |
93 | 93 |
94 void ThreadManager::SetCurrentThread(Thread *thread) { | 94 void ThreadManager::SetCurrentThread(Thread *thread) { |
95 TlsSetValue(key_, thread); | 95 TlsSetValue(key_, thread); |
96 } | 96 } |
97 #endif | 97 #endif |
98 | 98 |
99 Thread *ThreadManager::WrapCurrentThread() { | 99 Thread *ThreadManager::WrapCurrentThread() { |
100 Thread* result = CurrentThread(); | 100 Thread* result = CurrentThread(); |
101 if (NULL == result) { | 101 if (nullptr == result) { |
102 result = new Thread(); | 102 result = new Thread(); |
103 result->WrapCurrentWithThreadManager(this, true); | 103 result->WrapCurrentWithThreadManager(this, true); |
104 } | 104 } |
105 return result; | 105 return result; |
106 } | 106 } |
107 | 107 |
108 void ThreadManager::UnwrapCurrentThread() { | 108 void ThreadManager::UnwrapCurrentThread() { |
109 Thread* t = CurrentThread(); | 109 Thread* t = CurrentThread(); |
110 if (t && !(t->IsOwned())) { | 110 if (t && !(t->IsOwned())) { |
111 t->UnwrapCurrent(); | 111 t->UnwrapCurrent(); |
(...skipping 15 matching lines...) Expand all Loading... |
127 RTC_DCHECK(thread_->IsCurrent()); | 127 RTC_DCHECK(thread_->IsCurrent()); |
128 thread_->SetAllowBlockingCalls(previous_state_); | 128 thread_->SetAllowBlockingCalls(previous_state_); |
129 } | 129 } |
130 | 130 |
131 Thread::Thread() : Thread(SocketServer::CreateDefault()) {} | 131 Thread::Thread() : Thread(SocketServer::CreateDefault()) {} |
132 | 132 |
133 Thread::Thread(SocketServer* ss) | 133 Thread::Thread(SocketServer* ss) |
134 : MessageQueue(ss, false), | 134 : MessageQueue(ss, false), |
135 running_(true, false), | 135 running_(true, false), |
136 #if defined(WEBRTC_WIN) | 136 #if defined(WEBRTC_WIN) |
137 thread_(NULL), | 137 thread_(nullptr), |
138 thread_id_(0), | 138 thread_id_(0), |
139 #endif | 139 #endif |
140 owned_(true), | 140 owned_(true), |
141 blocking_calls_allowed_(true) { | 141 blocking_calls_allowed_(true) { |
142 SetName("Thread", this); // default name | 142 SetName("Thread", this); // default name |
143 DoInit(); | 143 DoInit(); |
144 } | 144 } |
145 | 145 |
146 Thread::Thread(std::unique_ptr<SocketServer> ss) | 146 Thread::Thread(std::unique_ptr<SocketServer> ss) |
147 : MessageQueue(std::move(ss), false), | 147 : MessageQueue(std::move(ss), false), |
148 running_(true, false), | 148 running_(true, false), |
149 #if defined(WEBRTC_WIN) | 149 #if defined(WEBRTC_WIN) |
150 thread_(NULL), | 150 thread_(nullptr), |
151 thread_id_(0), | 151 thread_id_(0), |
152 #endif | 152 #endif |
153 owned_(true), | 153 owned_(true), |
154 blocking_calls_allowed_(true) { | 154 blocking_calls_allowed_(true) { |
155 SetName("Thread", this); // default name | 155 SetName("Thread", this); // default name |
156 DoInit(); | 156 DoInit(); |
157 } | 157 } |
158 | 158 |
159 Thread::~Thread() { | 159 Thread::~Thread() { |
160 Stop(); | 160 Stop(); |
(...skipping 14 matching lines...) Expand all Loading... |
175 | 175 |
176 #if defined(WEBRTC_WIN) | 176 #if defined(WEBRTC_WIN) |
177 ::Sleep(milliseconds); | 177 ::Sleep(milliseconds); |
178 return true; | 178 return true; |
179 #else | 179 #else |
180 // POSIX has both a usleep() and a nanosleep(), but the former is deprecated, | 180 // POSIX has both a usleep() and a nanosleep(), but the former is deprecated, |
181 // so we use nanosleep() even though it has greater precision than necessary. | 181 // so we use nanosleep() even though it has greater precision than necessary. |
182 struct timespec ts; | 182 struct timespec ts; |
183 ts.tv_sec = milliseconds / 1000; | 183 ts.tv_sec = milliseconds / 1000; |
184 ts.tv_nsec = (milliseconds % 1000) * 1000000; | 184 ts.tv_nsec = (milliseconds % 1000) * 1000000; |
185 int ret = nanosleep(&ts, NULL); | 185 int ret = nanosleep(&ts, nullptr); |
186 if (ret != 0) { | 186 if (ret != 0) { |
187 LOG_ERR(LS_WARNING) << "nanosleep() returning early"; | 187 LOG_ERR(LS_WARNING) << "nanosleep() returning early"; |
188 return false; | 188 return false; |
189 } | 189 } |
190 return true; | 190 return true; |
191 #endif | 191 #endif |
192 } | 192 } |
193 | 193 |
194 bool Thread::SetName(const std::string& name, const void* obj) { | 194 bool Thread::SetName(const std::string& name, const void* obj) { |
195 if (running()) return false; | 195 if (running()) return false; |
(...skipping 15 matching lines...) Expand all Loading... |
211 Restart(); // reset IsQuitting() if the thread is being restarted | 211 Restart(); // reset IsQuitting() if the thread is being restarted |
212 | 212 |
213 // Make sure that ThreadManager is created on the main thread before | 213 // Make sure that ThreadManager is created on the main thread before |
214 // we start a new thread. | 214 // we start a new thread. |
215 ThreadManager::Instance(); | 215 ThreadManager::Instance(); |
216 | 216 |
217 ThreadInit* init = new ThreadInit; | 217 ThreadInit* init = new ThreadInit; |
218 init->thread = this; | 218 init->thread = this; |
219 init->runnable = runnable; | 219 init->runnable = runnable; |
220 #if defined(WEBRTC_WIN) | 220 #if defined(WEBRTC_WIN) |
221 thread_ = CreateThread(NULL, 0, PreRun, init, 0, &thread_id_); | 221 thread_ = CreateThread(nullptr, 0, PreRun, init, 0, &thread_id_); |
222 if (thread_) { | 222 if (thread_) { |
223 running_.Set(); | 223 running_.Set(); |
224 } else { | 224 } else { |
225 return false; | 225 return false; |
226 } | 226 } |
227 #elif defined(WEBRTC_POSIX) | 227 #elif defined(WEBRTC_POSIX) |
228 pthread_attr_t attr; | 228 pthread_attr_t attr; |
229 pthread_attr_init(&attr); | 229 pthread_attr_init(&attr); |
230 | 230 |
231 int error_code = pthread_create(&thread_, &attr, PreRun, init); | 231 int error_code = pthread_create(&thread_, &attr, PreRun, init); |
232 if (0 != error_code) { | 232 if (0 != error_code) { |
233 LOG(LS_ERROR) << "Unable to create pthread, error " << error_code; | 233 LOG(LS_ERROR) << "Unable to create pthread, error " << error_code; |
234 return false; | 234 return false; |
235 } | 235 } |
236 running_.Set(); | 236 running_.Set(); |
237 #endif | 237 #endif |
238 return true; | 238 return true; |
239 } | 239 } |
240 | 240 |
241 bool Thread::WrapCurrent() { | 241 bool Thread::WrapCurrent() { |
242 return WrapCurrentWithThreadManager(ThreadManager::Instance(), true); | 242 return WrapCurrentWithThreadManager(ThreadManager::Instance(), true); |
243 } | 243 } |
244 | 244 |
245 void Thread::UnwrapCurrent() { | 245 void Thread::UnwrapCurrent() { |
246 // Clears the platform-specific thread-specific storage. | 246 // Clears the platform-specific thread-specific storage. |
247 ThreadManager::Instance()->SetCurrentThread(NULL); | 247 ThreadManager::Instance()->SetCurrentThread(nullptr); |
248 #if defined(WEBRTC_WIN) | 248 #if defined(WEBRTC_WIN) |
249 if (thread_ != NULL) { | 249 if (thread_ != nullptr) { |
250 if (!CloseHandle(thread_)) { | 250 if (!CloseHandle(thread_)) { |
251 LOG_GLE(LS_ERROR) << "When unwrapping thread, failed to close handle."; | 251 LOG_GLE(LS_ERROR) << "When unwrapping thread, failed to close handle."; |
252 } | 252 } |
253 thread_ = NULL; | 253 thread_ = nullptr; |
254 } | 254 } |
255 #endif | 255 #endif |
256 running_.Reset(); | 256 running_.Reset(); |
257 } | 257 } |
258 | 258 |
259 void Thread::SafeWrapCurrent() { | 259 void Thread::SafeWrapCurrent() { |
260 WrapCurrentWithThreadManager(ThreadManager::Instance(), false); | 260 WrapCurrentWithThreadManager(ThreadManager::Instance(), false); |
261 } | 261 } |
262 | 262 |
263 void Thread::Join() { | 263 void Thread::Join() { |
264 if (running()) { | 264 if (running()) { |
265 RTC_DCHECK(!IsCurrent()); | 265 RTC_DCHECK(!IsCurrent()); |
266 if (Current() && !Current()->blocking_calls_allowed_) { | 266 if (Current() && !Current()->blocking_calls_allowed_) { |
267 LOG(LS_WARNING) << "Waiting for the thread to join, " | 267 LOG(LS_WARNING) << "Waiting for the thread to join, " |
268 << "but blocking calls have been disallowed"; | 268 << "but blocking calls have been disallowed"; |
269 } | 269 } |
270 | 270 |
271 #if defined(WEBRTC_WIN) | 271 #if defined(WEBRTC_WIN) |
272 RTC_DCHECK(thread_ != NULL); | 272 RTC_DCHECK(thread_ != nullptr); |
273 WaitForSingleObject(thread_, INFINITE); | 273 WaitForSingleObject(thread_, INFINITE); |
274 CloseHandle(thread_); | 274 CloseHandle(thread_); |
275 thread_ = NULL; | 275 thread_ = nullptr; |
276 thread_id_ = 0; | 276 thread_id_ = 0; |
277 #elif defined(WEBRTC_POSIX) | 277 #elif defined(WEBRTC_POSIX) |
278 void *pv; | 278 void *pv; |
279 pthread_join(thread_, &pv); | 279 pthread_join(thread_, &pv); |
280 #endif | 280 #endif |
281 running_.Reset(); | 281 running_.Reset(); |
282 } | 282 } |
283 } | 283 } |
284 | 284 |
285 bool Thread::SetAllowBlockingCalls(bool allow) { | 285 bool Thread::SetAllowBlockingCalls(bool allow) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 msg.pdata = pdata; | 353 msg.pdata = pdata; |
354 if (IsCurrent()) { | 354 if (IsCurrent()) { |
355 phandler->OnMessage(&msg); | 355 phandler->OnMessage(&msg); |
356 return; | 356 return; |
357 } | 357 } |
358 | 358 |
359 AssertBlockingIsAllowedOnCurrentThread(); | 359 AssertBlockingIsAllowedOnCurrentThread(); |
360 | 360 |
361 AutoThread thread; | 361 AutoThread thread; |
362 Thread *current_thread = Thread::Current(); | 362 Thread *current_thread = Thread::Current(); |
363 RTC_DCHECK(current_thread != NULL); // AutoThread ensures this | 363 RTC_DCHECK(current_thread != nullptr); // AutoThread ensures this |
364 | 364 |
365 bool ready = false; | 365 bool ready = false; |
366 { | 366 { |
367 CritScope cs(&crit_); | 367 CritScope cs(&crit_); |
368 _SendMessage smsg; | 368 _SendMessage smsg; |
369 smsg.thread = current_thread; | 369 smsg.thread = current_thread; |
370 smsg.msg = msg; | 370 smsg.msg = msg; |
371 smsg.ready = &ready; | 371 smsg.ready = &ready; |
372 sendlist_.push_back(smsg); | 372 sendlist_.push_back(smsg); |
373 } | 373 } |
(...skipping 24 matching lines...) Expand all Loading... |
398 // Post while waiting for the Send to complete, which means that when we exit | 398 // Post while waiting for the Send to complete, which means that when we exit |
399 // this loop, we need to issue another WakeUp, or else the Posted message | 399 // this loop, we need to issue another WakeUp, or else the Posted message |
400 // won't be processed in a timely manner. | 400 // won't be processed in a timely manner. |
401 | 401 |
402 if (waited) { | 402 if (waited) { |
403 current_thread->socketserver()->WakeUp(); | 403 current_thread->socketserver()->WakeUp(); |
404 } | 404 } |
405 } | 405 } |
406 | 406 |
407 void Thread::ReceiveSends() { | 407 void Thread::ReceiveSends() { |
408 ReceiveSendsFromThread(NULL); | 408 ReceiveSendsFromThread(nullptr); |
409 } | 409 } |
410 | 410 |
411 void Thread::ReceiveSendsFromThread(const Thread* source) { | 411 void Thread::ReceiveSendsFromThread(const Thread* source) { |
412 // Receive a sent message. Cleanup scenarios: | 412 // Receive a sent message. Cleanup scenarios: |
413 // - thread sending exits: We don't allow this, since thread can exit | 413 // - thread sending exits: We don't allow this, since thread can exit |
414 // only via Join, so Send must complete. | 414 // only via Join, so Send must complete. |
415 // - thread receiving exits: Wakeup/set ready in Thread::Clear() | 415 // - thread receiving exits: Wakeup/set ready in Thread::Clear() |
416 // - object target cleared: Wakeup/set ready in Thread::Clear() | 416 // - object target cleared: Wakeup/set ready in Thread::Clear() |
417 _SendMessage smsg; | 417 _SendMessage smsg; |
418 | 418 |
419 crit_.Enter(); | 419 crit_.Enter(); |
420 while (PopSendMessageFromThread(source, &smsg)) { | 420 while (PopSendMessageFromThread(source, &smsg)) { |
421 crit_.Leave(); | 421 crit_.Leave(); |
422 | 422 |
423 smsg.msg.phandler->OnMessage(&smsg.msg); | 423 smsg.msg.phandler->OnMessage(&smsg.msg); |
424 | 424 |
425 crit_.Enter(); | 425 crit_.Enter(); |
426 *smsg.ready = true; | 426 *smsg.ready = true; |
427 smsg.thread->socketserver()->WakeUp(); | 427 smsg.thread->socketserver()->WakeUp(); |
428 } | 428 } |
429 crit_.Leave(); | 429 crit_.Leave(); |
430 } | 430 } |
431 | 431 |
432 bool Thread::PopSendMessageFromThread(const Thread* source, _SendMessage* msg) { | 432 bool Thread::PopSendMessageFromThread(const Thread* source, _SendMessage* msg) { |
433 for (std::list<_SendMessage>::iterator it = sendlist_.begin(); | 433 for (std::list<_SendMessage>::iterator it = sendlist_.begin(); |
434 it != sendlist_.end(); ++it) { | 434 it != sendlist_.end(); ++it) { |
435 if (it->thread == source || source == NULL) { | 435 if (it->thread == source || source == nullptr) { |
436 *msg = *it; | 436 *msg = *it; |
437 sendlist_.erase(it); | 437 sendlist_.erase(it); |
438 return true; | 438 return true; |
439 } | 439 } |
440 } | 440 } |
441 return false; | 441 return false; |
442 } | 442 } |
443 | 443 |
444 void Thread::InvokeInternal(const Location& posted_from, | 444 void Thread::InvokeInternal(const Location& posted_from, |
445 MessageHandler* handler) { | 445 MessageHandler* handler) { |
446 TRACE_EVENT2("webrtc", "Thread::Invoke", "src_file_and_line", | 446 TRACE_EVENT2("webrtc", "Thread::Invoke", "src_file_and_line", |
447 posted_from.file_and_line(), "src_func", | 447 posted_from.file_and_line(), "src_func", |
448 posted_from.function_name()); | 448 posted_from.function_name()); |
449 Send(posted_from, handler); | 449 Send(posted_from, handler); |
450 } | 450 } |
451 | 451 |
452 void Thread::Clear(MessageHandler* phandler, | 452 void Thread::Clear(MessageHandler* phandler, |
453 uint32_t id, | 453 uint32_t id, |
454 MessageList* removed) { | 454 MessageList* removed) { |
455 CritScope cs(&crit_); | 455 CritScope cs(&crit_); |
456 | 456 |
457 // Remove messages on sendlist_ with phandler | 457 // Remove messages on sendlist_ with phandler |
458 // Object target cleared: remove from send list, wakeup/set ready | 458 // Object target cleared: remove from send list, wakeup/set ready |
459 // if sender not NULL. | 459 // if sender not null. |
460 | 460 |
461 std::list<_SendMessage>::iterator iter = sendlist_.begin(); | 461 std::list<_SendMessage>::iterator iter = sendlist_.begin(); |
462 while (iter != sendlist_.end()) { | 462 while (iter != sendlist_.end()) { |
463 _SendMessage smsg = *iter; | 463 _SendMessage smsg = *iter; |
464 if (smsg.msg.Match(phandler, id)) { | 464 if (smsg.msg.Match(phandler, id)) { |
465 if (removed) { | 465 if (removed) { |
466 removed->push_back(smsg.msg); | 466 removed->push_back(smsg.msg); |
467 } else { | 467 } else { |
468 delete smsg.msg.pdata; | 468 delete smsg.msg.pdata; |
469 } | 469 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 | 531 |
532 AutoThread::AutoThread() { | 532 AutoThread::AutoThread() { |
533 if (!ThreadManager::Instance()->CurrentThread()) { | 533 if (!ThreadManager::Instance()->CurrentThread()) { |
534 ThreadManager::Instance()->SetCurrentThread(this); | 534 ThreadManager::Instance()->SetCurrentThread(this); |
535 } | 535 } |
536 } | 536 } |
537 | 537 |
538 AutoThread::~AutoThread() { | 538 AutoThread::~AutoThread() { |
539 Stop(); | 539 Stop(); |
540 if (ThreadManager::Instance()->CurrentThread() == this) { | 540 if (ThreadManager::Instance()->CurrentThread() == this) { |
541 ThreadManager::Instance()->SetCurrentThread(NULL); | 541 ThreadManager::Instance()->SetCurrentThread(nullptr); |
542 } | 542 } |
543 } | 543 } |
544 | 544 |
545 #if defined(WEBRTC_WIN) | 545 #if defined(WEBRTC_WIN) |
546 ComThread::~ComThread() { | 546 ComThread::~ComThread() { |
547 Stop(); | 547 Stop(); |
548 } | 548 } |
549 | 549 |
550 void ComThread::Run() { | 550 void ComThread::Run() { |
551 HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); | 551 HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); |
552 RTC_DCHECK(SUCCEEDED(hr)); | 552 RTC_DCHECK(SUCCEEDED(hr)); |
553 if (SUCCEEDED(hr)) { | 553 if (SUCCEEDED(hr)) { |
554 Thread::Run(); | 554 Thread::Run(); |
555 CoUninitialize(); | 555 CoUninitialize(); |
556 } else { | 556 } else { |
557 LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr; | 557 LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr; |
558 } | 558 } |
559 } | 559 } |
560 #endif | 560 #endif |
561 | 561 |
562 } // namespace rtc | 562 } // namespace rtc |
OLD | NEW |