Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [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 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_PLATFORM_THREAD_H_ |
| 12 | #define RTC_BASE_PLATFORM_THREAD_H_ |
Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 13 | |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 14 | #ifndef WEBRTC_WIN |
| 15 | #include <pthread.h> |
| 16 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 17 | #include <string> |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 18 | |
Danil Chapovalov | 5a1a6db | 2019-01-17 19:55:46 +0100 | [diff] [blame] | 19 | #include "absl/strings/string_view.h" |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 20 | #include "api/sequence_checker.h" |
| 21 | #include "rtc_base/constructor_magic.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/platform_thread_types.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 23 | |
| 24 | namespace rtc { |
| 25 | |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 26 | // Callback function that the spawned thread will enter once spawned. |
| 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 |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 45 | struct ThreadAttributes { |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 46 | ThreadPriority priority = kNormalPriority; |
| 47 | bool joinable = true; |
| 48 | |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 49 | ThreadAttributes& SetPriority(ThreadPriority priority_param) { |
| 50 | priority = priority_param; |
| 51 | return *this; |
| 52 | } |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 53 | ThreadAttributes& SetDetached() { |
| 54 | joinable = false; |
| 55 | return *this; |
| 56 | } |
Markus Handell | 97c4458 | 2021-04-20 17:41:54 +0200 | [diff] [blame] | 57 | }; |
| 58 | |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 59 | // Represents a simple worker thread. The implementation must be assumed |
| 60 | // to be single threaded, meaning that all methods of the class, must be |
| 61 | // called from the same thread, including instantiation. |
| 62 | class PlatformThread { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 63 | public: |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 64 | PlatformThread(ThreadRunFunction func, |
| 65 | void* obj, |
| 66 | absl::string_view thread_name, |
| 67 | ThreadAttributes attributes = ThreadAttributes()); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 68 | virtual ~PlatformThread(); |
| 69 | |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 70 | const std::string& name() const { return name_; } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 71 | |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 72 | // Spawns a thread and tries to set thread priority according to the priority |
| 73 | // from when CreateThread was called. |
| 74 | // Start can only be called after the constructor or after a call to Stop(). |
| 75 | void Start(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 76 | |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 77 | bool IsRunning() const; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 78 | |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 79 | // Returns an identifier for the worker thread that can be used to do |
| 80 | // thread checks. |
| 81 | PlatformThreadRef GetThreadRef() const; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 82 | |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 83 | // Stop() prepares the PlatformThread for destruction or another call to |
| 84 | // Start(). For a PlatformThread that's been created with |
| 85 | // ThreadAttributes::joinable true (the default), Stop() suspends the calling |
| 86 | // thread until the created thread exits unless the thread has already exited. |
| 87 | // Stop() can only be called after calling Start(). |
| 88 | void Stop(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 89 | |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 90 | protected: |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 91 | #if defined(WEBRTC_WIN) |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 92 | // Exposed to derived classes to allow for special cases specific to Windows. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 93 | bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data); |
| 94 | #endif |
| 95 | |
| 96 | private: |
Guido Urdaneta | 793bac5 | 2021-05-06 13:12:47 +0000 | [diff] [blame^] | 97 | ThreadRunFunction const run_function_ = nullptr; |
| 98 | const ThreadAttributes attributes_; |
| 99 | void* const obj_; |
| 100 | // TODO(pbos): Make sure call sites use string literals and update to a const |
| 101 | // char* instead of a std::string. |
| 102 | const std::string name_; |
| 103 | webrtc::SequenceChecker thread_checker_; |
| 104 | #if defined(WEBRTC_WIN) |
| 105 | HANDLE thread_ = nullptr; |
| 106 | DWORD thread_id_ = 0; |
| 107 | #else |
| 108 | pthread_t thread_ = 0; |
| 109 | #endif // defined(WEBRTC_WIN) |
| 110 | RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | } // namespace rtc |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 114 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 115 | #endif // RTC_BASE_PLATFORM_THREAD_H_ |