henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [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" |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 25 | #include "webrtc/base/nullsocketserver.h" |
Tommi | ea14f0a | 2015-05-18 13:51:06 +0200 | [diff] [blame] | 26 | #include "webrtc/base/platform_thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 27 | #include "webrtc/base/stringutils.h" |
| 28 | #include "webrtc/base/timeutils.h" |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame^] | 29 | #include "webrtc/base/trace_event.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | |
| 31 | #if !__has_feature(objc_arc) && (defined(WEBRTC_MAC)) |
| 32 | #include "webrtc/base/maccocoathreadhelper.h" |
| 33 | #include "webrtc/base/scoped_autorelease_pool.h" |
| 34 | #endif |
| 35 | |
| 36 | namespace rtc { |
| 37 | |
| 38 | ThreadManager* ThreadManager::Instance() { |
Andrew MacDonald | 469c2c0 | 2015-05-22 17:50:26 -0700 | [diff] [blame] | 39 | RTC_DEFINE_STATIC_LOCAL(ThreadManager, thread_manager, ()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [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 +0000 | [diff] [blame] | 113 | result->WrapCurrentWithThreadManager(this, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [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 +0000 | [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 | |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 141 | Thread::Thread() : Thread(SocketServer::CreateDefault()) {} |
| 142 | |
| 143 | Thread::Thread(SocketServer* ss) |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 144 | : MessageQueue(ss, false), |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 145 | running_(true, false), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 146 | #if defined(WEBRTC_WIN) |
| 147 | thread_(NULL), |
| 148 | thread_id_(0), |
| 149 | #endif |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 150 | owned_(true), |
| 151 | blocking_calls_allowed_(true) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 152 | SetName("Thread", this); // default name |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 153 | DoInit(); |
| 154 | } |
| 155 | |
| 156 | Thread::Thread(std::unique_ptr<SocketServer> ss) |
| 157 | : MessageQueue(std::move(ss), false), |
| 158 | running_(true, false), |
| 159 | #if defined(WEBRTC_WIN) |
| 160 | thread_(NULL), |
| 161 | thread_id_(0), |
| 162 | #endif |
| 163 | owned_(true), |
| 164 | blocking_calls_allowed_(true) { |
| 165 | SetName("Thread", this); // default name |
| 166 | DoInit(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | Thread::~Thread() { |
| 170 | Stop(); |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 171 | DoDestroy(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 172 | } |
| 173 | |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 174 | std::unique_ptr<Thread> Thread::CreateWithSocketServer() { |
| 175 | return std::unique_ptr<Thread>(new Thread(SocketServer::CreateDefault())); |
| 176 | } |
| 177 | |
| 178 | std::unique_ptr<Thread> Thread::Create() { |
| 179 | return std::unique_ptr<Thread>( |
| 180 | new Thread(std::unique_ptr<SocketServer>(new NullSocketServer()))); |
| 181 | } |
| 182 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 183 | bool Thread::SleepMs(int milliseconds) { |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 184 | AssertBlockingIsAllowedOnCurrentThread(); |
| 185 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 186 | #if defined(WEBRTC_WIN) |
| 187 | ::Sleep(milliseconds); |
| 188 | return true; |
| 189 | #else |
| 190 | // POSIX has both a usleep() and a nanosleep(), but the former is deprecated, |
| 191 | // so we use nanosleep() even though it has greater precision than necessary. |
| 192 | struct timespec ts; |
| 193 | ts.tv_sec = milliseconds / 1000; |
| 194 | ts.tv_nsec = (milliseconds % 1000) * 1000000; |
| 195 | int ret = nanosleep(&ts, NULL); |
| 196 | if (ret != 0) { |
| 197 | LOG_ERR(LS_WARNING) << "nanosleep() returning early"; |
| 198 | return false; |
| 199 | } |
| 200 | return true; |
| 201 | #endif |
| 202 | } |
| 203 | |
| 204 | bool Thread::SetName(const std::string& name, const void* obj) { |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 205 | if (running()) return false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 206 | name_ = name; |
| 207 | if (obj) { |
| 208 | char buf[16]; |
| 209 | sprintfn(buf, sizeof(buf), " 0x%p", obj); |
| 210 | name_ += buf; |
| 211 | } |
| 212 | return true; |
| 213 | } |
| 214 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 215 | bool Thread::Start(Runnable* runnable) { |
| 216 | ASSERT(owned_); |
| 217 | if (!owned_) return false; |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 218 | ASSERT(!running()); |
| 219 | if (running()) return false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 220 | |
| 221 | Restart(); // reset fStop_ if the thread is being restarted |
| 222 | |
| 223 | // Make sure that ThreadManager is created on the main thread before |
| 224 | // we start a new thread. |
| 225 | ThreadManager::Instance(); |
| 226 | |
| 227 | ThreadInit* init = new ThreadInit; |
| 228 | init->thread = this; |
| 229 | init->runnable = runnable; |
| 230 | #if defined(WEBRTC_WIN) |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 231 | thread_ = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)PreRun, init, 0, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 232 | &thread_id_); |
| 233 | if (thread_) { |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 234 | running_.Set(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 235 | } else { |
| 236 | return false; |
| 237 | } |
| 238 | #elif defined(WEBRTC_POSIX) |
| 239 | pthread_attr_t attr; |
| 240 | pthread_attr_init(&attr); |
| 241 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 242 | int error_code = pthread_create(&thread_, &attr, PreRun, init); |
| 243 | if (0 != error_code) { |
| 244 | LOG(LS_ERROR) << "Unable to create pthread, error " << error_code; |
| 245 | return false; |
| 246 | } |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 247 | running_.Set(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 248 | #endif |
| 249 | return true; |
| 250 | } |
| 251 | |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 252 | bool Thread::WrapCurrent() { |
| 253 | return WrapCurrentWithThreadManager(ThreadManager::Instance(), true); |
| 254 | } |
| 255 | |
| 256 | void Thread::UnwrapCurrent() { |
| 257 | // Clears the platform-specific thread-specific storage. |
| 258 | ThreadManager::Instance()->SetCurrentThread(NULL); |
| 259 | #if defined(WEBRTC_WIN) |
| 260 | if (thread_ != NULL) { |
| 261 | if (!CloseHandle(thread_)) { |
| 262 | LOG_GLE(LS_ERROR) << "When unwrapping thread, failed to close handle."; |
| 263 | } |
| 264 | thread_ = NULL; |
| 265 | } |
| 266 | #endif |
| 267 | running_.Reset(); |
| 268 | } |
| 269 | |
| 270 | void Thread::SafeWrapCurrent() { |
| 271 | WrapCurrentWithThreadManager(ThreadManager::Instance(), false); |
| 272 | } |
| 273 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 274 | void Thread::Join() { |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 275 | if (running()) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 276 | ASSERT(!IsCurrent()); |
jiayl@webrtc.org | 1fd362c | 2014-09-26 16:57:07 +0000 | [diff] [blame] | 277 | if (Current() && !Current()->blocking_calls_allowed_) { |
| 278 | LOG(LS_WARNING) << "Waiting for the thread to join, " |
| 279 | << "but blocking calls have been disallowed"; |
| 280 | } |
| 281 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 282 | #if defined(WEBRTC_WIN) |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 283 | ASSERT(thread_ != NULL); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 284 | WaitForSingleObject(thread_, INFINITE); |
| 285 | CloseHandle(thread_); |
| 286 | thread_ = NULL; |
| 287 | thread_id_ = 0; |
| 288 | #elif defined(WEBRTC_POSIX) |
| 289 | void *pv; |
| 290 | pthread_join(thread_, &pv); |
| 291 | #endif |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 292 | running_.Reset(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 296 | bool Thread::SetAllowBlockingCalls(bool allow) { |
| 297 | ASSERT(IsCurrent()); |
| 298 | bool previous = blocking_calls_allowed_; |
| 299 | blocking_calls_allowed_ = allow; |
| 300 | return previous; |
| 301 | } |
| 302 | |
| 303 | // static |
| 304 | void Thread::AssertBlockingIsAllowedOnCurrentThread() { |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 305 | #if !defined(NDEBUG) |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 306 | Thread* current = Thread::Current(); |
| 307 | ASSERT(!current || current->blocking_calls_allowed_); |
| 308 | #endif |
| 309 | } |
| 310 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 311 | void* Thread::PreRun(void* pv) { |
| 312 | ThreadInit* init = static_cast<ThreadInit*>(pv); |
| 313 | ThreadManager::Instance()->SetCurrentThread(init->thread); |
Tommi | ea14f0a | 2015-05-18 13:51:06 +0200 | [diff] [blame] | 314 | rtc::SetCurrentThreadName(init->thread->name_.c_str()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 315 | #if __has_feature(objc_arc) |
| 316 | @autoreleasepool |
| 317 | #elif defined(WEBRTC_MAC) |
| 318 | // Make sure the new thread has an autoreleasepool |
| 319 | ScopedAutoreleasePool pool; |
| 320 | #endif |
| 321 | { |
| 322 | if (init->runnable) { |
| 323 | init->runnable->Run(init->thread); |
| 324 | } else { |
| 325 | init->thread->Run(); |
| 326 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 327 | delete init; |
| 328 | return NULL; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | void Thread::Run() { |
| 333 | ProcessMessages(kForever); |
| 334 | } |
| 335 | |
| 336 | bool Thread::IsOwned() { |
| 337 | return owned_; |
| 338 | } |
| 339 | |
| 340 | void Thread::Stop() { |
| 341 | MessageQueue::Quit(); |
| 342 | Join(); |
| 343 | } |
| 344 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame^] | 345 | void Thread::Send(const Location& posted_from, |
| 346 | MessageHandler* phandler, |
| 347 | uint32_t id, |
| 348 | MessageData* pdata) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 349 | if (fStop_) |
| 350 | return; |
| 351 | |
| 352 | // Sent messages are sent to the MessageHandler directly, in the context |
| 353 | // of "thread", like Win32 SendMessage. If in the right context, |
| 354 | // call the handler directly. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 355 | Message msg; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame^] | 356 | msg.posted_from = posted_from; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 357 | msg.phandler = phandler; |
| 358 | msg.message_id = id; |
| 359 | msg.pdata = pdata; |
| 360 | if (IsCurrent()) { |
| 361 | phandler->OnMessage(&msg); |
| 362 | return; |
| 363 | } |
| 364 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 365 | AssertBlockingIsAllowedOnCurrentThread(); |
| 366 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 367 | AutoThread thread; |
| 368 | Thread *current_thread = Thread::Current(); |
| 369 | ASSERT(current_thread != NULL); // AutoThread ensures this |
| 370 | |
| 371 | bool ready = false; |
| 372 | { |
| 373 | CritScope cs(&crit_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 374 | _SendMessage smsg; |
| 375 | smsg.thread = current_thread; |
| 376 | smsg.msg = msg; |
| 377 | smsg.ready = &ready; |
| 378 | sendlist_.push_back(smsg); |
| 379 | } |
| 380 | |
| 381 | // Wait for a reply |
jbauch | 9ccedc3 | 2016-02-25 01:14:56 -0800 | [diff] [blame] | 382 | WakeUpSocketServer(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 383 | |
| 384 | bool waited = false; |
| 385 | crit_.Enter(); |
| 386 | while (!ready) { |
| 387 | crit_.Leave(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 388 | // We need to limit "ReceiveSends" to |this| thread to avoid an arbitrary |
| 389 | // thread invoking calls on the current thread. |
| 390 | current_thread->ReceiveSendsFromThread(this); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 391 | current_thread->socketserver()->Wait(kForever, false); |
| 392 | waited = true; |
| 393 | crit_.Enter(); |
| 394 | } |
| 395 | crit_.Leave(); |
| 396 | |
| 397 | // Our Wait loop above may have consumed some WakeUp events for this |
| 398 | // MessageQueue, that weren't relevant to this Send. Losing these WakeUps can |
| 399 | // cause problems for some SocketServers. |
| 400 | // |
| 401 | // Concrete example: |
| 402 | // Win32SocketServer on thread A calls Send on thread B. While processing the |
| 403 | // message, thread B Posts a message to A. We consume the wakeup for that |
| 404 | // Post while waiting for the Send to complete, which means that when we exit |
| 405 | // this loop, we need to issue another WakeUp, or else the Posted message |
| 406 | // won't be processed in a timely manner. |
| 407 | |
| 408 | if (waited) { |
| 409 | current_thread->socketserver()->WakeUp(); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | void Thread::ReceiveSends() { |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 414 | ReceiveSendsFromThread(NULL); |
| 415 | } |
| 416 | |
| 417 | void Thread::ReceiveSendsFromThread(const Thread* source) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 418 | // Receive a sent message. Cleanup scenarios: |
| 419 | // - thread sending exits: We don't allow this, since thread can exit |
| 420 | // only via Join, so Send must complete. |
| 421 | // - thread receiving exits: Wakeup/set ready in Thread::Clear() |
| 422 | // - object target cleared: Wakeup/set ready in Thread::Clear() |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 423 | _SendMessage smsg; |
| 424 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 425 | crit_.Enter(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 426 | while (PopSendMessageFromThread(source, &smsg)) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 427 | crit_.Leave(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 428 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 429 | smsg.msg.phandler->OnMessage(&smsg.msg); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 430 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 431 | crit_.Enter(); |
| 432 | *smsg.ready = true; |
| 433 | smsg.thread->socketserver()->WakeUp(); |
| 434 | } |
| 435 | crit_.Leave(); |
| 436 | } |
| 437 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 438 | bool Thread::PopSendMessageFromThread(const Thread* source, _SendMessage* msg) { |
| 439 | for (std::list<_SendMessage>::iterator it = sendlist_.begin(); |
| 440 | it != sendlist_.end(); ++it) { |
| 441 | if (it->thread == source || source == NULL) { |
| 442 | *msg = *it; |
| 443 | sendlist_.erase(it); |
| 444 | return true; |
| 445 | } |
| 446 | } |
| 447 | return false; |
| 448 | } |
| 449 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame^] | 450 | void Thread::InvokeInternal(const Location& posted_from, |
| 451 | MessageHandler* handler) { |
| 452 | TRACE_EVENT2("webrtc", "Thread::Invoke", "src_file_and_line", |
| 453 | posted_from.file_and_line(), "src_func", |
| 454 | posted_from.function_name()); |
| 455 | Send(posted_from, handler); |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 458 | void Thread::Clear(MessageHandler* phandler, |
| 459 | uint32_t id, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 460 | MessageList* removed) { |
| 461 | CritScope cs(&crit_); |
| 462 | |
| 463 | // Remove messages on sendlist_ with phandler |
| 464 | // Object target cleared: remove from send list, wakeup/set ready |
| 465 | // if sender not NULL. |
| 466 | |
| 467 | std::list<_SendMessage>::iterator iter = sendlist_.begin(); |
| 468 | while (iter != sendlist_.end()) { |
| 469 | _SendMessage smsg = *iter; |
| 470 | if (smsg.msg.Match(phandler, id)) { |
| 471 | if (removed) { |
| 472 | removed->push_back(smsg.msg); |
| 473 | } else { |
| 474 | delete smsg.msg.pdata; |
| 475 | } |
| 476 | iter = sendlist_.erase(iter); |
| 477 | *smsg.ready = true; |
| 478 | smsg.thread->socketserver()->WakeUp(); |
| 479 | continue; |
| 480 | } |
| 481 | ++iter; |
| 482 | } |
| 483 | |
| 484 | MessageQueue::Clear(phandler, id, removed); |
| 485 | } |
| 486 | |
| 487 | bool Thread::ProcessMessages(int cmsLoop) { |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 488 | int64_t msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 489 | int cmsNext = cmsLoop; |
| 490 | |
| 491 | while (true) { |
| 492 | #if __has_feature(objc_arc) |
| 493 | @autoreleasepool |
| 494 | #elif defined(WEBRTC_MAC) |
| 495 | // see: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html |
| 496 | // Each thread is supposed to have an autorelease pool. Also for event loops |
| 497 | // like this, autorelease pool needs to be created and drained/released |
| 498 | // for each cycle. |
| 499 | ScopedAutoreleasePool pool; |
| 500 | #endif |
| 501 | { |
| 502 | Message msg; |
| 503 | if (!Get(&msg, cmsNext)) |
| 504 | return !IsQuitting(); |
| 505 | Dispatch(&msg); |
| 506 | |
| 507 | if (cmsLoop != kForever) { |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 508 | cmsNext = static_cast<int>(TimeUntil(msEnd)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 509 | if (cmsNext < 0) |
| 510 | return true; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 516 | bool Thread::WrapCurrentWithThreadManager(ThreadManager* thread_manager, |
| 517 | bool need_synchronize_access) { |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 518 | if (running()) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 519 | return false; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 520 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 521 | #if defined(WEBRTC_WIN) |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 522 | if (need_synchronize_access) { |
| 523 | // We explicitly ask for no rights other than synchronization. |
| 524 | // This gives us the best chance of succeeding. |
| 525 | thread_ = OpenThread(SYNCHRONIZE, FALSE, GetCurrentThreadId()); |
| 526 | if (!thread_) { |
| 527 | LOG_GLE(LS_ERROR) << "Unable to get handle to thread."; |
| 528 | return false; |
| 529 | } |
| 530 | thread_id_ = GetCurrentThreadId(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 531 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 532 | #elif defined(WEBRTC_POSIX) |
| 533 | thread_ = pthread_self(); |
| 534 | #endif |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 535 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 536 | owned_ = false; |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 537 | running_.Set(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 538 | thread_manager->SetCurrentThread(this); |
| 539 | return true; |
| 540 | } |
| 541 | |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 542 | AutoThread::AutoThread() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 543 | if (!ThreadManager::Instance()->CurrentThread()) { |
| 544 | ThreadManager::Instance()->SetCurrentThread(this); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | AutoThread::~AutoThread() { |
| 549 | Stop(); |
| 550 | if (ThreadManager::Instance()->CurrentThread() == this) { |
| 551 | ThreadManager::Instance()->SetCurrentThread(NULL); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | #if defined(WEBRTC_WIN) |
| 556 | void ComThread::Run() { |
| 557 | HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); |
| 558 | ASSERT(SUCCEEDED(hr)); |
| 559 | if (SUCCEEDED(hr)) { |
| 560 | Thread::Run(); |
| 561 | CoUninitialize(); |
| 562 | } else { |
| 563 | LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr; |
| 564 | } |
| 565 | } |
| 566 | #endif |
| 567 | |
| 568 | } // namespace rtc |