From cf7eb145739adfac773c59a70ae41e5666eeb3d9 Mon Sep 17 00:00:00 2001 From: loki Date: Sat, 26 Jun 2021 15:48:07 +0200 Subject: [PATCH] Remove redundent thread creation --- sunshine/main.cpp | 3 +- sunshine/platform/common.h | 2 +- sunshine/platform/linux/publish.cpp | 45 +++++++++++++++++------------ 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/sunshine/main.cpp b/sunshine/main.cpp index b8e17f17..e7478629 100644 --- a/sunshine/main.cpp +++ b/sunshine/main.cpp @@ -215,12 +215,11 @@ int main(int argc, char *argv[]) { task_pool.start(1); - std::thread publishThread { platf::publish::start }; + auto deinit = platf::publish::start(); std::thread httpThread { nvhttp::start }; std::thread configThread { confighttp::start }; stream::rtpThread(); - publishThread.join(); httpThread.join(); configThread.join(); diff --git a/sunshine/platform/common.h b/sunshine/platform/common.h index 54d59b44..e866ce4b 100644 --- a/sunshine/platform/common.h +++ b/sunshine/platform/common.h @@ -249,7 +249,7 @@ void free_gamepad(input_t &input, int nr); #define SERVICE_TYPE "_nvstream._tcp" namespace publish { -void start(); +[[nodiscard]] std::unique_ptr start(); } [[nodiscard]] std::unique_ptr init(); diff --git a/sunshine/platform/linux/publish.cpp b/sunshine/platform/linux/publish.cpp index 757752e9..f63e0881 100644 --- a/sunshine/platform/linux/publish.cpp +++ b/sunshine/platform/linux/publish.cpp @@ -282,6 +282,7 @@ using poll_t = util::dyn_safe_ptr avahi::EntryGroup *group = nullptr; poll_t poll; +client_t client; ptr_t name; @@ -374,40 +375,46 @@ void client_callback(avahi::Client *c, avahi::ClientState state, void *) { } } -void start() { - if(avahi::init_client()) { - return; - } +class deinit_t : public ::platf::deinit_t { +public: + std::thread poll_thread; - auto shutdown_event = mail::man->event(mail::shutdown); + deinit_t(std::thread poll_thread) : poll_thread { std::move(poll_thread) } {} + + ~deinit_t() override { + if(avahi::simple_poll_quit && poll) { + avahi::simple_poll_quit(poll.get()); + } + + if(poll_thread.joinable()) { + poll_thread.join(); + } + } +}; + +[[nodiscard]] std::unique_ptr<::platf::deinit_t> start() { + if(avahi::init_client()) { + return nullptr; + } int avhi_error; poll.reset(avahi::simple_poll_new()); if(!poll) { BOOST_LOG(error) << "Failed to create simple poll object."sv; - return; + return nullptr; } name.reset(avahi::strdup(SERVICE_NAME)); - client_t client { - avahi::client_new(avahi::simple_poll_get(poll.get()), avahi::ClientFlags(0), client_callback, nullptr, &avhi_error) - }; + client.reset( + avahi::client_new(avahi::simple_poll_get(poll.get()), avahi::ClientFlags(0), client_callback, nullptr, &avhi_error)); if(!client) { BOOST_LOG(error) << "Failed to create client: "sv << avahi::strerror(avhi_error); - return; + return nullptr; } - std::thread poll_thread { avahi::simple_poll_loop, poll.get() }; - - auto fg = util::fail_guard([&]() { - avahi::simple_poll_quit(poll.get()); - poll_thread.join(); - }); - - // Wait for any event - shutdown_event->view(); + return std::make_unique(std::thread { avahi::simple_poll_loop, poll.get() }); } }; // namespace platf::publish \ No newline at end of file