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