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