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