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