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