| 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/common.h" | 23 #include "webrtc/base/common.h" |
| 24 #include "webrtc/base/logging.h" | 24 #include "webrtc/base/logging.h" |
| 25 #include "webrtc/base/nullsocketserver.h" |
| 25 #include "webrtc/base/platform_thread.h" | 26 #include "webrtc/base/platform_thread.h" |
| 26 #include "webrtc/base/stringutils.h" | 27 #include "webrtc/base/stringutils.h" |
| 27 #include "webrtc/base/timeutils.h" | 28 #include "webrtc/base/timeutils.h" |
| 28 | 29 |
| 29 #if !__has_feature(objc_arc) && (defined(WEBRTC_MAC)) | 30 #if !__has_feature(objc_arc) && (defined(WEBRTC_MAC)) |
| 30 #include "webrtc/base/maccocoathreadhelper.h" | 31 #include "webrtc/base/maccocoathreadhelper.h" |
| 31 #include "webrtc/base/scoped_autorelease_pool.h" | 32 #include "webrtc/base/scoped_autorelease_pool.h" |
| 32 #endif | 33 #endif |
| 33 | 34 |
| 34 #include "webrtc/base/trace_event.h" | 35 #include "webrtc/base/trace_event.h" |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 ASSERT(thread_->IsCurrent()); |
| 138 thread_->SetAllowBlockingCalls(previous_state_); | 139 thread_->SetAllowBlockingCalls(previous_state_); |
| 139 } | 140 } |
| 140 | 141 |
| 141 Thread::Thread(SocketServer* ss, bool init_queue) | 142 Thread::Thread() : Thread(SocketServer::CreateDefault()) {} |
| 143 |
| 144 Thread::Thread(SocketServer* ss) |
| 142 : MessageQueue(ss, false), | 145 : MessageQueue(ss, false), |
| 143 running_(true, false), | 146 running_(true, false), |
| 144 #if defined(WEBRTC_WIN) | 147 #if defined(WEBRTC_WIN) |
| 145 thread_(NULL), | 148 thread_(NULL), |
| 146 thread_id_(0), | 149 thread_id_(0), |
| 147 #endif | 150 #endif |
| 148 owned_(true), | 151 owned_(true), |
| 149 blocking_calls_allowed_(true) { | 152 blocking_calls_allowed_(true) { |
| 150 SetName("Thread", this); // default name | 153 SetName("Thread", this); // default name |
| 151 if (init_queue) { | 154 DoInit(); |
| 152 DoInit(); | 155 } |
| 153 } | 156 |
| 157 Thread::Thread(std::unique_ptr<SocketServer> ss) |
| 158 : MessageQueue(std::move(ss), false), |
| 159 running_(true, false), |
| 160 #if defined(WEBRTC_WIN) |
| 161 thread_(NULL), |
| 162 thread_id_(0), |
| 163 #endif |
| 164 owned_(true), |
| 165 blocking_calls_allowed_(true) { |
| 166 SetName("Thread", this); // default name |
| 167 DoInit(); |
| 154 } | 168 } |
| 155 | 169 |
| 156 Thread::~Thread() { | 170 Thread::~Thread() { |
| 157 Stop(); | 171 Stop(); |
| 158 DoDestroy(); | 172 DoDestroy(); |
| 159 } | 173 } |
| 160 | 174 |
| 175 std::unique_ptr<Thread> Thread::CreateWithSocketServer() { |
| 176 return std::unique_ptr<Thread>(new Thread(SocketServer::CreateDefault())); |
| 177 } |
| 178 |
| 179 std::unique_ptr<Thread> Thread::Create() { |
| 180 return std::unique_ptr<Thread>( |
| 181 new Thread(std::unique_ptr<SocketServer>(new NullSocketServer()))); |
| 182 } |
| 183 |
| 161 bool Thread::SleepMs(int milliseconds) { | 184 bool Thread::SleepMs(int milliseconds) { |
| 162 AssertBlockingIsAllowedOnCurrentThread(); | 185 AssertBlockingIsAllowedOnCurrentThread(); |
| 163 | 186 |
| 164 #if defined(WEBRTC_WIN) | 187 #if defined(WEBRTC_WIN) |
| 165 ::Sleep(milliseconds); | 188 ::Sleep(milliseconds); |
| 166 return true; | 189 return true; |
| 167 #else | 190 #else |
| 168 // POSIX has both a usleep() and a nanosleep(), but the former is deprecated, | 191 // POSIX has both a usleep() and a nanosleep(), but the former is deprecated, |
| 169 // so we use nanosleep() even though it has greater precision than necessary. | 192 // so we use nanosleep() even though it has greater precision than necessary. |
| 170 struct timespec ts; | 193 struct timespec ts; |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 #elif defined(WEBRTC_POSIX) | 529 #elif defined(WEBRTC_POSIX) |
| 507 thread_ = pthread_self(); | 530 thread_ = pthread_self(); |
| 508 #endif | 531 #endif |
| 509 | 532 |
| 510 owned_ = false; | 533 owned_ = false; |
| 511 running_.Set(); | 534 running_.Set(); |
| 512 thread_manager->SetCurrentThread(this); | 535 thread_manager->SetCurrentThread(this); |
| 513 return true; | 536 return true; |
| 514 } | 537 } |
| 515 | 538 |
| 516 AutoThread::AutoThread(SocketServer* ss) : Thread(ss) { | 539 AutoThread::AutoThread() { |
| 517 if (!ThreadManager::Instance()->CurrentThread()) { | 540 if (!ThreadManager::Instance()->CurrentThread()) { |
| 518 ThreadManager::Instance()->SetCurrentThread(this); | 541 ThreadManager::Instance()->SetCurrentThread(this); |
| 519 } | 542 } |
| 520 } | 543 } |
| 521 | 544 |
| 522 AutoThread::~AutoThread() { | 545 AutoThread::~AutoThread() { |
| 523 Stop(); | 546 Stop(); |
| 524 if (ThreadManager::Instance()->CurrentThread() == this) { | 547 if (ThreadManager::Instance()->CurrentThread() == this) { |
| 525 ThreadManager::Instance()->SetCurrentThread(NULL); | 548 ThreadManager::Instance()->SetCurrentThread(NULL); |
| 526 } | 549 } |
| 527 } | 550 } |
| 528 | 551 |
| 529 #if defined(WEBRTC_WIN) | 552 #if defined(WEBRTC_WIN) |
| 530 void ComThread::Run() { | 553 void ComThread::Run() { |
| 531 HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); | 554 HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); |
| 532 ASSERT(SUCCEEDED(hr)); | 555 ASSERT(SUCCEEDED(hr)); |
| 533 if (SUCCEEDED(hr)) { | 556 if (SUCCEEDED(hr)) { |
| 534 Thread::Run(); | 557 Thread::Run(); |
| 535 CoUninitialize(); | 558 CoUninitialize(); |
| 536 } else { | 559 } else { |
| 537 LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr; | 560 LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr; |
| 538 } | 561 } |
| 539 } | 562 } |
| 540 #endif | 563 #endif |
| 541 | 564 |
| 542 } // namespace rtc | 565 } // namespace rtc |
| OLD | NEW |