refactor(sonar): migrate worker threads to std::jthread (#5398)

This commit is contained in:
Dave Lane
2026-07-12 00:30:05 -04:00
committed by GitHub
parent 40ae6c8002
commit 3377f7a73f
24 changed files with 79 additions and 48 deletions
+1 -1
View File
@@ -234,7 +234,7 @@ namespace audio {
platf::adjust_thread_priority(platf::thread_priority_e::critical);
auto samples = std::make_shared<sample_queue_t::element_type>(30);
std::thread thread {encodeThread, samples, config, channel_data};
std::jthread thread {encodeThread, samples, config, channel_data};
auto fg = util::fail_guard([&]() {
samples->stop();
+1 -1
View File
@@ -1845,7 +1845,7 @@ namespace confighttp {
return;
}
};
std::thread tcp {accept_and_run, &server};
std::jthread tcp {accept_and_run, &server};
// Wait for any event
shutdown_event->view();
+4 -4
View File
@@ -270,7 +270,7 @@ int main(int argc, char *argv[]) {
std::promise<void> session_monitor_join_thread_promise;
auto session_monitor_join_thread_future = session_monitor_join_thread_promise.get_future();
std::thread session_monitor_thread([&]() {
std::jthread session_monitor_thread([&]() {
platf::set_thread_name("session_monitor");
session_monitor_join_thread_promise.set_value_at_thread_exit();
@@ -437,9 +437,9 @@ int main(int argc, char *argv[]) {
return lifetime::desired_exit_code;
}
std::thread httpThread {nvhttp::start};
std::thread configThread {confighttp::start};
std::thread rtspThread {rtsp_stream::start};
std::jthread httpThread {nvhttp::start};
std::jthread configThread {confighttp::start};
std::jthread rtspThread {rtsp_stream::start};
#ifdef _WIN32
// If we're using the default port and GameStream is enabled, warn the user
+2 -2
View File
@@ -1401,8 +1401,8 @@ namespace nvhttp {
return;
}
};
std::thread ssl {accept_and_run, &https_server};
std::thread tcp {accept_and_run, &http_server};
std::jthread ssl {accept_and_run, &https_server};
std::jthread tcp {accept_and_run, &http_server};
// Wait for any event
shutdown_event->view();
+2 -2
View File
@@ -302,7 +302,7 @@ namespace platf {
std::unique_ptr<safe::event_t<ctx_event_e>> events; ///< Event queue receiving PulseAudio context state changes.
std::unique_ptr<std::function<void(ctx_t::pointer)>> events_cb; ///< Callback that translates PulseAudio context updates into events.
std::thread worker; ///< Thread running the PulseAudio mainloop.
std::jthread worker; ///< Thread running the PulseAudio mainloop.
/**
* @brief Initialize PulseAudio mainloop, context, and Sunshine null sinks.
@@ -344,7 +344,7 @@ namespace platf {
return -1;
}
worker = std::thread {
worker = std::jthread {
[](loop_t::pointer loop) {
int retval;
platf::set_thread_name("audio::pulseaudio");
+3 -3
View File
@@ -529,14 +529,14 @@ namespace platf::publish {
*/
class deinit_t: public ::platf::deinit_t {
public:
std::thread poll_thread; ///< Poll thread.
std::jthread poll_thread; ///< Poll thread.
/**
* @brief Store the Avahi polling thread for shutdown on destruction.
*
* @param poll_thread Poll thread.
*/
deinit_t(std::thread poll_thread):
deinit_t(std::jthread poll_thread):
poll_thread {std::move(poll_thread)} {
}
@@ -581,6 +581,6 @@ namespace platf::publish {
return nullptr;
}
return std::make_unique<deinit_t>(std::thread {avahi::simple_poll_loop, poll.get()});
return std::make_unique<deinit_t>(std::jthread {avahi::simple_poll_loop, poll.get()});
}
} // namespace platf::publish
+2 -2
View File
@@ -42,7 +42,7 @@ namespace platf::publish {
*/
deinit_t(DNSServiceRef serviceRef):
unique_ptr(serviceRef) {
_thread = std::thread {[serviceRef, &_stopRequested = std::as_const(_stopRequested)]() {
_thread = std::jthread {[serviceRef, &_stopRequested = std::as_const(_stopRequested)]() {
platf::set_thread_name("publish::mdns");
const auto socket = DNSServiceRefSockFD(serviceRef);
while (!_stopRequested) {
@@ -74,7 +74,7 @@ namespace platf::publish {
deinit_t &operator=(const deinit_t &) = delete;
private:
std::thread _thread; ///< Thread for polling the mDNS service for a response.
std::jthread _thread; ///< Thread for polling the mDNS service for a response.
std::atomic<bool> _stopRequested = false; ///< Whether to stop polling the mDNS service.
};
+1 -1
View File
@@ -1349,7 +1349,7 @@ namespace rtsp_stream {
return;
}
std::thread rtsp_thread {[&shutdown_event] {
std::jthread rtsp_thread {[&shutdown_event] {
platf::set_thread_name("rtsp::handler");
auto broadcast_shutdown_event = mail::man->event<bool>(mail::broadcast_shutdown);
+12 -12
View File
@@ -454,10 +454,10 @@ namespace stream {
struct broadcast_ctx_t {
message_queue_queue_t message_queue_queue; ///< Queues carrying encoded video and audio packets to sender threads.
std::thread recv_thread; ///< Thread that receives incoming control-channel messages.
std::thread video_thread; ///< Thread that sends encoded video packets.
std::thread audio_thread; ///< Thread that sends encoded audio packets.
std::thread control_thread; ///< Thread that runs the ENet control server.
std::jthread recv_thread; ///< Thread that receives incoming control-channel messages.
std::jthread video_thread; ///< Thread that sends encoded video packets.
std::jthread audio_thread; ///< Thread that sends encoded audio packets.
std::jthread control_thread; ///< Thread that runs the ENet control server.
asio::io_context io_context; ///< Asio context used by the UDP broadcast sockets.
@@ -477,8 +477,8 @@ namespace stream {
std::shared_ptr<input::input_t> input; ///< Platform input device state for this stream.
std::thread audioThread; ///< Audio thread.
std::thread videoThread; ///< Video thread.
std::jthread audioThread; ///< Audio thread.
std::jthread videoThread; ///< Video thread.
std::chrono::steady_clock::time_point pingTimeout; ///< Deadline for receiving the next client ping.
@@ -1960,11 +1960,11 @@ namespace stream {
ctx.message_queue_queue = std::make_shared<message_queue_queue_t::element_type>(30);
ctx.video_thread = std::thread {videoBroadcastThread, std::ref(ctx.video_sock)};
ctx.audio_thread = std::thread {audioBroadcastThread, std::ref(ctx.audio_sock)};
ctx.control_thread = std::thread {controlBroadcastThread, &ctx.control_server};
ctx.video_thread = std::jthread {videoBroadcastThread, std::ref(ctx.video_sock)};
ctx.audio_thread = std::jthread {audioBroadcastThread, std::ref(ctx.audio_sock)};
ctx.control_thread = std::jthread {controlBroadcastThread, &ctx.control_server};
ctx.recv_thread = std::thread {recvThread, std::ref(ctx)};
ctx.recv_thread = std::jthread {recvThread, std::ref(ctx)};
return 0;
}
@@ -2234,8 +2234,8 @@ namespace stream {
session.pingTimeout = std::chrono::steady_clock::now() + config::stream.ping_timeout;
session.audioThread = std::thread {audioThread, &session};
session.videoThread = std::thread {videoThread, &session};
session.audioThread = std::jthread {audioThread, &session};
session.videoThread = std::jthread {videoThread, &session};
session.state.store(state_e::RUNNING, std::memory_order_relaxed);
+1 -1
View File
@@ -445,7 +445,7 @@ namespace system_tray {
int init_tray_threaded() {
try {
auto tray_thread = std::thread(tray_thread_worker);
auto tray_thread = std::jthread(tray_thread_worker);
// The tray thread doesn't require strong lifetime management.
// It will exit asynchronously when tray_exit() is called.
+3 -3
View File
@@ -23,7 +23,7 @@ namespace thread_pool_util {
typedef TaskPool::__task __task;
private:
std::vector<std::thread> _thread;
std::vector<std::jthread> _thread;
std::condition_variable _cv;
std::mutex _lock;
@@ -44,7 +44,7 @@ namespace thread_pool_util {
_thread(threads),
_continue {true} {
for (auto &t : _thread) {
t = std::thread(&ThreadPool::_main, this);
t = std::jthread(&ThreadPool::_main, this);
}
}
@@ -113,7 +113,7 @@ namespace thread_pool_util {
_thread.resize(threads);
for (auto &t : _thread) {
t = std::thread(&ThreadPool::_main, this);
t = std::jthread(&ThreadPool::_main, this);
}
}
+2 -2
View File
@@ -94,7 +94,7 @@ namespace upnp {
}
// Start the mapping thread
upnp_thread = std::thread {&deinit_t::upnp_thread_proc, this};
upnp_thread = std::jthread {&deinit_t::upnp_thread_proc, this};
}
/**
@@ -373,7 +373,7 @@ namespace upnp {
}
std::vector<mapping_t> mappings; ///< Port mappings Sunshine should keep registered with the gateway.
std::thread upnp_thread; ///< Worker thread that refreshes mappings until shutdown.
std::jthread upnp_thread; ///< Worker thread that refreshes mappings until shutdown.
};
/**
+4 -4
View File
@@ -630,7 +630,7 @@ namespace video {
*/
struct capture_thread_async_ctx_t {
std::shared_ptr<safe::queue_t<capture_ctx_t>> capture_ctx_queue; ///< Capture ctx queue.
std::thread capture_thread; ///< Capture thread.
std::jthread capture_thread; ///< Capture thread.
safe::signal_t reinit_event; ///< Reinit event.
const encoder_t *encoder_p; ///< Encoder p.
@@ -2354,7 +2354,7 @@ namespace video {
// streaming to continue without requiring a full restart of Sunshine.
auto fail_guard = util::fail_guard([&encoder, &session] {
if (encoder.flags & ASYNC_TEARDOWN) {
std::thread encoder_teardown_thread {[session = std::move(session)]() mutable {
std::jthread encoder_teardown_thread {[session = std::move(session)]() mutable {
BOOST_LOG(info) << "Starting async encoder teardown";
session.reset();
BOOST_LOG(info) << "Async encoder teardown complete";
@@ -3593,7 +3593,7 @@ namespace video {
capture_thread_ctx.capture_ctx_queue = std::make_shared<safe::queue_t<capture_ctx_t>>(30);
capture_thread_ctx.capture_thread = std::thread {
capture_thread_ctx.capture_thread = std::jthread {
captureThread,
capture_thread_ctx.capture_ctx_queue,
std::ref(capture_thread_ctx.display_wp),
@@ -3617,7 +3617,7 @@ namespace video {
* @brief Start capture sync.
*/
int start_capture_sync(capture_thread_sync_ctx_t &ctx) {
std::thread {&captureThreadSync}.detach();
std::jthread {&captureThreadSync}.detach();
return 0;
}