From: Steinar H. Gunderson Date: Sat, 8 Oct 2016 17:05:11 +0000 (+0200) Subject: Shut down the ALSA inotify thread using an eventfd instead of busypolling every 100 ms. X-Git-Tag: 1.4.0~36 X-Git-Url: https://git.sesse.net/?p=nageru;a=commitdiff_plain;h=d02079ef22fb3ef05ced8592b2dbf28bb5b289b3 Shut down the ALSA inotify thread using an eventfd instead of busypolling every 100 ms. --- diff --git a/alsa_pool.cpp b/alsa_pool.cpp index 5983446..0a7da59 100644 --- a/alsa_pool.cpp +++ b/alsa_pool.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -20,6 +21,12 @@ using namespace std; using namespace std::placeholders; +ALSAPool::ALSAPool() +{ + should_quit_fd = eventfd(/*initval=*/0, /*flags=*/0); + assert(should_quit_fd != -1); +} + ALSAPool::~ALSAPool() { for (Device &device : devices) { @@ -28,6 +35,8 @@ ALSAPool::~ALSAPool() } } should_quit = true; + const uint64_t one = 1; + write(should_quit_fd, &one, sizeof(one)); inotify_thread.join(); while (retry_threads_running > 0) { @@ -289,12 +298,15 @@ void ALSAPool::inotify_thread_func() int size = sizeof(inotify_event) + NAME_MAX + 1; unique_ptr buf(new char[size]); while (!should_quit) { - pollfd fds; - fds.fd = inotify_fd; - fds.events = POLLIN; - fds.revents = 0; - - int ret = poll(&fds, 1, 100); + pollfd fds[2]; + fds[0].fd = inotify_fd; + fds[0].events = POLLIN; + fds[0].revents = 0; + fds[1].fd = should_quit_fd; + fds[1].events = POLLIN; + fds[1].revents = 0; + + int ret = poll(fds, 2, -1); if (ret == -1) { if (errno == EINTR) { continue; @@ -307,6 +319,8 @@ void ALSAPool::inotify_thread_func() continue; } + if (fds[1].revents) break; // should_quit_fd asserted. + ret = read(inotify_fd, buf.get(), size); if (ret == -1) { if (errno == EINTR) { @@ -350,6 +364,7 @@ void ALSAPool::inotify_thread_func() } close(watch_fd); close(inotify_fd); + close(should_quit_fd); } void ALSAPool::reset_device(unsigned index) diff --git a/alsa_pool.h b/alsa_pool.h index c9cd61a..7bab808 100644 --- a/alsa_pool.h +++ b/alsa_pool.h @@ -21,6 +21,7 @@ class DeviceSpecProto; // In particular, it deals with enumeration of cards, and hotplug of new ones. class ALSAPool { public: + ALSAPool(); ~ALSAPool(); struct Device { @@ -149,6 +150,7 @@ private: const std::string &address); std::atomic should_quit{false}; + int should_quit_fd; std::thread inotify_thread; std::atomic retry_threads_running{0};