Commit 8b7d91f2 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Use var_GetGlobalMutex

Also never destroys global mutex, otherwise we get a race condition
(which is pretty contradictory to the point for mutexes)
parent c553d1eb
...@@ -221,10 +221,10 @@ static int QTVideoInit( decoder_t * ); ...@@ -221,10 +221,10 @@ static int QTVideoInit( decoder_t * );
static int Open( vlc_object_t *p_this ) static int Open( vlc_object_t *p_this )
{ {
decoder_t *p_dec = (decoder_t*)p_this; decoder_t *p_dec = (decoder_t*)p_this;
if( var_GetGlobalMutex( "qt_mutex" ) == NULL )
return VLC_EGENERIC;
/* create a mutex */ /* create a mutex */
var_Create( p_this->p_libvlc_global, "qt_mutex", VLC_VAR_MUTEX );
switch( p_dec->fmt_in.i_codec ) switch( p_dec->fmt_in.i_codec )
{ {
case VLC_FOURCC('S','V','Q','3'): /* Sorenson v3 */ case VLC_FOURCC('S','V','Q','3'): /* Sorenson v3 */
...@@ -284,11 +284,11 @@ static void Close( vlc_object_t *p_this ) ...@@ -284,11 +284,11 @@ static void Close( vlc_object_t *p_this )
{ {
decoder_t *p_dec = (decoder_t*)p_this; decoder_t *p_dec = (decoder_t*)p_this;
decoder_sys_t *p_sys = p_dec->p_sys; decoder_sys_t *p_sys = p_dec->p_sys;
vlc_value_t lockval; vlc_mutex_t *lock;
/* get lock, avoid segfault */ /* get lock, avoid segfault */
var_Get( p_dec->p_libvlc_global, "qt_mutex", &lockval ); lock = var_GetGlobalMutex( "qt_mutex" );
vlc_mutex_lock( lockval.p_address ); vlc_mutex_lock( lock );
if( p_dec->fmt_out.i_cat == AUDIO_ES ) if( p_dec->fmt_out.i_cat == AUDIO_ES )
{ {
...@@ -304,11 +304,11 @@ static void Close( vlc_object_t *p_this ) ...@@ -304,11 +304,11 @@ static void Close( vlc_object_t *p_this )
i_error = p_sys->SoundConverterClose( p_sys->myConverter ); i_error = p_sys->SoundConverterClose( p_sys->myConverter );
msg_Dbg( p_dec, "SoundConverterClose => %d", i_error ); msg_Dbg( p_dec, "SoundConverterClose => %d", i_error );
if( p_sys->p_buffer ) free( p_sys->p_buffer ); free( p_sys->p_buffer );
} }
else if( p_dec->fmt_out.i_cat == VIDEO_ES ) else if( p_dec->fmt_out.i_cat == VIDEO_ES )
{ {
if( p_sys->plane) free( p_sys->plane ); free( p_sys->plane );
} }
#ifndef __APPLE__ #ifndef __APPLE__
...@@ -327,8 +327,7 @@ static void Close( vlc_object_t *p_this ) ...@@ -327,8 +327,7 @@ static void Close( vlc_object_t *p_this )
#endif #endif
#endif #endif
vlc_mutex_unlock( lockval.p_address ); vlc_mutex_unlock( lock );
var_Destroy( p_dec->p_libvlc_global, "qt_mutex" );
if( p_sys ) free( p_sys ); if( p_sys ) free( p_sys );
} }
...@@ -338,25 +337,26 @@ static void Close( vlc_object_t *p_this ) ...@@ -338,25 +337,26 @@ static void Close( vlc_object_t *p_this )
*****************************************************************************/ *****************************************************************************/
static int OpenAudio( decoder_t *p_dec ) static int OpenAudio( decoder_t *p_dec )
{ {
decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) ); decoder_sys_t *p_sys;
vlc_mutex_t *lock = var_GetGlobalMutex( "qt_mutex" );
vlc_value_t lockval;
int i_error; int i_error;
char fcc[4]; char fcc[4];
unsigned long WantedBufferSize; unsigned long WantedBufferSize;
unsigned long InputBufferSize = 0; unsigned long InputBufferSize = 0;
unsigned long OutputBufferSize = 0; unsigned long OutputBufferSize = 0;
memset( p_sys, 0, sizeof( decoder_sys_t ) ); if( lock == NULL )
return VLC_EGENERIC;
p_sys = calloc( sizeof( decoder_sys_t ), 1 );
p_dec->p_sys = p_sys; p_dec->p_sys = p_sys;
p_dec->pf_decode_audio = DecodeAudio; p_dec->pf_decode_audio = DecodeAudio;
memcpy( fcc, &p_dec->fmt_in.i_codec, 4 ); memcpy( fcc, &p_dec->fmt_in.i_codec, 4 );
/* get lock, avoid segfault */ /* get lock, avoid segfault */
var_Get( p_dec->p_libvlc_global, "qt_mutex", &lockval ); vlc_mutex_lock( lock );
vlc_mutex_lock( lockval.p_address );
#ifdef __APPLE__ #ifdef __APPLE__
EnterMovies(); EnterMovies();
...@@ -467,7 +467,7 @@ static int OpenAudio( decoder_t *p_dec ) ...@@ -467,7 +467,7 @@ static int OpenAudio( decoder_t *p_dec )
p_sys->i_out = 0; p_sys->i_out = 0;
p_sys->i_out_frames = 0; p_sys->i_out_frames = 0;
vlc_mutex_unlock( lockval.p_address ); vlc_mutex_unlock( lock );
return VLC_SUCCESS; return VLC_SUCCESS;
exit_error: exit_error:
...@@ -475,7 +475,7 @@ exit_error: ...@@ -475,7 +475,7 @@ exit_error:
#ifdef LOADER #ifdef LOADER
Restore_LDT_Keeper( p_sys->ldt_fs ); Restore_LDT_Keeper( p_sys->ldt_fs );
#endif #endif
vlc_mutex_unlock( lockval.p_address ); vlc_mutex_unlock( lock );
free( p_sys ); free( p_sys );
return VLC_EGENERIC; return VLC_EGENERIC;
...@@ -488,7 +488,6 @@ static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block ) ...@@ -488,7 +488,6 @@ static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
{ {
decoder_sys_t *p_sys = p_dec->p_sys; decoder_sys_t *p_sys = p_dec->p_sys;
vlc_value_t lockval;
block_t *p_block; block_t *p_block;
int i_error; int i_error;
...@@ -547,16 +546,16 @@ static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block ) ...@@ -547,16 +546,16 @@ static aout_buffer_t *DecodeAudio( decoder_t *p_dec, block_t **pp_block )
{ {
int i_frames = p_sys->i_buffer / p_sys->InFrameSize; int i_frames = p_sys->i_buffer / p_sys->InFrameSize;
unsigned long i_out_frames, i_out_bytes; unsigned long i_out_frames, i_out_bytes;
vlc_mutex_t *lock = var_GetGlobalMutex( "qt_mutex ");
var_Get( p_dec->p_libvlc_global, "qt_mutex", &lockval ); vlc_mutex_lock( lock );
vlc_mutex_lock( lockval.p_address );
i_error = p_sys->SoundConverterConvertBuffer( p_sys->myConverter, i_error = p_sys->SoundConverterConvertBuffer( p_sys->myConverter,
p_sys->p_buffer, p_sys->p_buffer,
i_frames, i_frames,
p_sys->out_buffer, p_sys->out_buffer,
&i_out_frames, &i_out_frames,
&i_out_bytes ); &i_out_bytes );
vlc_mutex_unlock( lockval.p_address ); vlc_mutex_unlock( lock );
/* /*
msg_Dbg( p_dec, "decoded %d frames -> %ld frames (error=%d)", msg_Dbg( p_dec, "decoded %d frames -> %ld frames (error=%d)",
...@@ -625,7 +624,7 @@ static int OpenVideo( decoder_t *p_dec ) ...@@ -625,7 +624,7 @@ static int OpenVideo( decoder_t *p_dec )
decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) ); decoder_sys_t *p_sys = malloc( sizeof( decoder_sys_t ) );
#ifndef WIN32 #ifndef WIN32
vlc_value_t lockval; vlc_mutex_t *lock;
long i_result; long i_result;
ComponentDescription desc; ComponentDescription desc;
Component prev; Component prev;
...@@ -654,8 +653,8 @@ static int OpenVideo( decoder_t *p_dec ) ...@@ -654,8 +653,8 @@ static int OpenVideo( decoder_t *p_dec )
fcc, p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height ); fcc, p_dec->fmt_in.video.i_width, p_dec->fmt_in.video.i_height );
/* get lock, avoid segfault */ /* get lock, avoid segfault */
var_Get( p_dec->p_libvlc_global, "qt_mutex", &lockval ); lock = var_GetGlobalMutex( "qt_mutex" );
vlc_mutex_lock( lockval.p_address ); vlc_mutex_lock( lock );
#ifdef __APPLE__ #ifdef __APPLE__
EnterMovies(); EnterMovies();
...@@ -792,14 +791,14 @@ static int OpenVideo( decoder_t *p_dec ) ...@@ -792,14 +791,14 @@ static int OpenVideo( decoder_t *p_dec )
p_dec->fmt_out.video.i_height= p_dec->fmt_in.video.i_height; p_dec->fmt_out.video.i_height= p_dec->fmt_in.video.i_height;
p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height; p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_dec->fmt_in.video.i_width / p_dec->fmt_in.video.i_height;
vlc_mutex_unlock( lockval.p_address ); vlc_mutex_unlock( lock );
return VLC_SUCCESS; return VLC_SUCCESS;
exit_error: exit_error:
#ifdef LOADER #ifdef LOADER
Restore_LDT_Keeper( p_sys->ldt_fs ); Restore_LDT_Keeper( p_sys->ldt_fs );
#endif #endif
vlc_mutex_unlock( lockval.p_address ); vlc_mutex_unlock( lock );
#endif /* !WIN32 */ #endif /* !WIN32 */
...@@ -813,7 +812,7 @@ exit_error: ...@@ -813,7 +812,7 @@ exit_error:
static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block ) static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
{ {
decoder_sys_t *p_sys = p_dec->p_sys; decoder_sys_t *p_sys = p_dec->p_sys;
vlc_value_t lockval; vlc_mutex_t *lock;
block_t *p_block; block_t *p_block;
picture_t *p_pic; picture_t *p_pic;
mtime_t i_pts; mtime_t i_pts;
...@@ -861,8 +860,8 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block ) ...@@ -861,8 +860,8 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
return NULL; return NULL;
} }
var_Get( p_dec->p_libvlc_global, "qt_mutex", &lockval ); lock = var_GetGlobalMutex( "qt_mutex" );
vlc_mutex_lock( lockval.p_address ); vlc_mutex_lock( lock );
if( ( p_pic = p_dec->pf_vout_buffer_new( p_dec ) ) ) if( ( p_pic = p_dec->pf_vout_buffer_new( p_dec ) ) )
{ {
...@@ -886,7 +885,7 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block ) ...@@ -886,7 +885,7 @@ static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
p_pic->date = i_pts; p_pic->date = i_pts;
} }
vlc_mutex_unlock( lockval.p_address ); vlc_mutex_unlock( lock );
block_Release( p_block ); block_Release( p_block );
return p_pic; return p_pic;
......
...@@ -1214,22 +1214,19 @@ static struct gcry_thread_cbs gcry_threads_vlc = ...@@ -1214,22 +1214,19 @@ static struct gcry_thread_cbs gcry_threads_vlc =
/***************************************************************************** /*****************************************************************************
* Module initialization * Module initialization
*****************************************************************************/ *****************************************************************************/
static unsigned refs = 0;
static int static int
Open( vlc_object_t *p_this ) Open( vlc_object_t *p_this )
{ {
tls_t *p_tls = (tls_t *)p_this; tls_t *p_tls = (tls_t *)p_this;
vlc_mutex_t *lock;
vlc_value_t lock, count; lock = var_GetGlobalMutex( "gnutls_mutex" );
vlc_mutex_lock( lock );
var_Create( p_this->p_libvlc_global, "gnutls_mutex", VLC_VAR_MUTEX );
var_Get( p_this->p_libvlc_global, "gnutls_mutex", &lock );
vlc_mutex_lock( lock.p_address );
/* Initialize GnuTLS only once */ /* Initialize GnuTLS only once */
var_Create( p_this->p_libvlc_global, "gnutls_count", VLC_VAR_INTEGER ); if( refs == 0 )
var_Get( p_this->p_libvlc_global, "gnutls_count", &count);
if( count.i_int == 0)
{ {
#ifdef NEED_THREAD_CONTEXT #ifdef NEED_THREAD_CONTEXT
__p_gcry_data = VLC_OBJECT( p_this->p_libvlc ); __p_gcry_data = VLC_OBJECT( p_this->p_libvlc );
...@@ -1239,7 +1236,7 @@ Open( vlc_object_t *p_this ) ...@@ -1239,7 +1236,7 @@ Open( vlc_object_t *p_this )
if( gnutls_global_init( ) ) if( gnutls_global_init( ) )
{ {
msg_Warn( p_this, "cannot initialize GnuTLS" ); msg_Warn( p_this, "cannot initialize GnuTLS" );
vlc_mutex_unlock( lock.p_address ); vlc_mutex_unlock( lock );
return VLC_EGENERIC; return VLC_EGENERIC;
} }
...@@ -1247,16 +1244,15 @@ Open( vlc_object_t *p_this ) ...@@ -1247,16 +1244,15 @@ Open( vlc_object_t *p_this )
if( psz_version == NULL ) if( psz_version == NULL )
{ {
gnutls_global_deinit( ); gnutls_global_deinit( );
vlc_mutex_unlock( lock.p_address ); vlc_mutex_unlock( lock );
msg_Err( p_this, "unsupported GnuTLS version" ); msg_Err( p_this, "unsupported GnuTLS version" );
return VLC_EGENERIC; return VLC_EGENERIC;
} }
msg_Dbg( p_this, "GnuTLS v%s initialized", psz_version ); msg_Dbg( p_this, "GnuTLS v%s initialized", psz_version );
} }
count.i_int++; refs++;
var_Set( p_this->p_libvlc_global, "gnutls_count", count); vlc_mutex_unlock( lock );
vlc_mutex_unlock( lock.p_address );
p_tls->pf_server_create = gnutls_ServerCreate; p_tls->pf_server_create = gnutls_ServerCreate;
p_tls->pf_client_create = gnutls_ClientCreate; p_tls->pf_client_create = gnutls_ClientCreate;
...@@ -1273,22 +1269,16 @@ Close( vlc_object_t *p_this ) ...@@ -1273,22 +1269,16 @@ Close( vlc_object_t *p_this )
/*tls_t *p_tls = (tls_t *)p_this; /*tls_t *p_tls = (tls_t *)p_this;
tls_sys_t *p_sys = (tls_sys_t *)(p_this->p_sys);*/ tls_sys_t *p_sys = (tls_sys_t *)(p_this->p_sys);*/
vlc_value_t lock, count; vlc_mutex_t *lock;
var_Create( p_this->p_libvlc_global, "gnutls_mutex", VLC_VAR_MUTEX );
var_Get( p_this->p_libvlc_global, "gnutls_mutex", &lock );
vlc_mutex_lock( lock.p_address );
var_Create( p_this->p_libvlc_global, "gnutls_count", VLC_VAR_INTEGER ); lock = var_GetGlobalMutex( "gnutls_mutex" );
var_Get( p_this->p_libvlc_global, "gnutls_count", &count); vlc_mutex_lock( lock );
count.i_int--;
var_Set( p_this->p_libvlc_global, "gnutls_count", count);
if( count.i_int == 0 ) if( --refs == 0 )
{ {
gnutls_global_deinit( ); gnutls_global_deinit( );
msg_Dbg( p_this, "GnuTLS deinitialized" ); msg_Dbg( p_this, "GnuTLS deinitialized" );
} }
vlc_mutex_unlock( lock.p_address ); vlc_mutex_unlock( lock );
} }
...@@ -83,18 +83,16 @@ vlc_module_end(); ...@@ -83,18 +83,16 @@ vlc_module_end();
*****************************************************************************/ *****************************************************************************/
static int Open( vlc_object_t *p_this ) static int Open( vlc_object_t *p_this )
{ {
vlc_value_t lockval; vlc_mutex_t *lock;
/* FIXME: put this in the module (de)initialization ASAP */ /* FIXME: put this in the module (de)initialization ASAP */
var_Create( p_this->p_libvlc_global, "gtk", VLC_VAR_MUTEX ); lock = var_GetGlobalCreate( "gtk" );
vlc_mutex_lock( lock );
var_Get( p_this->p_libvlc_global, "gtk", &lockval );
vlc_mutex_lock( lockval.p_address );
if( i_refcount > 0 ) if( i_refcount > 0 )
{ {
i_refcount++; i_refcount++;
vlc_mutex_unlock( lockval.p_address ); vlc_mutex_unlock( lock );
return VLC_SUCCESS; return VLC_SUCCESS;
} }
...@@ -114,13 +112,12 @@ static int Open( vlc_object_t *p_this ) ...@@ -114,13 +112,12 @@ static int Open( vlc_object_t *p_this )
{ {
vlc_object_destroy( p_gtk_main ); vlc_object_destroy( p_gtk_main );
i_refcount--; i_refcount--;
vlc_mutex_unlock( lockval.p_address ); vlc_mutex_unlock( lock );
var_Destroy( p_this->p_libvlc_global, "gtk" );
return VLC_ETHREAD; return VLC_ETHREAD;
} }
i_refcount++; i_refcount++;
vlc_mutex_unlock( lockval.p_address ); vlc_mutex_unlock( lock );
return VLC_SUCCESS; return VLC_SUCCESS;
} }
...@@ -130,17 +127,16 @@ static int Open( vlc_object_t *p_this ) ...@@ -130,17 +127,16 @@ static int Open( vlc_object_t *p_this )
*****************************************************************************/ *****************************************************************************/
static void Close( vlc_object_t *p_this ) static void Close( vlc_object_t *p_this )
{ {
vlc_value_t lockval; vlc_mutex_t *lock;
var_Get( p_this->p_libvlc_global, "gtk", &lockval ); lock = var_GetGlobalMutex( "gtk" );
vlc_mutex_lock( lockval.p_address ); vlc_mutex_lock( lock );
i_refcount--; i_refcount--;
if( i_refcount > 0 ) if( i_refcount > 0 )
{ {
vlc_mutex_unlock( lockval.p_address ); vlc_mutex_unlock( lock );
var_Destroy( p_this->p_libvlc_global, "gtk" );
return; return;
} }
...@@ -150,8 +146,7 @@ static void Close( vlc_object_t *p_this ) ...@@ -150,8 +146,7 @@ static void Close( vlc_object_t *p_this )
vlc_object_destroy( p_gtk_main ); vlc_object_destroy( p_gtk_main );
p_gtk_main = NULL; p_gtk_main = NULL;
vlc_mutex_unlock( lockval.p_address ); vlc_mutex_unlock( lock );
var_Destroy( p_this->p_libvlc_global, "gtk" );
} }
static gint foo( gpointer bar ) { return TRUE; } static gint foo( gpointer bar ) { return TRUE; }
......
...@@ -83,18 +83,15 @@ vlc_module_end(); ...@@ -83,18 +83,15 @@ vlc_module_end();
*****************************************************************************/ *****************************************************************************/
static int Open( vlc_object_t *p_this ) static int Open( vlc_object_t *p_this )
{ {
vlc_value_t lockval; vlc_mutex_t *lock;
/* FIXME: put this in the module (de)initialization ASAP */ lock = var_GetGlobalMutex( "qte" );
var_Create( p_this->p_libvlc_global, "qte", VLC_VAR_MUTEX ); vlc_mutex_lock( lockval );
var_Get( p_this->p_libvlc_global, "qte", &lockval );
vlc_mutex_lock( (vlc_mutex_t *) lockval.p_address );
if( i_refcount > 0 ) if( i_refcount > 0 )
{ {
i_refcount++; i_refcount++;
vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address ); vlc_mutex_unlock( lock );
return VLC_SUCCESS; return VLC_SUCCESS;
} }
...@@ -108,13 +105,12 @@ static int Open( vlc_object_t *p_this ) ...@@ -108,13 +105,12 @@ static int Open( vlc_object_t *p_this )
{ {
vlc_object_destroy( p_qte_main ); vlc_object_destroy( p_qte_main );
i_refcount--; i_refcount--;
vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address ); vlc_mutex_unlock( lock );
var_Destroy( p_this->p_libvlc_global, "qte" );
return VLC_ETHREAD; return VLC_ETHREAD;
} }
i_refcount++; i_refcount++;
vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address ); vlc_mutex_unlock( lock );
vlc_object_attach( p_qte_main, p_this ); vlc_object_attach( p_qte_main, p_this );
msg_Dbg( p_this, "qte_main running" ); msg_Dbg( p_this, "qte_main running" );
...@@ -127,17 +123,16 @@ static int Open( vlc_object_t *p_this ) ...@@ -127,17 +123,16 @@ static int Open( vlc_object_t *p_this )
*****************************************************************************/ *****************************************************************************/
static void Close( vlc_object_t *p_this ) static void Close( vlc_object_t *p_this )
{ {
vlc_value_t lockval; vlc_mutex_t *lock;
var_Get( p_this->p_libvlc_global, "qte", &lockval ); lock = var_GetGlobalMutex( "qte" );
vlc_mutex_lock( (vlc_mutex_t *) lockval.p_address ); vlc_mutex_lock( lock );
i_refcount--; i_refcount--;
if( i_refcount > 0 ) if( i_refcount > 0 )
{ {
vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address ); vlc_mutex_unlock( lock );
var_Destroy( p_this->p_libvlc_global, "qte" );
return; return;
} }
p_qte_main->p_qte_application->quit(); p_qte_main->p_qte_application->quit();
...@@ -152,8 +147,7 @@ static void Close( vlc_object_t *p_this ) ...@@ -152,8 +147,7 @@ static void Close( vlc_object_t *p_this )
vlc_object_destroy( p_qte_main ); vlc_object_destroy( p_qte_main );
p_qte_main = NULL; p_qte_main = NULL;
vlc_mutex_unlock( (vlc_mutex_t *) lockval.p_address ); vlc_mutex_unlock( lock );
var_Destroy( p_this->p_libvlc_global, "qte" );
} }
/***************************************************************************** /*****************************************************************************
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment