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 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [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" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/constructor_magic.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/platform_thread_types.h" |
| 22 | #include "rtc_base/thread_checker.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 23 | |
| 24 | namespace rtc { |
| 25 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 26 | // Callback function that the spawned thread will enter once spawned. |
| 27 | // A return value of false is interpreted as that the function has no |
| 28 | // more work to do and that the thread can be released. |
| 29 | typedef bool (*ThreadRunFunctionDeprecated)(void*); |
| 30 | typedef void (*ThreadRunFunction)(void*); |
| 31 | |
| 32 | enum ThreadPriority { |
| 33 | #ifdef WEBRTC_WIN |
| 34 | kLowPriority = THREAD_PRIORITY_BELOW_NORMAL, |
| 35 | kNormalPriority = THREAD_PRIORITY_NORMAL, |
| 36 | kHighPriority = THREAD_PRIORITY_ABOVE_NORMAL, |
| 37 | kHighestPriority = THREAD_PRIORITY_HIGHEST, |
| 38 | kRealtimePriority = THREAD_PRIORITY_TIME_CRITICAL |
| 39 | #else |
| 40 | kLowPriority = 1, |
| 41 | kNormalPriority = 2, |
| 42 | kHighPriority = 3, |
| 43 | kHighestPriority = 4, |
| 44 | kRealtimePriority = 5 |
| 45 | #endif |
| 46 | }; |
| 47 | |
| 48 | // Represents a simple worker thread. The implementation must be assumed |
| 49 | // to be single threaded, meaning that all methods of the class, must be |
| 50 | // called from the same thread, including instantiation. |
| 51 | class PlatformThread { |
| 52 | public: |
| 53 | PlatformThread(ThreadRunFunctionDeprecated func, |
| 54 | void* obj, |
Danil Chapovalov | 5a1a6db | 2019-01-17 19:55:46 +0100 | [diff] [blame] | 55 | absl::string_view thread_name); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 56 | PlatformThread(ThreadRunFunction func, |
| 57 | void* obj, |
Danil Chapovalov | 5a1a6db | 2019-01-17 19:55:46 +0100 | [diff] [blame] | 58 | absl::string_view thread_name, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 59 | ThreadPriority priority = kNormalPriority); |
| 60 | virtual ~PlatformThread(); |
| 61 | |
| 62 | const std::string& name() const { return name_; } |
| 63 | |
| 64 | // Spawns a thread and tries to set thread priority according to the priority |
| 65 | // from when CreateThread was called. |
| 66 | void Start(); |
| 67 | |
| 68 | bool IsRunning() const; |
| 69 | |
| 70 | // Returns an identifier for the worker thread that can be used to do |
| 71 | // thread checks. |
| 72 | PlatformThreadRef GetThreadRef() const; |
| 73 | |
| 74 | // Stops (joins) the spawned thread. |
| 75 | void Stop(); |
| 76 | |
| 77 | // Set the priority of the thread. Must be called when thread is running. |
| 78 | // TODO(tommi): Make private and only allow public support via ctor. |
| 79 | bool SetPriority(ThreadPriority priority); |
| 80 | |
| 81 | protected: |
| 82 | #if defined(WEBRTC_WIN) |
| 83 | // Exposed to derived classes to allow for special cases specific to Windows. |
| 84 | bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data); |
| 85 | #endif |
| 86 | |
| 87 | private: |
| 88 | void Run(); |
| 89 | |
| 90 | ThreadRunFunctionDeprecated const run_function_deprecated_ = nullptr; |
| 91 | ThreadRunFunction const run_function_ = nullptr; |
| 92 | const ThreadPriority priority_ = kNormalPriority; |
| 93 | void* const obj_; |
| 94 | // TODO(pbos): Make sure call sites use string literals and update to a const |
| 95 | // char* instead of a std::string. |
| 96 | const std::string name_; |
| 97 | rtc::ThreadChecker thread_checker_; |
| 98 | rtc::ThreadChecker spawned_thread_checker_; |
| 99 | #if defined(WEBRTC_WIN) |
| 100 | static DWORD WINAPI StartThread(void* param); |
| 101 | |
| 102 | bool stop_ = false; |
| 103 | HANDLE thread_ = nullptr; |
| 104 | DWORD thread_id_ = 0; |
| 105 | #else |
| 106 | static void* StartThread(void* param); |
| 107 | |
| 108 | // An atomic flag that we use to stop the thread. Only modified on the |
| 109 | // controlling thread and checked on the worker thread. |
| 110 | volatile int stop_flag_ = 0; |
| 111 | pthread_t thread_ = 0; |
| 112 | #endif // defined(WEBRTC_WIN) |
| 113 | RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread); |
| 114 | }; |
| 115 | |
| 116 | } // namespace rtc |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 117 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 118 | #endif // RTC_BASE_PLATFORM_THREAD_H_ |