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