Tommi | bebc690 | 2015-05-18 07:51:42 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef RTC_BASE_PLATFORM_THREAD_H_ |
| 12 | #define RTC_BASE_PLATFORM_THREAD_H_ |
Tommi | bebc690 | 2015-05-18 07:51:42 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #ifndef WEBRTC_WIN |
| 15 | #include <pthread.h> |
| 16 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 17 | #include <string> |
pbos | 12411ef | 2015-11-23 22:47:56 | [diff] [blame] | 18 | |
Danil Chapovalov | 5a1a6db | 2019-01-17 18:55:46 | [diff] [blame] | 19 | #include "absl/strings/string_view.h" |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 20 | #include "api/sequence_checker.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 21 | #include "rtc_base/constructor_magic.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 22 | #include "rtc_base/platform_thread_types.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 23 | |
| 24 | namespace rtc { |
| 25 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 26 | // Callback function that the spawned thread will enter once spawned. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 27 | typedef void (*ThreadRunFunction)(void*); |
| 28 | |
| 29 | enum ThreadPriority { |
| 30 | #ifdef WEBRTC_WIN |
| 31 | kLowPriority = THREAD_PRIORITY_BELOW_NORMAL, |
| 32 | kNormalPriority = THREAD_PRIORITY_NORMAL, |
| 33 | kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL, |
| 34 | kHighestPriority = THREAD_PRIORITY_HIGHEST, |
| 35 | kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL |
| 36 | #else |
| 37 | kLowPriority = 1, |
| 38 | kNormalPriority = 2, |
| 39 | kHighPriority = 3, |
| 40 | kHighestPriority = 4, |
| 41 | kRealtimePriority = 5 |
| 42 | #endif |
| 43 | }; |
| 44 | |
| 45 | // Represents a simple worker thread. The implementation must be assumed |
| 46 | // to be single threaded, meaning that all methods of the class, must be |
| 47 | // called from the same thread, including instantiation. |
| 48 | class PlatformThread { |
| 49 | public: |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 50 | PlatformThread(ThreadRunFunction func, |
| 51 | void* obj, |
Danil Chapovalov | 5a1a6db | 2019-01-17 18:55:46 | [diff] [blame] | 52 | absl::string_view thread_name, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 53 | ThreadPriority priority = kNormalPriority); |
| 54 | virtual ~PlatformThread(); |
| 55 | |
| 56 | const std::string& name() const { return name_; } |
| 57 | |
| 58 | // Spawns a thread and tries to set thread priority according to the priority |
| 59 | // from when CreateThread was called. |
| 60 | void Start(); |
| 61 | |
| 62 | bool IsRunning() const; |
| 63 | |
| 64 | // Returns an identifier for the worker thread that can be used to do |
| 65 | // thread checks. |
| 66 | PlatformThreadRef GetThreadRef() const; |
| 67 | |
| 68 | // Stops (joins) the spawned thread. |
| 69 | void Stop(); |
| 70 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 71 | protected: |
| 72 | #if defined(WEBRTC_WIN) |
| 73 | // Exposed to derived classes to allow for special cases specific to Windows. |
| 74 | bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data); |
| 75 | #endif |
| 76 | |
| 77 | private: |
| 78 | void Run(); |
Niels Möller | 4731f00 | 2019-05-03 07:34:24 | [diff] [blame] | 79 | bool SetPriority(ThreadPriority priority); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 80 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 81 | ThreadRunFunction const run_function_ = nullptr; |
| 82 | const ThreadPriority priority_ = kNormalPriority; |
| 83 | void* const obj_; |
| 84 | // TODO(pbos): Make sure call sites use string literals and update to a const |
| 85 | // char* instead of a std::string. |
| 86 | const std::string name_; |
Artem Titov | c8421c4 | 2021-02-02 09:57:19 | [diff] [blame] | 87 | webrtc::SequenceChecker thread_checker_; |
| 88 | webrtc::SequenceChecker spawned_thread_checker_; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 89 | #if defined(WEBRTC_WIN) |
| 90 | static DWORD WINAPI StartThread(void* param); |
| 91 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 92 | HANDLE thread_ = nullptr; |
| 93 | DWORD thread_id_ = 0; |
| 94 | #else |
| 95 | static void* StartThread(void* param); |
| 96 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 97 | pthread_t thread_ = 0; |
| 98 | #endif // defined(WEBRTC_WIN) |
| 99 | RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread); |
| 100 | }; |
| 101 | |
| 102 | } // namespace rtc |
pbos | 12411ef | 2015-11-23 22:47:56 | [diff] [blame] | 103 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 104 | #endif // RTC_BASE_PLATFORM_THREAD_H_ |