Remove redundent thread creation

This commit is contained in:
loki
2021-06-26 15:48:07 +02:00
parent ed5de34800
commit cf7eb14573
3 changed files with 28 additions and 22 deletions
+1 -2
View File
@@ -215,12 +215,11 @@ int main(int argc, char *argv[]) {
task_pool.start(1); task_pool.start(1);
std::thread publishThread { platf::publish::start }; auto deinit = platf::publish::start();
std::thread httpThread { nvhttp::start }; std::thread httpThread { nvhttp::start };
std::thread configThread { confighttp::start }; std::thread configThread { confighttp::start };
stream::rtpThread(); stream::rtpThread();
publishThread.join();
httpThread.join(); httpThread.join();
configThread.join(); configThread.join();
+1 -1
View File
@@ -249,7 +249,7 @@ void free_gamepad(input_t &input, int nr);
#define SERVICE_TYPE "_nvstream._tcp" #define SERVICE_TYPE "_nvstream._tcp"
namespace publish { namespace publish {
void start(); [[nodiscard]] std::unique_ptr<deinit_t> start();
} }
[[nodiscard]] std::unique_ptr<deinit_t> init(); [[nodiscard]] std::unique_ptr<deinit_t> init();
+26 -19
View File
@@ -282,6 +282,7 @@ using poll_t = util::dyn_safe_ptr<avahi::SimplePoll, &avahi::simple_poll_free>
avahi::EntryGroup *group = nullptr; avahi::EntryGroup *group = nullptr;
poll_t poll; poll_t poll;
client_t client;
ptr_t<char> name; ptr_t<char> name;
@@ -374,40 +375,46 @@ void client_callback(avahi::Client *c, avahi::ClientState state, void *) {
} }
} }
void start() { class deinit_t : public ::platf::deinit_t {
if(avahi::init_client()) { public:
return; std::thread poll_thread;
}
auto shutdown_event = mail::man->event<bool>(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; int avhi_error;
poll.reset(avahi::simple_poll_new()); poll.reset(avahi::simple_poll_new());
if(!poll) { if(!poll) {
BOOST_LOG(error) << "Failed to create simple poll object."sv; BOOST_LOG(error) << "Failed to create simple poll object."sv;
return; return nullptr;
} }
name.reset(avahi::strdup(SERVICE_NAME)); name.reset(avahi::strdup(SERVICE_NAME));
client_t client { client.reset(
avahi::client_new(avahi::simple_poll_get(poll.get()), avahi::ClientFlags(0), client_callback, nullptr, &avhi_error) avahi::client_new(avahi::simple_poll_get(poll.get()), avahi::ClientFlags(0), client_callback, nullptr, &avhi_error));
};
if(!client) { if(!client) {
BOOST_LOG(error) << "Failed to create client: "sv << avahi::strerror(avhi_error); 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() }; return std::make_unique<deinit_t>(std::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();
} }
}; // namespace platf::publish }; // namespace platf::publish