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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | #if defined(WEBRTC_WIN) |
| 14 | #include <comdef.h> |
| 15 | #elif defined(WEBRTC_POSIX) |
| 16 | #include <time.h> |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 17 | #else |
| 18 | #error "Either WEBRTC_WIN or WEBRTC_POSIX needs to be defined." |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | #endif |
| 20 | |
Artem Titov | 80d02ad | 2018-05-21 12:20:39 +0200 | [diff] [blame] | 21 | #if defined(WEBRTC_WIN) |
| 22 | // Disable warning that we don't care about: |
| 23 | // warning C4722: destructor never returns, potential memory leak |
| 24 | #pragma warning(disable : 4722) |
| 25 | #endif |
| 26 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 27 | #include <stdio.h> |
| 28 | #include <utility> |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 29 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "rtc_base/checks.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 31 | #include "rtc_base/criticalsection.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 32 | #include "rtc_base/logging.h" |
| 33 | #include "rtc_base/nullsocketserver.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 34 | #include "rtc_base/timeutils.h" |
| 35 | #include "rtc_base/trace_event.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | |
Kári Tristan Helgason | 62b1345 | 2018-10-12 12:57:49 +0200 | [diff] [blame] | 37 | #if defined(WEBRTC_MAC) |
| 38 | #include "rtc_base/system/cocoa_threading.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 39 | |
Kári Tristan Helgason | 62b1345 | 2018-10-12 12:57:49 +0200 | [diff] [blame] | 40 | /* |
| 41 | * These are forward-declarations for methods that are part of the |
| 42 | * ObjC runtime. They are declared in the private header objc-internal.h. |
| 43 | * These calls are what clang inserts when using @autoreleasepool in ObjC, |
| 44 | * but here they are used directly in order to keep this file C++. |
| 45 | * https://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime-support |
| 46 | */ |
| 47 | extern "C" { |
| 48 | void* objc_autoreleasePoolPush(void); |
| 49 | void objc_autoreleasePoolPop(void* pool); |
| 50 | } |
| 51 | |
| 52 | namespace { |
| 53 | class ScopedAutoReleasePool { |
| 54 | public: |
| 55 | ScopedAutoReleasePool() : pool_(objc_autoreleasePoolPush()) {} |
| 56 | ~ScopedAutoReleasePool() { objc_autoreleasePoolPop(pool_); } |
| 57 | |
| 58 | private: |
| 59 | void* const pool_; |
| 60 | }; |
| 61 | } // namespace |
| 62 | #endif |
| 63 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 64 | namespace rtc { |
| 65 | |
| 66 | ThreadManager* ThreadManager::Instance() { |
Niels Möller | 14682a3 | 2018-05-24 08:54:25 +0200 | [diff] [blame] | 67 | static ThreadManager* const thread_manager = new ThreadManager(); |
| 68 | return thread_manager; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | } |
| 70 | |
nisse | 7866cfe | 2017-04-26 01:45:31 -0700 | [diff] [blame] | 71 | ThreadManager::~ThreadManager() { |
| 72 | // By above RTC_DEFINE_STATIC_LOCAL. |
| 73 | RTC_NOTREACHED() << "ThreadManager should never be destructed."; |
| 74 | } |
| 75 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 76 | // static |
| 77 | Thread* Thread::Current() { |
nisse | 7866cfe | 2017-04-26 01:45:31 -0700 | [diff] [blame] | 78 | ThreadManager* manager = ThreadManager::Instance(); |
| 79 | Thread* thread = manager->CurrentThread(); |
| 80 | |
| 81 | #ifndef NO_MAIN_THREAD_WRAPPING |
| 82 | // Only autowrap the thread which instantiated the ThreadManager. |
| 83 | if (!thread && manager->IsMainThread()) { |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 84 | thread = new Thread(SocketServer::CreateDefault()); |
nisse | 7866cfe | 2017-04-26 01:45:31 -0700 | [diff] [blame] | 85 | thread->WrapCurrentWithThreadManager(manager, true); |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | return thread; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | #if defined(WEBRTC_POSIX) |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 93 | ThreadManager::ThreadManager() : main_thread_ref_(CurrentThreadRef()) { |
Kári Tristan Helgason | 62b1345 | 2018-10-12 12:57:49 +0200 | [diff] [blame] | 94 | #if defined(WEBRTC_MAC) |
| 95 | InitCocoaMultiThreading(); |
| 96 | #endif |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 97 | pthread_key_create(&key_, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 100 | Thread* ThreadManager::CurrentThread() { |
| 101 | return static_cast<Thread*>(pthread_getspecific(key_)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Tommi | 6f314bb | 2017-12-04 20:38:20 +0100 | [diff] [blame] | 104 | void ThreadManager::SetCurrentThread(Thread* thread) { |
| 105 | #if RTC_DLOG_IS_ON |
| 106 | if (CurrentThread() && thread) { |
| 107 | RTC_DLOG(LS_ERROR) << "SetCurrentThread: Overwriting an existing value?"; |
| 108 | } |
| 109 | #endif // RTC_DLOG_IS_ON |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 110 | pthread_setspecific(key_, thread); |
| 111 | } |
| 112 | #endif |
| 113 | |
| 114 | #if defined(WEBRTC_WIN) |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 115 | ThreadManager::ThreadManager() |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 116 | : key_(TlsAlloc()), main_thread_ref_(CurrentThreadRef()) {} |
| 117 | |
| 118 | Thread* ThreadManager::CurrentThread() { |
| 119 | return static_cast<Thread*>(TlsGetValue(key_)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 122 | void ThreadManager::SetCurrentThread(Thread* thread) { |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 123 | RTC_DCHECK(!CurrentThread() || !thread); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 124 | TlsSetValue(key_, thread); |
| 125 | } |
| 126 | #endif |
| 127 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 128 | Thread* ThreadManager::WrapCurrentThread() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 129 | Thread* result = CurrentThread(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 130 | if (nullptr == result) { |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 131 | result = new Thread(SocketServer::CreateDefault()); |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 132 | result->WrapCurrentWithThreadManager(this, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 133 | } |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | void ThreadManager::UnwrapCurrentThread() { |
| 138 | Thread* t = CurrentThread(); |
| 139 | if (t && !(t->IsOwned())) { |
| 140 | t->UnwrapCurrent(); |
| 141 | delete t; |
| 142 | } |
| 143 | } |
| 144 | |
nisse | 7866cfe | 2017-04-26 01:45:31 -0700 | [diff] [blame] | 145 | bool ThreadManager::IsMainThread() { |
| 146 | return IsThreadRefEqual(CurrentThreadRef(), main_thread_ref_); |
| 147 | } |
| 148 | |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 149 | Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls() |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 150 | : thread_(Thread::Current()), |
| 151 | previous_state_(thread_->SetAllowBlockingCalls(false)) {} |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 152 | |
| 153 | Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 154 | RTC_DCHECK(thread_->IsCurrent()); |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 155 | thread_->SetAllowBlockingCalls(previous_state_); |
| 156 | } |
| 157 | |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 158 | // DEPRECATED. |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 159 | Thread::Thread() : Thread(SocketServer::CreateDefault()) {} |
| 160 | |
Taylor Brandstetter | 0867260 | 2018-03-02 15:20:33 -0800 | [diff] [blame] | 161 | Thread::Thread(SocketServer* ss) : Thread(ss, /*do_init=*/true) {} |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 162 | |
| 163 | Thread::Thread(std::unique_ptr<SocketServer> ss) |
Taylor Brandstetter | 0867260 | 2018-03-02 15:20:33 -0800 | [diff] [blame] | 164 | : Thread(std::move(ss), /*do_init=*/true) {} |
| 165 | |
| 166 | Thread::Thread(SocketServer* ss, bool do_init) |
| 167 | : MessageQueue(ss, /*do_init=*/false) { |
| 168 | SetName("Thread", this); // default name |
| 169 | if (do_init) { |
| 170 | DoInit(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | Thread::Thread(std::unique_ptr<SocketServer> ss, bool do_init) |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 175 | : MessageQueue(std::move(ss), false) { |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 176 | SetName("Thread", this); // default name |
Taylor Brandstetter | 0867260 | 2018-03-02 15:20:33 -0800 | [diff] [blame] | 177 | if (do_init) { |
| 178 | DoInit(); |
| 179 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | Thread::~Thread() { |
| 183 | Stop(); |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 184 | DoDestroy(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 185 | } |
| 186 | |
nisse | 7866cfe | 2017-04-26 01:45:31 -0700 | [diff] [blame] | 187 | bool Thread::IsCurrent() const { |
| 188 | return ThreadManager::Instance()->CurrentThread() == this; |
| 189 | } |
| 190 | |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 191 | std::unique_ptr<Thread> Thread::CreateWithSocketServer() { |
| 192 | return std::unique_ptr<Thread>(new Thread(SocketServer::CreateDefault())); |
| 193 | } |
| 194 | |
| 195 | std::unique_ptr<Thread> Thread::Create() { |
| 196 | return std::unique_ptr<Thread>( |
| 197 | new Thread(std::unique_ptr<SocketServer>(new NullSocketServer()))); |
| 198 | } |
| 199 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 200 | bool Thread::SleepMs(int milliseconds) { |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 201 | AssertBlockingIsAllowedOnCurrentThread(); |
| 202 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 203 | #if defined(WEBRTC_WIN) |
| 204 | ::Sleep(milliseconds); |
| 205 | return true; |
| 206 | #else |
| 207 | // POSIX has both a usleep() and a nanosleep(), but the former is deprecated, |
| 208 | // so we use nanosleep() even though it has greater precision than necessary. |
| 209 | struct timespec ts; |
| 210 | ts.tv_sec = milliseconds / 1000; |
| 211 | ts.tv_nsec = (milliseconds % 1000) * 1000000; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 212 | int ret = nanosleep(&ts, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 213 | if (ret != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 214 | RTC_LOG_ERR(LS_WARNING) << "nanosleep() returning early"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 215 | return false; |
| 216 | } |
| 217 | return true; |
| 218 | #endif |
| 219 | } |
| 220 | |
| 221 | bool Thread::SetName(const std::string& name, const void* obj) { |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 222 | RTC_DCHECK(!IsRunning()); |
| 223 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 224 | name_ = name; |
| 225 | if (obj) { |
Niels Möller | aba0633 | 2018-10-16 15:14:15 +0200 | [diff] [blame] | 226 | // The %p specifier typically produce at most 16 hex digits, possibly with a |
| 227 | // 0x prefix. But format is implementation defined, so add some margin. |
| 228 | char buf[30]; |
| 229 | snprintf(buf, sizeof(buf), " 0x%p", obj); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 230 | name_ += buf; |
| 231 | } |
| 232 | return true; |
| 233 | } |
| 234 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 235 | bool Thread::Start(Runnable* runnable) { |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 236 | RTC_DCHECK(!IsRunning()); |
| 237 | |
| 238 | if (IsRunning()) |
| 239 | return false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 240 | |
André Susano Pinto | 02a5797 | 2016-07-22 13:30:05 +0200 | [diff] [blame] | 241 | Restart(); // reset IsQuitting() if the thread is being restarted |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 242 | |
| 243 | // Make sure that ThreadManager is created on the main thread before |
| 244 | // we start a new thread. |
| 245 | ThreadManager::Instance(); |
| 246 | |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 247 | owned_ = true; |
| 248 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 249 | ThreadInit* init = new ThreadInit; |
| 250 | init->thread = this; |
| 251 | init->runnable = runnable; |
| 252 | #if defined(WEBRTC_WIN) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 253 | thread_ = CreateThread(nullptr, 0, PreRun, init, 0, &thread_id_); |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 254 | if (!thread_) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | #elif defined(WEBRTC_POSIX) |
| 258 | pthread_attr_t attr; |
| 259 | pthread_attr_init(&attr); |
| 260 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 261 | int error_code = pthread_create(&thread_, &attr, PreRun, init); |
| 262 | if (0 != error_code) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 263 | RTC_LOG(LS_ERROR) << "Unable to create pthread, error " << error_code; |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 264 | thread_ = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 265 | return false; |
| 266 | } |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 267 | RTC_DCHECK(thread_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 268 | #endif |
| 269 | return true; |
| 270 | } |
| 271 | |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 272 | bool Thread::WrapCurrent() { |
| 273 | return WrapCurrentWithThreadManager(ThreadManager::Instance(), true); |
| 274 | } |
| 275 | |
| 276 | void Thread::UnwrapCurrent() { |
| 277 | // Clears the platform-specific thread-specific storage. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 278 | ThreadManager::Instance()->SetCurrentThread(nullptr); |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 279 | #if defined(WEBRTC_WIN) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 280 | if (thread_ != nullptr) { |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 281 | if (!CloseHandle(thread_)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 282 | RTC_LOG_GLE(LS_ERROR) |
| 283 | << "When unwrapping thread, failed to close handle."; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 284 | } |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 285 | thread_ = nullptr; |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 286 | thread_id_ = 0; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 287 | } |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 288 | #elif defined(WEBRTC_POSIX) |
| 289 | thread_ = 0; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 290 | #endif |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void Thread::SafeWrapCurrent() { |
| 294 | WrapCurrentWithThreadManager(ThreadManager::Instance(), false); |
| 295 | } |
| 296 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 297 | void Thread::Join() { |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 298 | if (!IsRunning()) |
| 299 | return; |
| 300 | |
| 301 | RTC_DCHECK(!IsCurrent()); |
| 302 | if (Current() && !Current()->blocking_calls_allowed_) { |
| 303 | RTC_LOG(LS_WARNING) << "Waiting for the thread to join, " |
| 304 | << "but blocking calls have been disallowed"; |
| 305 | } |
jiayl@webrtc.org | 1fd362c | 2014-09-26 16:57:07 +0000 | [diff] [blame] | 306 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 307 | #if defined(WEBRTC_WIN) |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 308 | RTC_DCHECK(thread_ != nullptr); |
| 309 | WaitForSingleObject(thread_, INFINITE); |
| 310 | CloseHandle(thread_); |
| 311 | thread_ = nullptr; |
| 312 | thread_id_ = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 313 | #elif defined(WEBRTC_POSIX) |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 314 | pthread_join(thread_, nullptr); |
| 315 | thread_ = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 316 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 317 | } |
| 318 | |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 319 | bool Thread::SetAllowBlockingCalls(bool allow) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 320 | RTC_DCHECK(IsCurrent()); |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 321 | bool previous = blocking_calls_allowed_; |
| 322 | blocking_calls_allowed_ = allow; |
| 323 | return previous; |
| 324 | } |
| 325 | |
| 326 | // static |
| 327 | void Thread::AssertBlockingIsAllowedOnCurrentThread() { |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 328 | #if !defined(NDEBUG) |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 329 | Thread* current = Thread::Current(); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 330 | RTC_DCHECK(!current || current->blocking_calls_allowed_); |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 331 | #endif |
| 332 | } |
| 333 | |
deadbeef | dc20e26 | 2017-01-31 15:10:44 -0800 | [diff] [blame] | 334 | // static |
| 335 | #if defined(WEBRTC_WIN) |
| 336 | DWORD WINAPI Thread::PreRun(LPVOID pv) { |
| 337 | #else |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 338 | void* Thread::PreRun(void* pv) { |
deadbeef | dc20e26 | 2017-01-31 15:10:44 -0800 | [diff] [blame] | 339 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 340 | ThreadInit* init = static_cast<ThreadInit*>(pv); |
| 341 | ThreadManager::Instance()->SetCurrentThread(init->thread); |
Tommi | ea14f0a | 2015-05-18 13:51:06 +0200 | [diff] [blame] | 342 | rtc::SetCurrentThreadName(init->thread->name_.c_str()); |
Kári Tristan Helgason | 62b1345 | 2018-10-12 12:57:49 +0200 | [diff] [blame] | 343 | #if defined(WEBRTC_MAC) |
| 344 | ScopedAutoReleasePool pool; |
| 345 | #endif |
kthelgason | de6adbe | 2017-02-22 00:42:11 -0800 | [diff] [blame] | 346 | if (init->runnable) { |
| 347 | init->runnable->Run(init->thread); |
| 348 | } else { |
| 349 | init->thread->Run(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 350 | } |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 351 | ThreadManager::Instance()->SetCurrentThread(nullptr); |
kthelgason | de6adbe | 2017-02-22 00:42:11 -0800 | [diff] [blame] | 352 | delete init; |
| 353 | #ifdef WEBRTC_WIN |
| 354 | return 0; |
| 355 | #else |
| 356 | return nullptr; |
| 357 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void Thread::Run() { |
| 361 | ProcessMessages(kForever); |
| 362 | } |
| 363 | |
| 364 | bool Thread::IsOwned() { |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 365 | RTC_DCHECK(IsRunning()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 366 | return owned_; |
| 367 | } |
| 368 | |
| 369 | void Thread::Stop() { |
| 370 | MessageQueue::Quit(); |
| 371 | Join(); |
| 372 | } |
| 373 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 374 | void Thread::Send(const Location& posted_from, |
| 375 | MessageHandler* phandler, |
| 376 | uint32_t id, |
| 377 | MessageData* pdata) { |
André Susano Pinto | 02a5797 | 2016-07-22 13:30:05 +0200 | [diff] [blame] | 378 | if (IsQuitting()) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 379 | return; |
| 380 | |
| 381 | // Sent messages are sent to the MessageHandler directly, in the context |
| 382 | // of "thread", like Win32 SendMessage. If in the right context, |
| 383 | // call the handler directly. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 384 | Message msg; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 385 | msg.posted_from = posted_from; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 386 | msg.phandler = phandler; |
| 387 | msg.message_id = id; |
| 388 | msg.pdata = pdata; |
| 389 | if (IsCurrent()) { |
| 390 | phandler->OnMessage(&msg); |
| 391 | return; |
| 392 | } |
| 393 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 394 | AssertBlockingIsAllowedOnCurrentThread(); |
| 395 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 396 | AutoThread thread; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 397 | Thread* current_thread = Thread::Current(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 398 | RTC_DCHECK(current_thread != nullptr); // AutoThread ensures this |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 399 | |
| 400 | bool ready = false; |
| 401 | { |
| 402 | CritScope cs(&crit_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 403 | _SendMessage smsg; |
| 404 | smsg.thread = current_thread; |
| 405 | smsg.msg = msg; |
| 406 | smsg.ready = &ready; |
| 407 | sendlist_.push_back(smsg); |
| 408 | } |
| 409 | |
| 410 | // Wait for a reply |
jbauch | 9ccedc3 | 2016-02-25 01:14:56 -0800 | [diff] [blame] | 411 | WakeUpSocketServer(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 412 | |
| 413 | bool waited = false; |
| 414 | crit_.Enter(); |
| 415 | while (!ready) { |
| 416 | crit_.Leave(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 417 | // We need to limit "ReceiveSends" to |this| thread to avoid an arbitrary |
| 418 | // thread invoking calls on the current thread. |
| 419 | current_thread->ReceiveSendsFromThread(this); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 420 | current_thread->socketserver()->Wait(kForever, false); |
| 421 | waited = true; |
| 422 | crit_.Enter(); |
| 423 | } |
| 424 | crit_.Leave(); |
| 425 | |
| 426 | // Our Wait loop above may have consumed some WakeUp events for this |
| 427 | // MessageQueue, that weren't relevant to this Send. Losing these WakeUps can |
| 428 | // cause problems for some SocketServers. |
| 429 | // |
| 430 | // Concrete example: |
| 431 | // Win32SocketServer on thread A calls Send on thread B. While processing the |
| 432 | // message, thread B Posts a message to A. We consume the wakeup for that |
| 433 | // Post while waiting for the Send to complete, which means that when we exit |
| 434 | // this loop, we need to issue another WakeUp, or else the Posted message |
| 435 | // won't be processed in a timely manner. |
| 436 | |
| 437 | if (waited) { |
| 438 | current_thread->socketserver()->WakeUp(); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | void Thread::ReceiveSends() { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 443 | ReceiveSendsFromThread(nullptr); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void Thread::ReceiveSendsFromThread(const Thread* source) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 447 | // Receive a sent message. Cleanup scenarios: |
| 448 | // - thread sending exits: We don't allow this, since thread can exit |
| 449 | // only via Join, so Send must complete. |
| 450 | // - thread receiving exits: Wakeup/set ready in Thread::Clear() |
| 451 | // - object target cleared: Wakeup/set ready in Thread::Clear() |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 452 | _SendMessage smsg; |
| 453 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 454 | crit_.Enter(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 455 | while (PopSendMessageFromThread(source, &smsg)) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 456 | crit_.Leave(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 457 | |
Ishan Khot | a3b6601 | 2018-06-26 20:04:43 -0700 | [diff] [blame] | 458 | Dispatch(&smsg.msg); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 459 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 460 | crit_.Enter(); |
| 461 | *smsg.ready = true; |
| 462 | smsg.thread->socketserver()->WakeUp(); |
| 463 | } |
| 464 | crit_.Leave(); |
| 465 | } |
| 466 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 467 | bool Thread::PopSendMessageFromThread(const Thread* source, _SendMessage* msg) { |
| 468 | for (std::list<_SendMessage>::iterator it = sendlist_.begin(); |
| 469 | it != sendlist_.end(); ++it) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 470 | if (it->thread == source || source == nullptr) { |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 471 | *msg = *it; |
| 472 | sendlist_.erase(it); |
| 473 | return true; |
| 474 | } |
| 475 | } |
| 476 | return false; |
| 477 | } |
| 478 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 479 | void Thread::InvokeInternal(const Location& posted_from, |
| 480 | MessageHandler* handler) { |
| 481 | TRACE_EVENT2("webrtc", "Thread::Invoke", "src_file_and_line", |
| 482 | posted_from.file_and_line(), "src_func", |
| 483 | posted_from.function_name()); |
| 484 | Send(posted_from, handler); |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Niels Möller | 8909a63 | 2018-09-06 08:42:44 +0200 | [diff] [blame] | 487 | bool Thread::IsProcessingMessagesForTesting() { |
| 488 | return (owned_ || IsCurrent()) && |
| 489 | MessageQueue::IsProcessingMessagesForTesting(); |
| 490 | } |
| 491 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 492 | void Thread::Clear(MessageHandler* phandler, |
| 493 | uint32_t id, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 494 | MessageList* removed) { |
| 495 | CritScope cs(&crit_); |
| 496 | |
| 497 | // Remove messages on sendlist_ with phandler |
| 498 | // Object target cleared: remove from send list, wakeup/set ready |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 499 | // if sender not null. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 500 | |
| 501 | std::list<_SendMessage>::iterator iter = sendlist_.begin(); |
| 502 | while (iter != sendlist_.end()) { |
| 503 | _SendMessage smsg = *iter; |
| 504 | if (smsg.msg.Match(phandler, id)) { |
| 505 | if (removed) { |
| 506 | removed->push_back(smsg.msg); |
| 507 | } else { |
| 508 | delete smsg.msg.pdata; |
| 509 | } |
| 510 | iter = sendlist_.erase(iter); |
| 511 | *smsg.ready = true; |
| 512 | smsg.thread->socketserver()->WakeUp(); |
| 513 | continue; |
| 514 | } |
| 515 | ++iter; |
| 516 | } |
| 517 | |
Niels Möller | 5e007b7 | 2018-09-07 12:35:44 +0200 | [diff] [blame] | 518 | ClearInternal(phandler, id, removed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | bool Thread::ProcessMessages(int cmsLoop) { |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 522 | // Using ProcessMessages with a custom clock for testing and a time greater |
| 523 | // than 0 doesn't work, since it's not guaranteed to advance the custom |
| 524 | // clock's time, and may get stuck in an infinite loop. |
| 525 | RTC_DCHECK(GetClockForTesting() == nullptr || cmsLoop == 0 || |
| 526 | cmsLoop == kForever); |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 527 | int64_t msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 528 | int cmsNext = cmsLoop; |
| 529 | |
| 530 | while (true) { |
Kári Tristan Helgason | 62b1345 | 2018-10-12 12:57:49 +0200 | [diff] [blame] | 531 | #if defined(WEBRTC_MAC) |
| 532 | ScopedAutoReleasePool pool; |
| 533 | #endif |
kthelgason | de6adbe | 2017-02-22 00:42:11 -0800 | [diff] [blame] | 534 | Message msg; |
| 535 | if (!Get(&msg, cmsNext)) |
| 536 | return !IsQuitting(); |
| 537 | Dispatch(&msg); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 538 | |
kthelgason | de6adbe | 2017-02-22 00:42:11 -0800 | [diff] [blame] | 539 | if (cmsLoop != kForever) { |
| 540 | cmsNext = static_cast<int>(TimeUntil(msEnd)); |
| 541 | if (cmsNext < 0) |
| 542 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 547 | bool Thread::WrapCurrentWithThreadManager(ThreadManager* thread_manager, |
| 548 | bool need_synchronize_access) { |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 549 | RTC_DCHECK(!IsRunning()); |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 550 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 551 | #if defined(WEBRTC_WIN) |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 552 | if (need_synchronize_access) { |
| 553 | // We explicitly ask for no rights other than synchronization. |
| 554 | // This gives us the best chance of succeeding. |
| 555 | thread_ = OpenThread(SYNCHRONIZE, FALSE, GetCurrentThreadId()); |
| 556 | if (!thread_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 557 | RTC_LOG_GLE(LS_ERROR) << "Unable to get handle to thread."; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 558 | return false; |
| 559 | } |
| 560 | thread_id_ = GetCurrentThreadId(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 561 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 562 | #elif defined(WEBRTC_POSIX) |
| 563 | thread_ = pthread_self(); |
| 564 | #endif |
| 565 | owned_ = false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 566 | thread_manager->SetCurrentThread(this); |
| 567 | return true; |
| 568 | } |
| 569 | |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 570 | bool Thread::IsRunning() { |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 571 | #if defined(WEBRTC_WIN) |
| 572 | return thread_ != nullptr; |
| 573 | #elif defined(WEBRTC_POSIX) |
| 574 | return thread_ != 0; |
| 575 | #endif |
| 576 | } |
| 577 | |
Taylor Brandstetter | 0867260 | 2018-03-02 15:20:33 -0800 | [diff] [blame] | 578 | AutoThread::AutoThread() |
| 579 | : Thread(SocketServer::CreateDefault(), /*do_init=*/false) { |
| 580 | DoInit(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 581 | if (!ThreadManager::Instance()->CurrentThread()) { |
| 582 | ThreadManager::Instance()->SetCurrentThread(this); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | AutoThread::~AutoThread() { |
| 587 | Stop(); |
Steve Anton | 3b80aac | 2017-10-19 10:17:12 -0700 | [diff] [blame] | 588 | DoDestroy(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 589 | if (ThreadManager::Instance()->CurrentThread() == this) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 590 | ThreadManager::Instance()->SetCurrentThread(nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 594 | AutoSocketServerThread::AutoSocketServerThread(SocketServer* ss) |
Taylor Brandstetter | 0867260 | 2018-03-02 15:20:33 -0800 | [diff] [blame] | 595 | : Thread(ss, /*do_init=*/false) { |
| 596 | DoInit(); |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 597 | old_thread_ = ThreadManager::Instance()->CurrentThread(); |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 598 | // Temporarily set the current thread to nullptr so that we can keep checks |
| 599 | // around that catch unintentional pointer overwrites. |
| 600 | rtc::ThreadManager::Instance()->SetCurrentThread(nullptr); |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 601 | rtc::ThreadManager::Instance()->SetCurrentThread(this); |
| 602 | if (old_thread_) { |
| 603 | MessageQueueManager::Remove(old_thread_); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | AutoSocketServerThread::~AutoSocketServerThread() { |
| 608 | RTC_DCHECK(ThreadManager::Instance()->CurrentThread() == this); |
| 609 | // Some tests post destroy messages to this thread. To avoid memory |
| 610 | // leaks, we have to process those messages. In particular |
| 611 | // P2PTransportChannelPingTest, relying on the message posted in |
| 612 | // cricket::Connection::Destroy. |
| 613 | ProcessMessages(0); |
Steve Anton | 3b80aac | 2017-10-19 10:17:12 -0700 | [diff] [blame] | 614 | // Stop and destroy the thread before clearing it as the current thread. |
| 615 | // Sometimes there are messages left in the MessageQueue that will be |
| 616 | // destroyed by DoDestroy, and sometimes the destructors of the message and/or |
| 617 | // its contents rely on this thread still being set as the current thread. |
| 618 | Stop(); |
| 619 | DoDestroy(); |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 620 | rtc::ThreadManager::Instance()->SetCurrentThread(nullptr); |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 621 | rtc::ThreadManager::Instance()->SetCurrentThread(old_thread_); |
| 622 | if (old_thread_) { |
| 623 | MessageQueueManager::Add(old_thread_); |
| 624 | } |
| 625 | } |
| 626 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 627 | } // namespace rtc |