henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [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 04:47:31 | [diff] [blame] | 11 | #include "rtc_base/thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 12 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 13 | #include "absl/strings/string_view.h" |
Markus Handell | 82da932 | 2022-12-16 14:50:24 | [diff] [blame] | 14 | #include "api/task_queue/task_queue_base.h" |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 | [diff] [blame] | 15 | #include "api/units/time_delta.h" |
| 16 | #include "rtc_base/socket_server.h" |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 17 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 18 | #if defined(WEBRTC_WIN) |
| 19 | #include <comdef.h> |
| 20 | #elif defined(WEBRTC_POSIX) |
| 21 | #include <time.h> |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 22 | #else |
| 23 | #error "Either WEBRTC_WIN or WEBRTC_POSIX needs to be defined." |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 24 | #endif |
| 25 | |
Artem Titov | 80d02ad | 2018-05-21 10:20:39 | [diff] [blame] | 26 | #if defined(WEBRTC_WIN) |
| 27 | // Disable warning that we don't care about: |
| 28 | // warning C4722: destructor never returns, potential memory leak |
| 29 | #pragma warning(disable : 4722) |
| 30 | #endif |
| 31 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 32 | #include <stdio.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 33 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 34 | #include <utility> |
Yves Gerey | 2e00abc | 2018-10-05 13:39:24 | [diff] [blame] | 35 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 36 | #include "absl/algorithm/container.h" |
Danil Chapovalov | 4bcf809 | 2022-07-06 17:42:34 | [diff] [blame] | 37 | #include "absl/cleanup/cleanup.h" |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 38 | #include "api/sequence_checker.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 39 | #include "rtc_base/checks.h" |
Tomas Gunnarsson | 0fd4c4e | 2020-09-04 14:33:25 | [diff] [blame] | 40 | #include "rtc_base/event.h" |
Mirko Bonadei | e5f4c6b | 2021-01-15 09:41:01 | [diff] [blame] | 41 | #include "rtc_base/internal/default_socket_server.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 42 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 43 | #include "rtc_base/null_socket_server.h" |
Danil Chapovalov | 3ab76d9 | 2022-09-23 10:39:21 | [diff] [blame] | 44 | #include "rtc_base/synchronization/mutex.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 45 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 46 | #include "rtc_base/trace_event.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 47 | |
Kári Tristan Helgason | 62b1345 | 2018-10-12 10:57:49 | [diff] [blame] | 48 | #if defined(WEBRTC_MAC) |
| 49 | #include "rtc_base/system/cocoa_threading.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 50 | |
Kári Tristan Helgason | 62b1345 | 2018-10-12 10:57:49 | [diff] [blame] | 51 | /* |
| 52 | * These are forward-declarations for methods that are part of the |
| 53 | * ObjC runtime. They are declared in the private header objc-internal.h. |
| 54 | * These calls are what clang inserts when using @autoreleasepool in ObjC, |
| 55 | * but here they are used directly in order to keep this file C++. |
| 56 | * https://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime-support |
| 57 | */ |
| 58 | extern "C" { |
| 59 | void* objc_autoreleasePoolPush(void); |
| 60 | void objc_autoreleasePoolPop(void* pool); |
| 61 | } |
| 62 | |
| 63 | namespace { |
| 64 | class ScopedAutoReleasePool { |
| 65 | public: |
| 66 | ScopedAutoReleasePool() : pool_(objc_autoreleasePoolPush()) {} |
| 67 | ~ScopedAutoReleasePool() { objc_autoreleasePoolPop(pool_); } |
| 68 | |
| 69 | private: |
| 70 | void* const pool_; |
| 71 | }; |
| 72 | } // namespace |
| 73 | #endif |
| 74 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 75 | namespace rtc { |
Steve Anton | bcc1a76 | 2019-12-11 19:21:53 | [diff] [blame] | 76 | |
Danil Chapovalov | 3ab76d9 | 2022-09-23 10:39:21 | [diff] [blame] | 77 | using ::webrtc::MutexLock; |
Danil Chapovalov | 0bd1665 | 2022-08-24 16:35:45 | [diff] [blame] | 78 | using ::webrtc::TimeDelta; |
| 79 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 80 | ThreadManager* ThreadManager::Instance() { |
Niels Möller | 14682a3 | 2018-05-24 06:54:25 | [diff] [blame] | 81 | static ThreadManager* const thread_manager = new ThreadManager(); |
| 82 | return thread_manager; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 83 | } |
| 84 | |
nisse | 7866cfe | 2017-04-26 08:45:31 | [diff] [blame] | 85 | ThreadManager::~ThreadManager() { |
| 86 | // By above RTC_DEFINE_STATIC_LOCAL. |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 87 | RTC_DCHECK_NOTREACHED() << "ThreadManager should never be destructed."; |
nisse | 7866cfe | 2017-04-26 08:45:31 | [diff] [blame] | 88 | } |
| 89 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 90 | // static |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 91 | void ThreadManager::Add(Thread* message_queue) { |
| 92 | return Instance()->AddInternal(message_queue); |
| 93 | } |
| 94 | void ThreadManager::AddInternal(Thread* message_queue) { |
Danil Chapovalov | c3c8934 | 2023-01-02 09:21:29 | [diff] [blame] | 95 | MutexLock cs(&crit_); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 96 | message_queues_.push_back(message_queue); |
| 97 | } |
| 98 | |
| 99 | // static |
| 100 | void ThreadManager::Remove(Thread* message_queue) { |
| 101 | return Instance()->RemoveInternal(message_queue); |
| 102 | } |
| 103 | void ThreadManager::RemoveInternal(Thread* message_queue) { |
| 104 | { |
Danil Chapovalov | c3c8934 | 2023-01-02 09:21:29 | [diff] [blame] | 105 | MutexLock cs(&crit_); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 106 | std::vector<Thread*>::iterator iter; |
| 107 | iter = absl::c_find(message_queues_, message_queue); |
| 108 | if (iter != message_queues_.end()) { |
| 109 | message_queues_.erase(iter); |
| 110 | } |
Sebastian Jansson | da7267a | 2020-03-03 09:48:05 | [diff] [blame] | 111 | #if RTC_DCHECK_IS_ON |
| 112 | RemoveFromSendGraph(message_queue); |
| 113 | #endif |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Sebastian Jansson | da7267a | 2020-03-03 09:48:05 | [diff] [blame] | 117 | #if RTC_DCHECK_IS_ON |
| 118 | void ThreadManager::RemoveFromSendGraph(Thread* thread) { |
| 119 | for (auto it = send_graph_.begin(); it != send_graph_.end();) { |
| 120 | if (it->first == thread) { |
| 121 | it = send_graph_.erase(it); |
| 122 | } else { |
| 123 | it->second.erase(thread); |
| 124 | ++it; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void ThreadManager::RegisterSendAndCheckForCycles(Thread* source, |
| 130 | Thread* target) { |
Tomas Gunnarsson | 0fd4c4e | 2020-09-04 14:33:25 | [diff] [blame] | 131 | RTC_DCHECK(source); |
| 132 | RTC_DCHECK(target); |
| 133 | |
Danil Chapovalov | c3c8934 | 2023-01-02 09:21:29 | [diff] [blame] | 134 | MutexLock cs(&crit_); |
Sebastian Jansson | da7267a | 2020-03-03 09:48:05 | [diff] [blame] | 135 | std::deque<Thread*> all_targets({target}); |
| 136 | // We check the pre-existing who-sends-to-who graph for any path from target |
| 137 | // to source. This loop is guaranteed to terminate because per the send graph |
| 138 | // invariant, there are no cycles in the graph. |
Jianjun Zhu | c33eeab | 2020-05-26 09:43:17 | [diff] [blame] | 139 | for (size_t i = 0; i < all_targets.size(); i++) { |
| 140 | const auto& targets = send_graph_[all_targets[i]]; |
Sebastian Jansson | da7267a | 2020-03-03 09:48:05 | [diff] [blame] | 141 | all_targets.insert(all_targets.end(), targets.begin(), targets.end()); |
| 142 | } |
| 143 | RTC_CHECK_EQ(absl::c_count(all_targets, source), 0) |
| 144 | << " send loop between " << source->name() << " and " << target->name(); |
| 145 | |
| 146 | // We may now insert source -> target without creating a cycle, since there |
| 147 | // was no path from target to source per the prior CHECK. |
| 148 | send_graph_[source].insert(target); |
| 149 | } |
| 150 | #endif |
| 151 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 152 | // static |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 153 | void ThreadManager::ProcessAllMessageQueuesForTesting() { |
| 154 | return Instance()->ProcessAllMessageQueuesInternal(); |
| 155 | } |
| 156 | |
| 157 | void ThreadManager::ProcessAllMessageQueuesInternal() { |
| 158 | // This works by posting a delayed message at the current time and waiting |
| 159 | // for it to be dispatched on all queues, which will ensure that all messages |
| 160 | // that came before it were also dispatched. |
Niels Möller | 7a66900 | 2022-06-27 07:47:02 | [diff] [blame] | 161 | std::atomic<int> queues_not_done(0); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 162 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 163 | { |
Danil Chapovalov | c3c8934 | 2023-01-02 09:21:29 | [diff] [blame] | 164 | MutexLock cs(&crit_); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 165 | for (Thread* queue : message_queues_) { |
| 166 | if (!queue->IsProcessingMessagesForTesting()) { |
| 167 | // If the queue is not processing messages, it can |
| 168 | // be ignored. If we tried to post a message to it, it would be dropped |
| 169 | // or ignored. |
| 170 | continue; |
| 171 | } |
Danil Chapovalov | 0bd1665 | 2022-08-24 16:35:45 | [diff] [blame] | 172 | queues_not_done.fetch_add(1); |
| 173 | // Whether the task is processed, or the thread is simply cleared, |
| 174 | // queues_not_done gets decremented. |
| 175 | absl::Cleanup sub = [&queues_not_done] { queues_not_done.fetch_sub(1); }; |
| 176 | // Post delayed task instead of regular task to wait for all delayed tasks |
| 177 | // that are ready for processing. |
| 178 | queue->PostDelayedTask([sub = std::move(sub)] {}, TimeDelta::Zero()); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | rtc::Thread* current = rtc::Thread::Current(); |
| 183 | // Note: One of the message queues may have been on this thread, which is |
| 184 | // why we can't synchronously wait for queues_not_done to go to 0; we need |
| 185 | // to process messages as well. |
Niels Möller | 7a66900 | 2022-06-27 07:47:02 | [diff] [blame] | 186 | while (queues_not_done.load() > 0) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 187 | if (current) { |
| 188 | current->ProcessMessages(0); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // static |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 194 | Thread* Thread::Current() { |
nisse | 7866cfe | 2017-04-26 08:45:31 | [diff] [blame] | 195 | ThreadManager* manager = ThreadManager::Instance(); |
| 196 | Thread* thread = manager->CurrentThread(); |
| 197 | |
nisse | 7866cfe | 2017-04-26 08:45:31 | [diff] [blame] | 198 | return thread; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | #if defined(WEBRTC_POSIX) |
Niels Möller | 98d26df | 2022-02-07 09:35:29 | [diff] [blame] | 202 | ThreadManager::ThreadManager() { |
Kári Tristan Helgason | 62b1345 | 2018-10-12 10:57:49 | [diff] [blame] | 203 | #if defined(WEBRTC_MAC) |
| 204 | InitCocoaMultiThreading(); |
| 205 | #endif |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 206 | pthread_key_create(&key_, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 207 | } |
| 208 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 209 | Thread* ThreadManager::CurrentThread() { |
| 210 | return static_cast<Thread*>(pthread_getspecific(key_)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 211 | } |
| 212 | |
Sebastian Jansson | 178a685 | 2020-01-14 10:12:26 | [diff] [blame] | 213 | void ThreadManager::SetCurrentThreadInternal(Thread* thread) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 214 | pthread_setspecific(key_, thread); |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | #if defined(WEBRTC_WIN) |
Niels Möller | 98d26df | 2022-02-07 09:35:29 | [diff] [blame] | 219 | ThreadManager::ThreadManager() : key_(TlsAlloc()) {} |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 220 | |
| 221 | Thread* ThreadManager::CurrentThread() { |
| 222 | return static_cast<Thread*>(TlsGetValue(key_)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 223 | } |
| 224 | |
Sebastian Jansson | 178a685 | 2020-01-14 10:12:26 | [diff] [blame] | 225 | void ThreadManager::SetCurrentThreadInternal(Thread* thread) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 226 | TlsSetValue(key_, thread); |
| 227 | } |
| 228 | #endif |
| 229 | |
Sebastian Jansson | 178a685 | 2020-01-14 10:12:26 | [diff] [blame] | 230 | void ThreadManager::SetCurrentThread(Thread* thread) { |
| 231 | #if RTC_DLOG_IS_ON |
| 232 | if (CurrentThread() && thread) { |
| 233 | RTC_DLOG(LS_ERROR) << "SetCurrentThread: Overwriting an existing value?"; |
| 234 | } |
| 235 | #endif // RTC_DLOG_IS_ON |
Tommi | 6866dc7 | 2020-05-15 08:11:56 | [diff] [blame] | 236 | |
| 237 | if (thread) { |
| 238 | thread->EnsureIsCurrentTaskQueue(); |
| 239 | } else { |
| 240 | Thread* current = CurrentThread(); |
| 241 | if (current) { |
| 242 | // The current thread is being cleared, e.g. as a result of |
| 243 | // UnwrapCurrent() being called or when a thread is being stopped |
| 244 | // (see PreRun()). This signals that the Thread instance is being detached |
| 245 | // from the thread, which also means that TaskQueue::Current() must not |
| 246 | // return a pointer to the Thread instance. |
| 247 | current->ClearCurrentTaskQueue(); |
| 248 | } |
| 249 | } |
| 250 | |
Sebastian Jansson | 178a685 | 2020-01-14 10:12:26 | [diff] [blame] | 251 | SetCurrentThreadInternal(thread); |
| 252 | } |
| 253 | |
| 254 | void rtc::ThreadManager::ChangeCurrentThreadForTest(rtc::Thread* thread) { |
| 255 | SetCurrentThreadInternal(thread); |
| 256 | } |
| 257 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 258 | Thread* ThreadManager::WrapCurrentThread() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 259 | Thread* result = CurrentThread(); |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 260 | if (nullptr == result) { |
Mirko Bonadei | e5f4c6b | 2021-01-15 09:41:01 | [diff] [blame] | 261 | result = new Thread(CreateDefaultSocketServer()); |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 262 | result->WrapCurrentWithThreadManager(this, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 263 | } |
| 264 | return result; |
| 265 | } |
| 266 | |
| 267 | void ThreadManager::UnwrapCurrentThread() { |
| 268 | Thread* t = CurrentThread(); |
| 269 | if (t && !(t->IsOwned())) { |
| 270 | t->UnwrapCurrent(); |
| 271 | delete t; |
| 272 | } |
| 273 | } |
| 274 | |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 | [diff] [blame] | 275 | Thread::ScopedDisallowBlockingCalls::ScopedDisallowBlockingCalls() |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 276 | : thread_(Thread::Current()), |
| 277 | previous_state_(thread_->SetAllowBlockingCalls(false)) {} |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 | [diff] [blame] | 278 | |
| 279 | Thread::ScopedDisallowBlockingCalls::~ScopedDisallowBlockingCalls() { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 280 | RTC_DCHECK(thread_->IsCurrent()); |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 | [diff] [blame] | 281 | thread_->SetAllowBlockingCalls(previous_state_); |
| 282 | } |
| 283 | |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 284 | #if RTC_DCHECK_IS_ON |
| 285 | Thread::ScopedCountBlockingCalls::ScopedCountBlockingCalls( |
| 286 | std::function<void(uint32_t, uint32_t)> callback) |
| 287 | : thread_(Thread::Current()), |
| 288 | base_blocking_call_count_(thread_->GetBlockingCallCount()), |
| 289 | base_could_be_blocking_call_count_( |
| 290 | thread_->GetCouldBeBlockingCallCount()), |
| 291 | result_callback_(std::move(callback)) {} |
| 292 | |
| 293 | Thread::ScopedCountBlockingCalls::~ScopedCountBlockingCalls() { |
Tomas Gunnarsson | 89f3dd5 | 2021-04-14 10:54:10 | [diff] [blame] | 294 | if (GetTotalBlockedCallCount() >= min_blocking_calls_for_callback_) { |
| 295 | result_callback_(GetBlockingCallCount(), GetCouldBeBlockingCallCount()); |
| 296 | } |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | uint32_t Thread::ScopedCountBlockingCalls::GetBlockingCallCount() const { |
| 300 | return thread_->GetBlockingCallCount() - base_blocking_call_count_; |
| 301 | } |
| 302 | |
| 303 | uint32_t Thread::ScopedCountBlockingCalls::GetCouldBeBlockingCallCount() const { |
| 304 | return thread_->GetCouldBeBlockingCallCount() - |
| 305 | base_could_be_blocking_call_count_; |
| 306 | } |
| 307 | |
| 308 | uint32_t Thread::ScopedCountBlockingCalls::GetTotalBlockedCallCount() const { |
| 309 | return GetBlockingCallCount() + GetCouldBeBlockingCallCount(); |
| 310 | } |
| 311 | #endif |
| 312 | |
Taylor Brandstetter | 0867260 | 2018-03-02 23:20:33 | [diff] [blame] | 313 | Thread::Thread(SocketServer* ss) : Thread(ss, /*do_init=*/true) {} |
danilchap | bebf54c | 2016-04-28 08:32:48 | [diff] [blame] | 314 | |
| 315 | Thread::Thread(std::unique_ptr<SocketServer> ss) |
Taylor Brandstetter | 0867260 | 2018-03-02 23:20:33 | [diff] [blame] | 316 | : Thread(std::move(ss), /*do_init=*/true) {} |
| 317 | |
| 318 | Thread::Thread(SocketServer* ss, bool do_init) |
Danil Chapovalov | 207f853 | 2022-08-24 10:19:46 | [diff] [blame] | 319 | : delayed_next_num_(0), |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 320 | fInitialized_(false), |
| 321 | fDestroyed_(false), |
| 322 | stop_(0), |
| 323 | ss_(ss) { |
| 324 | RTC_DCHECK(ss); |
| 325 | ss_->SetMessageQueue(this); |
Taylor Brandstetter | 0867260 | 2018-03-02 23:20:33 | [diff] [blame] | 326 | SetName("Thread", this); // default name |
| 327 | if (do_init) { |
| 328 | DoInit(); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | Thread::Thread(std::unique_ptr<SocketServer> ss, bool do_init) |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 333 | : Thread(ss.get(), do_init) { |
| 334 | own_ss_ = std::move(ss); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | Thread::~Thread() { |
| 338 | Stop(); |
jbauch | 25d1f28 | 2016-02-05 08:25:02 | [diff] [blame] | 339 | DoDestroy(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 340 | } |
| 341 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 342 | void Thread::DoInit() { |
| 343 | if (fInitialized_) { |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | fInitialized_ = true; |
| 348 | ThreadManager::Add(this); |
| 349 | } |
| 350 | |
| 351 | void Thread::DoDestroy() { |
| 352 | if (fDestroyed_) { |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | fDestroyed_ = true; |
| 357 | // The signal is done from here to ensure |
| 358 | // that it always gets called when the queue |
| 359 | // is going away. |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 360 | if (ss_) { |
| 361 | ss_->SetMessageQueue(nullptr); |
| 362 | } |
Niels Möller | 9bd2457 | 2021-04-19 10:18:27 | [diff] [blame] | 363 | ThreadManager::Remove(this); |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 364 | // Clear. |
Markus Handell | 82da932 | 2022-12-16 14:50:24 | [diff] [blame] | 365 | CurrentTaskQueueSetter set_current(this); |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 366 | messages_ = {}; |
| 367 | delayed_messages_ = {}; |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | SocketServer* Thread::socketserver() { |
| 371 | return ss_; |
| 372 | } |
| 373 | |
| 374 | void Thread::WakeUpSocketServer() { |
| 375 | ss_->WakeUp(); |
| 376 | } |
| 377 | |
| 378 | void Thread::Quit() { |
Niels Möller | 7a66900 | 2022-06-27 07:47:02 | [diff] [blame] | 379 | stop_.store(1, std::memory_order_release); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 380 | WakeUpSocketServer(); |
| 381 | } |
| 382 | |
| 383 | bool Thread::IsQuitting() { |
Niels Möller | 7a66900 | 2022-06-27 07:47:02 | [diff] [blame] | 384 | return stop_.load(std::memory_order_acquire) != 0; |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void Thread::Restart() { |
Niels Möller | 7a66900 | 2022-06-27 07:47:02 | [diff] [blame] | 388 | stop_.store(0, std::memory_order_release); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 389 | } |
| 390 | |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 391 | absl::AnyInvocable<void() &&> Thread::Get(int cmsWait) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 392 | // Get w/wait + timer scan / dispatch + socket / event multiplexer dispatch |
| 393 | |
| 394 | int64_t cmsTotal = cmsWait; |
| 395 | int64_t cmsElapsed = 0; |
| 396 | int64_t msStart = TimeMillis(); |
| 397 | int64_t msCurrent = msStart; |
| 398 | while (true) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 399 | // Check for posted events |
| 400 | int64_t cmsDelayNext = kForever; |
Danil Chapovalov | 0bd1665 | 2022-08-24 16:35:45 | [diff] [blame] | 401 | { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 402 | // All queue operations need to be locked, but nothing else in this loop |
Danil Chapovalov | 3ab76d9 | 2022-09-23 10:39:21 | [diff] [blame] | 403 | // can happen while holding the `mutex_`. |
| 404 | MutexLock lock(&mutex_); |
Danil Chapovalov | 0bd1665 | 2022-08-24 16:35:45 | [diff] [blame] | 405 | // Check for delayed messages that have been triggered and calculate the |
| 406 | // next trigger time. |
| 407 | while (!delayed_messages_.empty()) { |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 408 | if (msCurrent < delayed_messages_.top().run_time_ms) { |
Danil Chapovalov | 0bd1665 | 2022-08-24 16:35:45 | [diff] [blame] | 409 | cmsDelayNext = |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 410 | TimeDiff(delayed_messages_.top().run_time_ms, msCurrent); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 411 | break; |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 412 | } |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 413 | messages_.push(std::move(delayed_messages_.top().functor)); |
Danil Chapovalov | 0bd1665 | 2022-08-24 16:35:45 | [diff] [blame] | 414 | delayed_messages_.pop(); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 415 | } |
Danil Chapovalov | 0bd1665 | 2022-08-24 16:35:45 | [diff] [blame] | 416 | // Pull a message off the message queue, if available. |
| 417 | if (!messages_.empty()) { |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 418 | absl::AnyInvocable<void()&&> task = std::move(messages_.front()); |
| 419 | messages_.pop(); |
| 420 | return task; |
Danil Chapovalov | 0bd1665 | 2022-08-24 16:35:45 | [diff] [blame] | 421 | } |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | if (IsQuitting()) |
| 425 | break; |
| 426 | |
| 427 | // Which is shorter, the delay wait or the asked wait? |
| 428 | |
| 429 | int64_t cmsNext; |
| 430 | if (cmsWait == kForever) { |
| 431 | cmsNext = cmsDelayNext; |
| 432 | } else { |
| 433 | cmsNext = std::max<int64_t>(0, cmsTotal - cmsElapsed); |
| 434 | if ((cmsDelayNext != kForever) && (cmsDelayNext < cmsNext)) |
| 435 | cmsNext = cmsDelayNext; |
| 436 | } |
| 437 | |
| 438 | { |
| 439 | // Wait and multiplex in the meantime |
Markus Handell | 9a21c49 | 2022-08-25 11:40:13 | [diff] [blame] | 440 | if (!ss_->Wait(cmsNext == kForever ? SocketServer::kForever |
| 441 | : webrtc::TimeDelta::Millis(cmsNext), |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 442 | /*process_io=*/true)) |
| 443 | return nullptr; |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | // If the specified timeout expired, return |
| 447 | |
| 448 | msCurrent = TimeMillis(); |
| 449 | cmsElapsed = TimeDiff(msCurrent, msStart); |
| 450 | if (cmsWait != kForever) { |
| 451 | if (cmsElapsed >= cmsWait) |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 452 | return nullptr; |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 453 | } |
| 454 | } |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 455 | return nullptr; |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 456 | } |
| 457 | |
Markus Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 458 | void Thread::PostTaskImpl(absl::AnyInvocable<void() &&> task, |
| 459 | const PostTaskTraits& traits, |
| 460 | const webrtc::Location& location) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 461 | if (IsQuitting()) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 462 | return; |
| 463 | } |
| 464 | |
| 465 | // Keep thread safe |
| 466 | // Add the message to the end of the queue |
| 467 | // Signal for the multiplexer to return |
| 468 | |
| 469 | { |
Danil Chapovalov | 3ab76d9 | 2022-09-23 10:39:21 | [diff] [blame] | 470 | MutexLock lock(&mutex_); |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 471 | messages_.push(std::move(task)); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 472 | } |
| 473 | WakeUpSocketServer(); |
| 474 | } |
| 475 | |
Markus Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 476 | void Thread::PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task, |
| 477 | webrtc::TimeDelta delay, |
| 478 | const PostDelayedTaskTraits& traits, |
| 479 | const webrtc::Location& location) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 480 | if (IsQuitting()) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 481 | return; |
| 482 | } |
| 483 | |
| 484 | // Keep thread safe |
| 485 | // Add to the priority queue. Gets sorted soonest first. |
| 486 | // Signal for the multiplexer to return. |
| 487 | |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 488 | int64_t delay_ms = delay.RoundUpTo(webrtc::TimeDelta::Millis(1)).ms<int>(); |
| 489 | int64_t run_time_ms = TimeAfter(delay_ms); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 490 | { |
Danil Chapovalov | 3ab76d9 | 2022-09-23 10:39:21 | [diff] [blame] | 491 | MutexLock lock(&mutex_); |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 492 | delayed_messages_.push({.delay_ms = delay_ms, |
| 493 | .run_time_ms = run_time_ms, |
| 494 | .message_number = delayed_next_num_, |
| 495 | .functor = std::move(task)}); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 496 | // If this message queue processes 1 message every millisecond for 50 days, |
| 497 | // we will wrap this number. Even then, only messages with identical times |
| 498 | // will be misordered, and then only briefly. This is probably ok. |
Sebastian Jansson | 61380c0 | 2020-01-17 13:46:08 | [diff] [blame] | 499 | ++delayed_next_num_; |
| 500 | RTC_DCHECK_NE(0, delayed_next_num_); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 501 | } |
| 502 | WakeUpSocketServer(); |
| 503 | } |
| 504 | |
| 505 | int Thread::GetDelay() { |
Danil Chapovalov | 3ab76d9 | 2022-09-23 10:39:21 | [diff] [blame] | 506 | MutexLock lock(&mutex_); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 507 | |
Sebastian Jansson | 61380c0 | 2020-01-17 13:46:08 | [diff] [blame] | 508 | if (!messages_.empty()) |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 509 | return 0; |
| 510 | |
Sebastian Jansson | 61380c0 | 2020-01-17 13:46:08 | [diff] [blame] | 511 | if (!delayed_messages_.empty()) { |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 512 | int delay = TimeUntil(delayed_messages_.top().run_time_ms); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 513 | if (delay < 0) |
| 514 | delay = 0; |
| 515 | return delay; |
| 516 | } |
| 517 | |
| 518 | return kForever; |
| 519 | } |
| 520 | |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 521 | void Thread::Dispatch(absl::AnyInvocable<void() &&> task) { |
| 522 | TRACE_EVENT0("webrtc", "Thread::Dispatch"); |
Harald Alvestrand | ba69442 | 2021-01-27 21:52:14 | [diff] [blame] | 523 | RTC_DCHECK_RUN_ON(this); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 524 | int64_t start_time = TimeMillis(); |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 525 | std::move(task)(); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 526 | int64_t end_time = TimeMillis(); |
| 527 | int64_t diff = TimeDiff(end_time, start_time); |
Harald Alvestrand | ba69442 | 2021-01-27 21:52:14 | [diff] [blame] | 528 | if (diff >= dispatch_warning_ms_) { |
| 529 | RTC_LOG(LS_INFO) << "Message to " << name() << " took " << diff |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 530 | << "ms to dispatch."; |
Harald Alvestrand | ba69442 | 2021-01-27 21:52:14 | [diff] [blame] | 531 | // To avoid log spew, move the warning limit to only give warning |
| 532 | // for delays that are larger than the one observed. |
| 533 | dispatch_warning_ms_ = diff + 1; |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
nisse | 7866cfe | 2017-04-26 08:45:31 | [diff] [blame] | 537 | bool Thread::IsCurrent() const { |
| 538 | return ThreadManager::Instance()->CurrentThread() == this; |
| 539 | } |
| 540 | |
danilchap | bebf54c | 2016-04-28 08:32:48 | [diff] [blame] | 541 | std::unique_ptr<Thread> Thread::CreateWithSocketServer() { |
Mirko Bonadei | e5f4c6b | 2021-01-15 09:41:01 | [diff] [blame] | 542 | return std::unique_ptr<Thread>(new Thread(CreateDefaultSocketServer())); |
danilchap | bebf54c | 2016-04-28 08:32:48 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | std::unique_ptr<Thread> Thread::Create() { |
| 546 | return std::unique_ptr<Thread>( |
| 547 | new Thread(std::unique_ptr<SocketServer>(new NullSocketServer()))); |
| 548 | } |
| 549 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 550 | bool Thread::SleepMs(int milliseconds) { |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 | [diff] [blame] | 551 | AssertBlockingIsAllowedOnCurrentThread(); |
| 552 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 553 | #if defined(WEBRTC_WIN) |
| 554 | ::Sleep(milliseconds); |
| 555 | return true; |
| 556 | #else |
| 557 | // POSIX has both a usleep() and a nanosleep(), but the former is deprecated, |
| 558 | // so we use nanosleep() even though it has greater precision than necessary. |
| 559 | struct timespec ts; |
| 560 | ts.tv_sec = milliseconds / 1000; |
| 561 | ts.tv_nsec = (milliseconds % 1000) * 1000000; |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 562 | int ret = nanosleep(&ts, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 563 | if (ret != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 564 | RTC_LOG_ERR(LS_WARNING) << "nanosleep() returning early"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 565 | return false; |
| 566 | } |
| 567 | return true; |
| 568 | #endif |
| 569 | } |
| 570 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 571 | bool Thread::SetName(absl::string_view name, const void* obj) { |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 572 | RTC_DCHECK(!IsRunning()); |
| 573 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 574 | name_ = std::string(name); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 575 | if (obj) { |
Niels Möller | aba0633 | 2018-10-16 13:14:15 | [diff] [blame] | 576 | // The %p specifier typically produce at most 16 hex digits, possibly with a |
| 577 | // 0x prefix. But format is implementation defined, so add some margin. |
| 578 | char buf[30]; |
| 579 | snprintf(buf, sizeof(buf), " 0x%p", obj); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 580 | name_ += buf; |
| 581 | } |
| 582 | return true; |
| 583 | } |
| 584 | |
Harald Alvestrand | ba69442 | 2021-01-27 21:52:14 | [diff] [blame] | 585 | void Thread::SetDispatchWarningMs(int deadline) { |
| 586 | if (!IsCurrent()) { |
Danil Chapovalov | 4bcf809 | 2022-07-06 17:42:34 | [diff] [blame] | 587 | PostTask([this, deadline]() { SetDispatchWarningMs(deadline); }); |
Harald Alvestrand | ba69442 | 2021-01-27 21:52:14 | [diff] [blame] | 588 | return; |
| 589 | } |
| 590 | RTC_DCHECK_RUN_ON(this); |
| 591 | dispatch_warning_ms_ = deadline; |
| 592 | } |
| 593 | |
Niels Möller | d2e5013 | 2019-06-11 07:24:14 | [diff] [blame] | 594 | bool Thread::Start() { |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 595 | RTC_DCHECK(!IsRunning()); |
| 596 | |
| 597 | if (IsRunning()) |
| 598 | return false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 599 | |
André Susano Pinto | 02a5797 | 2016-07-22 11:30:05 | [diff] [blame] | 600 | Restart(); // reset IsQuitting() if the thread is being restarted |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 601 | |
| 602 | // Make sure that ThreadManager is created on the main thread before |
| 603 | // we start a new thread. |
| 604 | ThreadManager::Instance(); |
| 605 | |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 606 | owned_ = true; |
| 607 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 608 | #if defined(WEBRTC_WIN) |
Niels Möller | d2e5013 | 2019-06-11 07:24:14 | [diff] [blame] | 609 | thread_ = CreateThread(nullptr, 0, PreRun, this, 0, &thread_id_); |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 610 | if (!thread_) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 611 | return false; |
| 612 | } |
| 613 | #elif defined(WEBRTC_POSIX) |
| 614 | pthread_attr_t attr; |
| 615 | pthread_attr_init(&attr); |
| 616 | |
Niels Möller | d2e5013 | 2019-06-11 07:24:14 | [diff] [blame] | 617 | int error_code = pthread_create(&thread_, &attr, PreRun, this); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 618 | if (0 != error_code) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 619 | RTC_LOG(LS_ERROR) << "Unable to create pthread, error " << error_code; |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 620 | thread_ = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 621 | return false; |
| 622 | } |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 623 | RTC_DCHECK(thread_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 624 | #endif |
| 625 | return true; |
| 626 | } |
| 627 | |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 628 | bool Thread::WrapCurrent() { |
| 629 | return WrapCurrentWithThreadManager(ThreadManager::Instance(), true); |
| 630 | } |
| 631 | |
| 632 | void Thread::UnwrapCurrent() { |
| 633 | // Clears the platform-specific thread-specific storage. |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 634 | ThreadManager::Instance()->SetCurrentThread(nullptr); |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 635 | #if defined(WEBRTC_WIN) |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 636 | if (thread_ != nullptr) { |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 637 | if (!CloseHandle(thread_)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 638 | RTC_LOG_GLE(LS_ERROR) |
| 639 | << "When unwrapping thread, failed to close handle."; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 640 | } |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 641 | thread_ = nullptr; |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 642 | thread_id_ = 0; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 643 | } |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 644 | #elif defined(WEBRTC_POSIX) |
| 645 | thread_ = 0; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 646 | #endif |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | void Thread::SafeWrapCurrent() { |
| 650 | WrapCurrentWithThreadManager(ThreadManager::Instance(), false); |
| 651 | } |
| 652 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 653 | void Thread::Join() { |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 654 | if (!IsRunning()) |
| 655 | return; |
| 656 | |
| 657 | RTC_DCHECK(!IsCurrent()); |
| 658 | if (Current() && !Current()->blocking_calls_allowed_) { |
| 659 | RTC_LOG(LS_WARNING) << "Waiting for the thread to join, " |
Jonas Olsson | b2b2031 | 2020-01-14 11:11:31 | [diff] [blame] | 660 | "but blocking calls have been disallowed"; |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 661 | } |
jiayl@webrtc.org | 1fd362c | 2014-09-26 16:57:07 | [diff] [blame] | 662 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 663 | #if defined(WEBRTC_WIN) |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 664 | RTC_DCHECK(thread_ != nullptr); |
| 665 | WaitForSingleObject(thread_, INFINITE); |
| 666 | CloseHandle(thread_); |
| 667 | thread_ = nullptr; |
| 668 | thread_id_ = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 669 | #elif defined(WEBRTC_POSIX) |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 670 | pthread_join(thread_, nullptr); |
| 671 | thread_ = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 672 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 673 | } |
| 674 | |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 | [diff] [blame] | 675 | bool Thread::SetAllowBlockingCalls(bool allow) { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 676 | RTC_DCHECK(IsCurrent()); |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 | [diff] [blame] | 677 | bool previous = blocking_calls_allowed_; |
| 678 | blocking_calls_allowed_ = allow; |
| 679 | return previous; |
| 680 | } |
| 681 | |
| 682 | // static |
| 683 | void Thread::AssertBlockingIsAllowedOnCurrentThread() { |
tfarina | a41ab93 | 2015-10-30 23:08:48 | [diff] [blame] | 684 | #if !defined(NDEBUG) |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 | [diff] [blame] | 685 | Thread* current = Thread::Current(); |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 686 | RTC_DCHECK(!current || current->blocking_calls_allowed_); |
henrike@webrtc.org | 92a9bac | 2014-07-14 22:03:57 | [diff] [blame] | 687 | #endif |
| 688 | } |
| 689 | |
deadbeef | dc20e26 | 2017-01-31 23:10:44 | [diff] [blame] | 690 | // static |
| 691 | #if defined(WEBRTC_WIN) |
| 692 | DWORD WINAPI Thread::PreRun(LPVOID pv) { |
| 693 | #else |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 694 | void* Thread::PreRun(void* pv) { |
deadbeef | dc20e26 | 2017-01-31 23:10:44 | [diff] [blame] | 695 | #endif |
Niels Möller | d2e5013 | 2019-06-11 07:24:14 | [diff] [blame] | 696 | Thread* thread = static_cast<Thread*>(pv); |
| 697 | ThreadManager::Instance()->SetCurrentThread(thread); |
| 698 | rtc::SetCurrentThreadName(thread->name_.c_str()); |
Kári Tristan Helgason | 62b1345 | 2018-10-12 10:57:49 | [diff] [blame] | 699 | #if defined(WEBRTC_MAC) |
| 700 | ScopedAutoReleasePool pool; |
| 701 | #endif |
Niels Möller | d2e5013 | 2019-06-11 07:24:14 | [diff] [blame] | 702 | thread->Run(); |
| 703 | |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 704 | ThreadManager::Instance()->SetCurrentThread(nullptr); |
kthelgason | de6adbe | 2017-02-22 08:42:11 | [diff] [blame] | 705 | #ifdef WEBRTC_WIN |
| 706 | return 0; |
| 707 | #else |
| 708 | return nullptr; |
| 709 | #endif |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 710 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 711 | |
| 712 | void Thread::Run() { |
| 713 | ProcessMessages(kForever); |
| 714 | } |
| 715 | |
| 716 | bool Thread::IsOwned() { |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 717 | RTC_DCHECK(IsRunning()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 718 | return owned_; |
| 719 | } |
| 720 | |
| 721 | void Thread::Stop() { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 722 | Thread::Quit(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 723 | Join(); |
| 724 | } |
| 725 | |
Markus Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 726 | void Thread::BlockingCallImpl(rtc::FunctionView<void()> functor, |
| 727 | const webrtc::Location& location) { |
Danil Chapovalov | ca1454a | 2022-09-13 11:12:25 | [diff] [blame] | 728 | TRACE_EVENT0("webrtc", "Thread::BlockingCall"); |
| 729 | |
Sebastian Jansson | 5d9b964 | 2020-01-17 12:10:54 | [diff] [blame] | 730 | RTC_DCHECK(!IsQuitting()); |
André Susano Pinto | 02a5797 | 2016-07-22 11:30:05 | [diff] [blame] | 731 | if (IsQuitting()) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 732 | return; |
| 733 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 734 | if (IsCurrent()) { |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 735 | #if RTC_DCHECK_IS_ON |
Artem Titov | 1573716 | 2021-05-25 09:17:07 | [diff] [blame] | 736 | RTC_DCHECK(this->IsInvokeToThreadAllowed(this)); |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 737 | RTC_DCHECK_RUN_ON(this); |
| 738 | could_be_blocking_call_count_++; |
| 739 | #endif |
Danil Chapovalov | ca1454a | 2022-09-13 11:12:25 | [diff] [blame] | 740 | functor(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 741 | return; |
| 742 | } |
| 743 | |
Sebastian Jansson | da7267a | 2020-03-03 09:48:05 | [diff] [blame] | 744 | #if RTC_DCHECK_IS_ON |
Danil Chapovalov | 2ded55e | 2023-01-27 15:06:37 | [diff] [blame] | 745 | if (Thread* current_thread = Thread::Current()) { |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 746 | RTC_DCHECK_RUN_ON(current_thread); |
Danil Chapovalov | 2ded55e | 2023-01-27 15:06:37 | [diff] [blame] | 747 | RTC_DCHECK(current_thread->blocking_calls_allowed_); |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 748 | current_thread->blocking_call_count_++; |
Tomas Gunnarsson | 0fd4c4e | 2020-09-04 14:33:25 | [diff] [blame] | 749 | RTC_DCHECK(current_thread->IsInvokeToThreadAllowed(this)); |
| 750 | ThreadManager::Instance()->RegisterSendAndCheckForCycles(current_thread, |
| 751 | this); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 752 | } |
Tomas Gunnarsson | 0fd4c4e | 2020-09-04 14:33:25 | [diff] [blame] | 753 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 754 | |
Danil Chapovalov | 2ded55e | 2023-01-27 15:06:37 | [diff] [blame] | 755 | Event done; |
| 756 | absl::Cleanup cleanup = [&done] { done.Set(); }; |
Danil Chapovalov | ca1454a | 2022-09-13 11:12:25 | [diff] [blame] | 757 | PostTask([functor, cleanup = std::move(cleanup)] { functor(); }); |
Danil Chapovalov | 2ded55e | 2023-01-27 15:06:37 | [diff] [blame] | 758 | done.Wait(Event::kForever); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 759 | } |
| 760 | |
Tommi | 6866dc7 | 2020-05-15 08:11:56 | [diff] [blame] | 761 | // Called by the ThreadManager when being set as the current thread. |
| 762 | void Thread::EnsureIsCurrentTaskQueue() { |
| 763 | task_queue_registration_ = |
| 764 | std::make_unique<TaskQueueBase::CurrentTaskQueueSetter>(this); |
| 765 | } |
| 766 | |
| 767 | // Called by the ThreadManager when being set as the current thread. |
| 768 | void Thread::ClearCurrentTaskQueue() { |
| 769 | task_queue_registration_.reset(); |
| 770 | } |
| 771 | |
Artem Titov | dfc5f0d | 2020-07-03 10:09:26 | [diff] [blame] | 772 | void Thread::AllowInvokesToThread(Thread* thread) { |
Mirko Bonadei | 481e345 | 2021-07-30 11:57:25 | [diff] [blame] | 773 | #if (!defined(NDEBUG) || RTC_DCHECK_IS_ON) |
Artem Titov | dfc5f0d | 2020-07-03 10:09:26 | [diff] [blame] | 774 | if (!IsCurrent()) { |
Danil Chapovalov | 5286dcf | 2022-07-18 15:04:56 | [diff] [blame] | 775 | PostTask([thread, this]() { AllowInvokesToThread(thread); }); |
Artem Titov | dfc5f0d | 2020-07-03 10:09:26 | [diff] [blame] | 776 | return; |
| 777 | } |
| 778 | RTC_DCHECK_RUN_ON(this); |
| 779 | allowed_threads_.push_back(thread); |
| 780 | invoke_policy_enabled_ = true; |
| 781 | #endif |
| 782 | } |
| 783 | |
| 784 | void Thread::DisallowAllInvokes() { |
Mirko Bonadei | 481e345 | 2021-07-30 11:57:25 | [diff] [blame] | 785 | #if (!defined(NDEBUG) || RTC_DCHECK_IS_ON) |
Artem Titov | dfc5f0d | 2020-07-03 10:09:26 | [diff] [blame] | 786 | if (!IsCurrent()) { |
Danil Chapovalov | 5286dcf | 2022-07-18 15:04:56 | [diff] [blame] | 787 | PostTask([this]() { DisallowAllInvokes(); }); |
Artem Titov | dfc5f0d | 2020-07-03 10:09:26 | [diff] [blame] | 788 | return; |
| 789 | } |
| 790 | RTC_DCHECK_RUN_ON(this); |
| 791 | allowed_threads_.clear(); |
| 792 | invoke_policy_enabled_ = true; |
| 793 | #endif |
| 794 | } |
| 795 | |
Tommi | fe04164 | 2021-04-07 08:08:28 | [diff] [blame] | 796 | #if RTC_DCHECK_IS_ON |
| 797 | uint32_t Thread::GetBlockingCallCount() const { |
| 798 | RTC_DCHECK_RUN_ON(this); |
| 799 | return blocking_call_count_; |
| 800 | } |
| 801 | uint32_t Thread::GetCouldBeBlockingCallCount() const { |
| 802 | RTC_DCHECK_RUN_ON(this); |
| 803 | return could_be_blocking_call_count_; |
| 804 | } |
| 805 | #endif |
| 806 | |
Artem Titov | dfc5f0d | 2020-07-03 10:09:26 | [diff] [blame] | 807 | // Returns true if no policies added or if there is at least one policy |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 808 | // that permits invocation to `target` thread. |
Artem Titov | dfc5f0d | 2020-07-03 10:09:26 | [diff] [blame] | 809 | bool Thread::IsInvokeToThreadAllowed(rtc::Thread* target) { |
Mirko Bonadei | 481e345 | 2021-07-30 11:57:25 | [diff] [blame] | 810 | #if (!defined(NDEBUG) || RTC_DCHECK_IS_ON) |
Artem Titov | dfc5f0d | 2020-07-03 10:09:26 | [diff] [blame] | 811 | RTC_DCHECK_RUN_ON(this); |
| 812 | if (!invoke_policy_enabled_) { |
| 813 | return true; |
| 814 | } |
| 815 | for (const auto* thread : allowed_threads_) { |
| 816 | if (thread == target) { |
| 817 | return true; |
| 818 | } |
| 819 | } |
| 820 | return false; |
| 821 | #else |
| 822 | return true; |
| 823 | #endif |
| 824 | } |
| 825 | |
Danil Chapovalov | 912b3b8 | 2019-11-22 14:52:40 | [diff] [blame] | 826 | void Thread::Delete() { |
| 827 | Stop(); |
| 828 | delete this; |
| 829 | } |
| 830 | |
Niels Möller | 8909a63 | 2018-09-06 06:42:44 | [diff] [blame] | 831 | bool Thread::IsProcessingMessagesForTesting() { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 832 | return (owned_ || IsCurrent()) && !IsQuitting(); |
Niels Möller | 8909a63 | 2018-09-06 06:42:44 | [diff] [blame] | 833 | } |
| 834 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 835 | bool Thread::ProcessMessages(int cmsLoop) { |
deadbeef | 22e0814 | 2017-06-12 21:30:28 | [diff] [blame] | 836 | // Using ProcessMessages with a custom clock for testing and a time greater |
| 837 | // than 0 doesn't work, since it's not guaranteed to advance the custom |
| 838 | // clock's time, and may get stuck in an infinite loop. |
| 839 | RTC_DCHECK(GetClockForTesting() == nullptr || cmsLoop == 0 || |
| 840 | cmsLoop == kForever); |
Honghai Zhang | 82d7862 | 2016-05-06 18:29:15 | [diff] [blame] | 841 | int64_t msEnd = (kForever == cmsLoop) ? 0 : TimeAfter(cmsLoop); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 842 | int cmsNext = cmsLoop; |
| 843 | |
| 844 | while (true) { |
Kári Tristan Helgason | 62b1345 | 2018-10-12 10:57:49 | [diff] [blame] | 845 | #if defined(WEBRTC_MAC) |
| 846 | ScopedAutoReleasePool pool; |
| 847 | #endif |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 848 | absl::AnyInvocable<void()&&> task = Get(cmsNext); |
| 849 | if (!task) |
kthelgason | de6adbe | 2017-02-22 08:42:11 | [diff] [blame] | 850 | return !IsQuitting(); |
Danil Chapovalov | d44e341 | 2022-09-16 15:26:10 | [diff] [blame] | 851 | Dispatch(std::move(task)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 852 | |
kthelgason | de6adbe | 2017-02-22 08:42:11 | [diff] [blame] | 853 | if (cmsLoop != kForever) { |
| 854 | cmsNext = static_cast<int>(TimeUntil(msEnd)); |
| 855 | if (cmsNext < 0) |
| 856 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 861 | bool Thread::WrapCurrentWithThreadManager(ThreadManager* thread_manager, |
| 862 | bool need_synchronize_access) { |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 863 | RTC_DCHECK(!IsRunning()); |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 864 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 865 | #if defined(WEBRTC_WIN) |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 866 | if (need_synchronize_access) { |
| 867 | // We explicitly ask for no rights other than synchronization. |
| 868 | // This gives us the best chance of succeeding. |
| 869 | thread_ = OpenThread(SYNCHRONIZE, FALSE, GetCurrentThreadId()); |
| 870 | if (!thread_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 871 | RTC_LOG_GLE(LS_ERROR) << "Unable to get handle to thread."; |
jiayl@webrtc.org | ba737cb | 2014-09-18 16:45:21 | [diff] [blame] | 872 | return false; |
| 873 | } |
| 874 | thread_id_ = GetCurrentThreadId(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 875 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 876 | #elif defined(WEBRTC_POSIX) |
| 877 | thread_ = pthread_self(); |
| 878 | #endif |
| 879 | owned_ = false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 880 | thread_manager->SetCurrentThread(this); |
| 881 | return true; |
| 882 | } |
| 883 | |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 884 | bool Thread::IsRunning() { |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 885 | #if defined(WEBRTC_WIN) |
| 886 | return thread_ != nullptr; |
| 887 | #elif defined(WEBRTC_POSIX) |
| 888 | return thread_ != 0; |
| 889 | #endif |
| 890 | } |
| 891 | |
Taylor Brandstetter | 0867260 | 2018-03-02 23:20:33 | [diff] [blame] | 892 | AutoThread::AutoThread() |
Mirko Bonadei | e5f4c6b | 2021-01-15 09:41:01 | [diff] [blame] | 893 | : Thread(CreateDefaultSocketServer(), /*do_init=*/false) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 894 | if (!ThreadManager::Instance()->CurrentThread()) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 895 | // DoInit registers with ThreadManager. Do that only if we intend to |
Niels Möller | 5a8f860 | 2019-06-12 09:30:59 | [diff] [blame] | 896 | // be rtc::Thread::Current(), otherwise ProcessAllMessageQueuesInternal will |
| 897 | // post a message to a queue that no running thread is serving. |
| 898 | DoInit(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 899 | ThreadManager::Instance()->SetCurrentThread(this); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | AutoThread::~AutoThread() { |
| 904 | Stop(); |
Steve Anton | 3b80aac | 2017-10-19 17:17:12 | [diff] [blame] | 905 | DoDestroy(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 906 | if (ThreadManager::Instance()->CurrentThread() == this) { |
deadbeef | 37f5ecf | 2017-02-27 22:06:41 | [diff] [blame] | 907 | ThreadManager::Instance()->SetCurrentThread(nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
nisse | 7eaa4ea | 2017-05-08 12:25:41 | [diff] [blame] | 911 | AutoSocketServerThread::AutoSocketServerThread(SocketServer* ss) |
Taylor Brandstetter | 0867260 | 2018-03-02 23:20:33 | [diff] [blame] | 912 | : Thread(ss, /*do_init=*/false) { |
| 913 | DoInit(); |
nisse | 7eaa4ea | 2017-05-08 12:25:41 | [diff] [blame] | 914 | old_thread_ = ThreadManager::Instance()->CurrentThread(); |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 915 | // Temporarily set the current thread to nullptr so that we can keep checks |
| 916 | // around that catch unintentional pointer overwrites. |
| 917 | rtc::ThreadManager::Instance()->SetCurrentThread(nullptr); |
nisse | 7eaa4ea | 2017-05-08 12:25:41 | [diff] [blame] | 918 | rtc::ThreadManager::Instance()->SetCurrentThread(this); |
| 919 | if (old_thread_) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 920 | ThreadManager::Remove(old_thread_); |
nisse | 7eaa4ea | 2017-05-08 12:25:41 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
| 924 | AutoSocketServerThread::~AutoSocketServerThread() { |
| 925 | RTC_DCHECK(ThreadManager::Instance()->CurrentThread() == this); |
Steve Anton | 3b80aac | 2017-10-19 17:17:12 | [diff] [blame] | 926 | // Stop and destroy the thread before clearing it as the current thread. |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 927 | // Sometimes there are messages left in the Thread that will be |
Steve Anton | 3b80aac | 2017-10-19 17:17:12 | [diff] [blame] | 928 | // destroyed by DoDestroy, and sometimes the destructors of the message and/or |
| 929 | // its contents rely on this thread still being set as the current thread. |
| 930 | Stop(); |
| 931 | DoDestroy(); |
Tommi | 5149242 | 2017-12-04 14:18:23 | [diff] [blame] | 932 | rtc::ThreadManager::Instance()->SetCurrentThread(nullptr); |
nisse | 7eaa4ea | 2017-05-08 12:25:41 | [diff] [blame] | 933 | rtc::ThreadManager::Instance()->SetCurrentThread(old_thread_); |
| 934 | if (old_thread_) { |
Sebastian Jansson | 6ea2c6a | 2020-01-13 13:07:22 | [diff] [blame] | 935 | ThreadManager::Add(old_thread_); |
nisse | 7eaa4ea | 2017-05-08 12:25:41 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 939 | } // namespace rtc |