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