platform_thread.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2015 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. #ifndef RTC_BASE_PLATFORM_THREAD_H_
  11. #define RTC_BASE_PLATFORM_THREAD_H_
  12. #ifndef WEBRTC_WIN
  13. #include <pthread.h>
  14. #endif
  15. #include <string>
  16. #include "absl/strings/string_view.h"
  17. #include "rtc_base/constructor_magic.h"
  18. #include "rtc_base/platform_thread_types.h"
  19. #include "rtc_base/thread_checker.h"
  20. namespace rtc {
  21. // Callback function that the spawned thread will enter once spawned.
  22. typedef void (*ThreadRunFunction)(void*);
  23. enum ThreadPriority {
  24. #ifdef WEBRTC_WIN
  25. kLowPriority = THREAD_PRIORITY_BELOW_NORMAL,
  26. kNormalPriority = THREAD_PRIORITY_NORMAL,
  27. kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL,
  28. kHighestPriority = THREAD_PRIORITY_HIGHEST,
  29. kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL
  30. #else
  31. kLowPriority = 1,
  32. kNormalPriority = 2,
  33. kHighPriority = 3,
  34. kHighestPriority = 4,
  35. kRealtimePriority = 5
  36. #endif
  37. };
  38. // Represents a simple worker thread. The implementation must be assumed
  39. // to be single threaded, meaning that all methods of the class, must be
  40. // called from the same thread, including instantiation.
  41. class PlatformThread {
  42. public:
  43. PlatformThread(ThreadRunFunction func,
  44. void* obj,
  45. absl::string_view thread_name,
  46. ThreadPriority priority = kNormalPriority);
  47. virtual ~PlatformThread();
  48. const std::string& name() const { return name_; }
  49. // Spawns a thread and tries to set thread priority according to the priority
  50. // from when CreateThread was called.
  51. void Start();
  52. bool IsRunning() const;
  53. // Returns an identifier for the worker thread that can be used to do
  54. // thread checks.
  55. PlatformThreadRef GetThreadRef() const;
  56. // Stops (joins) the spawned thread.
  57. void Stop();
  58. protected:
  59. #if defined(WEBRTC_WIN)
  60. // Exposed to derived classes to allow for special cases specific to Windows.
  61. bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data);
  62. #endif
  63. private:
  64. void Run();
  65. bool SetPriority(ThreadPriority priority);
  66. ThreadRunFunction const run_function_ = nullptr;
  67. const ThreadPriority priority_ = kNormalPriority;
  68. void* const obj_;
  69. // TODO(pbos): Make sure call sites use string literals and update to a const
  70. // char* instead of a std::string.
  71. const std::string name_;
  72. rtc::ThreadChecker thread_checker_;
  73. rtc::ThreadChecker spawned_thread_checker_;
  74. #if defined(WEBRTC_WIN)
  75. static DWORD WINAPI StartThread(void* param);
  76. HANDLE thread_ = nullptr;
  77. DWORD thread_id_ = 0;
  78. #else
  79. static void* StartThread(void* param);
  80. pthread_t thread_ = 0;
  81. #endif // defined(WEBRTC_WIN)
  82. RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread);
  83. };
  84. } // namespace rtc
  85. #endif // RTC_BASE_PLATFORM_THREAD_H_