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