Skip to contentSkip to conversation

How to interrupt snd_pcm_writei in blocking mode?

DISCUSSION Hynek Hanke 4 participants 6 messages Jun 29, 2005 – Dec 20, 2013

lore
Share & tools
lore
#0 lore raw

Hello,

I'm seeking some help with a problem I encountered in my audio output module for alsa-lib.

I'm running it in two separate threads. One does all the preparations and calls snd_pcm_writei() in blocking mode. The other one then decides it wants to stop playing, so it calls snd_pcm_drop(). This causes the device to switch from state RUNNING to state SETUP, but the snd_pcm_writei() hangs forever and never returns. Why is this so?

What are the exact circumstances I'm allowed to use snd_pcm_drop() under and what exactly is it supposed to do? (The alsa-lib documentation doesn't provide enough details.)

Thank you,
Hynek Hanke

-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click

#1 lore raw
Quoted 5 lines · Hynek Hanke

I'm running it in two separate threads. One does all the preparations and calls snd_pcm_writei() in blocking mode. The other one then decides it wants to stop playing, so it calls snd_pcm_drop(). This causes the device to switch from state RUNNING to state SETUP, but the snd_pcm_writei() hangs forever and never returns. Why is this so?

The ALSA library isn't thread safe.

I guess you have to use non-blocking mode.

HTH
Clemens

-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click

#2 lore raw

At Thu, 30 Jun 2005 12:05:49 +0200 (METDST),

Quoted 11 lines · Clemens Ladisch
Quoted 5 lines · Hynek Hanke

I'm running it in two separate threads. One does all the preparations and calls snd_pcm_writei() in blocking mode. The other one then decides it wants to stop playing, so it calls snd_pcm_drop(). This causes the device to switch from state RUNNING to state SETUP, but the snd_pcm_writei() hangs forever and never returns. Why is this so?

The ALSA library isn't thread safe.

I guess you have to use non-blocking mode.

Well, but pcm_lib.c::snd_pcm_lib_write1() should check the state SETUP and break the loop immediately. Ditto for snd_pcm_lib_read1().

A question is what error should return. It's no XRUN, so -EPIPE doesn't seem fitting quite well. Maybe -EBADFD?

The quick hack is below. Please give a shot.

Takashi

Index: alsa-kernel/core/pcm_lib.c
===================================================================
RCS file: /home/iwai/cvs/alsa/alsa-kernel/core/pcm_lib.c,v retrieving revision 1.62

core/pcm_lib.c +14 -2
diff -u -r1.62 pcm_lib.c
@@ -2063,7 +2063,7 @@
 		if (((avail < runtime->control->avail_min && size > avail) ||
 		   (size >= runtime->xfer_align && avail < runtime->xfer_align))) {
 			wait_queue_t wait;
-			enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED } state;
+			enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state;
 			long tout;
 
 			if (nonblock) {
@@ -2097,6 +2097,9 @@
 				case SNDRV_PCM_STATE_SUSPENDED:
 					state = SUSPENDED;
 					goto _end_loop;
+				case SNDRV_PCM_STATE_SETUP:
+					state = DROPPED;
+					goto _end_loop;
 				default:
 					break;
 				}
@@ -2123,6 +2126,9 @@
 				snd_printd("playback write error (DMA or IRQ trouble?)\n");
 				err = -EIO;
 				goto _end_unlock;
+			case DROPPED:
+				err = -EBADFD;
+				goto _end_unlock;
 			default:
 				break;
 			}
@@ -2359,7 +2365,7 @@
 		} else if ((avail < runtime->control->avail_min && size > avail) ||
 			   (size >= runtime->xfer_align && avail < runtime->xfer_align)) {
 			wait_queue_t wait;
-			enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED } state;
+			enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state;
 			long tout;
 
 			if (nonblock) {
@@ -2394,6 +2400,9 @@
 					goto _end_loop;
 				case SNDRV_PCM_STATE_DRAINING:
 					goto __draining;
+				case SNDRV_PCM_STATE_SETUP:
+					state = DROPPED;
+					goto _end_loop;
 				default:
 					break;
 				}
@@ -2420,6 +2429,9 @@
 				snd_printd("capture read error (DMA or IRQ trouble?)\n");
 				err = -EIO;
 				goto _end_unlock;
+			case DROPPED:
+				err = -EBADFD;
+				goto _end_unlock;
 			default:
 				break;
 			}
#3 lore raw
Quoted 9 lines
Quoted 6 lines
Quoted 5 lines · Hynek Hanke

I'm running it in two separate threads. One does all the preparations and calls snd_pcm_writei() in blocking mode. The other one then decides it wants to stop playing, so it calls snd_pcm_drop(). This causes the device to switch from state RUNNING to state SETUP, but the snd_pcm_writei() hangs forever and never returns. Why is this so?

