PlatformThread: add support for detached threads.
The change introduces support for detachable PlatformThreads, for which
the Stop() call doesn't wait until the thread has finished executing.
The change also introduces rtc::ThreadAttributes that carries priority
and detachability thread attributes. It additionally refactors all
known use to use the new semantics.
Bug: b:181572711, webrtc:12659
Change-Id: Id96e87c2a0dafabc8047767d241fd5da4505d14c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/214704
Reviewed-by: Tommi <tommi@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Markus Handell <handellm@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33796}
diff --git a/rtc_base/platform_thread.h b/rtc_base/platform_thread.h
index 64a74d8..35c0e27 100644
--- a/rtc_base/platform_thread.h
+++ b/rtc_base/platform_thread.h
@@ -42,6 +42,20 @@
#endif
};
+struct ThreadAttributes {
+ ThreadPriority priority = kNormalPriority;
+ bool joinable = true;
+
+ ThreadAttributes& SetPriority(ThreadPriority priority_param) {
+ priority = priority_param;
+ return *this;
+ }
+ ThreadAttributes& SetDetached() {
+ joinable = false;
+ return *this;
+ }
+};
+
// Represents a simple worker thread. The implementation must be assumed
// to be single threaded, meaning that all methods of the class, must be
// called from the same thread, including instantiation.
@@ -50,13 +64,14 @@
PlatformThread(ThreadRunFunction func,
void* obj,
absl::string_view thread_name,
- ThreadPriority priority = kNormalPriority);
+ ThreadAttributes attributes = ThreadAttributes());
virtual ~PlatformThread();
const std::string& name() const { return name_; }
// Spawns a thread and tries to set thread priority according to the priority
// from when CreateThread was called.
+ // Start can only be called after the constructor or after a call to Stop().
void Start();
bool IsRunning() const;
@@ -65,7 +80,11 @@
// thread checks.
PlatformThreadRef GetThreadRef() const;
- // Stops (joins) the spawned thread.
+ // Stop() prepares the PlatformThread for destruction or another call to
+ // Start(). For a PlatformThread that's been created with
+ // ThreadAttributes::joinable true (the default), Stop() suspends the calling
+ // thread until the created thread exits unless the thread has already exited.
+ // Stop() can only be called after calling Start().
void Stop();
protected:
@@ -75,25 +94,17 @@
#endif
private:
- void Run();
- bool SetPriority(ThreadPriority priority);
-
ThreadRunFunction const run_function_ = nullptr;
- const ThreadPriority priority_ = kNormalPriority;
+ const ThreadAttributes attributes_;
void* const obj_;
// TODO(pbos): Make sure call sites use string literals and update to a const
// char* instead of a std::string.
const std::string name_;
webrtc::SequenceChecker thread_checker_;
- webrtc::SequenceChecker 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 // defined(WEBRTC_WIN)
RTC_DISALLOW_COPY_AND_ASSIGN(PlatformThread);