From 12ef90072622a8001f2c3cc853e5b89ba7d4e94e Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 21 Sep 2016 19:49:29 +0200 Subject: [PATCH] Shut down the ALSA inotify thread cleanly on exit. --- alsa_pool.cpp | 38 +++++++++++++++++++++++++++++++++++--- alsa_pool.h | 4 ++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/alsa_pool.cpp b/alsa_pool.cpp index 28e7ee0..22a61af 100644 --- a/alsa_pool.cpp +++ b/alsa_pool.cpp @@ -27,6 +27,8 @@ ALSAPool::~ALSAPool() device.input->stop_capture_thread(); } } + should_quit = true; + inotify_thread.join(); } std::vector ALSAPool::get_devices() @@ -255,7 +257,7 @@ void ALSAPool::unplug_device(unsigned card_index, unsigned dev_index) void ALSAPool::init() { - thread(&ALSAPool::inotify_thread_func, this).detach(); + inotify_thread = thread(&ALSAPool::inotify_thread_func, this); enumerate_devices(); } @@ -278,8 +280,36 @@ void ALSAPool::inotify_thread_func() int size = sizeof(inotify_event) + NAME_MAX + 1; unique_ptr buf(new char[size]); - for ( ;; ) { - int ret = read(inotify_fd, buf.get(), size); + while (!should_quit) { + pollfd fds; + fds.fd = inotify_fd; + fds.events = POLLIN; + fds.revents = 0; + + int ret = poll(&fds, 1, 100); + if (ret == -1) { + if (errno == EINTR) { + continue; + } else { + perror("poll(inotify_fd)"); + return; + } + } + if (ret == 0) { + continue; + } + + ret = read(inotify_fd, buf.get(), size); + if (ret == -1) { + if (errno == EINTR) { + continue; + } else { + perror("read(inotify_fd)"); + close(watch_fd); + close(inotify_fd); + return; + } + } if (ret < int(sizeof(inotify_event))) { fprintf(stderr, "inotify read unexpectedly returned %d, giving up hotplug of ALSA devices.\n", int(ret)); @@ -310,6 +340,8 @@ void ALSAPool::inotify_thread_func() } } } + close(watch_fd); + close(inotify_fd); } void ALSAPool::reset_device(unsigned index) diff --git a/alsa_pool.h b/alsa_pool.h index bc82201..b5d4cd5 100644 --- a/alsa_pool.h +++ b/alsa_pool.h @@ -148,6 +148,10 @@ private: unsigned num_channels, const std::string &address); + std::atomic should_quit{false}; + + std::thread inotify_thread; + friend class ALSAInput; }; -- 2.39.2