1*d9f75844SAndroid Build Coastguard Worker /* 2*d9f75844SAndroid Build Coastguard Worker * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 3*d9f75844SAndroid Build Coastguard Worker * 4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license 5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source 6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found 7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may 8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree. 9*d9f75844SAndroid Build Coastguard Worker */ 10*d9f75844SAndroid Build Coastguard Worker 11*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_BASE_PLATFORM_THREAD_H_ 12*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_PLATFORM_THREAD_H_ 13*d9f75844SAndroid Build Coastguard Worker 14*d9f75844SAndroid Build Coastguard Worker #include <functional> 15*d9f75844SAndroid Build Coastguard Worker #include <string> 16*d9f75844SAndroid Build Coastguard Worker 17*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h" 18*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h" 19*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/platform_thread_types.h" 20*d9f75844SAndroid Build Coastguard Worker 21*d9f75844SAndroid Build Coastguard Worker namespace rtc { 22*d9f75844SAndroid Build Coastguard Worker 23*d9f75844SAndroid Build Coastguard Worker enum class ThreadPriority { 24*d9f75844SAndroid Build Coastguard Worker kLow = 1, 25*d9f75844SAndroid Build Coastguard Worker kNormal, 26*d9f75844SAndroid Build Coastguard Worker kHigh, 27*d9f75844SAndroid Build Coastguard Worker kRealtime, 28*d9f75844SAndroid Build Coastguard Worker }; 29*d9f75844SAndroid Build Coastguard Worker 30*d9f75844SAndroid Build Coastguard Worker struct ThreadAttributes { 31*d9f75844SAndroid Build Coastguard Worker ThreadPriority priority = ThreadPriority::kNormal; SetPriorityThreadAttributes32*d9f75844SAndroid Build Coastguard Worker ThreadAttributes& SetPriority(ThreadPriority priority_param) { 33*d9f75844SAndroid Build Coastguard Worker priority = priority_param; 34*d9f75844SAndroid Build Coastguard Worker return *this; 35*d9f75844SAndroid Build Coastguard Worker } 36*d9f75844SAndroid Build Coastguard Worker }; 37*d9f75844SAndroid Build Coastguard Worker 38*d9f75844SAndroid Build Coastguard Worker // Represents a simple worker thread. 39*d9f75844SAndroid Build Coastguard Worker class PlatformThread final { 40*d9f75844SAndroid Build Coastguard Worker public: 41*d9f75844SAndroid Build Coastguard Worker // Handle is the base platform thread handle. 42*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_WIN) 43*d9f75844SAndroid Build Coastguard Worker using Handle = HANDLE; 44*d9f75844SAndroid Build Coastguard Worker #else 45*d9f75844SAndroid Build Coastguard Worker using Handle = pthread_t; 46*d9f75844SAndroid Build Coastguard Worker #endif // defined(WEBRTC_WIN) 47*d9f75844SAndroid Build Coastguard Worker // This ctor creates the PlatformThread with an unset handle (returning true 48*d9f75844SAndroid Build Coastguard Worker // in empty()) and is provided for convenience. 49*d9f75844SAndroid Build Coastguard Worker // TODO(bugs.webrtc.org/12727) Look into if default and move support can be 50*d9f75844SAndroid Build Coastguard Worker // removed. 51*d9f75844SAndroid Build Coastguard Worker PlatformThread() = default; 52*d9f75844SAndroid Build Coastguard Worker 53*d9f75844SAndroid Build Coastguard Worker // Moves `rhs` into this, storing an empty state in `rhs`. 54*d9f75844SAndroid Build Coastguard Worker // TODO(bugs.webrtc.org/12727) Look into if default and move support can be 55*d9f75844SAndroid Build Coastguard Worker // removed. 56*d9f75844SAndroid Build Coastguard Worker PlatformThread(PlatformThread&& rhs); 57*d9f75844SAndroid Build Coastguard Worker 58*d9f75844SAndroid Build Coastguard Worker // Copies won't work since we'd have problems with joinable threads. 59*d9f75844SAndroid Build Coastguard Worker PlatformThread(const PlatformThread&) = delete; 60*d9f75844SAndroid Build Coastguard Worker PlatformThread& operator=(const PlatformThread&) = delete; 61*d9f75844SAndroid Build Coastguard Worker 62*d9f75844SAndroid Build Coastguard Worker // Moves `rhs` into this, storing an empty state in `rhs`. 63*d9f75844SAndroid Build Coastguard Worker // TODO(bugs.webrtc.org/12727) Look into if default and move support can be 64*d9f75844SAndroid Build Coastguard Worker // removed. 65*d9f75844SAndroid Build Coastguard Worker PlatformThread& operator=(PlatformThread&& rhs); 66*d9f75844SAndroid Build Coastguard Worker 67*d9f75844SAndroid Build Coastguard Worker // For a PlatformThread that's been spawned joinable, the destructor suspends 68*d9f75844SAndroid Build Coastguard Worker // the calling thread until the created thread exits unless the thread has 69*d9f75844SAndroid Build Coastguard Worker // already exited. 70*d9f75844SAndroid Build Coastguard Worker virtual ~PlatformThread(); 71*d9f75844SAndroid Build Coastguard Worker 72*d9f75844SAndroid Build Coastguard Worker // Finalizes any allocated resources. 73*d9f75844SAndroid Build Coastguard Worker // For a PlatformThread that's been spawned joinable, Finalize() suspends 74*d9f75844SAndroid Build Coastguard Worker // the calling thread until the created thread exits unless the thread has 75*d9f75844SAndroid Build Coastguard Worker // already exited. 76*d9f75844SAndroid Build Coastguard Worker // empty() returns true after completion. 77*d9f75844SAndroid Build Coastguard Worker void Finalize(); 78*d9f75844SAndroid Build Coastguard Worker 79*d9f75844SAndroid Build Coastguard Worker // Returns true if default constructed, moved from, or Finalize()ed. empty()80*d9f75844SAndroid Build Coastguard Worker bool empty() const { return !handle_.has_value(); } 81*d9f75844SAndroid Build Coastguard Worker 82*d9f75844SAndroid Build Coastguard Worker // Creates a started joinable thread which will be joined when the returned 83*d9f75844SAndroid Build Coastguard Worker // PlatformThread destructs or Finalize() is called. 84*d9f75844SAndroid Build Coastguard Worker static PlatformThread SpawnJoinable( 85*d9f75844SAndroid Build Coastguard Worker std::function<void()> thread_function, 86*d9f75844SAndroid Build Coastguard Worker absl::string_view name, 87*d9f75844SAndroid Build Coastguard Worker ThreadAttributes attributes = ThreadAttributes()); 88*d9f75844SAndroid Build Coastguard Worker 89*d9f75844SAndroid Build Coastguard Worker // Creates a started detached thread. The caller has to use external 90*d9f75844SAndroid Build Coastguard Worker // synchronization as nothing is provided by the PlatformThread construct. 91*d9f75844SAndroid Build Coastguard Worker static PlatformThread SpawnDetached( 92*d9f75844SAndroid Build Coastguard Worker std::function<void()> thread_function, 93*d9f75844SAndroid Build Coastguard Worker absl::string_view name, 94*d9f75844SAndroid Build Coastguard Worker ThreadAttributes attributes = ThreadAttributes()); 95*d9f75844SAndroid Build Coastguard Worker 96*d9f75844SAndroid Build Coastguard Worker // Returns the base platform thread handle of this thread. 97*d9f75844SAndroid Build Coastguard Worker absl::optional<Handle> GetHandle() const; 98*d9f75844SAndroid Build Coastguard Worker 99*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_WIN) 100*d9f75844SAndroid Build Coastguard Worker // Queue a Windows APC function that runs when the thread is alertable. 101*d9f75844SAndroid Build Coastguard Worker bool QueueAPC(PAPCFUNC apc_function, ULONG_PTR data); 102*d9f75844SAndroid Build Coastguard Worker #endif 103*d9f75844SAndroid Build Coastguard Worker 104*d9f75844SAndroid Build Coastguard Worker private: 105*d9f75844SAndroid Build Coastguard Worker PlatformThread(Handle handle, bool joinable); 106*d9f75844SAndroid Build Coastguard Worker static PlatformThread SpawnThread(std::function<void()> thread_function, 107*d9f75844SAndroid Build Coastguard Worker absl::string_view name, 108*d9f75844SAndroid Build Coastguard Worker ThreadAttributes attributes, 109*d9f75844SAndroid Build Coastguard Worker bool joinable); 110*d9f75844SAndroid Build Coastguard Worker 111*d9f75844SAndroid Build Coastguard Worker absl::optional<Handle> handle_; 112*d9f75844SAndroid Build Coastguard Worker bool joinable_ = false; 113*d9f75844SAndroid Build Coastguard Worker }; 114*d9f75844SAndroid Build Coastguard Worker 115*d9f75844SAndroid Build Coastguard Worker } // namespace rtc 116*d9f75844SAndroid Build Coastguard Worker 117*d9f75844SAndroid Build Coastguard Worker #endif // RTC_BASE_PLATFORM_THREAD_H_ 118