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 | #ifndef RTC_BASE_THREAD_H_ |
| 12 | #define RTC_BASE_THREAD_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | #include <list> |
Sebastian Jansson | da7267a | 2020-03-03 10:48:05 +0100 | [diff] [blame] | 17 | #include <map> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | #include <memory> |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 19 | #include <queue> |
Sebastian Jansson | da7267a | 2020-03-03 10:48:05 +0100 | [diff] [blame] | 20 | #include <set> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 21 | #include <string> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 22 | #include <type_traits> |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 23 | #include <vector> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 25 | #if defined(WEBRTC_POSIX) |
| 26 | #include <pthread.h> |
| 27 | #endif |
Henrik Boström | 2deee4b | 2022-01-20 11:58:05 +0100 | [diff] [blame^] | 28 | #include "absl/base/attributes.h" |
Danil Chapovalov | 8931345 | 2019-11-29 12:56:43 +0100 | [diff] [blame] | 29 | #include "api/function_view.h" |
Danil Chapovalov | 912b3b8 | 2019-11-22 15:52:40 +0100 | [diff] [blame] | 30 | #include "api/task_queue/queued_task.h" |
| 31 | #include "api/task_queue/task_queue_base.h" |
Mirko Bonadei | 481e345 | 2021-07-30 13:57:25 +0200 | [diff] [blame] | 32 | #include "rtc_base/checks.h" |
Markus Handell | 3cb525b | 2020-07-16 16:16:09 +0200 | [diff] [blame] | 33 | #include "rtc_base/deprecated/recursive_critical_section.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 34 | #include "rtc_base/location.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 35 | #include "rtc_base/message_handler.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 36 | #include "rtc_base/platform_thread_types.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 37 | #include "rtc_base/socket_server.h" |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 38 | #include "rtc_base/system/rtc_export.h" |
Henrik Boström | 2deee4b | 2022-01-20 11:58:05 +0100 | [diff] [blame^] | 39 | #include "rtc_base/task_utils/to_queued_task.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 40 | #include "rtc_base/thread_annotations.h" |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 41 | #include "rtc_base/thread_message.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 42 | |
| 43 | #if defined(WEBRTC_WIN) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 44 | #include "rtc_base/win32.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 45 | #endif |
| 46 | |
Tommi | fe04164 | 2021-04-07 10:08:28 +0200 | [diff] [blame] | 47 | #if RTC_DCHECK_IS_ON |
| 48 | // Counts how many blocking Thread::Invoke or Thread::Send calls are made from |
| 49 | // within a scope and logs the number of blocking calls at the end of the scope. |
| 50 | #define RTC_LOG_THREAD_BLOCK_COUNT() \ |
| 51 | rtc::Thread::ScopedCountBlockingCalls blocked_call_count_printer( \ |
| 52 | [func = __func__](uint32_t actual_block, uint32_t could_block) { \ |
| 53 | auto total = actual_block + could_block; \ |
| 54 | if (total) { \ |
| 55 | RTC_LOG(LS_WARNING) << "Blocking " << func << ": total=" << total \ |
| 56 | << " (actual=" << actual_block \ |
| 57 | << ", could=" << could_block << ")"; \ |
| 58 | } \ |
| 59 | }) |
| 60 | |
| 61 | // Adds an RTC_DCHECK_LE that checks that the number of blocking calls are |
| 62 | // less than or equal to a specific value. Use to avoid regressing in the |
| 63 | // number of blocking thread calls. |
| 64 | // Note: Use of this macro, requires RTC_LOG_THREAD_BLOCK_COUNT() to be called |
| 65 | // first. |
Tomas Gunnarsson | 89f3dd5 | 2021-04-14 12:54:10 +0200 | [diff] [blame] | 66 | #define RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(x) \ |
| 67 | do { \ |
| 68 | blocked_call_count_printer.set_minimum_call_count_for_callback(x + 1); \ |
| 69 | RTC_DCHECK_LE(blocked_call_count_printer.GetTotalBlockedCallCount(), x); \ |
| 70 | } while (0) |
Tommi | fe04164 | 2021-04-07 10:08:28 +0200 | [diff] [blame] | 71 | #else |
| 72 | #define RTC_LOG_THREAD_BLOCK_COUNT() |
| 73 | #define RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(x) |
| 74 | #endif |
| 75 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 76 | namespace rtc { |
| 77 | |
| 78 | class Thread; |
| 79 | |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 80 | namespace rtc_thread_internal { |
| 81 | |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame] | 82 | class MessageLikeTask : public MessageData { |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 83 | public: |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame] | 84 | virtual void Run() = 0; |
| 85 | }; |
| 86 | |
| 87 | template <class FunctorT> |
| 88 | class MessageWithFunctor final : public MessageLikeTask { |
| 89 | public: |
| 90 | explicit MessageWithFunctor(FunctorT&& functor) |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 91 | : functor_(std::forward<FunctorT>(functor)) {} |
| 92 | |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 93 | MessageWithFunctor(const MessageWithFunctor&) = delete; |
| 94 | MessageWithFunctor& operator=(const MessageWithFunctor&) = delete; |
| 95 | |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame] | 96 | void Run() override { functor_(); } |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 97 | |
| 98 | private: |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame] | 99 | ~MessageWithFunctor() override {} |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 100 | |
| 101 | typename std::remove_reference<FunctorT>::type functor_; |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame] | 102 | }; |
| 103 | |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 104 | } // namespace rtc_thread_internal |
| 105 | |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 106 | class RTC_EXPORT ThreadManager { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 107 | public: |
| 108 | static const int kForever = -1; |
| 109 | |
| 110 | // Singleton, constructor and destructor are private. |
| 111 | static ThreadManager* Instance(); |
| 112 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 113 | static void Add(Thread* message_queue); |
| 114 | static void Remove(Thread* message_queue); |
| 115 | static void Clear(MessageHandler* handler); |
| 116 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 117 | // For testing purposes, for use with a simulated clock. |
| 118 | // Ensures that all message queues have processed delayed messages |
| 119 | // up until the current point in time. |
| 120 | static void ProcessAllMessageQueuesForTesting(); |
| 121 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 122 | Thread* CurrentThread(); |
| 123 | void SetCurrentThread(Thread* thread); |
Sebastian Jansson | 178a685 | 2020-01-14 11:12:26 +0100 | [diff] [blame] | 124 | // Allows changing the current thread, this is intended for tests where we |
| 125 | // want to simulate multiple threads running on a single physical thread. |
| 126 | void ChangeCurrentThreadForTest(Thread* thread); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 127 | |
| 128 | // Returns a thread object with its thread_ ivar set |
| 129 | // to whatever the OS uses to represent the thread. |
| 130 | // If there already *is* a Thread object corresponding to this thread, |
| 131 | // this method will return that. Otherwise it creates a new Thread |
| 132 | // object whose wrapped() method will return true, and whose |
| 133 | // handle will, on Win32, be opened with only synchronization privileges - |
| 134 | // if you need more privilegs, rather than changing this method, please |
| 135 | // write additional code to adjust the privileges, or call a different |
| 136 | // factory method of your own devising, because this one gets used in |
| 137 | // unexpected contexts (like inside browser plugins) and it would be a |
| 138 | // shame to break it. It is also conceivable on Win32 that we won't even |
| 139 | // be able to get synchronization privileges, in which case the result |
| 140 | // will have a null handle. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 141 | Thread* WrapCurrentThread(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 142 | void UnwrapCurrentThread(); |
| 143 | |
Niels Moller | 9d1840c | 2019-05-21 07:26:37 +0000 | [diff] [blame] | 144 | bool IsMainThread(); |
| 145 | |
Sebastian Jansson | da7267a | 2020-03-03 10:48:05 +0100 | [diff] [blame] | 146 | #if RTC_DCHECK_IS_ON |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 147 | // Registers that a Send operation is to be performed between `source` and |
| 148 | // `target`, while checking that this does not cause a send cycle that could |
Sebastian Jansson | da7267a | 2020-03-03 10:48:05 +0100 | [diff] [blame] | 149 | // potentially cause a deadlock. |
| 150 | void RegisterSendAndCheckForCycles(Thread* source, Thread* target); |
| 151 | #endif |
| 152 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 153 | private: |
| 154 | ThreadManager(); |
| 155 | ~ThreadManager(); |
| 156 | |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 157 | ThreadManager(const ThreadManager&) = delete; |
| 158 | ThreadManager& operator=(const ThreadManager&) = delete; |
| 159 | |
Sebastian Jansson | 178a685 | 2020-01-14 11:12:26 +0100 | [diff] [blame] | 160 | void SetCurrentThreadInternal(Thread* thread); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 161 | void AddInternal(Thread* message_queue); |
| 162 | void RemoveInternal(Thread* message_queue); |
| 163 | void ClearInternal(MessageHandler* handler); |
| 164 | void ProcessAllMessageQueuesInternal(); |
Sebastian Jansson | da7267a | 2020-03-03 10:48:05 +0100 | [diff] [blame] | 165 | #if RTC_DCHECK_IS_ON |
| 166 | void RemoveFromSendGraph(Thread* thread) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 167 | #endif |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 168 | |
| 169 | // This list contains all live Threads. |
| 170 | std::vector<Thread*> message_queues_ RTC_GUARDED_BY(crit_); |
| 171 | |
| 172 | // Methods that don't modify the list of message queues may be called in a |
| 173 | // re-entrant fashion. "processing_" keeps track of the depth of re-entrant |
| 174 | // calls. |
Markus Handell | 3cb525b | 2020-07-16 16:16:09 +0200 | [diff] [blame] | 175 | RecursiveCriticalSection crit_; |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 176 | size_t processing_ RTC_GUARDED_BY(crit_) = 0; |
Sebastian Jansson | da7267a | 2020-03-03 10:48:05 +0100 | [diff] [blame] | 177 | #if RTC_DCHECK_IS_ON |
| 178 | // Represents all thread seand actions by storing all send targets per thread. |
| 179 | // This is used by RegisterSendAndCheckForCycles. This graph has no cycles |
| 180 | // since we will trigger a CHECK failure if a cycle is introduced. |
| 181 | std::map<Thread*, std::set<Thread*>> send_graph_ RTC_GUARDED_BY(crit_); |
| 182 | #endif |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 183 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 184 | #if defined(WEBRTC_POSIX) |
| 185 | pthread_key_t key_; |
| 186 | #endif |
| 187 | |
| 188 | #if defined(WEBRTC_WIN) |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 189 | const DWORD key_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 190 | #endif |
| 191 | |
Niels Moller | 9d1840c | 2019-05-21 07:26:37 +0000 | [diff] [blame] | 192 | // The thread to potentially autowrap. |
| 193 | const PlatformThreadRef main_thread_ref_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 194 | }; |
| 195 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 196 | // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread(). |
| 197 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 198 | class RTC_LOCKABLE RTC_EXPORT Thread : public webrtc::TaskQueueBase { |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame] | 199 | public: |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 200 | static const int kForever = -1; |
| 201 | |
| 202 | // Create a new Thread and optionally assign it to the passed |
| 203 | // SocketServer. Subclasses that override Clear should pass false for |
| 204 | // init_queue and call DoInit() from their constructor to prevent races |
| 205 | // with the ThreadManager using the object while the vtable is still |
| 206 | // being created. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 207 | explicit Thread(SocketServer* ss); |
| 208 | explicit Thread(std::unique_ptr<SocketServer> ss); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 209 | |
Taylor Brandstetter | 0867260 | 2018-03-02 15:20:33 -0800 | [diff] [blame] | 210 | // Constructors meant for subclasses; they should call DoInit themselves and |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 211 | // pass false for `do_init`, so that DoInit is called only on the fully |
Taylor Brandstetter | 0867260 | 2018-03-02 15:20:33 -0800 | [diff] [blame] | 212 | // instantiated class, which avoids a vptr data race. |
| 213 | Thread(SocketServer* ss, bool do_init); |
| 214 | Thread(std::unique_ptr<SocketServer> ss, bool do_init); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 215 | |
| 216 | // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or |
| 217 | // guarantee Stop() is explicitly called before the subclass is destroyed). |
| 218 | // This is required to avoid a data race between the destructor modifying the |
| 219 | // vtable, and the Thread::PreRun calling the virtual method Run(). |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 220 | |
| 221 | // NOTE: SUBCLASSES OF Thread THAT OVERRIDE Clear MUST CALL |
| 222 | // DoDestroy() IN THEIR DESTRUCTORS! This is required to avoid a data race |
| 223 | // between the destructor modifying the vtable, and the ThreadManager |
| 224 | // calling Clear on the object from a different thread. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 225 | ~Thread() override; |
| 226 | |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 227 | Thread(const Thread&) = delete; |
| 228 | Thread& operator=(const Thread&) = delete; |
| 229 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 230 | static std::unique_ptr<Thread> CreateWithSocketServer(); |
| 231 | static std::unique_ptr<Thread> Create(); |
| 232 | static Thread* Current(); |
| 233 | |
| 234 | // Used to catch performance regressions. Use this to disallow blocking calls |
| 235 | // (Invoke) for a given scope. If a synchronous call is made while this is in |
| 236 | // effect, an assert will be triggered. |
| 237 | // Note that this is a single threaded class. |
| 238 | class ScopedDisallowBlockingCalls { |
| 239 | public: |
| 240 | ScopedDisallowBlockingCalls(); |
Sebastian Jansson | 9debe5a | 2019-03-22 15:42:38 +0100 | [diff] [blame] | 241 | ScopedDisallowBlockingCalls(const ScopedDisallowBlockingCalls&) = delete; |
| 242 | ScopedDisallowBlockingCalls& operator=(const ScopedDisallowBlockingCalls&) = |
| 243 | delete; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 244 | ~ScopedDisallowBlockingCalls(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 245 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 246 | private: |
| 247 | Thread* const thread_; |
| 248 | const bool previous_state_; |
| 249 | }; |
| 250 | |
Tommi | fe04164 | 2021-04-07 10:08:28 +0200 | [diff] [blame] | 251 | #if RTC_DCHECK_IS_ON |
| 252 | class ScopedCountBlockingCalls { |
| 253 | public: |
| 254 | ScopedCountBlockingCalls(std::function<void(uint32_t, uint32_t)> callback); |
| 255 | ScopedCountBlockingCalls(const ScopedDisallowBlockingCalls&) = delete; |
| 256 | ScopedCountBlockingCalls& operator=(const ScopedDisallowBlockingCalls&) = |
| 257 | delete; |
| 258 | ~ScopedCountBlockingCalls(); |
| 259 | |
| 260 | uint32_t GetBlockingCallCount() const; |
| 261 | uint32_t GetCouldBeBlockingCallCount() const; |
| 262 | uint32_t GetTotalBlockedCallCount() const; |
| 263 | |
Tomas Gunnarsson | 89f3dd5 | 2021-04-14 12:54:10 +0200 | [diff] [blame] | 264 | void set_minimum_call_count_for_callback(uint32_t minimum) { |
| 265 | min_blocking_calls_for_callback_ = minimum; |
| 266 | } |
| 267 | |
Tommi | fe04164 | 2021-04-07 10:08:28 +0200 | [diff] [blame] | 268 | private: |
| 269 | Thread* const thread_; |
| 270 | const uint32_t base_blocking_call_count_; |
| 271 | const uint32_t base_could_be_blocking_call_count_; |
Tomas Gunnarsson | 89f3dd5 | 2021-04-14 12:54:10 +0200 | [diff] [blame] | 272 | // The minimum number of blocking calls required in order to issue the |
| 273 | // result_callback_. This is used by RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN to |
| 274 | // tame log spam. |
| 275 | // By default we always issue the callback, regardless of callback count. |
| 276 | uint32_t min_blocking_calls_for_callback_ = 0; |
Tommi | fe04164 | 2021-04-07 10:08:28 +0200 | [diff] [blame] | 277 | std::function<void(uint32_t, uint32_t)> result_callback_; |
| 278 | }; |
| 279 | |
| 280 | uint32_t GetBlockingCallCount() const; |
| 281 | uint32_t GetCouldBeBlockingCallCount() const; |
| 282 | #endif |
| 283 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 284 | SocketServer* socketserver(); |
| 285 | |
| 286 | // Note: The behavior of Thread has changed. When a thread is stopped, |
| 287 | // futher Posts and Sends will fail. However, any pending Sends and *ready* |
| 288 | // Posts (as opposed to unexpired delayed Posts) will be delivered before |
| 289 | // Get (or Peek) returns false. By guaranteeing delivery of those messages, |
| 290 | // we eliminate the race condition when an MessageHandler and Thread |
| 291 | // may be destroyed independently of each other. |
| 292 | virtual void Quit(); |
| 293 | virtual bool IsQuitting(); |
| 294 | virtual void Restart(); |
| 295 | // Not all message queues actually process messages (such as SignalThread). |
| 296 | // In those cases, it's important to know, before posting, that it won't be |
| 297 | // Processed. Normally, this would be true until IsQuitting() is true. |
| 298 | virtual bool IsProcessingMessagesForTesting(); |
| 299 | |
| 300 | // Get() will process I/O until: |
| 301 | // 1) A message is available (returns true) |
| 302 | // 2) cmsWait seconds have elapsed (returns false) |
| 303 | // 3) Stop() is called (returns false) |
| 304 | virtual bool Get(Message* pmsg, |
| 305 | int cmsWait = kForever, |
| 306 | bool process_io = true); |
| 307 | virtual bool Peek(Message* pmsg, int cmsWait = 0); |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 308 | // `time_sensitive` is deprecated and should always be false. |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 309 | virtual void Post(const Location& posted_from, |
| 310 | MessageHandler* phandler, |
| 311 | uint32_t id = 0, |
| 312 | MessageData* pdata = nullptr, |
| 313 | bool time_sensitive = false); |
| 314 | virtual void PostDelayed(const Location& posted_from, |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 315 | int delay_ms, |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 316 | MessageHandler* phandler, |
| 317 | uint32_t id = 0, |
| 318 | MessageData* pdata = nullptr); |
| 319 | virtual void PostAt(const Location& posted_from, |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 320 | int64_t run_at_ms, |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 321 | MessageHandler* phandler, |
| 322 | uint32_t id = 0, |
| 323 | MessageData* pdata = nullptr); |
| 324 | virtual void Clear(MessageHandler* phandler, |
| 325 | uint32_t id = MQID_ANY, |
| 326 | MessageList* removed = nullptr); |
| 327 | virtual void Dispatch(Message* pmsg); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 328 | |
| 329 | // Amount of time until the next message can be retrieved |
| 330 | virtual int GetDelay(); |
| 331 | |
| 332 | bool empty() const { return size() == 0u; } |
| 333 | size_t size() const { |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 334 | CritScope cs(&crit_); |
| 335 | return messages_.size() + delayed_messages_.size() + (fPeekKeep_ ? 1u : 0u); |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | // Internally posts a message which causes the doomed object to be deleted |
| 339 | template <class T> |
| 340 | void Dispose(T* doomed) { |
| 341 | if (doomed) { |
| 342 | Post(RTC_FROM_HERE, nullptr, MQID_DISPOSE, new DisposeData<T>(doomed)); |
| 343 | } |
| 344 | } |
| 345 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 346 | bool IsCurrent() const; |
| 347 | |
| 348 | // Sleeps the calling thread for the specified number of milliseconds, during |
| 349 | // which time no processing is performed. Returns false if sleeping was |
| 350 | // interrupted by a signal (POSIX only). |
| 351 | static bool SleepMs(int millis); |
| 352 | |
| 353 | // Sets the thread's name, for debugging. Must be called before Start(). |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 354 | // If `obj` is non-null, its value is appended to `name`. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 355 | const std::string& name() const { return name_; } |
| 356 | bool SetName(const std::string& name, const void* obj); |
| 357 | |
Harald Alvestrand | ba69442 | 2021-01-27 21:52:14 +0000 | [diff] [blame] | 358 | // Sets the expected processing time in ms. The thread will write |
| 359 | // log messages when Invoke() takes more time than this. |
| 360 | // Default is 50 ms. |
| 361 | void SetDispatchWarningMs(int deadline); |
| 362 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 363 | // Starts the execution of the thread. |
Niels Möller | d2e5013 | 2019-06-11 09:24:14 +0200 | [diff] [blame] | 364 | bool Start(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 365 | |
| 366 | // Tells the thread to stop and waits until it is joined. |
| 367 | // Never call Stop on the current thread. Instead use the inherited Quit |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 368 | // function which will exit the base Thread without terminating the |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 369 | // underlying OS thread. |
| 370 | virtual void Stop(); |
| 371 | |
| 372 | // By default, Thread::Run() calls ProcessMessages(kForever). To do other |
| 373 | // work, override Run(). To receive and dispatch messages, call |
| 374 | // ProcessMessages occasionally. |
| 375 | virtual void Run(); |
| 376 | |
| 377 | virtual void Send(const Location& posted_from, |
| 378 | MessageHandler* phandler, |
| 379 | uint32_t id = 0, |
| 380 | MessageData* pdata = nullptr); |
| 381 | |
| 382 | // Convenience method to invoke a functor on another thread. Caller must |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 383 | // provide the `ReturnT` template argument, which cannot (easily) be deduced. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 384 | // Uses Send() internally, which blocks the current thread until execution |
| 385 | // is complete. |
| 386 | // Ex: bool result = thread.Invoke<bool>(RTC_FROM_HERE, |
| 387 | // &MyFunctionReturningBool); |
| 388 | // NOTE: This function can only be called when synchronous calls are allowed. |
| 389 | // See ScopedDisallowBlockingCalls for details. |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 390 | // NOTE: Blocking invokes are DISCOURAGED, consider if what you're doing can |
| 391 | // be achieved with PostTask() and callbacks instead. |
Danil Chapovalov | 8931345 | 2019-11-29 12:56:43 +0100 | [diff] [blame] | 392 | template < |
| 393 | class ReturnT, |
| 394 | typename = typename std::enable_if<!std::is_void<ReturnT>::value>::type> |
| 395 | ReturnT Invoke(const Location& posted_from, FunctionView<ReturnT()> functor) { |
| 396 | ReturnT result; |
| 397 | InvokeInternal(posted_from, [functor, &result] { result = functor(); }); |
| 398 | return result; |
| 399 | } |
| 400 | |
| 401 | template < |
| 402 | class ReturnT, |
| 403 | typename = typename std::enable_if<std::is_void<ReturnT>::value>::type> |
| 404 | void Invoke(const Location& posted_from, FunctionView<void()> functor) { |
| 405 | InvokeInternal(posted_from, functor); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 408 | // Allows invoke to specified `thread`. Thread never will be dereferenced and |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 409 | // will be used only for reference-based comparison, so instance can be safely |
Mirko Bonadei | 481e345 | 2021-07-30 13:57:25 +0200 | [diff] [blame] | 410 | // deleted. If NDEBUG is defined and RTC_DCHECK_IS_ON is undefined do |
Mirko Bonadei | 8c185fc | 2021-07-21 13:12:38 +0200 | [diff] [blame] | 411 | // nothing. |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 412 | void AllowInvokesToThread(Thread* thread); |
Tomas Gunnarsson | abdb470 | 2020-09-05 18:43:36 +0200 | [diff] [blame] | 413 | |
Mirko Bonadei | 481e345 | 2021-07-30 13:57:25 +0200 | [diff] [blame] | 414 | // If NDEBUG is defined and RTC_DCHECK_IS_ON is undefined do nothing. |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 415 | void DisallowAllInvokes(); |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 416 | // Returns true if `target` was allowed by AllowInvokesToThread() or if no |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 417 | // calls were made to AllowInvokesToThread and DisallowAllInvokes. Otherwise |
| 418 | // returns false. |
Mirko Bonadei | 481e345 | 2021-07-30 13:57:25 +0200 | [diff] [blame] | 419 | // If NDEBUG is defined and RTC_DCHECK_IS_ON is undefined always returns |
Mirko Bonadei | 8c185fc | 2021-07-21 13:12:38 +0200 | [diff] [blame] | 420 | // true. |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 421 | bool IsInvokeToThreadAllowed(rtc::Thread* target); |
| 422 | |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 423 | // Posts a task to invoke the functor on `this` thread asynchronously, i.e. |
| 424 | // without blocking the thread that invoked PostTask(). Ownership of `functor` |
| 425 | // is passed and (usually, see below) destroyed on `this` thread after it is |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame] | 426 | // invoked. |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 427 | // Requirements of FunctorT: |
| 428 | // - FunctorT is movable. |
| 429 | // - FunctorT implements "T operator()()" or "T operator()() const" for some T |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 430 | // (if T is not void, the return value is discarded on `this` thread). |
| 431 | // - FunctorT has a public destructor that can be invoked from `this` thread |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 432 | // after operation() has been invoked. |
| 433 | // - The functor must not cause the thread to quit before PostTask() is done. |
| 434 | // |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame] | 435 | // Destruction of the functor/task mimics what TaskQueue::PostTask does: If |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 436 | // the task is run, it will be destroyed on `this` thread. However, if there |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame] | 437 | // are pending tasks by the time the Thread is destroyed, or a task is posted |
| 438 | // to a thread that is quitting, the task is destroyed immediately, on the |
| 439 | // calling thread. Destroying the Thread only blocks for any currently running |
| 440 | // task to complete. Note that TQ abstraction is even vaguer on how |
| 441 | // destruction happens in these cases, allowing destruction to happen |
| 442 | // asynchronously at a later time and on some arbitrary thread. So to ease |
| 443 | // migration, don't depend on Thread::PostTask destroying un-run tasks |
| 444 | // immediately. |
| 445 | // |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 446 | // Example - Calling a class method: |
| 447 | // class Foo { |
| 448 | // public: |
| 449 | // void DoTheThing(); |
| 450 | // }; |
| 451 | // Foo foo; |
| 452 | // thread->PostTask(RTC_FROM_HERE, Bind(&Foo::DoTheThing, &foo)); |
| 453 | // |
| 454 | // Example - Calling a lambda function: |
| 455 | // thread->PostTask(RTC_FROM_HERE, |
| 456 | // [&x, &y] { x.TrackComputations(y.Compute()); }); |
Henrik Boström | cf9899c | 2022-01-20 09:46:16 +0100 | [diff] [blame] | 457 | // |
| 458 | // TODO(https://crbug.com/webrtc/13582): Deprecate and remove in favor of the |
Henrik Boström | 2deee4b | 2022-01-20 11:58:05 +0100 | [diff] [blame^] | 459 | // PostTask() method inherited from TaskQueueBase and template helpers defined |
| 460 | // here in rtc::Thread for performing webrtc::ToQueuedTask(). Migration is |
| 461 | // easy, just remove RTC_FROM_HERE like so: |
| 462 | // |
| 463 | // Before: |
| 464 | // thread->PostTask(RTC_FROM_HERE, []() { printfln("wow"); }); |
| 465 | // After: |
| 466 | // thread->PostTask([]() { printfln("wow"); }); |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 467 | template <class FunctorT> |
Henrik Boström | 2deee4b | 2022-01-20 11:58:05 +0100 | [diff] [blame^] | 468 | void DEPRECATED_PostTask(const Location& posted_from, FunctorT&& functor) { |
Steve Anton | bcc1a76 | 2019-12-11 11:21:53 -0800 | [diff] [blame] | 469 | Post(posted_from, GetPostTaskMessageHandler(), /*id=*/0, |
Niels Möller | f13a096 | 2019-05-17 10:15:06 +0200 | [diff] [blame] | 470 | new rtc_thread_internal::MessageWithFunctor<FunctorT>( |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 471 | std::forward<FunctorT>(functor))); |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 472 | } |
Steve Anton | bcc1a76 | 2019-12-11 11:21:53 -0800 | [diff] [blame] | 473 | template <class FunctorT> |
Henrik Boström | 2deee4b | 2022-01-20 11:58:05 +0100 | [diff] [blame^] | 474 | ABSL_DEPRECATED("bugs.webrtc.org/13582") |
| 475 | void PostTask(const Location& posted_from, FunctorT&& functor) { |
| 476 | DEPRECATED_PostTask(posted_from, std::forward<FunctorT>(functor)); |
| 477 | } |
| 478 | template <class FunctorT> |
| 479 | void DEPRECATED_PostDelayedTask(const Location& posted_from, |
| 480 | FunctorT&& functor, |
| 481 | uint32_t milliseconds) { |
Steve Anton | bcc1a76 | 2019-12-11 11:21:53 -0800 | [diff] [blame] | 482 | PostDelayed(posted_from, milliseconds, GetPostTaskMessageHandler(), |
| 483 | /*id=*/0, |
| 484 | new rtc_thread_internal::MessageWithFunctor<FunctorT>( |
| 485 | std::forward<FunctorT>(functor))); |
| 486 | } |
Henrik Boström | 2deee4b | 2022-01-20 11:58:05 +0100 | [diff] [blame^] | 487 | template <class FunctorT> |
| 488 | ABSL_DEPRECATED("bugs.webrtc.org/13582") |
| 489 | void PostDelayedTask(const Location& posted_from, |
| 490 | FunctorT&& functor, |
| 491 | uint32_t milliseconds) { |
| 492 | DEPRECATED_PostDelayedTask(posted_from, std::forward<FunctorT>(functor), |
| 493 | milliseconds); |
| 494 | } |
Henrik Boström | ba4dcc3 | 2019-02-28 09:34:06 +0100 | [diff] [blame] | 495 | |
Danil Chapovalov | 912b3b8 | 2019-11-22 15:52:40 +0100 | [diff] [blame] | 496 | // From TaskQueueBase |
| 497 | void PostTask(std::unique_ptr<webrtc::QueuedTask> task) override; |
| 498 | void PostDelayedTask(std::unique_ptr<webrtc::QueuedTask> task, |
| 499 | uint32_t milliseconds) override; |
Henrik Boström | cf9899c | 2022-01-20 09:46:16 +0100 | [diff] [blame] | 500 | void PostDelayedHighPrecisionTask(std::unique_ptr<webrtc::QueuedTask> task, |
| 501 | uint32_t milliseconds) override; |
Danil Chapovalov | 912b3b8 | 2019-11-22 15:52:40 +0100 | [diff] [blame] | 502 | void Delete() override; |
| 503 | |
Henrik Boström | 2deee4b | 2022-01-20 11:58:05 +0100 | [diff] [blame^] | 504 | // Helper methods to avoid having to do ToQueuedTask() at the calling places. |
| 505 | template <class Closure, |
| 506 | typename std::enable_if<!std::is_convertible< |
| 507 | Closure, |
| 508 | std::unique_ptr<webrtc::QueuedTask>>::value>::type* = nullptr> |
| 509 | void PostTask(Closure&& closure) { |
| 510 | PostTask(webrtc::ToQueuedTask(std::forward<Closure>(closure))); |
| 511 | } |
| 512 | template <class Closure, |
| 513 | typename std::enable_if<!std::is_convertible< |
| 514 | Closure, |
| 515 | std::unique_ptr<webrtc::QueuedTask>>::value>::type* = nullptr> |
| 516 | void PostDelayedTask(Closure&& closure, uint32_t milliseconds) { |
| 517 | PostDelayedTask(webrtc::ToQueuedTask(std::forward<Closure>(closure)), |
| 518 | milliseconds); |
| 519 | } |
| 520 | template <class Closure, |
| 521 | typename std::enable_if<!std::is_convertible< |
| 522 | Closure, |
| 523 | std::unique_ptr<webrtc::QueuedTask>>::value>::type* = nullptr> |
| 524 | void PostDelayedHighPrecisionTask(Closure&& closure, uint32_t milliseconds) { |
| 525 | PostDelayedHighPrecisionTask( |
| 526 | webrtc::ToQueuedTask(std::forward<Closure>(closure)), milliseconds); |
| 527 | } |
| 528 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 529 | // ProcessMessages will process I/O and dispatch messages until: |
| 530 | // 1) cms milliseconds have elapsed (returns true) |
| 531 | // 2) Stop() is called (returns false) |
| 532 | bool ProcessMessages(int cms); |
| 533 | |
| 534 | // Returns true if this is a thread that we created using the standard |
| 535 | // constructor, false if it was created by a call to |
| 536 | // ThreadManager::WrapCurrentThread(). The main thread of an application |
| 537 | // is generally not owned, since the OS representation of the thread |
| 538 | // obviously exists before we can get to it. |
| 539 | // You cannot call Start on non-owned threads. |
| 540 | bool IsOwned(); |
| 541 | |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 542 | // Expose private method IsRunning() for tests. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 543 | // |
| 544 | // DANGER: this is a terrible public API. Most callers that might want to |
| 545 | // call this likely do not have enough control/knowledge of the Thread in |
| 546 | // question to guarantee that the returned value remains true for the duration |
| 547 | // of whatever code is conditionally executing because of the return value! |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 548 | bool RunningForTest() { return IsRunning(); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 549 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 550 | // These functions are public to avoid injecting test hooks. Don't call them |
| 551 | // outside of tests. |
| 552 | // This method should be called when thread is created using non standard |
| 553 | // method, like derived implementation of rtc::Thread and it can not be |
| 554 | // started by calling Start(). This will set started flag to true and |
| 555 | // owned to false. This must be called from the current thread. |
| 556 | bool WrapCurrent(); |
| 557 | void UnwrapCurrent(); |
| 558 | |
Karl Wiberg | 3256225 | 2019-02-21 13:38:30 +0100 | [diff] [blame] | 559 | // Sets the per-thread allow-blocking-calls flag to false; this is |
| 560 | // irrevocable. Must be called on this thread. |
| 561 | void DisallowBlockingCalls() { SetAllowBlockingCalls(false); } |
| 562 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 563 | protected: |
Sebastian Jansson | 6ce033a | 2020-01-22 10:12:56 +0100 | [diff] [blame] | 564 | class CurrentThreadSetter : CurrentTaskQueueSetter { |
| 565 | public: |
| 566 | explicit CurrentThreadSetter(Thread* thread) |
| 567 | : CurrentTaskQueueSetter(thread), |
| 568 | manager_(rtc::ThreadManager::Instance()), |
| 569 | previous_(manager_->CurrentThread()) { |
| 570 | manager_->ChangeCurrentThreadForTest(thread); |
| 571 | } |
| 572 | ~CurrentThreadSetter() { manager_->ChangeCurrentThreadForTest(previous_); } |
| 573 | |
| 574 | private: |
| 575 | rtc::ThreadManager* const manager_; |
| 576 | rtc::Thread* const previous_; |
| 577 | }; |
| 578 | |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 579 | // DelayedMessage goes into a priority queue, sorted by trigger time. Messages |
| 580 | // with the same trigger time are processed in num_ (FIFO) order. |
| 581 | class DelayedMessage { |
| 582 | public: |
| 583 | DelayedMessage(int64_t delay, |
| 584 | int64_t run_time_ms, |
| 585 | uint32_t num, |
| 586 | const Message& msg) |
| 587 | : delay_ms_(delay), |
| 588 | run_time_ms_(run_time_ms), |
| 589 | message_number_(num), |
| 590 | msg_(msg) {} |
| 591 | |
| 592 | bool operator<(const DelayedMessage& dmsg) const { |
| 593 | return (dmsg.run_time_ms_ < run_time_ms_) || |
| 594 | ((dmsg.run_time_ms_ == run_time_ms_) && |
| 595 | (dmsg.message_number_ < message_number_)); |
| 596 | } |
| 597 | |
| 598 | int64_t delay_ms_; // for debugging |
| 599 | int64_t run_time_ms_; |
| 600 | // Monotonicaly incrementing number used for ordering of messages |
| 601 | // targeted to execute at the same time. |
| 602 | uint32_t message_number_; |
| 603 | Message msg_; |
| 604 | }; |
| 605 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 606 | class PriorityQueue : public std::priority_queue<DelayedMessage> { |
| 607 | public: |
| 608 | container_type& container() { return c; } |
| 609 | void reheap() { make_heap(c.begin(), c.end(), comp); } |
| 610 | }; |
| 611 | |
| 612 | void DoDelayPost(const Location& posted_from, |
| 613 | int64_t cmsDelay, |
| 614 | int64_t tstamp, |
| 615 | MessageHandler* phandler, |
| 616 | uint32_t id, |
| 617 | MessageData* pdata); |
| 618 | |
| 619 | // Perform initialization, subclasses must call this from their constructor |
| 620 | // if false was passed as init_queue to the Thread constructor. |
| 621 | void DoInit(); |
| 622 | |
| 623 | // Does not take any lock. Must be called either while holding crit_, or by |
| 624 | // the destructor (by definition, the latter has exclusive access). |
| 625 | void ClearInternal(MessageHandler* phandler, |
| 626 | uint32_t id, |
| 627 | MessageList* removed) RTC_EXCLUSIVE_LOCKS_REQUIRED(&crit_); |
| 628 | |
| 629 | // Perform cleanup; subclasses must call this from the destructor, |
| 630 | // and are not expected to actually hold the lock. |
| 631 | void DoDestroy() RTC_EXCLUSIVE_LOCKS_REQUIRED(&crit_); |
| 632 | |
| 633 | void WakeUpSocketServer(); |
| 634 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 635 | // Same as WrapCurrent except that it never fails as it does not try to |
| 636 | // acquire the synchronization access of the thread. The caller should never |
| 637 | // call Stop() or Join() on this thread. |
| 638 | void SafeWrapCurrent(); |
| 639 | |
| 640 | // Blocks the calling thread until this thread has terminated. |
| 641 | void Join(); |
| 642 | |
| 643 | static void AssertBlockingIsAllowedOnCurrentThread(); |
| 644 | |
| 645 | friend class ScopedDisallowBlockingCalls; |
| 646 | |
Markus Handell | 3cb525b | 2020-07-16 16:16:09 +0200 | [diff] [blame] | 647 | RecursiveCriticalSection* CritForTest() { return &crit_; } |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 648 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 649 | private: |
Harald Alvestrand | ba69442 | 2021-01-27 21:52:14 +0000 | [diff] [blame] | 650 | static const int kSlowDispatchLoggingThreshold = 50; // 50 ms |
| 651 | |
Danil Chapovalov | 912b3b8 | 2019-11-22 15:52:40 +0100 | [diff] [blame] | 652 | class QueuedTaskHandler final : public MessageHandler { |
| 653 | public: |
Tomas Gunnarsson | 77baeee | 2020-09-24 22:39:21 +0200 | [diff] [blame] | 654 | QueuedTaskHandler() {} |
Danil Chapovalov | 912b3b8 | 2019-11-22 15:52:40 +0100 | [diff] [blame] | 655 | void OnMessage(Message* msg) override; |
| 656 | }; |
Steve Anton | bcc1a76 | 2019-12-11 11:21:53 -0800 | [diff] [blame] | 657 | |
Karl Wiberg | 3256225 | 2019-02-21 13:38:30 +0100 | [diff] [blame] | 658 | // Sets the per-thread allow-blocking-calls flag and returns the previous |
| 659 | // value. Must be called on this thread. |
| 660 | bool SetAllowBlockingCalls(bool allow); |
| 661 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 662 | #if defined(WEBRTC_WIN) |
| 663 | static DWORD WINAPI PreRun(LPVOID context); |
| 664 | #else |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 665 | static void* PreRun(void* pv); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 666 | #endif |
| 667 | |
| 668 | // ThreadManager calls this instead WrapCurrent() because |
| 669 | // ThreadManager::Instance() cannot be used while ThreadManager is |
| 670 | // being created. |
| 671 | // The method tries to get synchronization rights of the thread on Windows if |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 672 | // `need_synchronize_access` is true. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 673 | bool WrapCurrentWithThreadManager(ThreadManager* thread_manager, |
| 674 | bool need_synchronize_access); |
| 675 | |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 676 | // Return true if the thread is currently running. |
| 677 | bool IsRunning(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 678 | |
Danil Chapovalov | 8931345 | 2019-11-29 12:56:43 +0100 | [diff] [blame] | 679 | void InvokeInternal(const Location& posted_from, |
| 680 | rtc::FunctionView<void()> functor); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 681 | |
Tommi | 6866dc7 | 2020-05-15 10:11:56 +0200 | [diff] [blame] | 682 | // Called by the ThreadManager when being set as the current thread. |
| 683 | void EnsureIsCurrentTaskQueue(); |
| 684 | |
| 685 | // Called by the ThreadManager when being unset as the current thread. |
| 686 | void ClearCurrentTaskQueue(); |
| 687 | |
Steve Anton | bcc1a76 | 2019-12-11 11:21:53 -0800 | [diff] [blame] | 688 | // Returns a static-lifetime MessageHandler which runs message with |
| 689 | // MessageLikeTask payload data. |
| 690 | static MessageHandler* GetPostTaskMessageHandler(); |
| 691 | |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 692 | bool fPeekKeep_; |
| 693 | Message msgPeek_; |
Sebastian Jansson | 61380c0 | 2020-01-17 14:46:08 +0100 | [diff] [blame] | 694 | MessageList messages_ RTC_GUARDED_BY(crit_); |
| 695 | PriorityQueue delayed_messages_ RTC_GUARDED_BY(crit_); |
| 696 | uint32_t delayed_next_num_ RTC_GUARDED_BY(crit_); |
Tommi | fe04164 | 2021-04-07 10:08:28 +0200 | [diff] [blame] | 697 | #if RTC_DCHECK_IS_ON |
| 698 | uint32_t blocking_call_count_ RTC_GUARDED_BY(this) = 0; |
| 699 | uint32_t could_be_blocking_call_count_ RTC_GUARDED_BY(this) = 0; |
Artem Titov | dfc5f0d | 2020-07-03 12:09:26 +0200 | [diff] [blame] | 700 | std::vector<Thread*> allowed_threads_ RTC_GUARDED_BY(this); |
| 701 | bool invoke_policy_enabled_ RTC_GUARDED_BY(this) = false; |
| 702 | #endif |
Markus Handell | 3cb525b | 2020-07-16 16:16:09 +0200 | [diff] [blame] | 703 | RecursiveCriticalSection crit_; |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 704 | bool fInitialized_; |
| 705 | bool fDestroyed_; |
| 706 | |
| 707 | volatile int stop_; |
| 708 | |
| 709 | // The SocketServer might not be owned by Thread. |
| 710 | SocketServer* const ss_; |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 711 | // Used if SocketServer ownership lies with `this`. |
Sebastian Jansson | 6ea2c6a | 2020-01-13 14:07:22 +0100 | [diff] [blame] | 712 | std::unique_ptr<SocketServer> own_ss_; |
| 713 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 714 | std::string name_; |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 715 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 716 | // TODO(tommi): Add thread checks for proper use of control methods. |
| 717 | // Ideally we should be able to just use PlatformThread. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 718 | |
| 719 | #if defined(WEBRTC_POSIX) |
Tommi | 6cea2b0 | 2017-12-04 18:51:16 +0100 | [diff] [blame] | 720 | pthread_t thread_ = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 721 | #endif |
| 722 | |
| 723 | #if defined(WEBRTC_WIN) |
Tommi | 6cea2b0 | 2017-12-04 18:51:16 +0100 | [diff] [blame] | 724 | HANDLE thread_ = nullptr; |
| 725 | DWORD thread_id_ = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 726 | #endif |
| 727 | |
Tommi | 5149242 | 2017-12-04 15:18:23 +0100 | [diff] [blame] | 728 | // Indicates whether or not ownership of the worker thread lies with |
| 729 | // this instance or not. (i.e. owned_ == !wrapped). |
| 730 | // Must only be modified when the worker thread is not running. |
| 731 | bool owned_ = true; |
| 732 | |
| 733 | // Only touched from the worker thread itself. |
| 734 | bool blocking_calls_allowed_ = true; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 735 | |
Danil Chapovalov | 912b3b8 | 2019-11-22 15:52:40 +0100 | [diff] [blame] | 736 | // Runs webrtc::QueuedTask posted to the Thread. |
| 737 | QueuedTaskHandler queued_task_handler_; |
Tommi | 6866dc7 | 2020-05-15 10:11:56 +0200 | [diff] [blame] | 738 | std::unique_ptr<TaskQueueBase::CurrentTaskQueueSetter> |
| 739 | task_queue_registration_; |
Danil Chapovalov | 912b3b8 | 2019-11-22 15:52:40 +0100 | [diff] [blame] | 740 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 741 | friend class ThreadManager; |
| 742 | |
Harald Alvestrand | ba69442 | 2021-01-27 21:52:14 +0000 | [diff] [blame] | 743 | int dispatch_warning_ms_ RTC_GUARDED_BY(this) = kSlowDispatchLoggingThreshold; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 744 | }; |
| 745 | |
| 746 | // AutoThread automatically installs itself at construction |
| 747 | // uninstalls at destruction, if a Thread object is |
| 748 | // _not already_ associated with the current OS thread. |
Tomas Gunnarsson | 0fd4c4e | 2020-09-04 16:33:25 +0200 | [diff] [blame] | 749 | // |
| 750 | // NOTE: *** This class should only be used by tests *** |
| 751 | // |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 752 | class AutoThread : public Thread { |
| 753 | public: |
| 754 | AutoThread(); |
| 755 | ~AutoThread() override; |
| 756 | |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 757 | AutoThread(const AutoThread&) = delete; |
| 758 | AutoThread& operator=(const AutoThread&) = delete; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 759 | }; |
| 760 | |
| 761 | // AutoSocketServerThread automatically installs itself at |
| 762 | // construction and uninstalls at destruction. If a Thread object is |
| 763 | // already associated with the current OS thread, it is temporarily |
| 764 | // disassociated and restored by the destructor. |
| 765 | |
| 766 | class AutoSocketServerThread : public Thread { |
| 767 | public: |
| 768 | explicit AutoSocketServerThread(SocketServer* ss); |
| 769 | ~AutoSocketServerThread() override; |
| 770 | |
Byoungchan Lee | 14af762 | 2022-01-12 05:24:58 +0900 | [diff] [blame] | 771 | AutoSocketServerThread(const AutoSocketServerThread&) = delete; |
| 772 | AutoSocketServerThread& operator=(const AutoSocketServerThread&) = delete; |
| 773 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 774 | private: |
| 775 | rtc::Thread* old_thread_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 776 | }; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 777 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 778 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 779 | #endif // RTC_BASE_THREAD_H_ |