@@ -298,6 +298,8 @@ typedef struct {
298298typedef struct {
299299 int32_t guest_playing ;
300300 uint32_t stream_id ;
301+ pthread_mutex_t ctrl_mutex ;
302+ pthread_cond_t ctrl_cond ;
301303} vsnd_stream_sel_t ;
302304
303305typedef struct {
@@ -339,9 +341,7 @@ static virtio_snd_prop_t vsnd_props[VSND_DEV_CNT_MAX] = {
339341static int vsnd_dev_cnt = 0 ;
340342
341343static pthread_mutex_t virtio_snd_mutex = PTHREAD_MUTEX_INITIALIZER ;
342- static pthread_mutex_t virtio_snd_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER ;
343344static pthread_cond_t virtio_snd_tx_cond = PTHREAD_COND_INITIALIZER ;
344- static pthread_cond_t virtio_snd_ctrl_cond = PTHREAD_COND_INITIALIZER ;
345345static int tx_ev_notify ;
346346
347347/* Forward declaration */
@@ -566,6 +566,8 @@ static void virtio_snd_read_pcm_prepare(const virtio_snd_pcm_hdr_t *query,
566566 pthread_mutex_init (& props -> ring .lock , NULL );
567567 pthread_cond_init (& props -> ring .readable , NULL );
568568 pthread_cond_init (& props -> ring .writable , NULL );
569+ pthread_mutex_init (& props -> v .ctrl_mutex , NULL );
570+ pthread_cond_init (& props -> v .ctrl_cond , NULL );
569571 INIT_LIST_HEAD (& props -> buf_queue_head );
570572 props -> immediate =
571573 (void * ) malloc (sizeof (* props -> immediate ) * cnfa_period_bytes );
@@ -597,7 +599,7 @@ static void virtio_snd_read_pcm_start(const virtio_snd_pcm_hdr_t *query,
597599 /* Control the callback to start playing */
598600 props -> pp .hdr .hdr .code = VIRTIO_SND_R_PCM_START ;
599601 props -> v .guest_playing ++ ;
600- pthread_cond_signal (& virtio_snd_ctrl_cond );
602+ pthread_cond_signal (& props -> v . ctrl_cond );
601603
602604 * plen = 0 ;
603605 fprintf (stderr , "end virtio_snd_read_pcm_start\n" );
@@ -622,7 +624,7 @@ static void virtio_snd_read_pcm_stop(const virtio_snd_pcm_hdr_t *query,
622624 /* Control the callback to stop playing */
623625 props -> pp .hdr .hdr .code = VIRTIO_SND_R_PCM_STOP ;
624626 props -> v .guest_playing -- ;
625- pthread_cond_signal (& virtio_snd_ctrl_cond );
627+ pthread_cond_signal (& props -> v . ctrl_cond );
626628
627629 * plen = 0 ;
628630 fprintf (stderr , "end virtio_snd_read_pcm_stop\n" );
@@ -666,6 +668,9 @@ static void virtio_snd_read_pcm_release(const virtio_snd_pcm_hdr_t *query,
666668
667669 CNFAClose (props -> audio_host );
668670 fprintf (stderr , "pass CNFAclose\n" );
671+ /* Tear down stream related locking mechanisms */
672+ pthread_mutex_destroy (& props -> v .ctrl_mutex );
673+ pthread_cond_destroy (& props -> v .ctrl_cond );
669674
670675 * plen = 0 ;
671676 fprintf (stderr , "virtio_snd_read_pcm_release\n" );
@@ -774,11 +779,11 @@ static void virtio_snd_cb(struct CNFADriver *dev,
774779 int channels = dev -> channelsPlay ;
775780 uint32_t out_buf_sz = framesp * channels ;
776781
777- pthread_mutex_lock (& virtio_snd_ctrl_mutex );
782+ pthread_mutex_lock (& v_ptr -> ctrl_mutex );
778783 while (v_ptr -> guest_playing == 0 ) {
779784 fprintf (stderr , "wait ctrl cond\n" );
780785 memset (out , 0 , sizeof (* out ) * out_buf_sz );
781- pthread_cond_wait (& virtio_snd_ctrl_cond , & virtio_snd_ctrl_mutex );
786+ pthread_cond_wait (& v_ptr -> ctrl_cond , & v_ptr -> ctrl_mutex );
782787 }
783788
784789 totalframesr += framesr ;
@@ -809,7 +814,7 @@ static void virtio_snd_cb(struct CNFADriver *dev,
809814 fprintf (stderr , "totalframesp %d totalframesr %d\n" , totalframesp ,
810815 totalframesr );
811816
812- pthread_mutex_unlock (& virtio_snd_ctrl_mutex );
817+ pthread_mutex_unlock (& v_ptr -> ctrl_mutex );
813818}
814819
815820#define VSND_DESC_CNT 3
0 commit comments