X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=alsa_pool.cpp;h=348c6232303ab561d3a4039ca69932261bd3e8bc;hb=fa54f2630c56a1df0046923d6a77b1bd58abf240;hp=28e7ee08829e42218a9dbe63153403ddd62486d0;hpb=34dabe75b7985a42432a2c0f948a9c5a9404e57b;p=nageru diff --git a/alsa_pool.cpp b/alsa_pool.cpp index 28e7ee0..348c623 100644 --- a/alsa_pool.cpp +++ b/alsa_pool.cpp @@ -1,15 +1,22 @@ #include "alsa_pool.h" -#include -#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 +27,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) { @@ -27,6 +40,17 @@ ALSAPool::~ALSAPool() device.input->stop_capture_thread(); } } + should_quit = true; + const uint64_t one = 1; + if (write(should_quit_fd, &one, sizeof(one)) != sizeof(one)) { + perror("write(should_quit_fd)"); + exit(1); + } + inotify_thread.join(); + + while (retry_threads_running > 0) { + this_thread::sleep_for(std::chrono::milliseconds(100)); + } } std::vector ALSAPool::get_devices() @@ -102,6 +126,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(); } @@ -110,16 +135,21 @@ void ALSAPool::probe_device_retry_thread_func(unsigned card_index, unsigned dev_ char address[256]; snprintf(address, sizeof(address), "hw:%d,%d", card_index, dev_index); + char thread_name[16]; + snprintf(thread_name, sizeof(thread_name), "Reprobe_hw:%d,%d", card_index, dev_index); + pthread_setname_np(pthread_self(), thread_name); + for ( ;; ) { // Termination condition within the loop. sleep(1); // 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). @@ -127,11 +157,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. @@ -139,6 +169,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) @@ -255,12 +287,14 @@ 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(); } void ALSAPool::inotify_thread_func() { + pthread_setname_np(pthread_self(), "ALSA_Hotplug"); + int inotify_fd = inotify_init(); if (inotify_fd == -1) { perror("inotify_init()"); @@ -278,8 +312,41 @@ 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[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; + } else { + perror("poll(inotify_fd)"); + return; + } + } + if (ret == 0) { + continue; + } + + if (fds[1].revents) break; // should_quit_fd asserted. + + 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 +377,9 @@ void ALSAPool::inotify_thread_func() } } } + close(watch_fd); + close(inotify_fd); + close(should_quit_fd); } void ALSAPool::reset_device(unsigned index) @@ -323,7 +393,7 @@ void ALSAPool::reset_device(unsigned index) inputs[index].reset(); } else { // TODO: Put on a background thread instead of locking? - auto callback = bind(&AudioMixer::add_audio, global_audio_mixer, DeviceSpec{InputSourceType::ALSA_INPUT, index}, _1, _2, _3, _4); + auto callback = bind(&AudioMixer::add_audio, global_audio_mixer, DeviceSpec{InputSourceType::ALSA_INPUT, index}, _1, _2, _3, _4, _5); inputs[index].reset(new ALSAInput(device->address.c_str(), OUTPUT_FREQUENCY, device->num_channels, callback, this, index)); inputs[index]->start_capture_thread(); }