All of lore.kernel.org
 help / color / mirror / Atom feed
From: GitHub issues - opened <github@alsa-project.org>
To: alsa-devel@alsa-project.org
Subject: snd_pcm_drain stucked.
Date: Mon,  2 Jun 2025 13:21:13 +0200 (CEST)	[thread overview]
Message-ID: <184535e12c94bf00-webhooks-bot@alsa-project.org> (raw)
In-Reply-To: <184535e12c8de800-webhooks-bot@alsa-project.org>

alsa-project/alsa-lib issue #457 was opened from yangyagami:

When snd_pcm_drain is called, if snd_pcm_drop is called in another thread, snd_pcm_drain will get stuck.
```cpp
#include <iostream>
#include <cmath>
#include <thread>

#include <alsa/asoundlib.h>

#define SAMPLE_RATE 44100
#define FREQUENCY 440.0 // A4 note
#define DURATION 5.0    // seconds

using namespace std::chrono_literals;

int main() {
    snd_pcm_t *handle;
    snd_pcm_hw_params_t *params;
    unsigned int rate = SAMPLE_RATE;
    int dir;
    int pcm;

    pcm = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
    if (pcm < 0) {
        std::cerr << "Unable to open PCM device: " << snd_strerror(pcm) << std::endl;
        return 1;
    }

    snd_pcm_hw_params_alloca(&params);
    snd_pcm_hw_params_any(handle, params);

    snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
    snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
    snd_pcm_hw_params_set_rate_near(handle, params, &rate, &dir);
    snd_pcm_hw_params_set_channels(handle, params, 1);

    pcm = snd_pcm_hw_params(handle, params);
    if (pcm < 0) {
        std::cerr << "Unable to set HW parameters: " << snd_strerror(pcm) << std::endl;
        return 1;
    }

    int samples = static_cast<int>(DURATION * SAMPLE_RATE);
    int16_t *buffer = new int16_t[samples];

    for (int i = 0; i < samples; ++i) {
        buffer[i] = static_cast<int16_t>(32767 * sin(2 * M_PI * FREQUENCY * i / SAMPLE_RATE));
    }

    pcm = snd_pcm_writei(handle, buffer, samples);
    if (pcm < 0) {
        std::cerr << "Playback error: " << snd_strerror(pcm) << std::endl;
    }

    std::thread t([handle](){
      std::this_thread::sleep_for(500ms);
      snd_pcm_drop(handle);
    });

    snd_pcm_drain(handle);

    t.join();

    delete[] buffer;
    snd_pcm_close(handle);

    return 0;
}
```

gdb output
```shell
Thread 1 "main" received signal SIGINT, Interrupt.
0x00007ffff71b5e22 in ?? () from /usr/lib/libc.so.6
(gdb) bt
#0  0x00007ffff71b5e22 in ?? () from /usr/lib/libc.so.6
#1  0x00007ffff71a9fda in ?? () from /usr/lib/libc.so.6
#2  0x00007ffff71aa64c in ?? () from /usr/lib/libc.so.6
#3  0x00007ffff71acd1e in pthread_cond_wait () from /usr/lib/libc.so.6
#4  0x00007ffff6b9e2b9 in pw_thread_loop_wait () from /usr/lib/libpipewire-0.3.so.0
#5  0x00007ffff7f2ec8f in ?? () from /usr/lib/alsa-lib/libasound_module_pcm_pipewire.so
#6  0x00007ffff777c2b8 in ?? () from /usr/lib/libasound.so.2
#7  0x0000555555558d6f in main () at main.cc:116
(gdb) 
```

Issue URL     : https://github.com/alsa-project/alsa-lib/issues/457
Repository URL: https://github.com/alsa-project/alsa-lib

           reply	other threads:[~2025-06-02 11:21 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <184535e12c8de800-webhooks-bot@alsa-project.org>]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=184535e12c94bf00-webhooks-bot@alsa-project.org \
    --to=github@alsa-project.org \
    --cc=alsa-devel@alsa-project.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.