]> git.sesse.net Git - nageru/commitdiff
Shut down the ALSA inotify thread cleanly on exit.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 21 Sep 2016 17:49:29 +0000 (19:49 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 19 Oct 2016 22:55:44 +0000 (00:55 +0200)
alsa_pool.cpp
alsa_pool.h

index 28e7ee08829e42218a9dbe63153403ddd62486d0..22a61af88d27e982a229bf8f8eccbe593c8c9612 100644 (file)
@@ -27,6 +27,8 @@ ALSAPool::~ALSAPool()
                        device.input->stop_capture_thread();
                }
        }
+       should_quit = true;
+       inotify_thread.join();
 }
 
 std::vector<ALSAPool::Device> 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<char[]> 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)
index bc82201bf76a8c9be329328ff2e5f6cc3c541010..b5d4cd542742b6dfbfdbf4c862a56961335aab99 100644 (file)
@@ -148,6 +148,10 @@ private:
                                        unsigned num_channels,
                                        const std::string &address);
 
+       std::atomic<bool> should_quit{false};
+
+       std::thread inotify_thread;
+
        friend class ALSAInput;
 };