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