Skip to content

Commit

Permalink
lib/alsa/NonBlock: use a persistent pollfd array
Browse files Browse the repository at this point in the history
This implements the semantic API change introduced by commit
alsa-project/alsa-lib@cd04da2
  • Loading branch information
MaxKellermann committed Jul 11, 2024
1 parent 4486b2e commit 90dfa43
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ver 0.24 (not yet released)
* input
- alsa: limit ALSA buffer time to 2 seconds
- alsa: set up a channel map
- alsa: support the alsa-lib 1.2.11 API
- curl: add "connect_timeout" configuration
* decoder
- ffmpeg: require FFmpeg 4.0 or later
Expand Down
38 changes: 16 additions & 22 deletions src/lib/alsa/NonBlock.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
namespace Alsa {

std::span<pollfd>
NonBlock::CopyReturnedEvents(MultiSocketMonitor &m, std::size_t n) noexcept
NonBlock::CopyReturnedEvents(MultiSocketMonitor &m) noexcept
{
const auto pfds = buffer.Get(n), end = pfds + n;

auto *i = pfds;
m.ForEachReturnedEvent([&i, end](SocketDescriptor s, unsigned events){
if (i >= end)
return;

i->fd = s.Get();
i->events = i->revents = events;
++i;
const std::span<pollfd> pfds = buffer;

for (auto &i : pfds)
i.revents = 0;

m.ForEachReturnedEvent([pfds](SocketDescriptor s, unsigned events){
for (auto &i : pfds) {
if (i.fd == s.Get()) {
i.revents = events;
return;
}
}
});

return {pfds, static_cast<std::size_t>(i - pfds)};
return pfds;

}

Expand Down Expand Up @@ -54,11 +56,7 @@ NonBlockPcm::PrepareSockets(MultiSocketMonitor &m, snd_pcm_t *pcm)
void
NonBlockPcm::DispatchSockets(MultiSocketMonitor &m, snd_pcm_t *pcm)
{
int count = snd_pcm_poll_descriptors_count(pcm);
if (count <= 0)
return;

const auto pfds = base.CopyReturnedEvents(m, count);
const auto pfds = base.CopyReturnedEvents(m);

unsigned short dummy;
int err = snd_pcm_poll_descriptors_revents(pcm, pfds.data(), pfds.size(), &dummy);
Expand Down Expand Up @@ -88,11 +86,7 @@ NonBlockMixer::PrepareSockets(MultiSocketMonitor &m, snd_mixer_t *mixer) noexcep
void
NonBlockMixer::DispatchSockets(MultiSocketMonitor &m, snd_mixer_t *mixer) noexcept
{
int count = snd_mixer_poll_descriptors_count(mixer);
if (count <= 0)
return;

const auto pfds = base.CopyReturnedEvents(m, count);
const auto pfds = base.CopyReturnedEvents(m);

unsigned short dummy;
snd_mixer_poll_descriptors_revents(mixer, pfds.data(), pfds.size(), &dummy);
Expand Down
10 changes: 5 additions & 5 deletions src/lib/alsa/NonBlock.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#pragma once

#include "event/Chrono.hxx"
#include "util/ReusableArray.hxx"
#include "util/AllocatedArray.hxx"

#include <alsa/asoundlib.h>

Expand All @@ -15,15 +15,15 @@ class MultiSocketMonitor;
namespace Alsa {

class NonBlock {
ReusableArray<pollfd> buffer;
AllocatedArray<pollfd> buffer;

public:
std::span<pollfd> Allocate(std::size_t n) noexcept {
return {buffer.Get(n), n};
buffer.ResizeDiscard(n);
return buffer;
}

std::span<pollfd> CopyReturnedEvents(MultiSocketMonitor &m,
std::size_t n) noexcept;
std::span<pollfd> CopyReturnedEvents(MultiSocketMonitor &m) noexcept;
};

/**
Expand Down

0 comments on commit 90dfa43

Please sign in to comment.