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