| 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 |
| 11 | #ifndef RTC_BASE_THREAD_H_ |
| 12 | #define RTC_BASE_THREAD_H_ |
| 13 |
| 14 | #include <stdint.h> |
| 15 |
| 16 | #include <list> |
| 17 | #include <map> |
| 18 | #include <memory> |
| 19 | #include <queue> |
| 20 | #include <set> |
| 21 | #include <string> |
| 22 | #include <type_traits> |
| 23 | #include <utility> |
| 24 | #include <vector> |
| 25 |
| 26 | #include "absl/strings/string_view.h" |
| 27 |
| 28 | #if defined(WEBRTC_POSIX) |
| 29 | #include <pthread.h> |
| 30 | #endif |
| 31 | #include "absl/base/attributes.h" |
| 32 | #include "absl/functional/any_invocable.h" |
| 33 | #include "api/function_view.h" |
| 34 | #include "api/location.h" |
| 35 | #include "api/task_queue/task_queue_base.h" |
| 36 | #include "api/units/time_delta.h" |
| 37 | #include "rtc_base/checks.h" |
| 38 | #include "rtc_base/platform_thread_types.h" |
| 39 | #include "rtc_base/socket_server.h" |
| 40 | #include "rtc_base/synchronization/mutex.h" |
| 41 | #include "rtc_base/system/rtc_export.h" |
| 42 | #include "rtc_base/thread_annotations.h" |
| 43 |
| 44 | #if defined(WEBRTC_WIN) |
| 45 | #include "rtc_base/win32.h" |
| 46 | #endif |
| 47 |
| 48 | #if RTC_DCHECK_IS_ON |
| 49 | // Counts how many `Thread::BlockingCall` are made from within a scope and logs |
| 50 | // the number of blocking calls at the end of the scope. |
| 51 | #define RTC_LOG_THREAD_BLOCK_COUNT() \ |
| 52 | rtc::Thread::ScopedCountBlockingCalls blocked_call_count_printer( \ |
| 53 | [func = __func__](uint32_t actual_block, uint32_t could_block) { \ |
| 54 | auto total = actual_block + could_block; \ |
| 55 | if (total) { \ |
| 56 | RTC_LOG(LS_WARNING) << "Blocking " << func << ": total=" << total \ |
| 57 | << " (actual=" << actual_block \ |
| 58 | << ", could=" << could_block << ")"; \ |
| 59 | } \ |
| 60 | }) |
| 61 |
| 62 | // Adds an RTC_DCHECK_LE that checks that the number of blocking calls are |
| 63 | // less than or equal to a specific value. Use to avoid regressing in the |
| 64 | // number of blocking thread calls. |
| 65 | // Note: Use of this macro, requires RTC_LOG_THREAD_BLOCK_COUNT() to be called |
| 66 | // first. |
| 67 | #define RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(x) \ |
| 68 | do { \ |
| 69 | blocked_call_count_printer.set_minimum_call_count_for_callback(x + 1); \ |
| 70 | RTC_DCHECK_LE(blocked_call_count_printer.GetTotalBlockedCallCount(), x); \ |
| 71 | } while (0) |
| 72 | #else |
| 73 | #define RTC_LOG_THREAD_BLOCK_COUNT() |
| 74 | #define RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(x) |
| 75 | #endif |
| 76 |
| 77 | namespace rtc { |
| 78 |
| 79 | class Thread; |
| 80 |
| 81 | class RTC_EXPORT ThreadManager { |
| 82 | public: |
| 83 | static const int kForever = -1; |
| 84 |
| 85 | // Singleton, constructor and destructor are private. |
| 86 | static ThreadManager* Instance(); |
| 87 |
| 88 | static void Add(Thread* message_queue); |
| 89 | static void Remove(Thread* message_queue); |
| 90 |
| 91 | // For testing purposes, for use with a simulated clock. |
| 92 | // Ensures that all message queues have processed delayed messages |
| 93 | // up until the current point in time. |
| 94 | static void ProcessAllMessageQueuesForTesting(); |
| 95 |
| 96 | Thread* CurrentThread(); |
| 97 | void SetCurrentThread(Thread* thread); |
| 98 | // Allows changing the current thread, this is intended for tests where we |
| 99 | // want to simulate multiple threads running on a single physical thread. |
| 100 | void ChangeCurrentThreadForTest(Thread* thread); |
| 101 |
| 102 | // Returns a thread object with its thread_ ivar set |
| 103 | // to whatever the OS uses to represent the thread. |
| 104 | // If there already *is* a Thread object corresponding to this thread, |
| 105 | // this method will return that. Otherwise it creates a new Thread |
| 106 | // object whose wrapped() method will return true, and whose |
| 107 | // handle will, on Win32, be opened with only synchronization privileges - |
| 108 | // if you need more privilegs, rather than changing this method, please |
| 109 | // write additional code to adjust the privileges, or call a different |
| 110 | // factory method of your own devising, because this one gets used in |
| 111 | // unexpected contexts (like inside browser plugins) and it would be a |
| 112 | // shame to break it. It is also conceivable on Win32 that we won't even |
| 113 | // be able to get synchronization privileges, in which case the result |
| 114 | // will have a null handle. |
| 115 | Thread* WrapCurrentThread(); |
| 116 | void UnwrapCurrentThread(); |
| 117 |
| 118 | #if RTC_DCHECK_IS_ON |
| 119 | // Registers that a Send operation is to be performed between `source` and |
| 120 | // `target`, while checking that this does not cause a send cycle that could |
| 121 | // potentially cause a deadlock. |
| 122 | void RegisterSendAndCheckForCycles(Thread* source, Thread* target); |
| 123 | #endif |
| 124 |
| 125 | private: |
| 126 | ThreadManager(); |
| 127 | ~ThreadManager(); |
| 128 |
| 129 | ThreadManager(const ThreadManager&) = delete; |
| 130 | ThreadManager& operator=(const ThreadManager&) = delete; |
| 131 |
| 132 | void SetCurrentThreadInternal(Thread* thread); |
| 133 | void AddInternal(Thread* message_queue); |
| 134 | void RemoveInternal(Thread* message_queue); |
| 135 | void ProcessAllMessageQueuesInternal(); |
| 136 | #if RTC_DCHECK_IS_ON |
| 137 | void RemoveFromSendGraph(Thread* thread) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 138 | #endif |
| 139 |
| 140 | // This list contains all live Threads. |
| 141 | std::vector<Thread*> message_queues_ RTC_GUARDED_BY(crit_); |
| 142 |
| 143 | webrtc::Mutex crit_; |
| 144 |
| 145 | #if RTC_DCHECK_IS_ON |
| 146 | // Represents all thread seand actions by storing all send targets per thread. |
| 147 | // This is used by RegisterSendAndCheckForCycles. This graph has no cycles |
| 148 | // since we will trigger a CHECK failure if a cycle is introduced. |
| 149 | std::map<Thread*, std::set<Thread*>> send_graph_ RTC_GUARDED_BY(crit_); |
| 150 | #endif |
| 151 |
| 152 | #if defined(WEBRTC_POSIX) |
| 153 | pthread_key_t key_; |
| 154 | #endif |
| 155 |
| 156 | #if defined(WEBRTC_WIN) |
| 157 | const DWORD key_; |
| 158 | #endif |
| 159 | }; |
| 160 |
| 161 | // WARNING! SUBCLASSES MUST CALL Stop() IN THEIR DESTRUCTORS! See ~Thread(). |
| 162 |
| 163 | class RTC_LOCKABLE RTC_EXPORT Thread : public webrtc::TaskQueueBase { |
| 164 | public: |
| 165 | static const int kForever = -1; |
| 166 |
| 167 | // Create a new Thread and optionally assign it to the passed |
| 168 | // SocketServer. Subclasses that override Clear should pass false for |
| 169 | // init_queue and call DoInit() from their constructor to prevent races |
| 170 | // with the ThreadManager using the object while the vtable is still |
| 171 | // being created. |
| 172 | explicit Thread(SocketServer* ss); |
| 173 | explicit Thread(std::unique_ptr<SocketServer> ss); |
| 174 |
| 175 | // Constructors meant for subclasses; they should call DoInit themselves and |
| 176 | // pass false for `do_init`, so that DoInit is called only on the fully |
| 177 | // instantiated class, which avoids a vptr data race. |
| 178 | Thread(SocketServer* ss, bool do_init); |
| 179 | Thread(std::unique_ptr<SocketServer> ss, bool do_init); |
| 180 |
| 181 | // NOTE: ALL SUBCLASSES OF Thread MUST CALL Stop() IN THEIR DESTRUCTORS (or |
| 182 | // guarantee Stop() is explicitly called before the subclass is destroyed). |
| 183 | // This is required to avoid a data race between the destructor modifying the |
| 184 | // vtable, and the Thread::PreRun calling the virtual method Run(). |
| 185 |
| 186 | // NOTE: SUBCLASSES OF Thread THAT OVERRIDE Clear MUST CALL |
| 187 | // DoDestroy() IN THEIR DESTRUCTORS! This is required to avoid a data race |
| 188 | // between the destructor modifying the vtable, and the ThreadManager |
| 189 | // calling Clear on the object from a different thread. |
| 190 | ~Thread() override; |
| 191 |
| 192 | Thread(const Thread&) = delete; |
| 193 | Thread& operator=(const Thread&) = delete; |
| 194 |
| 195 | static std::unique_ptr<Thread> CreateWithSocketServer(); |
| 196 | static std::unique_ptr<Thread> Create(); |
| 197 | static Thread* Current(); |
| 198 |
| 199 | // Used to catch performance regressions. Use this to disallow BlockingCall |
| 200 | // for a given scope. If a synchronous call is made while this is in |
| 201 | // effect, an assert will be triggered. |
| 202 | // Note that this is a single threaded class. |
| 203 | class ScopedDisallowBlockingCalls { |
| 204 | public: |
| 205 | ScopedDisallowBlockingCalls(); |
| 206 | ScopedDisallowBlockingCalls(const ScopedDisallowBlockingCalls&) = delete; |
| 207 | ScopedDisallowBlockingCalls& operator=(const ScopedDisallowBlockingCalls&) = |
| 208 | delete; |
| 209 | ~ScopedDisallowBlockingCalls(); |
| 210 |
| 211 | private: |
| 212 | Thread* const thread_; |
| 213 | const bool previous_state_; |
| 214 | }; |
| 215 |
| 216 | #if RTC_DCHECK_IS_ON |
| 217 | class ScopedCountBlockingCalls { |
| 218 | public: |
| 219 | ScopedCountBlockingCalls(std::function<void(uint32_t, uint32_t)> callback); |
| 220 | ScopedCountBlockingCalls(const ScopedDisallowBlockingCalls&) = delete; |
| 221 | ScopedCountBlockingCalls& operator=(const ScopedDisallowBlockingCalls&) = |
| 222 | delete; |
| 223 | ~ScopedCountBlockingCalls(); |
| 224 |
| 225 | uint32_t GetBlockingCallCount() const; |
| 226 | uint32_t GetCouldBeBlockingCallCount() const; |
| 227 | uint32_t GetTotalBlockedCallCount() const; |
| 228 |
| 229 | void set_minimum_call_count_for_callback(uint32_t minimum) { |
| 230 | min_blocking_calls_for_callback_ = minimum; |
| 231 | } |
| 232 |
| 233 | private: |
| 234 | Thread* const thread_; |
| 235 | const uint32_t base_blocking_call_count_; |
| 236 | const uint32_t base_could_be_blocking_call_count_; |
| 237 | // The minimum number of blocking calls required in order to issue the |
| 238 | // result_callback_. This is used by RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN to |
| 239 | // tame log spam. |
| 240 | // By default we always issue the callback, regardless of callback count. |
| 241 | uint32_t min_blocking_calls_for_callback_ = 0; |
| 242 | std::function<void(uint32_t, uint32_t)> result_callback_; |
| 243 | }; |
| 244 |
| 245 | uint32_t GetBlockingCallCount() const; |
| 246 | uint32_t GetCouldBeBlockingCallCount() const; |
| 247 | #endif |
| 248 |
| 249 | SocketServer* socketserver(); |
| 250 |
| 251 | // Note: The behavior of Thread has changed. When a thread is stopped, |
| 252 | // futher Posts and Sends will fail. However, any pending Sends and *ready* |
| 253 | // Posts (as opposed to unexpired delayed Posts) will be delivered before |
| 254 | // Get (or Peek) returns false. By guaranteeing delivery of those messages, |
| 255 | // we eliminate the race condition when an MessageHandler and Thread |
| 256 | // may be destroyed independently of each other. |
| 257 | virtual void Quit(); |
| 258 | virtual bool IsQuitting(); |
| 259 | virtual void Restart(); |
| 260 | // Not all message queues actually process messages (such as SignalThread). |
| 261 | // In those cases, it's important to know, before posting, that it won't be |
| 262 | // Processed. Normally, this would be true until IsQuitting() is true. |
| 263 | virtual bool IsProcessingMessagesForTesting(); |
| 264 |
| 265 | // Amount of time until the next message can be retrieved |
| 266 | virtual int GetDelay(); |
| 267 |
| 268 | bool empty() const { return size() == 0u; } |
| 269 | size_t size() const { |
| 270 | webrtc::MutexLock lock(&mutex_); |
| 271 | return messages_.size() + delayed_messages_.size(); |
| 272 | } |
| 273 |
| 274 | bool IsCurrent() const; |
| 275 |
| 276 | // Sleeps the calling thread for the specified number of milliseconds, during |
| 277 | // which time no processing is performed. Returns false if sleeping was |
| 278 | // interrupted by a signal (POSIX only). |
| 279 | static bool SleepMs(int millis); |
| 280 |
| 281 | // Sets the thread's name, for debugging. Must be called before Start(). |
| 282 | // If `obj` is non-null, its value is appended to `name`. |
| 283 | const std::string& name() const { return name_; } |
| 284 | bool SetName(absl::string_view name, const void* obj); |
| 285 |
| 286 | // Sets the expected processing time in ms. The thread will write |
| 287 | // log messages when Dispatch() takes more time than this. |
| 288 | // Default is 50 ms. |
| 289 | void SetDispatchWarningMs(int deadline); |
| 290 |
| 291 | // Starts the execution of the thread. |
| 292 | bool Start(); |
| 293 |
| 294 | // Tells the thread to stop and waits until it is joined. |
| 295 | // Never call Stop on the current thread. Instead use the inherited Quit |
| 296 | // function which will exit the base Thread without terminating the |
| 297 | // underlying OS thread. |
| 298 | virtual void Stop(); |
| 299 |
| 300 | // By default, Thread::Run() calls ProcessMessages(kForever). To do other |
| 301 | // work, override Run(). To receive and dispatch messages, call |
| 302 | // ProcessMessages occasionally. |
| 303 | virtual void Run(); |
| 304 |
| 305 | // Convenience method to invoke a functor on another thread. |
| 306 | // Blocks the current thread until execution is complete. |
| 307 | // Ex: thread.BlockingCall([&] { result = MyFunctionReturningBool(); }); |
| 308 | // NOTE: This function can only be called when synchronous calls are allowed. |
| 309 | // See ScopedDisallowBlockingCalls for details. |
| 310 | // NOTE: Blocking calls are DISCOURAGED, consider if what you're doing can |
| 311 | // be achieved with PostTask() and callbacks instead. |
| 312 | void BlockingCall( |
| 313 | FunctionView<void()> functor, |
| 314 | const webrtc::Location& location = webrtc::Location::Current()) { |
| 315 | BlockingCallImpl(std::move(functor), location); |
| 316 | } |
| 317 |
| 318 | template <typename Functor, |
| 319 | typename ReturnT = std::invoke_result_t<Functor>, |
| 320 | typename = typename std::enable_if_t<!std::is_void_v<ReturnT>>> |
| 321 | ReturnT BlockingCall( |
| 322 | Functor&& functor, |
| 323 | const webrtc::Location& location = webrtc::Location::Current()) { |
| 324 | ReturnT result; |
| 325 | BlockingCall([&] { result = std::forward<Functor>(functor)(); }, location); |
| 326 | return result; |
| 327 | } |
| 328 |
| 329 | // Allows BlockingCall to specified `thread`. Thread never will be |
| 330 | // dereferenced and will be used only for reference-based comparison, so |
| 331 | // instance can be safely deleted. If NDEBUG is defined and RTC_DCHECK_IS_ON |
| 332 | // is undefined do nothing. |
| 333 | void AllowInvokesToThread(Thread* thread); |
| 334 |
| 335 | // If NDEBUG is defined and RTC_DCHECK_IS_ON is undefined do nothing. |
| 336 | void DisallowAllInvokes(); |
| 337 | // Returns true if `target` was allowed by AllowInvokesToThread() or if no |
| 338 | // calls were made to AllowInvokesToThread and DisallowAllInvokes. Otherwise |
| 339 | // returns false. |
| 340 | // If NDEBUG is defined and RTC_DCHECK_IS_ON is undefined always returns |
| 341 | // true. |
| 342 | bool IsInvokeToThreadAllowed(rtc::Thread* target); |
| 343 |
| 344 | // From TaskQueueBase |
| 345 | void Delete() override; |
| 346 |
| 347 | // ProcessMessages will process I/O and dispatch messages until: |
| 348 | // 1) cms milliseconds have elapsed (returns true) |
| 349 | // 2) Stop() is called (returns false) |
| 350 | bool ProcessMessages(int cms); |
| 351 |
| 352 | // Returns true if this is a thread that we created using the standard |
| 353 | // constructor, false if it was created by a call to |
| 354 | // ThreadManager::WrapCurrentThread(). The main thread of an application |
| 355 | // is generally not owned, since the OS representation of the thread |
| 356 | // obviously exists before we can get to it. |
| 357 | // You cannot call Start on non-owned threads. |
| 358 | bool IsOwned(); |
| 359 |
| 360 | // Expose private method IsRunning() for tests. |
| 361 | // |
| 362 | // DANGER: this is a terrible public API. Most callers that might want to |
| 363 | // call this likely do not have enough control/knowledge of the Thread in |
| 364 | // question to guarantee that the returned value remains true for the duration |
| 365 | // of whatever code is conditionally executing because of the return value! |
| 366 | bool RunningForTest() { return IsRunning(); } |
| 367 |
| 368 | // These functions are public to avoid injecting test hooks. Don't call them |
| 369 | // outside of tests. |
| 370 | // This method should be called when thread is created using non standard |
| 371 | // method, like derived implementation of rtc::Thread and it can not be |
| 372 | // started by calling Start(). This will set started flag to true and |
| 373 | // owned to false. This must be called from the current thread. |
| 374 | bool WrapCurrent(); |
| 375 | void UnwrapCurrent(); |
| 376 |
| 377 | // Sets the per-thread allow-blocking-calls flag to false; this is |
| 378 | // irrevocable. Must be called on this thread. |
| 379 | void DisallowBlockingCalls() { SetAllowBlockingCalls(false); } |
| 380 |
| 381 | protected: |
| 382 | class CurrentThreadSetter : CurrentTaskQueueSetter { |
| 383 | public: |
| 384 | explicit CurrentThreadSetter(Thread* thread) |
| 385 | : CurrentTaskQueueSetter(thread), |
| 386 | manager_(rtc::ThreadManager::Instance()), |
| 387 | previous_(manager_->CurrentThread()) { |
| 388 | manager_->ChangeCurrentThreadForTest(thread); |
| 389 | } |
| 390 | ~CurrentThreadSetter() { manager_->ChangeCurrentThreadForTest(previous_); } |
| 391 |
| 392 | private: |
| 393 | rtc::ThreadManager* const manager_; |
| 394 | rtc::Thread* const previous_; |
| 395 | }; |
| 396 |
| 397 | // DelayedMessage goes into a priority queue, sorted by trigger time. Messages |
| 398 | // with the same trigger time are processed in num_ (FIFO) order. |
| 399 | struct DelayedMessage { |
| 400 | bool operator<(const DelayedMessage& dmsg) const { |
| 401 | return (dmsg.run_time_ms < run_time_ms) || |
| 402 | ((dmsg.run_time_ms == run_time_ms) && |
| 403 | (dmsg.message_number < message_number)); |
| 404 | } |
| 405 |
| 406 | int64_t delay_ms; // for debugging |
| 407 | int64_t run_time_ms; |
| 408 | // Monotonicaly incrementing number used for ordering of messages |
| 409 | // targeted to execute at the same time. |
| 410 | uint32_t message_number; |
| 411 | // std::priority_queue doesn't allow to extract elements, but functor |
| 412 | // is move-only and thus need to be changed when pulled out of the |
| 413 | // priority queue. That is ok because `functor` doesn't affect operator< |
| 414 | mutable absl::AnyInvocable<void() &&> functor; |
| 415 | }; |
| 416 |
| 417 | // TaskQueueBase implementation. |
| 418 | void PostTaskImpl(absl::AnyInvocable<void() &&> task, |
| 419 | const PostTaskTraits& traits, |
| 420 | const webrtc::Location& location) override; |
| 421 | void PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task, |
| 422 | webrtc::TimeDelta delay, |
| 423 | const PostDelayedTaskTraits& traits, |
| 424 | const webrtc::Location& location) override; |
| 425 |
| 426 | virtual void BlockingCallImpl(FunctionView<void()> functor, |
| 427 | const webrtc::Location& location); |
| 428 |
| 429 | // Perform initialization, subclasses must call this from their constructor |
| 430 | // if false was passed as init_queue to the Thread constructor. |
| 431 | void DoInit(); |
| 432 |
| 433 | // Perform cleanup; subclasses must call this from the destructor, |
| 434 | // and are not expected to actually hold the lock. |
| 435 | void DoDestroy() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
| 436 |
| 437 | void WakeUpSocketServer(); |
| 438 |
| 439 | // Same as WrapCurrent except that it never fails as it does not try to |
| 440 | // acquire the synchronization access of the thread. The caller should never |
| 441 | // call Stop() or Join() on this thread. |
| 442 | void SafeWrapCurrent(); |
| 443 |
| 444 | // Blocks the calling thread until this thread has terminated. |
| 445 | void Join(); |
| 446 |
| 447 | static void AssertBlockingIsAllowedOnCurrentThread(); |
| 448 |
| 449 | friend class ScopedDisallowBlockingCalls; |
| 450 |
| 451 | private: |
| 452 | static const int kSlowDispatchLoggingThreshold = 50; // 50 ms |
| 453 |
| 454 | // Get() will process I/O until: |
| 455 | // 1) A task is available (returns it) |
| 456 | // 2) cmsWait seconds have elapsed (returns empty task) |
| 457 | // 3) Stop() is called (returns empty task) |
| 458 | absl::AnyInvocable<void() &&> Get(int cmsWait); |
| 459 | void Dispatch(absl::AnyInvocable<void() &&> task); |
| 460 |
| 461 | // Sets the per-thread allow-blocking-calls flag and returns the previous |
| 462 | // value. Must be called on this thread. |
| 463 | bool SetAllowBlockingCalls(bool allow); |
| 464 |
| 465 | #if defined(WEBRTC_WIN) |
| 466 | static DWORD WINAPI PreRun(LPVOID context); |
| 467 | #else |
| 468 | static void* PreRun(void* pv); |
| 469 | #endif |
| 470 |
| 471 | // ThreadManager calls this instead WrapCurrent() because |
| 472 | // ThreadManager::Instance() cannot be used while ThreadManager is |
| 473 | // being created. |
| 474 | // The method tries to get synchronization rights of the thread on Windows if |
| 475 | // `need_synchronize_access` is true. |
| 476 | bool WrapCurrentWithThreadManager(ThreadManager* thread_manager, |
| 477 | bool need_synchronize_access); |
| 478 |
| 479 | // Return true if the thread is currently running. |
| 480 | bool IsRunning(); |
| 481 |
| 482 | // Called by the ThreadManager when being set as the current thread. |
| 483 | void EnsureIsCurrentTaskQueue(); |
| 484 |
| 485 | // Called by the ThreadManager when being unset as the current thread. |
| 486 | void ClearCurrentTaskQueue(); |
| 487 |
| 488 | std::queue<absl::AnyInvocable<void() &&>> messages_ RTC_GUARDED_BY(mutex_); |
| 489 | std::priority_queue<DelayedMessage> delayed_messages_ RTC_GUARDED_BY(mutex_); |
| 490 | uint32_t delayed_next_num_ RTC_GUARDED_BY(mutex_); |
| 491 | #if RTC_DCHECK_IS_ON |
| 492 | uint32_t blocking_call_count_ RTC_GUARDED_BY(this) = 0; |
| 493 | uint32_t could_be_blocking_call_count_ RTC_GUARDED_BY(this) = 0; |
| 494 | std::vector<Thread*> allowed_threads_ RTC_GUARDED_BY(this); |
| 495 | bool invoke_policy_enabled_ RTC_GUARDED_BY(this) = false; |
| 496 | #endif |
| 497 | mutable webrtc::Mutex mutex_; |
| 498 | bool fInitialized_; |
| 499 | bool fDestroyed_; |
| 500 |
| 501 | std::atomic<int> stop_; |
| 502 |
| 503 | // The SocketServer might not be owned by Thread. |
| 504 | SocketServer* const ss_; |
| 505 | // Used if SocketServer ownership lies with `this`. |
| 506 | std::unique_ptr<SocketServer> own_ss_; |
| 507 |
| 508 | std::string name_; |
| 509 |
| 510 | // TODO(tommi): Add thread checks for proper use of control methods. |
| 511 | // Ideally we should be able to just use PlatformThread. |
| 512 |
| 513 | #if defined(WEBRTC_POSIX) |
| 514 | pthread_t thread_ = 0; |
| 515 | #endif |
| 516 |
| 517 | #if defined(WEBRTC_WIN) |
| 518 | HANDLE thread_ = nullptr; |
| 519 | DWORD thread_id_ = 0; |
| 520 | #endif |
| 521 |
| 522 | // Indicates whether or not ownership of the worker thread lies with |
| 523 | // this instance or not. (i.e. owned_ == !wrapped). |
| 524 | // Must only be modified when the worker thread is not running. |
| 525 | bool owned_ = true; |
| 526 |
| 527 | // Only touched from the worker thread itself. |
| 528 | bool blocking_calls_allowed_ = true; |
| 529 |
| 530 | std::unique_ptr<TaskQueueBase::CurrentTaskQueueSetter> |
| 531 | task_queue_registration_; |
| 532 |
| 533 | friend class ThreadManager; |
| 534 |
| 535 | int dispatch_warning_ms_ RTC_GUARDED_BY(this) = kSlowDispatchLoggingThreshold; |
| 536 | }; |
| 537 |
| 538 | // AutoThread automatically installs itself at construction |
| 539 | // uninstalls at destruction, if a Thread object is |
| 540 | // _not already_ associated with the current OS thread. |
| 541 | // |
| 542 | // NOTE: *** This class should only be used by tests *** |
| 543 | // |
| 544 | class AutoThread : public Thread { |
| 545 | public: |
| 546 | AutoThread(); |
| 547 | ~AutoThread() override; |
| 548 |
| 549 | AutoThread(const AutoThread&) = delete; |
| 550 | AutoThread& operator=(const AutoThread&) = delete; |
| 551 | }; |
| 552 |
| 553 | // AutoSocketServerThread automatically installs itself at |
| 554 | // construction and uninstalls at destruction. If a Thread object is |
| 555 | // already associated with the current OS thread, it is temporarily |
| 556 | // disassociated and restored by the destructor. |
| 557 |
| 558 | class AutoSocketServerThread : public Thread { |
| 559 | public: |
| 560 | explicit AutoSocketServerThread(SocketServer* ss); |
| 561 | ~AutoSocketServerThread() override; |
| 562 |
| 563 | AutoSocketServerThread(const AutoSocketServerThread&) = delete; |
| 564 | AutoSocketServerThread& operator=(const AutoSocketServerThread&) = delete; |
| 565 |
| 566 | private: |
| 567 | rtc::Thread* old_thread_; |
| 568 | }; |
| 569 | } // namespace rtc |
| 570 |
| 571 | #endif // RTC_BASE_THREAD_H_ |
| 572 |