123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #ifndef RTC_BASE_PLATFORM_THREAD_H_
- #define RTC_BASE_PLATFORM_THREAD_H_
- #ifndef WEBRTC_WIN
- #include <pthread.h>
- #endif
- #include <string>
- #include "absl/strings/string_view.h"
- #include "rtc_base/constructor_magic.h"
- #include "rtc_base/platform_thread_types.h"
- #include "rtc_base/thread_checker.h"
- namespace rtc {
- typedef void (*ThreadRunFunction)(void*);
- enum ThreadPriority {
- #ifdef WEBRTC_WIN
- kLowPriority = THREAD_PRIORITY_BELOW_NORMAL,
- kNormalPriority = THREAD_PRIORITY_NORMAL,
- kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL,
- kHighestPriority = THREAD_PRIORITY_HIGHEST,
- kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL
- #else
- kLowPriority = 1,
- kNormalPriority = 2,
- kHighPriority = 3,
- kHighestPriority = 4,
- kRealtimePriority = 5
- #endif
- };
- class PlatformThread {
- public:
- PlatformThread(ThreadRunFunction func,
- void* obj,
- absl::string_view thread_name,
- ThreadPriority priority = kNormalPriority);
- virtual ~PlatformThread();
- const std::string& name() const { return name_; }
-
-
- void Start();
- bool IsRunning() const;
-
-
- PlatformThreadRef GetThreadRef() const;
-
- void Stop();
- protected:
- #if defined(WEBRTC_WIN)
-
- bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data);
- #endif
- private:
- void Run();
- bool SetPriority(ThreadPriority priority);
- ThreadRunFunction const run_function_ = nullptr;
- const ThreadPriority priority_ = kNormalPriority;
- void* const obj_;
-
-
- const std::string name_;
- rtc::ThreadChecker thread_checker_;
- rtc::ThreadChecker spawned_thread_checker_;
- #if defined(WEBRTC_WIN)
- static DWORD WINAPI StartThread(void* param);
- HANDLE thread_ = nullptr;
- DWORD thread_id_ = 0;
- #else
- static void* StartThread(void* param);
- pthread_t thread_ = 0;
- #endif
- RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread);
- };
- }
- #endif
|