henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/base/thread.h" |
| 12 | |
| 13 | #ifndef __has_feature |
| 14 | #define __has_feature(x) 0 // Compatibility with non-clang or LLVM compilers. |
| 15 | #endif // __has_feature |
| 16 | |
| 17 | #if defined(WEBRTC_WIN) |
| 18 | #include <comdef.h> |
| 19 | #elif defined(WEBRTC_POSIX) |
| 20 | #include <time.h> |
| 21 | #endif |
| 22 | |
| 23 | #include "webrtc/base/common.h" |
| 24 | #include "webrtc/base/logging.h" |
Tommi | ea14f0a | 2015-05-18 11:51:06 | [diff] [blame] | 25 | #include "webrtc/base/platform_thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 26 | #include "webrtc/base/stringutils.h" |
| 27 | #include "webrtc/base/timeutils.h" |
| 28 | |
| 29 | #if !__has_feature(objc_arc) && (defined(WEBRTC_MAC)) |
| 30 | #include "webrtc/base/maccocoathreadhelper.h" |
| 31 | #include "webrtc/base/scoped_autorelease_pool.h" |
| 32 | #endif |
| 33 | |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 34 | #include "webrtc/base/trace_event.h" |
| 35 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 36 | namespace rtc { |
| 37 | |
| 38 | ThreadManager* ThreadManager::Instance() { |
Andrew MacDonald | 469c2c0 | 2015-05-23 00:50:26 | [diff] [blame] | 39 | RTC_DEFINE_STATIC_LOCAL(ThreadManager, thread_manager, ()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 40 | return &thread_manager; |
| 41 | } |
| 42 | |
| 43 | // static |
| 44 | Thread* Thread::Current() { |
| 45 | return ThreadManager::Instance()->CurrentThread(); |
| 46 | } |
| 47 | |
| 48 | #if defined(WEBRTC_POSIX) |
| 49 | ThreadManager::ThreadManager() { |
| 50 | pthread_key_create(&key_, NULL); |
| 51 | #ifndef NO_MAIN_THREAD_WRAPPING |
| 52 | WrapCurrentThread(); |
| 53 | #endif |
| 54 | #if !__has_feature(objc_arc) && (defined(WEBRTC_MAC)) |
| 55 | // Under Automatic Reference Counting (ARC), you cannot use autorelease pools |
| 56 | // directly. Instead, you use @autoreleasepool blocks instead. Also, we are |
| 57 | // maintaining thread safety using immutability within context of GCD dispatch |
| 58 | // queues in this case. |
| 59 | InitCocoaMultiThreading(); |
| 60 | #endif |
| 61 | } |
| 62 | |
| 63 | ThreadManager::~ThreadManager() { |
| 64 | #if __has_feature(objc_arc) |
| 65 | @autoreleasepool |
| 66 | #elif defined(WEBRTC_MAC) |
| 67 | // This is called during exit, at which point apparently no NSAutoreleasePools |
| 68 | // are available; but we might still need them to do cleanup (or we get the |
| 69 | // "no autoreleasepool in place, just leaking" warning when exiting). |
| 70 | ScopedAutoreleasePool pool; |
| 71 | #endif |
| 72 | { |
| 73 | UnwrapCurrentThread(); |
| 74 | pthread_key_delete(key_); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | Thread *ThreadManager::CurrentThread() { |
| 79 | return static_cast<Thread *>(pthread_getspecific(key_)); |
| 80 | } |
| 81 | |
| 82 | void ThreadManager::SetCurrentThread(Thread *thread) { |
| 83 | pthread_setspecific(key_, thread); |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | #if defined(WEBRTC_WIN) |
| 88 | ThreadManager::ThreadManager() { |
| 89 | key_ = TlsAlloc(); |
| 90 | #ifndef NO_MAIN_THREAD_WRAPPING |
| 91 | WrapCurrentThread(); |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | ThreadManager::~ThreadManager() { |
| 96 | UnwrapCurrentThread(); |
| 97 | TlsFree(key_); |
| 98 | } |
| 99 | |
| 100 | Thread *ThreadManager::CurrentThread() { |
| 101 | return static_cast<Thread *>(TlsGetValue(key_)); |
| 102 | } |
| 103 | |
| 104 | void ThreadManager::SetCurrentThread(Thread *thread) { |
| 105 | TlsSetValue(key_, thread); |
| 106 | } |
| 107 | #endif |
| 108 | |
| 109 | Thread *ThreadManager::WrapCurrentThread() { |
| 110 | Thread* result = CurrentThread(); |
| 111 | if (NULL == result) { |
| 112 | result = new Thread(); |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 113 | result->WrapCurrentWithThreadManager(this, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 114 | } |
| 115 | return result; |
| 116 | } |
| 117 | |
| 118 | void ThreadManager::UnwrapCurrentThread() { |
| 119 | Thread* t = CurrentThread(); |
| 120 | if (t && !(t->IsOwned())) { |
| 121 | t->UnwrapCurrent(); |
| 122 | delete t; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | struct ThreadInit { |
| 127 | Thread* thread; |
| 128 | Runnable* runnable; |
| 129 | }; |
| 130 | |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 | [diff] [blame] | 131 | Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls() |
| 132 | : thread_(Thread::Current()), |
| 133 | previous_state_(thread_->SetAllowBlockingCalls(false)) { |
| 134 | } |
| 135 | |
| 136 | Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() { |
| 137 | ASSERT(thread_->IsCurrent()); |
| 138 | thread_->SetAllowBlockingCalls(previous_state_); |
| 139 | } |
| 140 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 141 | Thread::Thread(SocketServer* ss) |
| 142 | : MessageQueue(ss), |
| 143 | priority_(PRIORITY_NORMAL), |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 | [diff] [blame] | 144 | running_(true, false), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 145 | #if defined(WEBRTC_WIN) |
| 146 | thread_(NULL), |
| 147 | thread_id_(0), |
| 148 | #endif |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 | [diff] [blame] | 149 | owned_(true), |
| 150 | blocking_calls_allowed_(true) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 151 | SetName("Thread", this); // default name |
| 152 | } |
| 153 | |
| 154 | Thread::~Thread() { |
| 155 | Stop(); |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 | [diff] [blame] | 156 | Clear(NULL); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | bool Thread::SleepMs(int milliseconds) { |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 | [diff] [blame] | 160 | AssertBlockingIsAllowedOnCurrentThread(); |
| 161 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 162 | #if defined(WEBRTC_WIN) |
| 163 | ::Sleep(milliseconds); |
| 164 | return true; |
| 165 | #else |
| 166 | // POSIX has both a usleep() and a nanosleep(), but the former is deprecated, |
| 167 | // so we use nanosleep() even though it has greater precision than necessary. |
| 168 | struct timespec ts; |
| 169 | ts.tv_sec = milliseconds / 1000; |
| 170 | ts.tv_nsec = (milliseconds % 1000) * 1000000; |
| 171 | int ret = nanosleep(&ts, NULL); |
| 172 | if (ret != 0) { |
| 173 | LOG_ERR(LS_WARNING) << "nanosleep() returning early"; |
| 174 | return false; |
| 175 | } |
| 176 | return true; |
| 177 | #endif |
| 178 | } |
| 179 | |
| 180 | bool Thread::SetName(const std::string& name, const void* obj) { |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 | [diff] [blame] | 181 | if (running()) return false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 182 | name_ = name; |
| 183 | if (obj) { |
| 184 | char buf[16]; |
| 185 | sprintfn(buf, sizeof(buf), " 0x%p", obj); |
| 186 | name_ += buf; |
| 187 | } |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | bool Thread::SetPriority(ThreadPriority priority) { |
| 192 | #if defined(WEBRTC_WIN) |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 | [diff] [blame] | 193 | if (running()) { |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 194 | ASSERT(thread_ != NULL); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 195 | BOOL ret = FALSE; |
| 196 | if (priority == PRIORITY_NORMAL) { |
| 197 | ret = ::SetThreadPriority(thread_, THREAD_PRIORITY_NORMAL); |
| 198 | } else if (priority == PRIORITY_HIGH) { |
| 199 | ret = ::SetThreadPriority(thread_, THREAD_PRIORITY_HIGHEST); |
| 200 | } else if (priority == PRIORITY_ABOVE_NORMAL) { |
| 201 | ret = ::SetThreadPriority(thread_, THREAD_PRIORITY_ABOVE_NORMAL); |
| 202 | } else if (priority == PRIORITY_IDLE) { |
| 203 | ret = ::SetThreadPriority(thread_, THREAD_PRIORITY_IDLE); |
| 204 | } |
| 205 | if (!ret) { |
| 206 | return false; |
| 207 | } |
| 208 | } |
| 209 | priority_ = priority; |
| 210 | return true; |
| 211 | #else |
| 212 | // TODO: Implement for Linux/Mac if possible. |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 | [diff] [blame] | 213 | if (running()) return false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 214 | priority_ = priority; |
| 215 | return true; |
| 216 | #endif |
| 217 | } |
| 218 | |
| 219 | bool Thread::Start(Runnable* runnable) { |
| 220 | ASSERT(owned_); |
| 221 | if (!owned_) return false; |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 | [diff] [blame] | 222 | ASSERT(!running()); |
| 223 | if (running()) return false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 224 | |
| 225 | Restart(); // reset fStop_ if the thread is being restarted |
| 226 | |
| 227 | // Make sure that ThreadManager is created on the main thread before |
| 228 | // we start a new thread. |
| 229 | ThreadManager::Instance(); |
| 230 | |
| 231 | ThreadInit* init = new ThreadInit; |
| 232 | init->thread = this; |
| 233 | init->runnable = runnable; |
| 234 | #if defined(WEBRTC_WIN) |
| 235 | DWORD flags = 0; |
| 236 | if (priority_ != PRIORITY_NORMAL) { |
| 237 | flags = CREATE_SUSPENDED; |
| 238 | } |
| 239 | thread_ = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PreRun, init, flags, |
| 240 | &thread_id_); |
| 241 | if (thread_) { |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 | [diff] [blame] | 242 | running_.Set(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 243 | if (priority_ != PRIORITY_NORMAL) { |
| 244 | SetPriority(priority_); |
| 245 | ::ResumeThread(thread_); |
| 246 | } |
| 247 | } else { |
| 248 | return false; |
| 249 | } |
| 250 | #elif defined(WEBRTC_POSIX) |
| 251 | pthread_attr_t attr; |
| 252 | pthread_attr_init(&attr); |
| 253 | |
| 254 | // Thread priorities are not supported in NaCl. |
| 255 | #if !defined(__native_client__) |
| 256 | if (priority_ != PRIORITY_NORMAL) { |
| 257 | if (priority_ == PRIORITY_IDLE) { |
| 258 | // There is no POSIX-standard way to set a below-normal priority for an |
| 259 | // individual thread (only whole process), so let's not support it. |
| 260 | LOG(LS_WARNING) << "PRIORITY_IDLE not supported"; |
| 261 | } else { |
| 262 | // Set real-time round-robin policy. |
| 263 | if (pthread_attr_setschedpolicy(&attr, SCHED_RR) != 0) { |
| 264 | LOG(LS_ERROR) << "pthread_attr_setschedpolicy"; |
| 265 | } |
| 266 | struct sched_param param; |
| 267 | if (pthread_attr_getschedparam(&attr, ¶m) != 0) { |
| 268 | LOG(LS_ERROR) << "pthread_attr_getschedparam"; |
| 269 | } else { |
| 270 | // The numbers here are arbitrary. |
| 271 | if (priority_ == PRIORITY_HIGH) { |
| 272 | param.sched_priority = 6; // 6 = HIGH |
| 273 | } else { |
| 274 | ASSERT(priority_ == PRIORITY_ABOVE_NORMAL); |
| 275 | param.sched_priority = 4; // 4 = ABOVE_NORMAL |
| 276 | } |
| 277 | if (pthread_attr_setschedparam(&attr, ¶m) != 0) { |
| 278 | LOG(LS_ERROR) << "pthread_attr_setschedparam"; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | #endif // !defined(__native_client__) |
| 284 | |
| 285 | int error_code = pthread_create(&thread_, &attr, PreRun, init); |
| 286 | if (0 != error_code) { |
| 287 | LOG(LS_ERROR) << "Unable to create pthread, error " << error_code; |
| 288 | return false; |
| 289 | } |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 | [diff] [blame] | 290 | running_.Set(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 291 | #endif |
| 292 | return true; |
| 293 | } |
| 294 | |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 295 | bool Thread::WrapCurrent() { |
| 296 | return WrapCurrentWithThreadManager(ThreadManager::Instance(), true); |
| 297 | } |
| 298 | |
| 299 | void Thread::UnwrapCurrent() { |
| 300 | // Clears the platform-specific thread-specific storage. |
| 301 | ThreadManager::Instance()->SetCurrentThread(NULL); |
| 302 | #if defined(WEBRTC_WIN) |
| 303 | if (thread_ != NULL) { |
| 304 | if (!CloseHandle(thread_)) { |
| 305 | LOG_GLE(LS_ERROR) << "When unwrapping thread, failed to close handle."; |
| 306 | } |
| 307 | thread_ = NULL; |
| 308 | } |
| 309 | #endif |
| 310 | running_.Reset(); |
| 311 | } |
| 312 | |
| 313 | void Thread::SafeWrapCurrent() { |
| 314 | WrapCurrentWithThreadManager(ThreadManager::Instance(), false); |
| 315 | } |
| 316 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 317 | void Thread::Join() { |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 | [diff] [blame] | 318 | if (running()) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 319 | ASSERT(!IsCurrent()); |
jiayl@webrtc.org | 1fd362c | 2014-09-26 16:57:07 | [diff] [blame] | 320 | if (Current() && !Current()->blocking_calls_allowed_) { |
| 321 | LOG(LS_WARNING) << "Waiting for the thread to join, " |
| 322 | << "but blocking calls have been disallowed"; |
| 323 | } |
| 324 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 325 | #if defined(WEBRTC_WIN) |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 326 | ASSERT(thread_ != NULL); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 327 | WaitForSingleObject(thread_, INFINITE); |
| 328 | CloseHandle(thread_); |
| 329 | thread_ = NULL; |
| 330 | thread_id_ = 0; |
| 331 | #elif defined(WEBRTC_POSIX) |
| 332 | void *pv; |
| 333 | pthread_join(thread_, &pv); |
| 334 | #endif |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 | [diff] [blame] | 335 | running_.Reset(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 | [diff] [blame] | 339 | bool Thread::SetAllowBlockingCalls(bool allow) { |
| 340 | ASSERT(IsCurrent()); |
| 341 | bool previous = blocking_calls_allowed_; |
| 342 | blocking_calls_allowed_ = allow; |
| 343 | return previous; |
| 344 | } |
| 345 | |
| 346 | // static |
| 347 | void Thread::AssertBlockingIsAllowedOnCurrentThread() { |
| 348 | #ifdef _DEBUG |
| 349 | Thread* current = Thread::Current(); |
| 350 | ASSERT(!current || current->blocking_calls_allowed_); |
| 351 | #endif |
| 352 | } |
| 353 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 354 | void* Thread::PreRun(void* pv) { |
| 355 | ThreadInit* init = static_cast<ThreadInit*>(pv); |
| 356 | ThreadManager::Instance()->SetCurrentThread(init->thread); |
Tommi | ea14f0a | 2015-05-18 11:51:06 | [diff] [blame] | 357 | rtc::SetCurrentThreadName(init->thread->name_.c_str()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 358 | #if __has_feature(objc_arc) |
| 359 | @autoreleasepool |
| 360 | #elif defined(WEBRTC_MAC) |
| 361 | // Make sure the new thread has an autoreleasepool |
| 362 | ScopedAutoreleasePool pool; |
| 363 | #endif |
| 364 | { |
| 365 | if (init->runnable) { |
| 366 | init->runnable->Run(init->thread); |
| 367 | } else { |
| 368 | init->thread->Run(); |
| 369 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 370 | delete init; |
| 371 | return NULL; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | void Thread::Run() { |
| 376 | ProcessMessages(kForever); |
| 377 | } |
| 378 | |
| 379 | bool Thread::IsOwned() { |
| 380 | return owned_; |
| 381 | } |
| 382 | |
| 383 | void Thread::Stop() { |
| 384 | MessageQueue::Quit(); |
| 385 | Join(); |
| 386 | } |
| 387 | |
| 388 | void Thread::Send(MessageHandler *phandler, uint32 id, MessageData *pdata) { |
| 389 | if (fStop_) |
| 390 | return; |
| 391 | |
| 392 | // Sent messages are sent to the MessageHandler directly, in the context |
| 393 | // of "thread", like Win32 SendMessage. If in the right context, |
| 394 | // call the handler directly. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 395 | Message msg; |
| 396 | msg.phandler = phandler; |
| 397 | msg.message_id = id; |
| 398 | msg.pdata = pdata; |
| 399 | if (IsCurrent()) { |
| 400 | phandler->OnMessage(&msg); |
| 401 | return; |
| 402 | } |
| 403 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 | [diff] [blame] | 404 | AssertBlockingIsAllowedOnCurrentThread(); |
| 405 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 406 | AutoThread thread; |
| 407 | Thread *current_thread = Thread::Current(); |
| 408 | ASSERT(current_thread != NULL); // AutoThread ensures this |
| 409 | |
| 410 | bool ready = false; |
| 411 | { |
| 412 | CritScope cs(&crit_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 413 | _SendMessage smsg; |
| 414 | smsg.thread = current_thread; |
| 415 | smsg.msg = msg; |
| 416 | smsg.ready = &ready; |
| 417 | sendlist_.push_back(smsg); |
| 418 | } |
| 419 | |
| 420 | // Wait for a reply |
| 421 | |
| 422 | ss_->WakeUp(); |
| 423 | |
| 424 | bool waited = false; |
| 425 | crit_.Enter(); |
| 426 | while (!ready) { |
| 427 | crit_.Leave(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 | [diff] [blame] | 428 | // We need to limit "ReceiveSends" to |this| thread to avoid an arbitrary |
| 429 | // thread invoking calls on the current thread. |
| 430 | current_thread->ReceiveSendsFromThread(this); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 431 | current_thread->socketserver()->Wait(kForever, false); |
| 432 | waited = true; |
| 433 | crit_.Enter(); |
| 434 | } |
| 435 | crit_.Leave(); |
| 436 | |
| 437 | // Our Wait loop above may have consumed some WakeUp events for this |
| 438 | // MessageQueue, that weren't relevant to this Send. Losing these WakeUps can |
| 439 | // cause problems for some SocketServers. |
| 440 | // |
| 441 | // Concrete example: |
| 442 | // Win32SocketServer on thread A calls Send on thread B. While processing the |
| 443 | // message, thread B Posts a message to A. We consume the wakeup for that |
| 444 | // Post while waiting for the Send to complete, which means that when we exit |
| 445 | // this loop, we need to issue another WakeUp, or else the Posted message |
| 446 | // won't be processed in a timely manner. |
| 447 | |
| 448 | if (waited) { |
| 449 | current_thread->socketserver()->WakeUp(); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void Thread::ReceiveSends() { |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 | [diff] [blame] | 454 | ReceiveSendsFromThread(NULL); |
| 455 | } |
| 456 | |
| 457 | void Thread::ReceiveSendsFromThread(const Thread* source) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 458 | // Receive a sent message. Cleanup scenarios: |
| 459 | // - thread sending exits: We don't allow this, since thread can exit |
| 460 | // only via Join, so Send must complete. |
| 461 | // - thread receiving exits: Wakeup/set ready in Thread::Clear() |
| 462 | // - object target cleared: Wakeup/set ready in Thread::Clear() |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 | [diff] [blame] | 463 | _SendMessage smsg; |
| 464 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 465 | crit_.Enter(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 | [diff] [blame] | 466 | while (PopSendMessageFromThread(source, &smsg)) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 467 | crit_.Leave(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 | [diff] [blame] | 468 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 469 | smsg.msg.phandler->OnMessage(&smsg.msg); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 | [diff] [blame] | 470 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 471 | crit_.Enter(); |
| 472 | *smsg.ready = true; |
| 473 | smsg.thread->socketserver()->WakeUp(); |
| 474 | } |
| 475 | crit_.Leave(); |
| 476 | } |
| 477 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 | [diff] [blame] | 478 | bool Thread::PopSendMessageFromThread(const Thread* source, _SendMessage* msg) { |
| 479 | for (std::list<_SendMessage>::iterator it = sendlist_.begin(); |
| 480 | it != sendlist_.end(); ++it) { |
| 481 | if (it->thread == source || source == NULL) { |
| 482 | *msg = *it; |
| 483 | sendlist_.erase(it); |
| 484 | return true; |
| 485 | } |
| 486 | } |
| 487 | return false; |
| 488 | } |
| 489 | |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 | [diff] [blame] | 490 | void Thread::InvokeBegin() { |
| 491 | TRACE_EVENT_BEGIN0("webrtc", "Thread::Invoke"); |
| 492 | } |
| 493 | |
| 494 | void Thread::InvokeEnd() { |
| 495 | TRACE_EVENT_END0("webrtc", "Thread::Invoke"); |
| 496 | } |
| 497 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 498 | void Thread::Clear(MessageHandler *phandler, uint32 id, |
| 499 | MessageList* removed) { |
| 500 | CritScope cs(&crit_); |
| 501 | |
| 502 | // Remove messages on sendlist_ with phandler |
| 503 | // Object target cleared: remove from send list, wakeup/set ready |
| 504 | // if sender not NULL. |
| 505 | |
| 506 | std::list<_SendMessage>::iterator iter = sendlist_.begin(); |
| 507 | while (iter != sendlist_.end()) { |
| 508 | _SendMessage smsg = *iter; |
| 509 | if (smsg.msg.Match(phandler, id)) { |
| 510 | if (removed) { |
| 511 | removed->push_back(smsg.msg); |
| 512 | } else { |
| 513 | delete smsg.msg.pdata; |
| 514 | } |
| 515 | iter = sendlist_.erase(iter); |
| 516 | *smsg.ready = true; |
| 517 | smsg.thread->socketserver()->WakeUp(); |
| 518 | continue; |
| 519 | } |
| 520 | ++iter; |
| 521 | } |
| 522 | |
| 523 | MessageQueue::Clear(phandler, id, removed); |
| 524 | } |
| 525 | |
| 526 | bool Thread::ProcessMessages(int cmsLoop) { |
| 527 | uint32 msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop); |
| 528 | int cmsNext = cmsLoop; |
| 529 | |
| 530 | while (true) { |
| 531 | #if __has_feature(objc_arc) |
| 532 | @autoreleasepool |
| 533 | #elif defined(WEBRTC_MAC) |
| 534 | // see: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html |
| 535 | // Each thread is supposed to have an autorelease pool. Also for event loops |
| 536 | // like this, autorelease pool needs to be created and drained/released |
| 537 | // for each cycle. |
| 538 | ScopedAutoreleasePool pool; |
| 539 | #endif |
| 540 | { |
| 541 | Message msg; |
| 542 | if (!Get(&msg, cmsNext)) |
| 543 | return !IsQuitting(); |
| 544 | Dispatch(&msg); |
| 545 | |
| 546 | if (cmsLoop != kForever) { |
| 547 | cmsNext = TimeUntil(msEnd); |
| 548 | if (cmsNext < 0) |
| 549 | return true; |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 555 | bool Thread::WrapCurrentWithThreadManager(ThreadManager* thread_manager, |
| 556 | bool need_synchronize_access) { |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 | [diff] [blame] | 557 | if (running()) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 558 | return false; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 559 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 560 | #if defined(WEBRTC_WIN) |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 561 | if (need_synchronize_access) { |
| 562 | // We explicitly ask for no rights other than synchronization. |
| 563 | // This gives us the best chance of succeeding. |
| 564 | thread_ = OpenThread(SYNCHRONIZE, FALSE, GetCurrentThreadId()); |
| 565 | if (!thread_) { |
| 566 | LOG_GLE(LS_ERROR) << "Unable to get handle to thread."; |
| 567 | return false; |
| 568 | } |
| 569 | thread_id_ = GetCurrentThreadId(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 570 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 571 | #elif defined(WEBRTC_POSIX) |
| 572 | thread_ = pthread_self(); |
| 573 | #endif |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 574 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 575 | owned_ = false; |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 | [diff] [blame] | 576 | running_.Set(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 577 | thread_manager->SetCurrentThread(this); |
| 578 | return true; |
| 579 | } |
| 580 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 581 | AutoThread::AutoThread(SocketServer* ss) : Thread(ss) { |
| 582 | if (!ThreadManager::Instance()->CurrentThread()) { |
| 583 | ThreadManager::Instance()->SetCurrentThread(this); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | AutoThread::~AutoThread() { |
| 588 | Stop(); |
| 589 | if (ThreadManager::Instance()->CurrentThread() == this) { |
| 590 | ThreadManager::Instance()->SetCurrentThread(NULL); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | #if defined(WEBRTC_WIN) |
| 595 | void ComThread::Run() { |
| 596 | HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); |
| 597 | ASSERT(SUCCEEDED(hr)); |
| 598 | if (SUCCEEDED(hr)) { |
| 599 | Thread::Run(); |
| 600 | CoUninitialize(); |
| 601 | } else { |
| 602 | LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr; |
| 603 | } |
| 604 | } |
| 605 | #endif |
| 606 | |
| 607 | } // namespace rtc |