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