]> git.sesse.net Git - vlc/blobdiff - src/interface/interface.c
Cleanup handling of thread-less interfaces
[vlc] / src / interface / interface.c
index 21f0a25bbe6a7849996b6889e00d7cc5b605717f..c4d75f18e18810da900b32af60b923e8110dfa70 100644 (file)
@@ -54,6 +54,24 @@ static void RunInterface( intf_thread_t *p_intf );
 static int AddIntfCallback( vlc_object_t *, char const *,
                             vlc_value_t , vlc_value_t , void * );
 
+/**
+ * \brief Destroy the interface after the main loop endeed.
+ *
+ * \param p_intf the interface thread
+ * \return nothing
+ */
+static void intf_Destroy( vlc_object_t *obj )
+{
+    intf_thread_t *p_intf = (intf_thread_t *)obj;
+
+    /* Unlock module if present (a switch may have failed) */
+    if( p_intf->p_module )
+        module_Unneed( p_intf, p_intf->p_module );
+
+    free( p_intf->psz_intf );
+    vlc_mutex_destroy( &p_intf->change_lock );
+}
+
 /*****************************************************************************
  * intf_Create: prepare interface before main loop
  *****************************************************************************
@@ -110,10 +128,11 @@ intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module,
     p_intf->b_menu_change = false;
 
     /* Initialize mutexes */
-    vlc_mutex_init( p_intf, &p_intf->change_lock );
+    vlc_mutex_init( &p_intf->change_lock );
 
     /* Attach interface to its parent object */
     vlc_object_attach( p_intf, p_this );
+    vlc_object_set_destructor( p_intf, intf_Destroy );
 
     return p_intf;
 }
@@ -131,15 +150,13 @@ intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module,
  */
 int intf_RunThread( intf_thread_t *p_intf )
 {
-    /* This interface doesn't need to be run */
-    if( p_intf->pf_run == NULL )
-        return VLC_SUCCESS;
-
     /* Hack to get Mac OS X Cocoa runtime running
      * (it needs access to the main thread) */
     if( p_intf->b_should_run_on_first_thread )
     {
         RunInterface( p_intf );
+        vlc_object_detach( p_intf );
+        vlc_object_release( p_intf );
         return VLC_SUCCESS;
     }
     
@@ -165,36 +182,10 @@ void intf_StopThread( intf_thread_t *p_intf )
 {
     /* Tell the interface to die */
     vlc_object_kill( p_intf );
-    if( p_intf->pf_run != NULL )
-    {
-        vlc_cond_signal( &p_intf->object_wait );
-        vlc_thread_join( p_intf );
-    }
+    vlc_cond_signal( &p_intf->object_wait );
+    vlc_thread_join( p_intf );
 }
 
-/**
- * \brief Destroy the interface after the main loop endeed.
- *
- * Destroys interfaces and closes output devices
- * \param p_intf the interface thread
- * \return nothing
- */
-void intf_Destroy( intf_thread_t *p_intf )
-{
-    /* Unlock module if present (a switch may have failed) */
-    if( p_intf->p_module )
-    {
-        module_Unneed( p_intf, p_intf->p_module );
-    }
-    free( p_intf->psz_intf );
-
-    vlc_mutex_destroy( &p_intf->change_lock );
-
-    /* Free structure */
-    vlc_object_release( p_intf );
-}
-
-
 /* Following functions are local */
 
 /*****************************************************************************
@@ -231,7 +222,10 @@ static void RunInterface( intf_thread_t *p_intf )
     do
     {
         /* Give control to the interface */
-        p_intf->pf_run( p_intf );
+        if( p_intf->pf_run )
+            p_intf->pf_run( p_intf );
+        else
+            while( vlc_object_lock_and_wait( p_intf ) == 0 );
 
         /* Reset play on start status */
         p_intf->b_play = false;
@@ -283,7 +277,7 @@ static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
     if( intf_RunThread( p_intf ) != VLC_SUCCESS )
     {
         vlc_object_detach( p_intf );
-        intf_Destroy( p_intf );
+        vlc_object_release( p_intf );
         return VLC_EGENERIC;
     }