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 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame^] | 23 | #include "webrtc/base/checks.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | #include "webrtc/base/common.h" |
| 25 | #include "webrtc/base/logging.h" |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 26 | #include "webrtc/base/nullsocketserver.h" |
Tommi | ea14f0a | 2015-05-18 13:51:06 +0200 | [diff] [blame] | 27 | #include "webrtc/base/platform_thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 28 | #include "webrtc/base/stringutils.h" |
| 29 | #include "webrtc/base/timeutils.h" |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 30 | #include "webrtc/base/trace_event.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 31 | |
| 32 | #if !__has_feature(objc_arc) && (defined(WEBRTC_MAC)) |
| 33 | #include "webrtc/base/maccocoathreadhelper.h" |
| 34 | #include "webrtc/base/scoped_autorelease_pool.h" |
| 35 | #endif |
| 36 | |
| 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() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame^] | 138 | RTC_DCHECK(thread_->IsCurrent()); |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 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) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame^] | 217 | RTC_DCHECK(owned_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 218 | if (!owned_) return false; |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame^] | 219 | RTC_DCHECK(!running()); |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 220 | if (running()) return false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 221 | |
André Susano Pinto | 02a5797 | 2016-07-22 13:30:05 +0200 | [diff] [blame] | 222 | Restart(); // reset IsQuitting() if the thread is being restarted |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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()) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame^] | 277 | RTC_DCHECK(!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) |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame^] | 284 | RTC_DCHECK(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) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame^] | 298 | RTC_DCHECK(IsCurrent()); |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 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(); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame^] | 308 | RTC_DCHECK(!current || current->blocking_calls_allowed_); |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 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 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 346 | void Thread::Send(const Location& posted_from, |
| 347 | MessageHandler* phandler, |
| 348 | uint32_t id, |
| 349 | MessageData* pdata) { |
André Susano Pinto | 02a5797 | 2016-07-22 13:30:05 +0200 | [diff] [blame] | 350 | if (IsQuitting()) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 351 | return; |
| 352 | |
| 353 | // Sent messages are sent to the MessageHandler directly, in the context |
| 354 | // of "thread", like Win32 SendMessage. If in the right context, |
| 355 | // call the handler directly. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 356 | Message msg; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 357 | msg.posted_from = posted_from; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 358 | msg.phandler = phandler; |
| 359 | msg.message_id = id; |
| 360 | msg.pdata = pdata; |
| 361 | if (IsCurrent()) { |
| 362 | phandler->OnMessage(&msg); |
| 363 | return; |
| 364 | } |
| 365 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 366 | AssertBlockingIsAllowedOnCurrentThread(); |
| 367 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 368 | AutoThread thread; |
| 369 | Thread *current_thread = Thread::Current(); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame^] | 370 | RTC_DCHECK(current_thread != NULL); // AutoThread ensures this |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 371 | |
| 372 | bool ready = false; |
| 373 | { |
| 374 | CritScope cs(&crit_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 375 | _SendMessage smsg; |
| 376 | smsg.thread = current_thread; |
| 377 | smsg.msg = msg; |
| 378 | smsg.ready = &ready; |
| 379 | sendlist_.push_back(smsg); |
| 380 | } |
| 381 | |
| 382 | // Wait for a reply |
jbauch | 9ccedc3 | 2016-02-25 01:14:56 -0800 | [diff] [blame] | 383 | WakeUpSocketServer(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 384 | |
| 385 | bool waited = false; |
| 386 | crit_.Enter(); |
| 387 | while (!ready) { |
| 388 | crit_.Leave(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 389 | // We need to limit "ReceiveSends" to |this| thread to avoid an arbitrary |
| 390 | // thread invoking calls on the current thread. |
| 391 | current_thread->ReceiveSendsFromThread(this); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 392 | current_thread->socketserver()->Wait(kForever, false); |
| 393 | waited = true; |
| 394 | crit_.Enter(); |
| 395 | } |
| 396 | crit_.Leave(); |
| 397 | |
| 398 | // Our Wait loop above may have consumed some WakeUp events for this |
| 399 | // MessageQueue, that weren't relevant to this Send. Losing these WakeUps can |
| 400 | // cause problems for some SocketServers. |
| 401 | // |
| 402 | // Concrete example: |
| 403 | // Win32SocketServer on thread A calls Send on thread B. While processing the |
| 404 | // message, thread B Posts a message to A. We consume the wakeup for that |
| 405 | // Post while waiting for the Send to complete, which means that when we exit |
| 406 | // this loop, we need to issue another WakeUp, or else the Posted message |
| 407 | // won't be processed in a timely manner. |
| 408 | |
| 409 | if (waited) { |
| 410 | current_thread->socketserver()->WakeUp(); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | void Thread::ReceiveSends() { |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 415 | ReceiveSendsFromThread(NULL); |
| 416 | } |
| 417 | |
| 418 | void Thread::ReceiveSendsFromThread(const Thread* source) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 419 | // Receive a sent message. Cleanup scenarios: |
| 420 | // - thread sending exits: We don't allow this, since thread can exit |
| 421 | // only via Join, so Send must complete. |
| 422 | // - thread receiving exits: Wakeup/set ready in Thread::Clear() |
| 423 | // - object target cleared: Wakeup/set ready in Thread::Clear() |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 424 | _SendMessage smsg; |
| 425 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 426 | crit_.Enter(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 427 | while (PopSendMessageFromThread(source, &smsg)) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 428 | crit_.Leave(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 429 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 430 | smsg.msg.phandler->OnMessage(&smsg.msg); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 431 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 432 | crit_.Enter(); |
| 433 | *smsg.ready = true; |
| 434 | smsg.thread->socketserver()->WakeUp(); |
| 435 | } |
| 436 | crit_.Leave(); |
| 437 | } |
| 438 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 439 | bool Thread::PopSendMessageFromThread(const Thread* source, _SendMessage* msg) { |
| 440 | for (std::list<_SendMessage>::iterator it = sendlist_.begin(); |
| 441 | it != sendlist_.end(); ++it) { |
| 442 | if (it->thread == source || source == NULL) { |
| 443 | *msg = *it; |
| 444 | sendlist_.erase(it); |
| 445 | return true; |
| 446 | } |
| 447 | } |
| 448 | return false; |
| 449 | } |
| 450 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 451 | void Thread::InvokeInternal(const Location& posted_from, |
| 452 | MessageHandler* handler) { |
| 453 | TRACE_EVENT2("webrtc", "Thread::Invoke", "src_file_and_line", |
| 454 | posted_from.file_and_line(), "src_func", |
| 455 | posted_from.function_name()); |
| 456 | Send(posted_from, handler); |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 459 | void Thread::Clear(MessageHandler* phandler, |
| 460 | uint32_t id, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 461 | MessageList* removed) { |
| 462 | CritScope cs(&crit_); |
| 463 | |
| 464 | // Remove messages on sendlist_ with phandler |
| 465 | // Object target cleared: remove from send list, wakeup/set ready |
| 466 | // if sender not NULL. |
| 467 | |
| 468 | std::list<_SendMessage>::iterator iter = sendlist_.begin(); |
| 469 | while (iter != sendlist_.end()) { |
| 470 | _SendMessage smsg = *iter; |
| 471 | if (smsg.msg.Match(phandler, id)) { |
| 472 | if (removed) { |
| 473 | removed->push_back(smsg.msg); |
| 474 | } else { |
| 475 | delete smsg.msg.pdata; |
| 476 | } |
| 477 | iter = sendlist_.erase(iter); |
| 478 | *smsg.ready = true; |
| 479 | smsg.thread->socketserver()->WakeUp(); |
| 480 | continue; |
| 481 | } |
| 482 | ++iter; |
| 483 | } |
| 484 | |
| 485 | MessageQueue::Clear(phandler, id, removed); |
| 486 | } |
| 487 | |
| 488 | bool Thread::ProcessMessages(int cmsLoop) { |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 489 | int64_t msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 490 | int cmsNext = cmsLoop; |
| 491 | |
| 492 | while (true) { |
| 493 | #if __has_feature(objc_arc) |
| 494 | @autoreleasepool |
| 495 | #elif defined(WEBRTC_MAC) |
| 496 | // see: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html |
| 497 | // Each thread is supposed to have an autorelease pool. Also for event loops |
| 498 | // like this, autorelease pool needs to be created and drained/released |
| 499 | // for each cycle. |
| 500 | ScopedAutoreleasePool pool; |
| 501 | #endif |
| 502 | { |
| 503 | Message msg; |
| 504 | if (!Get(&msg, cmsNext)) |
| 505 | return !IsQuitting(); |
| 506 | Dispatch(&msg); |
| 507 | |
| 508 | if (cmsLoop != kForever) { |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 509 | cmsNext = static_cast<int>(TimeUntil(msEnd)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 510 | if (cmsNext < 0) |
| 511 | return true; |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 517 | bool Thread::WrapCurrentWithThreadManager(ThreadManager* thread_manager, |
| 518 | bool need_synchronize_access) { |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 519 | if (running()) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 520 | return false; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 521 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 522 | #if defined(WEBRTC_WIN) |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 523 | if (need_synchronize_access) { |
| 524 | // We explicitly ask for no rights other than synchronization. |
| 525 | // This gives us the best chance of succeeding. |
| 526 | thread_ = OpenThread(SYNCHRONIZE, FALSE, GetCurrentThreadId()); |
| 527 | if (!thread_) { |
| 528 | LOG_GLE(LS_ERROR) << "Unable to get handle to thread."; |
| 529 | return false; |
| 530 | } |
| 531 | thread_id_ = GetCurrentThreadId(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 532 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 533 | #elif defined(WEBRTC_POSIX) |
| 534 | thread_ = pthread_self(); |
| 535 | #endif |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 536 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 537 | owned_ = false; |
fischman@webrtc.org | e5063b1 | 2014-05-23 17:28:50 +0000 | [diff] [blame] | 538 | running_.Set(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 539 | thread_manager->SetCurrentThread(this); |
| 540 | return true; |
| 541 | } |
| 542 | |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 543 | AutoThread::AutoThread() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 544 | if (!ThreadManager::Instance()->CurrentThread()) { |
| 545 | ThreadManager::Instance()->SetCurrentThread(this); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | AutoThread::~AutoThread() { |
| 550 | Stop(); |
| 551 | if (ThreadManager::Instance()->CurrentThread() == this) { |
| 552 | ThreadManager::Instance()->SetCurrentThread(NULL); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | #if defined(WEBRTC_WIN) |
| 557 | void ComThread::Run() { |
| 558 | HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame^] | 559 | RTC_DCHECK(SUCCEEDED(hr)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 560 | if (SUCCEEDED(hr)) { |
| 561 | Thread::Run(); |
| 562 | CoUninitialize(); |
| 563 | } else { |
| 564 | LOG(LS_ERROR) << "CoInitialize failed, hr=" << hr; |
| 565 | } |
| 566 | } |
| 567 | #endif |
| 568 | |
| 569 | } // namespace rtc |