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