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