Well, but pcm_lib.c::snd_pcm_lib_write1() should check the state SETUP and break the loop immediately. Ditto for snd_pcm_lib_read1(). The quick hack is below. Please give a shot.

I tried to apply the patch to the 2.6.11 kernel but after compilation/instalation, there is no difference. It still hangs. Logfiles say it was in the state RUNNING just before snd_pcm_drop() was called and got into state SETUP immediatelly after. The logfile from the other thread shows snd_pcm_writei() never returns (not even after the sound would surely be completed).

So, what can I do? Do I really have to use the non-blocking mode or is this supposed to work (if I take care about all the other thread related issues myself)? My colleague is reporting it's working for him under some different configuration.

Can I enable some debugging messages or something to help you to see what's wrong?

With Reagards,
Hynek

-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click

#4 lore raw

At Thu, 30 Jun 2005 20:42:28 +0200,

Quoted 11 lines · Hynek Hanke
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines · Hynek Hanke

I'm running it in two separate threads. One does all the preparations and calls snd_pcm_writei() in blocking mode. The other one then decides it wants to stop playing, so it calls snd_pcm_drop(). This causes the device to switch from state RUNNING to state SETUP, but the snd_pcm_writei() hangs forever and never returns. Why is this so?

Well, but pcm_lib.c::snd_pcm_lib_write1() should check the state SETUP and break the loop immediately. Ditto for snd_pcm_lib_read1(). The quick hack is below. Please give a shot.

Quoted 6 lines

I tried to apply the patch to the 2.6.11 kernel but after compilation/instalation, there is no difference. It still hangs. Logfiles say it was in the state RUNNING just before snd_pcm_drop() was called and got into state SETUP immediatelly after. The logfile from the other thread shows snd_pcm_writei() never returns (not even after the sound would surely be completed).

Hmm, then still something is missing. I'll check on it later.

Quoted 4 lines

So, what can I do? Do I really have to use the non-blocking mode or is this supposed to work (if I take care about all the other thread related issues myself)? My colleague is reporting it's working for him under some different configuration.

The non-blocking mode and poll() should work (hopefully :) If not, let me know.

Quoted 2 lines

Can I enable some debugging messages or something to help you to see what's wrong?

Check the code flow in snd_pcm_lib_write1(). You can see the places my patch tried to fix. Basically, the thread should be woken up and get out of the loop. But according to your report, it didn't work as expected by some reason...

Takashi

-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click

#5 lore raw
Quoted 6 lines · Takashi Iwai <tiwai <at> suse.de> writes:

At Thu, 30 Jun 2005 20:42:28 +0200,

Quoted 3 lines · Hynek Hanke
Quoted 3 lines
Quoted 3 lines
Quoted 1 line · Hynek Hanke

I'm running it in two separate threads. One does all the

preparations and calls

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

snd_pcm_writei() in blocking mode. The other one then decides it

wants to stop

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

playing, so it calls snd_pcm_drop(). This causes the device to

switch from

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

state RUNNING to state SETUP, but the snd_pcm_writei() hangs

forever and never

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

returns. Why is this so?

Well, but pcm_lib.c::snd_pcm_lib_write1() should check the state SETUP and break the loop immediately. Ditto for snd_pcm_lib_read1(). The quick hack is below. Please give a shot.

I tried to apply the patch to the 2.6.11 kernel but after compilation/instalation, there is no difference. It still hangs.

Logfiles say

Quoted 1 line
Quoted 1 line

it was in the state RUNNING just before snd_pcm_drop() was called and

got into

Quoted 22 lines
Quoted 3 lines

state SETUP immediatelly after. The logfile from the other thread shows snd_pcm_writei() never returns (not even after the sound would surely be completed).

Hmm, then still something is missing. I'll check on it later.

Quoted 4 lines

So, what can I do? Do I really have to use the non-blocking mode or is this supposed to work (if I take care about all the other thread related issues myself)? My colleague is reporting it's working for him under some different configuration.

The non-blocking mode and poll() should work (hopefully :) If not, let me know.

Quoted 2 lines

Can I enable some debugging messages or something to help you to see what's wrong?

Check the code flow in snd_pcm_lib_write1(). You can see the places my patch tried to fix. Basically, the thread should be woken up and get out of the loop. But according to your report, it didn't work as expected by some reason...

Quoted 8 lines

Takashi

-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click

Hello Takashi,
We have the same problem.
snd_pcm_writei blocks when we switch between playback and record continuously for several times.

Is this issue is fixed in any kernel versions.
I am running kernel version 2.6.31
Alsa version 1.0.18

is it a issue of buffers we allocate using
snd_pcm_hw_params_set_period_size_near

whats the way to calculate this value and how should be the ideal value.

I appreciate any help/suggestions in this regard.

Thanks and Regards,
Giri