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> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 28 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 29 | #include <utility> |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 30 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 31 | #include "absl/algorithm/container.h" |
| 32 | #include "rtc_base/atomic_ops.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 33 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 34 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 35 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 36 | #include "rtc_base/null_socket_server.h" |
| 37 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 38 | #include "rtc_base/trace_event.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 39 | |
Kári Tristan Helgason | 62b1345 | 2018-10-12 12:57:49 +0200 | [diff] [blame] | 40 | #if defined(WEBRTC_MAC) |
| 41 | #include "rtc_base/system/cocoa_threading.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 42 | |
Kári Tristan Helgason | 62b1345 | 2018-10-12 12:57:49 +0200 | [diff] [blame] | 43 | /* |
| 44 | * These are forward-declarations for methods that are part of the |
| 45 | * ObjC runtime. They are declared in the private header objc-internal.h. |
| 46 | * These calls are what clang inserts when using @autoreleasepool in ObjC, |
| 47 | * but here they are used directly in order to keep this file C++. |
| 48 | * https://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime-support |
| 49 | */ |
| 50 | extern "C" { |
| 51 | void* objc_autoreleasePoolPush(void); |
| 52 | void objc_autoreleasePoolPop(void* pool); |
| 53 | } |
| 54 | |
| 55 | namespace { |
| 56 | class ScopedAutoReleasePool { |
| 57 | public: |
| 58 | ScopedAutoReleasePool() : pool_(objc_autoreleasePoolPush()) {} |
| 59 | ~ScopedAutoReleasePool() { objc_autoreleasePoolPop(pool_); } |
| 60 | |
| 61 | private: |
| 62 | void* const pool_; |
| 63 | }; |
| 64 | } // namespace |
| 65 | #endif |
| 66 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | namespace rtc { |
Steve Anton | bcc1a76 | 2019-12-11 11:21:53 -0800 | [diff] [blame] | 68 | namespace { |
| 69 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 70 | const int kSlowDispatchLoggingThreshold = 50; // 50 ms |
| 71 | |
Steve Anton | bcc1a76 | 2019-12-11 11:21:53 -0800 | [diff] [blame] | 72 | class MessageHandlerWithTask final : public MessageHandler { |
| 73 | public: |
| 74 | MessageHandlerWithTask() = default; |
| 75 | |
| 76 | void OnMessage(Message* msg) override { |
| 77 | static_cast<rtc_thread_internal::MessageLikeTask*>(msg->pdata)->Run(); |
| 78 | delete msg->pdata; |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | ~MessageHandlerWithTask() override {} |
| 83 | |
| 84 | RTC_DISALLOW_COPY_AND_ASSIGN(MessageHandlerWithTask); |
| 85 | }; |
| 86 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 87 | class RTC_SCOPED_LOCKABLE MarkProcessingCritScope { |
| 88 | public: |
| 89 | MarkProcessingCritScope(const CriticalSection* cs, size_t* processing) |
| 90 | RTC_EXCLUSIVE_LOCK_FUNCTION(cs) |
| 91 | : cs_(cs), processing_(processing) { |
| 92 | cs_->Enter(); |
| 93 | *processing_ += 1; |
| 94 | } |
| 95 | |
| 96 | ~MarkProcessingCritScope() RTC_UNLOCK_FUNCTION() { |
| 97 | *processing_ -= 1; |
| 98 | cs_->Leave(); |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | const CriticalSection* const cs_; |
| 103 | size_t* processing_; |
| 104 | |
| 105 | RTC_DISALLOW_COPY_AND_ASSIGN(MarkProcessingCritScope); |
| 106 | }; |
| 107 | |
Steve Anton | bcc1a76 | 2019-12-11 11:21:53 -0800 | [diff] [blame] | 108 | } // namespace |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 109 | |
| 110 | ThreadManager* ThreadManager::Instance() { |
Niels Möller | 14682a3 | 2018-05-24 08:54:25 +0200 | [diff] [blame] | 111 | static ThreadManager* const thread_manager = new ThreadManager(); |
| 112 | return thread_manager; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 113 | } |
| 114 | |
nisse | 7866cfe | 2017-04-26 01:45:31 -0700 | [diff] [blame] | 115 | ThreadManager::~ThreadManager() { |
| 116 | // By above RTC_DEFINE_STATIC_LOCAL. |
| 117 | RTC_NOTREACHED() << "ThreadManager should never be destructed."; |
| 118 | } |
| 119 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 120 | // static |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 121 | void ThreadManager::Add(Thread* message_queue) { |
| 122 | return Instance()->AddInternal(message_queue); |
| 123 | } |
| 124 | void ThreadManager::AddInternal(Thread* message_queue) { |
| 125 | CritScope cs(&crit_); |
| 126 | // Prevent changes while the list of message queues is processed. |
| 127 | RTC_DCHECK_EQ(processing_, 0); |
| 128 | message_queues_.push_back(message_queue); |
| 129 | } |
| 130 | |
| 131 | // static |
| 132 | void ThreadManager::Remove(Thread* message_queue) { |
| 133 | return Instance()->RemoveInternal(message_queue); |
| 134 | } |
| 135 | void ThreadManager::RemoveInternal(Thread* message_queue) { |
| 136 | { |
| 137 | CritScope cs(&crit_); |
| 138 | // Prevent changes while the list of message queues is processed. |
| 139 | RTC_DCHECK_EQ(processing_, 0); |
| 140 | std::vector<Thread*>::iterator iter; |
| 141 | iter = absl::c_find(message_queues_, message_queue); |
| 142 | if (iter != message_queues_.end()) { |
| 143 | message_queues_.erase(iter); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // static |
| 149 | void ThreadManager::Clear(MessageHandler* handler) { |
| 150 | return Instance()->ClearInternal(handler); |
| 151 | } |
| 152 | void ThreadManager::ClearInternal(MessageHandler* handler) { |
| 153 | // Deleted objects may cause re-entrant calls to ClearInternal. This is |
| 154 | // allowed as the list of message queues does not change while queues are |
| 155 | // cleared. |
| 156 | MarkProcessingCritScope cs(&crit_, &processing_); |
| 157 | for (Thread* queue : message_queues_) { |
| 158 | queue->Clear(handler); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // static |
| 163 | void ThreadManager::ProcessAllMessageQueuesForTesting() { |
| 164 | return Instance()->ProcessAllMessageQueuesInternal(); |
| 165 | } |
| 166 | |
| 167 | void ThreadManager::ProcessAllMessageQueuesInternal() { |
| 168 | // This works by posting a delayed message at the current time and waiting |
| 169 | // for it to be dispatched on all queues, which will ensure that all messages |
| 170 | // that came before it were also dispatched. |
| 171 | volatile int queues_not_done = 0; |
| 172 | |
| 173 | // This class is used so that whether the posted message is processed, or the |
| 174 | // message queue is simply cleared, queues_not_done gets decremented. |
| 175 | class ScopedIncrement : public MessageData { |
| 176 | public: |
| 177 | ScopedIncrement(volatile int* value) : value_(value) { |
| 178 | AtomicOps::Increment(value_); |
| 179 | } |
| 180 | ~ScopedIncrement() override { AtomicOps::Decrement(value_); } |
| 181 | |
| 182 | private: |
| 183 | volatile int* value_; |
| 184 | }; |
| 185 | |
| 186 | { |
| 187 | MarkProcessingCritScope cs(&crit_, &processing_); |
| 188 | for (Thread* queue : message_queues_) { |
| 189 | if (!queue->IsProcessingMessagesForTesting()) { |
| 190 | // If the queue is not processing messages, it can |
| 191 | // be ignored. If we tried to post a message to it, it would be dropped |
| 192 | // or ignored. |
| 193 | continue; |
| 194 | } |
| 195 | queue->PostDelayed(RTC_FROM_HERE, 0, nullptr, MQID_DISPOSE, |
| 196 | new ScopedIncrement(&queues_not_done)); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | rtc::Thread* current = rtc::Thread::Current(); |
| 201 | // Note: One of the message queues may have been on this thread, which is |
| 202 | // why we can't synchronously wait for queues_not_done to go to 0; we need |
| 203 | // to process messages as well. |
| 204 | while (AtomicOps::AcquireLoad(&queues_not_done) > 0) { |
| 205 | if (current) { |
| 206 | current->ProcessMessages(0); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // static |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 212 | Thread* Thread::Current() { |
nisse | 7866cfe | 2017-04-26 01:45:31 -0700 | [diff] [blame] | 213 | ThreadManager* manager = ThreadManager::Instance(); |
| 214 | Thread* thread = manager->CurrentThread(); |
| 215 | |
Niels Moller | 9d1840c | 2019-05-21 07:26:37 +0000 | [diff] [blame] | 216 | #ifndef NO_MAIN_THREAD_WRAPPING |
| 217 | // Only autowrap the thread which instantiated the ThreadManager. |
| 218 | if (!thread && manager->IsMainThread()) { |
| 219 | thread = new Thread(SocketServer::CreateDefault()); |
| 220 | thread->WrapCurrentWithThreadManager(manager, true); |
| 221 | } |
| 222 | #endif |
| 223 | |
nisse | 7866cfe | 2017-04-26 01:45:31 -0700 | [diff] [blame] | 224 | return thread; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | #if defined(WEBRTC_POSIX) |
Niels Moller | 9d1840c | 2019-05-21 07:26:37 +0000 | [diff] [blame] | 228 | ThreadManager::ThreadManager() : main_thread_ref_(CurrentThreadRef()) { |
Kári Tristan Helgason | 62b1345 | 2018-10-12 12:57:49 +0200 | [diff] [blame] | 229 | #if defined(WEBRTC_MAC) |
| 230 | InitCocoaMultiThreading(); |
| 231 | #endif |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 232 | pthread_key_create(&key_, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 235 | Thread* ThreadManager::CurrentThread() { |
| 236 | return static_cast<Thread*>(pthread_getspecific(key_)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Sebastian Jansson | 178a685 | 2020-01-14 11:12:26 +0100 | [diff] [blame] | 239 | void ThreadManager::SetCurrentThreadInternal(Thread* thread) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 240 | pthread_setspecific(key_, thread); |
| 241 | } |
| 242 | #endif |
| 243 | |
| 244 | #if defined(WEBRTC_WIN) |
Niels Moller | 9d1840c | 2019-05-21 07:26:37 +0000 | [diff] [blame] | 245 | ThreadManager::ThreadManager() |
| 246 | : key_(TlsAlloc()), main_thread_ref_(CurrentThreadRef()) {} |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 247 | |
| 248 | Thread* ThreadManager::CurrentThread() { |
| 249 | return static_cast<Thread*>(TlsGetValue(key_)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Sebastian Jansson | 178a685 | 2020-01-14 11:12:26 +0100 | [diff] [blame] | 252 | void ThreadManager::SetCurrentThreadInternal(Thread* thread) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 253 | TlsSetValue(key_, thread); |
| 254 | } |
| 255 | #endif |
| 256 | |
Sebastian Jansson | 178a685 | 2020-01-14 11:12:26 +0100 | [diff] [blame] | 257 | void ThreadManager::SetCurrentThread(Thread* thread) { |
| 258 | #if RTC_DLOG_IS_ON |
| 259 | if (CurrentThread() && thread) { |
| 260 | RTC_DLOG(LS_ERROR) << "SetCurrentThread: Overwriting an existing value?"; |
| 261 | } |
| 262 | #endif // RTC_DLOG_IS_ON |
| 263 | SetCurrentThreadInternal(thread); |
| 264 | } |
| 265 | |
| 266 | void rtc::ThreadManager::ChangeCurrentThreadForTest(rtc::Thread* thread) { |
| 267 | SetCurrentThreadInternal(thread); |
| 268 | } |
| 269 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 270 | Thread* ThreadManager::WrapCurrentThread() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 271 | Thread* result = CurrentThread(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 272 | if (nullptr == result) { |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 273 | result = new Thread(SocketServer::CreateDefault()); |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 274 | result->WrapCurrentWithThreadManager(this, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 275 | } |
| 276 | return result; |
| 277 | } |
| 278 | |
| 279 | void ThreadManager::UnwrapCurrentThread() { |
| 280 | Thread* t = CurrentThread(); |
| 281 | if (t && !(t->IsOwned())) { |
| 282 | t->UnwrapCurrent(); |
| 283 | delete t; |
| 284 | } |
| 285 | } |
| 286 | |
Niels Moller | 9d1840c | 2019-05-21 07:26:37 +0000 | [diff] [blame] | 287 | bool ThreadManager::IsMainThread() { |
| 288 | return IsThreadRefEqual(CurrentThreadRef(), main_thread_ref_); |
| 289 | } |
| 290 | |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 291 | Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls() |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 292 | : thread_(Thread::Current()), |
| 293 | previous_state_(thread_->SetAllowBlockingCalls(false)) {} |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 294 | |
| 295 | Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 296 | RTC_DCHECK(thread_->IsCurrent()); |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 297 | thread_->SetAllowBlockingCalls(previous_state_); |
| 298 | } |
| 299 | |
Taylor Brandstetter | 0867260 | 2018-03-02 15:20:33 -0800 | [diff] [blame] | 300 | Thread::Thread(SocketServer* ss) : Thread(ss, /*do_init=*/true) {} |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 301 | |
| 302 | Thread::Thread(std::unique_ptr<SocketServer> ss) |
Taylor Brandstetter | 0867260 | 2018-03-02 15:20:33 -0800 | [diff] [blame] | 303 | : Thread(std::move(ss), /*do_init=*/true) {} |
| 304 | |
| 305 | Thread::Thread(SocketServer* ss, bool do_init) |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 306 | : fPeekKeep_(false), |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 307 | delayed_next_num_(0), |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 308 | fInitialized_(false), |
| 309 | fDestroyed_(false), |
| 310 | stop_(0), |
| 311 | ss_(ss) { |
| 312 | RTC_DCHECK(ss); |
| 313 | ss_->SetMessageQueue(this); |
Taylor Brandstetter | 0867260 | 2018-03-02 15:20:33 -0800 | [diff] [blame] | 314 | SetName("Thread", this); // default name |
| 315 | if (do_init) { |
| 316 | DoInit(); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | Thread::Thread(std::unique_ptr<SocketServer> ss, bool do_init) |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 321 | : Thread(ss.get(), do_init) { |
| 322 | own_ss_ = std::move(ss); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | Thread::~Thread() { |
| 326 | Stop(); |
jbauch | 25d1f28 | 2016-02-05 00:25:02 -0800 | [diff] [blame] | 327 | DoDestroy(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 330 | void Thread::DoInit() { |
| 331 | if (fInitialized_) { |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | fInitialized_ = true; |
| 336 | ThreadManager::Add(this); |
| 337 | } |
| 338 | |
| 339 | void Thread::DoDestroy() { |
| 340 | if (fDestroyed_) { |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | fDestroyed_ = true; |
| 345 | // The signal is done from here to ensure |
| 346 | // that it always gets called when the queue |
| 347 | // is going away. |
| 348 | SignalQueueDestroyed(); |
| 349 | ThreadManager::Remove(this); |
| 350 | ClearInternal(nullptr, MQID_ANY, nullptr); |
| 351 | |
| 352 | if (ss_) { |
| 353 | ss_->SetMessageQueue(nullptr); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | SocketServer* Thread::socketserver() { |
| 358 | return ss_; |
| 359 | } |
| 360 | |
| 361 | void Thread::WakeUpSocketServer() { |
| 362 | ss_->WakeUp(); |
| 363 | } |
| 364 | |
| 365 | void Thread::Quit() { |
| 366 | AtomicOps::ReleaseStore(&stop_, 1); |
| 367 | WakeUpSocketServer(); |
| 368 | } |
| 369 | |
| 370 | bool Thread::IsQuitting() { |
| 371 | return AtomicOps::AcquireLoad(&stop_) != 0; |
| 372 | } |
| 373 | |
| 374 | void Thread::Restart() { |
| 375 | AtomicOps::ReleaseStore(&stop_, 0); |
| 376 | } |
| 377 | |
| 378 | bool Thread::Peek(Message* pmsg, int cmsWait) { |
| 379 | if (fPeekKeep_) { |
| 380 | *pmsg = msgPeek_; |
| 381 | return true; |
| 382 | } |
| 383 | if (!Get(pmsg, cmsWait)) |
| 384 | return false; |
| 385 | msgPeek_ = *pmsg; |
| 386 | fPeekKeep_ = true; |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | bool Thread::Get(Message* pmsg, int cmsWait, bool process_io) { |
| 391 | // Return and clear peek if present |
| 392 | // Always return the peek if it exists so there is Peek/Get symmetry |
| 393 | |
| 394 | if (fPeekKeep_) { |
| 395 | *pmsg = msgPeek_; |
| 396 | fPeekKeep_ = false; |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | // Get w/wait + timer scan / dispatch + socket / event multiplexer dispatch |
| 401 | |
| 402 | int64_t cmsTotal = cmsWait; |
| 403 | int64_t cmsElapsed = 0; |
| 404 | int64_t msStart = TimeMillis(); |
| 405 | int64_t msCurrent = msStart; |
| 406 | while (true) { |
| 407 | // Check for sent messages |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 408 | ReceiveSendsFromThread(nullptr); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 409 | |
| 410 | // Check for posted events |
| 411 | int64_t cmsDelayNext = kForever; |
| 412 | bool first_pass = true; |
| 413 | while (true) { |
| 414 | // All queue operations need to be locked, but nothing else in this loop |
| 415 | // (specifically handling disposed message) can happen inside the crit. |
| 416 | // Otherwise, disposed MessageHandlers will cause deadlocks. |
| 417 | { |
| 418 | CritScope cs(&crit_); |
| 419 | // On the first pass, check for delayed messages that have been |
| 420 | // triggered and calculate the next trigger time. |
| 421 | if (first_pass) { |
| 422 | first_pass = false; |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 423 | while (!delayed_messages_.empty()) { |
| 424 | if (msCurrent < delayed_messages_.top().run_time_ms_) { |
| 425 | cmsDelayNext = |
| 426 | TimeDiff(delayed_messages_.top().run_time_ms_, msCurrent); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 427 | break; |
| 428 | } |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 429 | messages_.push_back(delayed_messages_.top().msg_); |
| 430 | delayed_messages_.pop(); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | // Pull a message off the message queue, if available. |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 434 | if (messages_.empty()) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 435 | break; |
| 436 | } else { |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 437 | *pmsg = messages_.front(); |
| 438 | messages_.pop_front(); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 439 | } |
| 440 | } // crit_ is released here. |
| 441 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 442 | // If this was a dispose message, delete it and skip it. |
| 443 | if (MQID_DISPOSE == pmsg->message_id) { |
| 444 | RTC_DCHECK(nullptr == pmsg->phandler); |
| 445 | delete pmsg->pdata; |
| 446 | *pmsg = Message(); |
| 447 | continue; |
| 448 | } |
| 449 | return true; |
| 450 | } |
| 451 | |
| 452 | if (IsQuitting()) |
| 453 | break; |
| 454 | |
| 455 | // Which is shorter, the delay wait or the asked wait? |
| 456 | |
| 457 | int64_t cmsNext; |
| 458 | if (cmsWait == kForever) { |
| 459 | cmsNext = cmsDelayNext; |
| 460 | } else { |
| 461 | cmsNext = std::max<int64_t>(0, cmsTotal - cmsElapsed); |
| 462 | if ((cmsDelayNext != kForever) && (cmsDelayNext < cmsNext)) |
| 463 | cmsNext = cmsDelayNext; |
| 464 | } |
| 465 | |
| 466 | { |
| 467 | // Wait and multiplex in the meantime |
| 468 | if (!ss_->Wait(static_cast<int>(cmsNext), process_io)) |
| 469 | return false; |
| 470 | } |
| 471 | |
| 472 | // If the specified timeout expired, return |
| 473 | |
| 474 | msCurrent = TimeMillis(); |
| 475 | cmsElapsed = TimeDiff(msCurrent, msStart); |
| 476 | if (cmsWait != kForever) { |
| 477 | if (cmsElapsed >= cmsWait) |
| 478 | return false; |
| 479 | } |
| 480 | } |
| 481 | return false; |
| 482 | } |
| 483 | |
| 484 | void Thread::Post(const Location& posted_from, |
| 485 | MessageHandler* phandler, |
| 486 | uint32_t id, |
| 487 | MessageData* pdata, |
| 488 | bool time_sensitive) { |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 489 | RTC_DCHECK(!time_sensitive); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 490 | if (IsQuitting()) { |
| 491 | delete pdata; |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | // Keep thread safe |
| 496 | // Add the message to the end of the queue |
| 497 | // Signal for the multiplexer to return |
| 498 | |
| 499 | { |
| 500 | CritScope cs(&crit_); |
| 501 | Message msg; |
| 502 | msg.posted_from = posted_from; |
| 503 | msg.phandler = phandler; |
| 504 | msg.message_id = id; |
| 505 | msg.pdata = pdata; |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 506 | messages_.push_back(msg); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 507 | } |
| 508 | WakeUpSocketServer(); |
| 509 | } |
| 510 | |
| 511 | void Thread::PostDelayed(const Location& posted_from, |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 512 | int delay_ms, |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 513 | MessageHandler* phandler, |
| 514 | uint32_t id, |
| 515 | MessageData* pdata) { |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 516 | return DoDelayPost(posted_from, delay_ms, TimeAfter(delay_ms), phandler, id, |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 517 | pdata); |
| 518 | } |
| 519 | |
| 520 | void Thread::PostAt(const Location& posted_from, |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 521 | int64_t run_at_ms, |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 522 | MessageHandler* phandler, |
| 523 | uint32_t id, |
| 524 | MessageData* pdata) { |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 525 | return DoDelayPost(posted_from, TimeUntil(run_at_ms), run_at_ms, phandler, id, |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 526 | pdata); |
| 527 | } |
| 528 | |
| 529 | void Thread::DoDelayPost(const Location& posted_from, |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 530 | int64_t delay_ms, |
| 531 | int64_t run_at_ms, |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 532 | MessageHandler* phandler, |
| 533 | uint32_t id, |
| 534 | MessageData* pdata) { |
| 535 | if (IsQuitting()) { |
| 536 | delete pdata; |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | // Keep thread safe |
| 541 | // Add to the priority queue. Gets sorted soonest first. |
| 542 | // Signal for the multiplexer to return. |
| 543 | |
| 544 | { |
| 545 | CritScope cs(&crit_); |
| 546 | Message msg; |
| 547 | msg.posted_from = posted_from; |
| 548 | msg.phandler = phandler; |
| 549 | msg.message_id = id; |
| 550 | msg.pdata = pdata; |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 551 | DelayedMessage delayed(delay_ms, run_at_ms, delayed_next_num_, msg); |
| 552 | delayed_messages_.push(delayed); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 553 | // If this message queue processes 1 message every millisecond for 50 days, |
| 554 | // we will wrap this number. Even then, only messages with identical times |
| 555 | // will be misordered, and then only briefly. This is probably ok. |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 556 | ++delayed_next_num_; |
| 557 | RTC_DCHECK_NE(0, delayed_next_num_); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 558 | } |
| 559 | WakeUpSocketServer(); |
| 560 | } |
| 561 | |
| 562 | int Thread::GetDelay() { |
| 563 | CritScope cs(&crit_); |
| 564 | |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 565 | if (!messages_.empty()) |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 566 | return 0; |
| 567 | |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 568 | if (!delayed_messages_.empty()) { |
| 569 | int delay = TimeUntil(delayed_messages_.top().run_time_ms_); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 570 | if (delay < 0) |
| 571 | delay = 0; |
| 572 | return delay; |
| 573 | } |
| 574 | |
| 575 | return kForever; |
| 576 | } |
| 577 | |
| 578 | void Thread::ClearInternal(MessageHandler* phandler, |
| 579 | uint32_t id, |
| 580 | MessageList* removed) { |
| 581 | // Remove messages with phandler |
| 582 | |
| 583 | if (fPeekKeep_ && msgPeek_.Match(phandler, id)) { |
| 584 | if (removed) { |
| 585 | removed->push_back(msgPeek_); |
| 586 | } else { |
| 587 | delete msgPeek_.pdata; |
| 588 | } |
| 589 | fPeekKeep_ = false; |
| 590 | } |
| 591 | |
| 592 | // Remove from ordered message queue |
| 593 | |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 594 | for (auto it = messages_.begin(); it != messages_.end();) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 595 | if (it->Match(phandler, id)) { |
| 596 | if (removed) { |
| 597 | removed->push_back(*it); |
| 598 | } else { |
| 599 | delete it->pdata; |
| 600 | } |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 601 | it = messages_.erase(it); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 602 | } else { |
| 603 | ++it; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | // Remove from priority queue. Not directly iterable, so use this approach |
| 608 | |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 609 | auto new_end = delayed_messages_.container().begin(); |
| 610 | for (auto it = new_end; it != delayed_messages_.container().end(); ++it) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 611 | if (it->msg_.Match(phandler, id)) { |
| 612 | if (removed) { |
| 613 | removed->push_back(it->msg_); |
| 614 | } else { |
| 615 | delete it->msg_.pdata; |
| 616 | } |
| 617 | } else { |
| 618 | *new_end++ = *it; |
| 619 | } |
| 620 | } |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 621 | delayed_messages_.container().erase(new_end, |
| 622 | delayed_messages_.container().end()); |
| 623 | delayed_messages_.reheap(); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | void Thread::Dispatch(Message* pmsg) { |
| 627 | TRACE_EVENT2("webrtc", "Thread::Dispatch", "src_file", |
| 628 | pmsg->posted_from.file_name(), "src_func", |
| 629 | pmsg->posted_from.function_name()); |
| 630 | int64_t start_time = TimeMillis(); |
| 631 | pmsg->phandler->OnMessage(pmsg); |
| 632 | int64_t end_time = TimeMillis(); |
| 633 | int64_t diff = TimeDiff(end_time, start_time); |
| 634 | if (diff >= kSlowDispatchLoggingThreshold) { |
| 635 | RTC_LOG(LS_INFO) << "Message took " << diff |
| 636 | << "ms to dispatch. Posted from: " |
| 637 | << pmsg->posted_from.ToString(); |
| 638 | } |
| 639 | } |
| 640 | |
nisse | 7866cfe | 2017-04-26 01:45:31 -0700 | [diff] [blame] | 641 | bool Thread::IsCurrent() const { |
| 642 | return ThreadManager::Instance()->CurrentThread() == this; |
| 643 | } |
| 644 | |
danilchap | bebf54c | 2016-04-28 01:32:48 -0700 | [diff] [blame] | 645 | std::unique_ptr<Thread> Thread::CreateWithSocketServer() { |
| 646 | return std::unique_ptr<Thread>(new Thread(SocketServer::CreateDefault())); |
| 647 | } |
| 648 | |
| 649 | std::unique_ptr<Thread> Thread::Create() { |
| 650 | return std::unique_ptr<Thread>( |
| 651 | new Thread(std::unique_ptr<SocketServer>(new NullSocketServer()))); |
| 652 | } |
| 653 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 654 | bool Thread::SleepMs(int milliseconds) { |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 655 | AssertBlockingIsAllowedOnCurrentThread(); |
| 656 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 657 | #if defined(WEBRTC_WIN) |
| 658 | ::Sleep(milliseconds); |
| 659 | return true; |
| 660 | #else |
| 661 | // POSIX has both a usleep() and a nanosleep(), but the former is deprecated, |
| 662 | // so we use nanosleep() even though it has greater precision than necessary. |
| 663 | struct timespec ts; |
| 664 | ts.tv_sec = milliseconds / 1000; |
| 665 | ts.tv_nsec = (milliseconds % 1000) * 1000000; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 666 | int ret = nanosleep(&ts, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 667 | if (ret != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 668 | RTC_LOG_ERR(LS_WARNING) << "nanosleep() returning early"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 669 | return false; |
| 670 | } |
| 671 | return true; |
| 672 | #endif |
| 673 | } |
| 674 | |
| 675 | bool Thread::SetName(const std::string& name, const void* obj) { |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 676 | RTC_DCHECK(!IsRunning()); |
| 677 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 678 | name_ = name; |
| 679 | if (obj) { |
Niels Möller | aba0633 | 2018-10-16 15:14:15 +0200 | [diff] [blame] | 680 | // The %p specifier typically produce at most 16 hex digits, possibly with a |
| 681 | // 0x prefix. But format is implementation defined, so add some margin. |
| 682 | char buf[30]; |
| 683 | snprintf(buf, sizeof(buf), " 0x%p", obj); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 684 | name_ += buf; |
| 685 | } |
| 686 | return true; |
| 687 | } |
| 688 | |
Niels Möller | d2e5013 | 2019-06-11 09:24:14 +0200 | [diff] [blame] | 689 | bool Thread::Start() { |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 690 | RTC_DCHECK(!IsRunning()); |
| 691 | |
| 692 | if (IsRunning()) |
| 693 | return false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 694 | |
André Susano Pinto | 02a5797 | 2016-07-22 13:30:05 +0200 | [diff] [blame] | 695 | Restart(); // reset IsQuitting() if the thread is being restarted |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 696 | |
| 697 | // Make sure that ThreadManager is created on the main thread before |
| 698 | // we start a new thread. |
| 699 | ThreadManager::Instance(); |
| 700 | |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 701 | owned_ = true; |
| 702 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 703 | #if defined(WEBRTC_WIN) |
Niels Möller | d2e5013 | 2019-06-11 09:24:14 +0200 | [diff] [blame] | 704 | thread_ = CreateThread(nullptr, 0, PreRun, this, 0, &thread_id_); |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 705 | if (!thread_) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 706 | return false; |
| 707 | } |
| 708 | #elif defined(WEBRTC_POSIX) |
| 709 | pthread_attr_t attr; |
| 710 | pthread_attr_init(&attr); |
| 711 | |
Niels Möller | d2e5013 | 2019-06-11 09:24:14 +0200 | [diff] [blame] | 712 | int error_code = pthread_create(&thread_, &attr, PreRun, this); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 713 | if (0 != error_code) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 714 | RTC_LOG(LS_ERROR) << "Unable to create pthread, error " << error_code; |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 715 | thread_ = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 716 | return false; |
| 717 | } |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 718 | RTC_DCHECK(thread_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 719 | #endif |
| 720 | return true; |
| 721 | } |
| 722 | |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 723 | bool Thread::WrapCurrent() { |
| 724 | return WrapCurrentWithThreadManager(ThreadManager::Instance(), true); |
| 725 | } |
| 726 | |
| 727 | void Thread::UnwrapCurrent() { |
| 728 | // Clears the platform-specific thread-specific storage. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 729 | ThreadManager::Instance()->SetCurrentThread(nullptr); |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 730 | #if defined(WEBRTC_WIN) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 731 | if (thread_ != nullptr) { |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 732 | if (!CloseHandle(thread_)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 733 | RTC_LOG_GLE(LS_ERROR) |
| 734 | << "When unwrapping thread, failed to close handle."; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 735 | } |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 736 | thread_ = nullptr; |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 737 | thread_id_ = 0; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 738 | } |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 739 | #elif defined(WEBRTC_POSIX) |
| 740 | thread_ = 0; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 741 | #endif |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | void Thread::SafeWrapCurrent() { |
| 745 | WrapCurrentWithThreadManager(ThreadManager::Instance(), false); |
| 746 | } |
| 747 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 748 | void Thread::Join() { |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 749 | if (!IsRunning()) |
| 750 | return; |
| 751 | |
| 752 | RTC_DCHECK(!IsCurrent()); |
| 753 | if (Current() && !Current()->blocking_calls_allowed_) { |
| 754 | RTC_LOG(LS_WARNING) << "Waiting for the thread to join, " |
Jonas Olsson | b2b2031 | 2020-01-14 12:11:31 +0100 | [diff] [blame] | 755 | "but blocking calls have been disallowed"; |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 756 | } |
jiayl@webrtc.org | 1fd362c | 2014-09-26 16:57:07 +0000 | [diff] [blame] | 757 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 758 | #if defined(WEBRTC_WIN) |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 759 | RTC_DCHECK(thread_ != nullptr); |
| 760 | WaitForSingleObject(thread_, INFINITE); |
| 761 | CloseHandle(thread_); |
| 762 | thread_ = nullptr; |
| 763 | thread_id_ = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 764 | #elif defined(WEBRTC_POSIX) |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 765 | pthread_join(thread_, nullptr); |
| 766 | thread_ = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 767 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 768 | } |
| 769 | |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 770 | bool Thread::SetAllowBlockingCalls(bool allow) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 771 | RTC_DCHECK(IsCurrent()); |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 772 | bool previous = blocking_calls_allowed_; |
| 773 | blocking_calls_allowed_ = allow; |
| 774 | return previous; |
| 775 | } |
| 776 | |
| 777 | // static |
| 778 | void Thread::AssertBlockingIsAllowedOnCurrentThread() { |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 779 | #if !defined(NDEBUG) |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 780 | Thread* current = Thread::Current(); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 781 | RTC_DCHECK(!current || current->blocking_calls_allowed_); |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 +0000 | [diff] [blame] | 782 | #endif |
| 783 | } |
| 784 | |
deadbeef | dc20e26 | 2017-01-31 15:10:44 -0800 | [diff] [blame] | 785 | // static |
| 786 | #if defined(WEBRTC_WIN) |
| 787 | DWORD WINAPI Thread::PreRun(LPVOID pv) { |
| 788 | #else |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 789 | void* Thread::PreRun(void* pv) { |
deadbeef | dc20e26 | 2017-01-31 15:10:44 -0800 | [diff] [blame] | 790 | #endif |
Niels Möller | d2e5013 | 2019-06-11 09:24:14 +0200 | [diff] [blame] | 791 | Thread* thread = static_cast<Thread*>(pv); |
| 792 | ThreadManager::Instance()->SetCurrentThread(thread); |
| 793 | rtc::SetCurrentThreadName(thread->name_.c_str()); |
Danil Chapovalov | 912b3b8 | 2019-11-22 15:52:40 +0100 | [diff] [blame] | 794 | CurrentTaskQueueSetter set_current_task_queue(thread); |
Kári Tristan Helgason | 62b1345 | 2018-10-12 12:57:49 +0200 | [diff] [blame] | 795 | #if defined(WEBRTC_MAC) |
| 796 | ScopedAutoReleasePool pool; |
| 797 | #endif |
Niels Möller | d2e5013 | 2019-06-11 09:24:14 +0200 | [diff] [blame] | 798 | thread->Run(); |
| 799 | |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 800 | ThreadManager::Instance()->SetCurrentThread(nullptr); |
kthelgason | de6adbe | 2017-02-22 00:42:11 -0800 | [diff] [blame] | 801 | #ifdef WEBRTC_WIN |
| 802 | return 0; |
| 803 | #else |
| 804 | return nullptr; |
| 805 | #endif |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 806 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 807 | |
| 808 | void Thread::Run() { |
| 809 | ProcessMessages(kForever); |
| 810 | } |
| 811 | |
| 812 | bool Thread::IsOwned() { |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 813 | RTC_DCHECK(IsRunning()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 814 | return owned_; |
| 815 | } |
| 816 | |
| 817 | void Thread::Stop() { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 818 | Thread::Quit(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 819 | Join(); |
| 820 | } |
| 821 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 822 | void Thread::Send(const Location& posted_from, |
| 823 | MessageHandler* phandler, |
| 824 | uint32_t id, |
| 825 | MessageData* pdata) { |
Sebastian Jansson | 5d9b964 | 2020-01-17 13:10:54 +0100 | [diff] [blame] | 826 | RTC_DCHECK(!IsQuitting()); |
André Susano Pinto | 02a5797 | 2016-07-22 13:30:05 +0200 | [diff] [blame] | 827 | if (IsQuitting()) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 828 | return; |
| 829 | |
| 830 | // Sent messages are sent to the MessageHandler directly, in the context |
| 831 | // of "thread", like Win32 SendMessage. If in the right context, |
| 832 | // call the handler directly. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 833 | Message msg; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 834 | msg.posted_from = posted_from; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 835 | msg.phandler = phandler; |
| 836 | msg.message_id = id; |
| 837 | msg.pdata = pdata; |
| 838 | if (IsCurrent()) { |
| 839 | phandler->OnMessage(&msg); |
| 840 | return; |
| 841 | } |
| 842 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 843 | AssertBlockingIsAllowedOnCurrentThread(); |
| 844 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 845 | AutoThread thread; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 846 | Thread* current_thread = Thread::Current(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 847 | RTC_DCHECK(current_thread != nullptr); // AutoThread ensures this |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 848 | |
| 849 | bool ready = false; |
| 850 | { |
| 851 | CritScope cs(&crit_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 852 | _SendMessage smsg; |
| 853 | smsg.thread = current_thread; |
| 854 | smsg.msg = msg; |
| 855 | smsg.ready = &ready; |
| 856 | sendlist_.push_back(smsg); |
| 857 | } |
| 858 | |
| 859 | // Wait for a reply |
jbauch | 9ccedc3 | 2016-02-25 01:14:56 -0800 | [diff] [blame] | 860 | WakeUpSocketServer(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 861 | |
| 862 | bool waited = false; |
| 863 | crit_.Enter(); |
| 864 | while (!ready) { |
| 865 | crit_.Leave(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 866 | // We need to limit "ReceiveSends" to |this| thread to avoid an arbitrary |
| 867 | // thread invoking calls on the current thread. |
| 868 | current_thread->ReceiveSendsFromThread(this); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 869 | current_thread->socketserver()->Wait(kForever, false); |
| 870 | waited = true; |
| 871 | crit_.Enter(); |
| 872 | } |
| 873 | crit_.Leave(); |
| 874 | |
| 875 | // Our Wait loop above may have consumed some WakeUp events for this |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 876 | // Thread, that weren't relevant to this Send. Losing these WakeUps can |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 877 | // cause problems for some SocketServers. |
| 878 | // |
| 879 | // Concrete example: |
| 880 | // Win32SocketServer on thread A calls Send on thread B. While processing the |
| 881 | // message, thread B Posts a message to A. We consume the wakeup for that |
| 882 | // Post while waiting for the Send to complete, which means that when we exit |
| 883 | // this loop, we need to issue another WakeUp, or else the Posted message |
| 884 | // won't be processed in a timely manner. |
| 885 | |
| 886 | if (waited) { |
| 887 | current_thread->socketserver()->WakeUp(); |
| 888 | } |
| 889 | } |
| 890 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 891 | void Thread::ReceiveSendsFromThread(const Thread* source) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 892 | // Receive a sent message. Cleanup scenarios: |
| 893 | // - thread sending exits: We don't allow this, since thread can exit |
| 894 | // only via Join, so Send must complete. |
| 895 | // - thread receiving exits: Wakeup/set ready in Thread::Clear() |
| 896 | // - object target cleared: Wakeup/set ready in Thread::Clear() |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 897 | _SendMessage smsg; |
| 898 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 899 | crit_.Enter(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 900 | while (PopSendMessageFromThread(source, &smsg)) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 901 | crit_.Leave(); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 902 | |
Ishan Khot | a3b6601 | 2018-06-26 20:04:43 -0700 | [diff] [blame] | 903 | Dispatch(&smsg.msg); |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 904 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 905 | crit_.Enter(); |
| 906 | *smsg.ready = true; |
| 907 | smsg.thread->socketserver()->WakeUp(); |
| 908 | } |
| 909 | crit_.Leave(); |
| 910 | } |
| 911 | |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 912 | bool Thread::PopSendMessageFromThread(const Thread* source, _SendMessage* msg) { |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 913 | for (auto it = sendlist_.begin(); it != sendlist_.end(); ++it) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 914 | if (it->thread == source || source == nullptr) { |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 915 | *msg = *it; |
| 916 | sendlist_.erase(it); |
| 917 | return true; |
| 918 | } |
| 919 | } |
| 920 | return false; |
| 921 | } |
| 922 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 923 | void Thread::InvokeInternal(const Location& posted_from, |
Danil Chapovalov | 8931345 | 2019-11-29 12:56:43 +0100 | [diff] [blame] | 924 | rtc::FunctionView<void()> functor) { |
Steve Anton | c5d7c52 | 2019-12-03 10:14:05 -0800 | [diff] [blame] | 925 | TRACE_EVENT2("webrtc", "Thread::Invoke", "src_file", posted_from.file_name(), |
| 926 | "src_func", posted_from.function_name()); |
Danil Chapovalov | 8931345 | 2019-11-29 12:56:43 +0100 | [diff] [blame] | 927 | |
| 928 | class FunctorMessageHandler : public MessageHandler { |
| 929 | public: |
| 930 | explicit FunctorMessageHandler(rtc::FunctionView<void()> functor) |
| 931 | : functor_(functor) {} |
| 932 | void OnMessage(Message* msg) override { functor_(); } |
| 933 | |
| 934 | private: |
| 935 | rtc::FunctionView<void()> functor_; |
| 936 | } handler(functor); |
| 937 | |
| 938 | Send(posted_from, &handler); |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Danil Chapovalov | 912b3b8 | 2019-11-22 15:52:40 +0100 | [diff] [blame] | 941 | void Thread::QueuedTaskHandler::OnMessage(Message* msg) { |
| 942 | RTC_DCHECK(msg); |
| 943 | auto* data = static_cast<ScopedMessageData<webrtc::QueuedTask>*>(msg->pdata); |
| 944 | std::unique_ptr<webrtc::QueuedTask> task = std::move(data->data()); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 945 | // Thread expects handler to own Message::pdata when OnMessage is called |
Danil Chapovalov | 912b3b8 | 2019-11-22 15:52:40 +0100 | [diff] [blame] | 946 | // Since MessageData is no longer needed, delete it. |
| 947 | delete data; |
| 948 | |
| 949 | // QueuedTask interface uses Run return value to communicate who owns the |
| 950 | // task. false means QueuedTask took the ownership. |
| 951 | if (!task->Run()) |
| 952 | task.release(); |
| 953 | } |
| 954 | |
| 955 | void Thread::PostTask(std::unique_ptr<webrtc::QueuedTask> task) { |
| 956 | // Though Post takes MessageData by raw pointer (last parameter), it still |
| 957 | // takes it with ownership. |
| 958 | Post(RTC_FROM_HERE, &queued_task_handler_, |
| 959 | /*id=*/0, new ScopedMessageData<webrtc::QueuedTask>(std::move(task))); |
| 960 | } |
| 961 | |
| 962 | void Thread::PostDelayedTask(std::unique_ptr<webrtc::QueuedTask> task, |
| 963 | uint32_t milliseconds) { |
| 964 | // Though PostDelayed takes MessageData by raw pointer (last parameter), |
| 965 | // it still takes it with ownership. |
| 966 | PostDelayed(RTC_FROM_HERE, milliseconds, &queued_task_handler_, |
| 967 | /*id=*/0, |
| 968 | new ScopedMessageData<webrtc::QueuedTask>(std::move(task))); |
| 969 | } |
| 970 | |
| 971 | void Thread::Delete() { |
| 972 | Stop(); |
| 973 | delete this; |
| 974 | } |
| 975 | |
Niels Möller | 8909a63 | 2018-09-06 08:42:44 +0200 | [diff] [blame] | 976 | bool Thread::IsProcessingMessagesForTesting() { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 977 | return (owned_ || IsCurrent()) && !IsQuitting(); |
Niels Möller | 8909a63 | 2018-09-06 08:42:44 +0200 | [diff] [blame] | 978 | } |
| 979 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 980 | void Thread::Clear(MessageHandler* phandler, |
| 981 | uint32_t id, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 982 | MessageList* removed) { |
| 983 | CritScope cs(&crit_); |
| 984 | |
| 985 | // Remove messages on sendlist_ with phandler |
| 986 | // Object target cleared: remove from send list, wakeup/set ready |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 987 | // if sender not null. |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 988 | for (auto iter = sendlist_.begin(); iter != sendlist_.end();) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 989 | _SendMessage smsg = *iter; |
| 990 | if (smsg.msg.Match(phandler, id)) { |
| 991 | if (removed) { |
| 992 | removed->push_back(smsg.msg); |
| 993 | } else { |
| 994 | delete smsg.msg.pdata; |
| 995 | } |
| 996 | iter = sendlist_.erase(iter); |
| 997 | *smsg.ready = true; |
| 998 | smsg.thread->socketserver()->WakeUp(); |
| 999 | continue; |
| 1000 | } |
| 1001 | ++iter; |
| 1002 | } |
| 1003 | |
Niels Möller | 5e007b7 | 2018-09-07 12:35:44 +0200 | [diff] [blame] | 1004 | ClearInternal(phandler, id, removed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | bool Thread::ProcessMessages(int cmsLoop) { |
deadbeef | 22e0814 | 2017-06-12 14:30:28 -0700 | [diff] [blame] | 1008 | // Using ProcessMessages with a custom clock for testing and a time greater |
| 1009 | // than 0 doesn't work, since it's not guaranteed to advance the custom |
| 1010 | // clock's time, and may get stuck in an infinite loop. |
| 1011 | RTC_DCHECK(GetClockForTesting() == nullptr || cmsLoop == 0 || |
| 1012 | cmsLoop == kForever); |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 1013 | int64_t msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1014 | int cmsNext = cmsLoop; |
| 1015 | |
| 1016 | while (true) { |
Kári Tristan Helgason | 62b1345 | 2018-10-12 12:57:49 +0200 | [diff] [blame] | 1017 | #if defined(WEBRTC_MAC) |
| 1018 | ScopedAutoReleasePool pool; |
| 1019 | #endif |
kthelgason | de6adbe | 2017-02-22 00:42:11 -0800 | [diff] [blame] | 1020 | Message msg; |
| 1021 | if (!Get(&msg, cmsNext)) |
| 1022 | return !IsQuitting(); |
| 1023 | Dispatch(&msg); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1024 | |
kthelgason | de6adbe | 2017-02-22 00:42:11 -0800 | [diff] [blame] | 1025 | if (cmsLoop != kForever) { |
| 1026 | cmsNext = static_cast<int>(TimeUntil(msEnd)); |
| 1027 | if (cmsNext < 0) |
| 1028 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 1033 | bool Thread::WrapCurrentWithThreadManager(ThreadManager* thread_manager, |
| 1034 | bool need_synchronize_access) { |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 1035 | RTC_DCHECK(!IsRunning()); |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 1036 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1037 | #if defined(WEBRTC_WIN) |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 1038 | if (need_synchronize_access) { |
| 1039 | // We explicitly ask for no rights other than synchronization. |
| 1040 | // This gives us the best chance of succeeding. |
| 1041 | thread_ = OpenThread(SYNCHRONIZE, FALSE, GetCurrentThreadId()); |
| 1042 | if (!thread_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1043 | RTC_LOG_GLE(LS_ERROR) << "Unable to get handle to thread."; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 +0000 | [diff] [blame] | 1044 | return false; |
| 1045 | } |
| 1046 | thread_id_ = GetCurrentThreadId(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1047 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1048 | #elif defined(WEBRTC_POSIX) |
| 1049 | thread_ = pthread_self(); |
| 1050 | #endif |
| 1051 | owned_ = false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1052 | thread_manager->SetCurrentThread(this); |
| 1053 | return true; |
| 1054 | } |
| 1055 | |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 1056 | bool Thread::IsRunning() { |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 1057 | #if defined(WEBRTC_WIN) |
| 1058 | return thread_ != nullptr; |
| 1059 | #elif defined(WEBRTC_POSIX) |
| 1060 | return thread_ != 0; |
| 1061 | #endif |
| 1062 | } |
| 1063 | |
Steve Anton | bcc1a76 | 2019-12-11 11:21:53 -0800 | [diff] [blame] | 1064 | // static |
| 1065 | MessageHandler* Thread::GetPostTaskMessageHandler() { |
| 1066 | // Allocate at first call, never deallocate. |
| 1067 | static MessageHandler* handler = new MessageHandlerWithTask; |
| 1068 | return handler; |
| 1069 | } |
| 1070 | |
Taylor Brandstetter | 0867260 | 2018-03-02 15:20:33 -0800 | [diff] [blame] | 1071 | AutoThread::AutoThread() |
| 1072 | : Thread(SocketServer::CreateDefault(), /*do_init=*/false) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1073 | if (!ThreadManager::Instance()->CurrentThread()) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 1074 | // DoInit registers with ThreadManager. Do that only if we intend to |
Niels Möller | 5a8f860 | 2019-06-12 11:30:59 +0200 | [diff] [blame] | 1075 | // be rtc::Thread::Current(), otherwise ProcessAllMessageQueuesInternal will |
| 1076 | // post a message to a queue that no running thread is serving. |
| 1077 | DoInit(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1078 | ThreadManager::Instance()->SetCurrentThread(this); |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | AutoThread::~AutoThread() { |
| 1083 | Stop(); |
Steve Anton | 3b80aac | 2017-10-19 10:17:12 -0700 | [diff] [blame] | 1084 | DoDestroy(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1085 | if (ThreadManager::Instance()->CurrentThread() == this) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1086 | ThreadManager::Instance()->SetCurrentThread(nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 1090 | AutoSocketServerThread::AutoSocketServerThread(SocketServer* ss) |
Taylor Brandstetter | 0867260 | 2018-03-02 15:20:33 -0800 | [diff] [blame] | 1091 | : Thread(ss, /*do_init=*/false) { |
| 1092 | DoInit(); |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 1093 | old_thread_ = ThreadManager::Instance()->CurrentThread(); |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 1094 | // Temporarily set the current thread to nullptr so that we can keep checks |
| 1095 | // around that catch unintentional pointer overwrites. |
| 1096 | rtc::ThreadManager::Instance()->SetCurrentThread(nullptr); |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 1097 | rtc::ThreadManager::Instance()->SetCurrentThread(this); |
| 1098 | if (old_thread_) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 1099 | ThreadManager::Remove(old_thread_); |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | AutoSocketServerThread::~AutoSocketServerThread() { |
| 1104 | RTC_DCHECK(ThreadManager::Instance()->CurrentThread() == this); |
| 1105 | // Some tests post destroy messages to this thread. To avoid memory |
| 1106 | // leaks, we have to process those messages. In particular |
| 1107 | // P2PTransportChannelPingTest, relying on the message posted in |
| 1108 | // cricket::Connection::Destroy. |
| 1109 | ProcessMessages(0); |
Steve Anton | 3b80aac | 2017-10-19 10:17:12 -0700 | [diff] [blame] | 1110 | // Stop and destroy the thread before clearing it as the current thread. |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 1111 | // Sometimes there are messages left in the Thread that will be |
Steve Anton | 3b80aac | 2017-10-19 10:17:12 -0700 | [diff] [blame] | 1112 | // destroyed by DoDestroy, and sometimes the destructors of the message and/or |
| 1113 | // its contents rely on this thread still being set as the current thread. |
| 1114 | Stop(); |
| 1115 | DoDestroy(); |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 1116 | rtc::ThreadManager::Instance()->SetCurrentThread(nullptr); |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 1117 | rtc::ThreadManager::Instance()->SetCurrentThread(old_thread_); |
| 1118 | if (old_thread_) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 1119 | ThreadManager::Add(old_thread_); |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1123 | } // namespace rtc |