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