Commit 1ac60c8c authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Keep a thread-safe list of interfaces

parent f645d20b
...@@ -51,6 +51,7 @@ typedef struct intf_thread_t ...@@ -51,6 +51,7 @@ typedef struct intf_thread_t
{ {
VLC_COMMON_MEMBERS VLC_COMMON_MEMBERS
struct intf_thread_t *p_next; /** LibVLC interfaces book keeping */
/* Thread properties and locks */ /* Thread properties and locks */
#if defined( __APPLE__ ) || defined( WIN32 ) #if defined( __APPLE__ ) || defined( WIN32 )
bool b_should_run_on_first_thread; bool b_should_run_on_first_thread;
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
# include "config.h" # include "config.h"
#endif #endif
#include <assert.h>
#include <vlc_common.h> #include <vlc_common.h>
#include <vlc_aout.h> #include <vlc_aout.h>
...@@ -58,6 +59,8 @@ static void * MonitorLibVLCDeath( vlc_object_t *p_this ); ...@@ -58,6 +59,8 @@ static void * MonitorLibVLCDeath( vlc_object_t *p_this );
static int AddIntfCallback( vlc_object_t *, char const *, static int AddIntfCallback( vlc_object_t *, char const *,
vlc_value_t , vlc_value_t , void * ); vlc_value_t , vlc_value_t , void * );
static vlc_mutex_t lock = VLC_STATIC_MUTEX;
#undef intf_Create #undef intf_Create
/** /**
* Create and start an interface. * Create and start an interface.
...@@ -68,10 +71,11 @@ static int AddIntfCallback( vlc_object_t *, char const *, ...@@ -68,10 +71,11 @@ static int AddIntfCallback( vlc_object_t *, char const *,
*/ */
int intf_Create( vlc_object_t *p_this, const char *psz_module ) int intf_Create( vlc_object_t *p_this, const char *psz_module )
{ {
libvlc_int_t *p_libvlc = p_this->p_libvlc;
intf_thread_t * p_intf; intf_thread_t * p_intf;
/* Allocate structure */ /* Allocate structure */
p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF ); p_intf = vlc_object_create( p_libvlc, VLC_OBJECT_INTF );
if( !p_intf ) if( !p_intf )
return VLC_ENOMEM; return VLC_ENOMEM;
...@@ -100,8 +104,8 @@ int intf_Create( vlc_object_t *p_this, const char *psz_module ) ...@@ -100,8 +104,8 @@ int intf_Create( vlc_object_t *p_this, const char *psz_module )
var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL ); var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
/* Attach interface to its parent object */ /* Attach interface to LibVLC */
vlc_object_attach( p_intf, p_this ); vlc_object_attach( p_intf, p_libvlc );
#if defined( __APPLE__ ) || defined( WIN32 ) #if defined( __APPLE__ ) || defined( WIN32 )
p_intf->b_should_run_on_first_thread = false; p_intf->b_should_run_on_first_thread = false;
#endif #endif
...@@ -122,9 +126,12 @@ int intf_Create( vlc_object_t *p_this, const char *psz_module ) ...@@ -122,9 +126,12 @@ int intf_Create( vlc_object_t *p_this, const char *psz_module )
goto error; goto error;
} }
if( p_intf->pf_run == NULL ) vlc_mutex_lock( &lock );
return VLC_SUCCESS; if( !vlc_object_alive( p_libvlc ) )
{
vlc_mutex_unlock( &lock );
goto error; /* Too late! */
}
#if defined( __APPLE__ ) || defined( WIN32 ) #if defined( __APPLE__ ) || defined( WIN32 )
/* Hack to get Mac OS X Cocoa runtime running /* Hack to get Mac OS X Cocoa runtime running
* (it needs access to the main thread) */ * (it needs access to the main thread) */
...@@ -134,8 +141,10 @@ int intf_Create( vlc_object_t *p_this, const char *psz_module ) ...@@ -134,8 +141,10 @@ int intf_Create( vlc_object_t *p_this, const char *psz_module )
VLC_THREAD_PRIORITY_LOW ) ) VLC_THREAD_PRIORITY_LOW ) )
{ {
msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" ); msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
vlc_mutex_unlock( &lock );
goto error; goto error;
} }
assert( p_intf->pf_run );
p_intf->pf_run( p_intf ); p_intf->pf_run( p_intf );
/* It is monitoring libvlc, not the p_intf */ /* It is monitoring libvlc, not the p_intf */
...@@ -144,13 +153,19 @@ int intf_Create( vlc_object_t *p_this, const char *psz_module ) ...@@ -144,13 +153,19 @@ int intf_Create( vlc_object_t *p_this, const char *psz_module )
else else
#endif #endif
/* Run the interface in a separate thread */ /* Run the interface in a separate thread */
if( vlc_thread_create( p_intf, "interface", RunInterface, if( p_intf->pf_run
&& vlc_thread_create( p_intf, "interface", RunInterface,
VLC_THREAD_PRIORITY_LOW ) ) VLC_THREAD_PRIORITY_LOW ) )
{ {
msg_Err( p_intf, "cannot spawn interface thread" ); msg_Err( p_intf, "cannot spawn interface thread" );
vlc_mutex_unlock( &lock );
goto error; goto error;
} }
p_intf->p_next = libvlc_priv( p_libvlc )->p_intf;
libvlc_priv( p_libvlc )->p_intf = p_intf;
vlc_mutex_unlock( &lock );
return VLC_SUCCESS; return VLC_SUCCESS;
error: error:
...@@ -169,28 +184,35 @@ error: ...@@ -169,28 +184,35 @@ error:
*/ */
void intf_DestroyAll( libvlc_int_t *p_libvlc ) void intf_DestroyAll( libvlc_int_t *p_libvlc )
{ {
vlc_list_t *l = vlc_list_find( VLC_OBJECT(p_libvlc), VLC_OBJECT_INTF, FIND_CHILD ); intf_thread_t *p_first;
assert( !vlc_object_alive( p_libvlc ) );
vlc_mutex_lock( &lock );
p_first = libvlc_priv( p_libvlc )->p_intf;
#ifndef NDEBUG
libvlc_priv( p_libvlc )->p_intf = NULL;
#endif
vlc_mutex_unlock( &lock );
/* Tell the interfaces to die */ /* Tell the interfaces to die */
for( int i = 0; i < l->i_count; i++ ) for( intf_thread_t *p_intf = p_first; p_intf; p_intf = p_intf->p_next )
vlc_object_kill( l->p_values[i].p_object ); vlc_object_kill( p_intf );
/* Cleanup the interfaces */ /* Cleanup the interfaces */
for( int i = 0; i < l->i_count; i++ ) for( intf_thread_t *p_intf = p_first; p_intf != NULL; )
{ {
intf_thread_t *p_intf = (intf_thread_t *)l->p_values[i].p_object; intf_thread_t *p_next = p_intf->p_next;
if( p_intf->pf_run ) if( p_intf->pf_run )
vlc_thread_join( p_intf ); vlc_thread_join( p_intf );
module_unneed( p_intf, p_intf->p_module ); module_unneed( p_intf, p_intf->p_module );
free( p_intf->psz_intf ); free( p_intf->psz_intf );
config_ChainDestroy( p_intf->p_cfg ); config_ChainDestroy( p_intf->p_cfg );
} vlc_object_release( p_intf );
/* Destroy objects */ p_intf = p_next;
for( int i = 0; i < l->i_count; i++ ) }
vlc_object_release( l->p_values[i].p_object ); /* for intf_Create() */
vlc_list_release( l );
} }
/* Following functions are local */ /* Following functions are local */
......
...@@ -224,6 +224,11 @@ typedef struct libvlc_priv_t ...@@ -224,6 +224,11 @@ typedef struct libvlc_priv_t
#ifdef ENABLE_SOUT #ifdef ENABLE_SOUT
sap_handler_t *p_sap; ///< SAP SDP advertiser sap_handler_t *p_sap; ///< SAP SDP advertiser
#endif #endif
/* Interfaces */
struct intf_thread_t *p_intf; ///< Interfaces linked-list
/* Objects tree */
vlc_mutex_t structure_lock; vlc_mutex_t structure_lock;
} libvlc_priv_t; } libvlc_priv_t;
......
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