[alsa-devel] Third time: Unlink PCM resource ?

Takashi Iwai tiwai at suse.de
Tue Jul 7 17:06:23 CEST 2009


At Tue, 26 May 2009 10:17:41 +0200,
I wrote:
> 
> At Tue, 26 May 2009 10:14:56 +0200,
> Werner Van Belle wrote:
> > 
> > Takashi Iwai wrote:
> > > OK, in a case without exec, we need an explicit call.
> > > But I guess snd_pcm_close() isn't appropriate (as Werner experienced)
> > > since it calls snd_pcm_drop() there.  I guess this call isn't needed
> > > as the driver itself takes care?
> > >   
> > You mean: modifying the snd_pcm_close routine to not call snd_pcm_drop ?
> 
> Yes.
> 
> > I guess that might work as well to solve my problem.
> > Will such a fix appear in a new alsa release ?
> 
> We need more tests :)  I might overlook something.

Finally had some time to take a look at this pending issue...

Through tests, I found that this can be really a problem, too.
The below is a quick fix, adding a new function, snd_pcm_detach().
This works like snd_pcm_close() but without stopping it.  Give it a
try.

If anyone sees a problem with it, let me know.


thanks,

Takashi

---
From bdbce35b6d582d4ee4a0813190f09232bd8dc02c Mon Sep 17 00:00:00 2001
From: Takashi Iwai <tiwai at suse.de>
Date: Tue, 7 Jul 2009 16:59:09 +0200
Subject: [PATCH] Add snd_pcm_detach() function

Added a new function, snd_pcm_detach().
This function closes the given PCM stream without stopping.
It's useful for closing the inherited PCM stream after forking
without interfering the parent process.

Signed-off-by: Takashi Iwai <tiwai at suse.de>
---
 include/pcm.h |    1 +
 src/pcm/pcm.c |   16 ++++++++++++++++
 2 files changed, 17 insertions(+), 0 deletions(-)

diff --git a/include/pcm.h b/include/pcm.h
index 15e9cb2..9992d53 100644
--- a/include/pcm.h
+++ b/include/pcm.h
@@ -412,6 +412,7 @@ int snd_pcm_open_lconf(snd_pcm_t **pcm, const char *name,
 		       snd_config_t *lconf);
 
 int snd_pcm_close(snd_pcm_t *pcm);
+int snd_pcm_detach(snd_pcm_t *pcm);
 const char *snd_pcm_name(snd_pcm_t *pcm);
 snd_pcm_type_t snd_pcm_type(snd_pcm_t *pcm);
 snd_pcm_stream_t snd_pcm_stream(snd_pcm_t *pcm);
diff --git a/src/pcm/pcm.c b/src/pcm/pcm.c
index e63bbe9..059840f 100644
--- a/src/pcm/pcm.c
+++ b/src/pcm/pcm.c
@@ -713,6 +713,22 @@ int snd_pcm_close(snd_pcm_t *pcm)
 }	
 
 /**
+ * \brief close PCM handle without stopping the assigned stream
+ * \param pcm PCM handle
+ * \return 0 on success otherwise a negative error code
+ *
+ * Closes the specified PCM handle without stopping the stream.
+ * This can be useful if you need to close and release the inherited
+ * PCM stream after forking.  Unlike snd_pcm_stop(), this function
+ * doesn't influence the PCM stream of the parent.
+ */
+int snd_pcm_detach(snd_pcm_t *pcm)
+{
+	pcm->donot_close = 1;
+	return snd_pcm_close(pcm);
+}
+
+/**
  * \brief set nonblock mode
  * \param pcm PCM handle
  * \param nonblock 0 = block, 1 = nonblock mode
-- 
1.6.3.2



More information about the Alsa-devel mailing list