| 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 |
| 11 #include "webrtc/base/thread.h" | 11 #include "webrtc/base/thread.h" |
| 12 | 12 |
| 13 #ifndef __has_feature | 13 #ifndef __has_feature |
| 14 #define __has_feature(x) 0 // Compatibility with non-clang or LLVM compilers. | 14 #define __has_feature(x) 0 // Compatibility with non-clang or LLVM compilers. |
| 15 #endif // __has_feature | 15 #endif // __has_feature |
| 16 | 16 |
| 17 #if defined(WEBRTC_WIN) | 17 #if defined(WEBRTC_WIN) |
| 18 #include <comdef.h> | 18 #include <comdef.h> |
| 19 #elif defined(WEBRTC_POSIX) | 19 #elif defined(WEBRTC_POSIX) |
| 20 #include <time.h> | 20 #include <time.h> |
| 21 #endif | 21 #endif |
| 22 | 22 |
| 23 #include "webrtc/base/checks.h" |
| 23 #include "webrtc/base/common.h" | 24 #include "webrtc/base/common.h" |
| 24 #include "webrtc/base/logging.h" | 25 #include "webrtc/base/logging.h" |
| 25 #include "webrtc/base/nullsocketserver.h" | 26 #include "webrtc/base/nullsocketserver.h" |
| 26 #include "webrtc/base/platform_thread.h" | 27 #include "webrtc/base/platform_thread.h" |
| 27 #include "webrtc/base/stringutils.h" | 28 #include "webrtc/base/stringutils.h" |
| 28 #include "webrtc/base/timeutils.h" | 29 #include "webrtc/base/timeutils.h" |
| 29 #include "webrtc/base/trace_event.h" | 30 #include "webrtc/base/trace_event.h" |
| 30 | 31 |
| 31 #if !__has_feature(objc_arc) && (defined(WEBRTC_MAC)) | 32 #if !__has_feature(objc_arc) && (defined(WEBRTC_MAC)) |
| 32 #include "webrtc/base/maccocoathreadhelper.h" | 33 #include "webrtc/base/maccocoathreadhelper.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 Thread* thread; | 128 Thread* thread; |
| 128 Runnable* runnable; | 129 Runnable* runnable; |
| 129 }; | 130 }; |
| 130 | 131 |
| 131 Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls() | 132 Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls() |
| 132 : thread_(Thread::Current()), | 133 : thread_(Thread::Current()), |
| 133 previous_state_(thread_->SetAllowBlockingCalls(false)) { | 134 previous_state_(thread_->SetAllowBlockingCalls(false)) { |
| 134 } | 135 } |
| 135 | 136 |
| 136 Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() { | 137 Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() { |
| 137 ASSERT(thread_->IsCurrent()); | 138 RTC_DCHECK(thread_->IsCurrent()); |
| 138 thread_->SetAllowBlockingCalls(previous_state_); | 139 thread_->SetAllowBlockingCalls(previous_state_); |
| 139 } | 140 } |
| 140 | 141 |
| 141 Thread::Thread() : Thread(SocketServer::CreateDefault()) {} | 142 Thread::Thread() : Thread(SocketServer::CreateDefault()) {} |
| 142 | 143 |
| 143 Thread::Thread(SocketServer* ss) | 144 Thread::Thread(SocketServer* ss) |
| 144 : MessageQueue(ss, false), | 145 : MessageQueue(ss, false), |
| 145 running_(true, false), | 146 running_(true, false), |
| 146 #if defined(WEBRTC_WIN) | 147 #if defined(WEBRTC_WIN) |
| 147 thread_(NULL), | 148 thread_(NULL), |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 name_ = name; | 207 name_ = name; |
| 207 if (obj) { | 208 if (obj) { |
| 208 char buf[16]; | 209 char buf[16]; |
| 209 sprintfn(buf, sizeof(buf), " 0x%p", obj); | 210 sprintfn(buf, sizeof(buf), " 0x%p", obj); |
| 210 name_ += buf; | 211 name_ += buf; |
| 211 } | 212 } |
| 212 return true; | 213 return true; |
| 213 } | 214 } |
| 214 | 215 |
| 215 bool Thread::Start(Runnable* runnable) { | 216 bool Thread::Start(Runnable* runnable) { |
| 216 ASSERT(owned_); | 217 RTC_DCHECK(owned_); |
| 217 if (!owned_) return false; | 218 if (!owned_) return false; |
| 218 ASSERT(!running()); | 219 RTC_DCHECK(!running()); |
| 219 if (running()) return false; | 220 if (running()) return false; |
| 220 | 221 |
| 221 Restart(); // reset IsQuitting() if the thread is being restarted | 222 Restart(); // reset IsQuitting() if the thread is being restarted |
| 222 | 223 |
| 223 // Make sure that ThreadManager is created on the main thread before | 224 // Make sure that ThreadManager is created on the main thread before |
| 224 // we start a new thread. | 225 // we start a new thread. |
| 225 ThreadManager::Instance(); | 226 ThreadManager::Instance(); |
| 226 | 227 |
| 227 ThreadInit* init = new ThreadInit; | 228 ThreadInit* init = new ThreadInit; |
| 228 init->thread = this; | 229 init->thread = this; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 #endif | 267 #endif |
| 267 running_.Reset(); | 268 running_.Reset(); |
| 268 } | 269 } |
| 269 | 270 |
| 270 void Thread::SafeWrapCurrent() { | 271 void Thread::SafeWrapCurrent() { |
| 271 WrapCurrentWithThreadManager(ThreadManager::Instance(), false); | 272 WrapCurrentWithThreadManager(ThreadManager::Instance(), false); |
| 272 } | 273 } |
| 273 | 274 |
| 274 void Thread::Join() { | 275 void Thread::Join() { |
| 275 if (running()) { | 276 if (running()) { |
| 276 ASSERT(!IsCurrent()); | 277 RTC_DCHECK(!IsCurrent()); |
| 277 if (Current() && !Current()->blocking_calls_allowed_) { | 278 if (Current() && !Current()->blocking_calls_allowed_) { |
| 278 LOG(LS_WARNING) << "Waiting for the thread to join, " | 279 LOG(LS_WARNING) << "Waiting for the thread to join, " |
| 279 << "but blocking calls have been disallowed"; | 280 << "but blocking calls have been disallowed"; |
| 280 } | 281 } |
| 281 | 282 |
| 282 #if defined(WEBRTC_WIN) | 283 #if defined(WEBRTC_WIN) |
| 283 ASSERT(thread_ != NULL); | 284 RTC_DCHECK(thread_ != NULL); |
| 284 WaitForSingleObject(thread_, INFINITE); | 285 WaitForSingleObject(thread_, INFINITE); |
| 285 CloseHandle(thread_); | 286 CloseHandle(thread_); |
| 286 thread_ = NULL; | 287 thread_ = NULL; |
| 287 thread_id_ = 0; | 288 thread_id_ = 0; |
| 288 #elif defined(WEBRTC_POSIX) | 289 #elif defined(WEBRTC_POSIX) |
| 289 void *pv; | 290 void *pv; |
| 290 pthread_join(thread_, &pv); | 291 pthread_join(thread_, &pv); |
| 291 #endif | 292 #endif |
| 292 running_.Reset(); | 293 running_.Reset(); |
| 293 } | 294 } |
| 294 } | 295 } |
| 295 | 296 |
| 296 bool Thread::SetAllowBlockingCalls(bool allow) { | 297 bool Thread::SetAllowBlockingCalls(bool allow) { |
| 297 ASSERT(IsCurrent()); | 298 RTC_DCHECK(IsCurrent()); |
| 298 bool previous = blocking_calls_allowed_; | 299 bool previous = blocking_calls_allowed_; |
| 299 blocking_calls_allowed_ = allow; | 300 blocking_calls_allowed_ = allow; |
| 300 return previous; | 301 return previous; |
| 301 } | 302 } |
| 302 | 303 |
| 303 // static | 304 // static |
| 304 void Thread::AssertBlockingIsAllowedOnCurrentThread() { | 305 void Thread::AssertBlockingIsAllowedOnCurrentThread() { |
| 305 #if !defined(NDEBUG) | 306 #if !defined(NDEBUG) |
| 306 Thread* current = Thread::Current(); | 307 Thread* current = Thread::Current(); |
| 307 ASSERT(!current || current->blocking_calls_allowed_); | 308 RTC_DCHECK(!current || current->blocking_calls_allowed_); |
| 308 #endif | 309 #endif |
| 309 } | 310 } |
| 310 | 311 |
| 311 void* Thread::PreRun(void* pv) { | 312 void* Thread::PreRun(void* pv) { |
| 312 ThreadInit* init = static_cast<ThreadInit*>(pv); | 313 ThreadInit* init = static_cast<ThreadInit*>(pv); |
| 313 ThreadManager::Instance()->SetCurrentThread(init->thread); | 314 ThreadManager::Instance()->SetCurrentThread(init->thread); |
| 314 rtc::SetCurrentThreadName(init->thread->name_.c_str()); | 315 rtc::SetCurrentThreadName(init->thread->name_.c_str()); |
| 315 #if __has_feature(objc_arc) | 316 #if __has_feature(objc_arc) |
| 316 @autoreleasepool | 317 @autoreleasepool |
| 317 #elif defined(WEBRTC_MAC) | 318 #elif defined(WEBRTC_MAC) |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 msg.pdata = pdata; | 360 msg.pdata = pdata; |
| 360 if (IsCurrent()) { | 361 if (IsCurrent()) { |
| 361 phandler->OnMessage(&msg); | 362 phandler->OnMessage(&msg); |
| 362 return; | 363 return; |
| 363 } | 364 } |
| 364 | 365 |
| 365 AssertBlockingIsAllowedOnCurrentThread(); | 366 AssertBlockingIsAllowedOnCurrentThread(); |
| 366 | 367 |
| 367 AutoThread thread; | 368 AutoThread thread; |
| 368 Thread *current_thread = Thread::Current(); | 369 Thread *current_thread = Thread::Current(); |
| 369 ASSERT(current_thread != NULL); // AutoThread ensures this | 370 RTC_DCHECK(current_thread != NULL); // AutoThread ensures this |
| 370 | 371 |
| 371 bool ready = false; | 372 bool ready = false; |
| 372 { | 373 { |
| 373 CritScope cs(&crit_); | 374 CritScope cs(&crit_); |
| 374 _SendMessage smsg; | 375 _SendMessage smsg; |
| 375 smsg.thread = current_thread; | 376 smsg.thread = current_thread; |
| 376 smsg.msg = msg; | 377 smsg.msg = msg; |
| 377 smsg.ready = &ready; | 378 smsg.ready = &ready; |
| 378 sendlist_.push_back(smsg); | 379 sendlist_.push_back(smsg); |
| 379 } | 380 } |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 548 AutoThread::~AutoThread() { | 549 AutoThread::~AutoThread() { |
| 549 Stop(); | 550 Stop(); |
| 550 if (ThreadManager::Instance()->CurrentThread() == this) { | 551 if (ThreadManager::Instance()->CurrentThread() == this) { |
| 551 ThreadManager::Instance()->SetCurrentThread(NULL); | 552 ThreadManager::Instance()->SetCurrentThread(NULL); |
| 552 } | 553 } |
| 553 } | 554 } |
| 554 | 555 |
| 555 #if defined(WEBRTC_WIN) | 556 #if defined(WEBRTC_WIN) |
| 556 void ComThread::Run() { | 557 void ComThread::Run() { |
| 557 HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); | 558 HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); |
| 558 ASSERT(SUCCEEDED(hr)); | 559 RTC_DCHECK(SUCCEEDED(hr)); |
| 559 if (SUCCEEDED(hr)) { | 560 if (SUCCEEDED(hr)) { |
| 560 Thread::Run(); | 561 Thread::Run(); |
| 561 CoUninitialize(); | 562 CoUninitialize(); |
| 562 } else { | 563 } else { |
| 563 LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr; | 564 LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr; |
| 564 } | 565 } |
| 565 } | 566 } |
| 566 #endif | 567 #endif |
| 567 | 568 |
| 568 } // namespace rtc | 569 } // namespace rtc |
| OLD | NEW |