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