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