X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=alsa_pool.cpp;h=3b6dd7ed3bcaab379accd31f26cd58e00e6de466;hb=9e93fbea58c068d14dd9dad5d0f5226edeef3395;hp=22a61af88d27e982a229bf8f8eccbe593c8c9612;hpb=12ef90072622a8001f2c3cc853e5b89ba7d4e94e;p=nageru diff --git a/alsa_pool.cpp b/alsa_pool.cpp index 22a61af..3b6dd7e 100644 --- a/alsa_pool.cpp +++ b/alsa_pool.cpp @@ -1,15 +1,21 @@ #include "alsa_pool.h" -#include -#include -#include +#include #include +#include #include +#include +#include #include +#include #include #include #include +#include +#include +#include #include +#include #include "alsa_input.h" #include "audio_mixer.h" @@ -20,6 +26,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,7 +40,13 @@ 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) { + this_thread::sleep_for(std::chrono::milliseconds(100)); + } } std::vector ALSAPool::get_devices() @@ -104,6 +122,7 @@ void ALSAPool::probe_device_with_retry(unsigned card_index, unsigned dev_index) // then start it ourselves. fprintf(stderr, "Trying %s again in one second...\n", address); add_device_tries_left[address] = num_retries; + ++retry_threads_running; thread(&ALSAPool::probe_device_retry_thread_func, this, card_index, dev_index).detach(); } @@ -117,11 +136,12 @@ void ALSAPool::probe_device_retry_thread_func(unsigned card_index, unsigned dev_ // See if there are any retries left. lock_guard lock(add_device_mutex); - if (!add_device_tries_left.count(address) || + if (should_quit || + !add_device_tries_left.count(address) || add_device_tries_left[address] == 0) { add_device_tries_left.erase(address); fprintf(stderr, "Giving up probe of %s.\n", address); - return; + break; } // Seemingly there were. Give it a try (we still hold the mutex). @@ -129,11 +149,11 @@ void ALSAPool::probe_device_retry_thread_func(unsigned card_index, unsigned dev_ if (result == ProbeResult::SUCCESS) { add_device_tries_left.erase(address); fprintf(stderr, "Probe of %s succeeded.\n", address); - return; + break; } else if (result == ProbeResult::FAILURE || --add_device_tries_left[address] == 0) { add_device_tries_left.erase(address); fprintf(stderr, "Giving up probe of %s.\n", address); - return; + break; } // Failed again. @@ -141,6 +161,8 @@ void ALSAPool::probe_device_retry_thread_func(unsigned card_index, unsigned dev_ fprintf(stderr, "Trying %s again in one second (%d tries left)...\n", address, add_device_tries_left[address]); } + + --retry_threads_running; } ALSAPool::ProbeResult ALSAPool::probe_device_once(unsigned card_index, unsigned dev_index) @@ -281,12 +303,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; @@ -299,6 +324,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) { @@ -342,6 +369,7 @@ void ALSAPool::inotify_thread_func() } close(watch_fd); close(inotify_fd); + close(should_quit_fd); } void ALSAPool::reset_device(unsigned index)