fetcher: detach thread and race to idle
authorRémi Denis-Courmont <remi@remlab.net>
Sun, 2 Aug 2009 06:43:27 +0000 (09:43 +0300)
committerRémi Denis-Courmont <remi@remlab.net>
Sun, 2 Aug 2009 06:44:27 +0000 (09:44 +0300)
src/playlist/fetcher.c

index aa6d16f..d2b6356 100644 (file)
@@ -44,9 +44,9 @@ struct playlist_fetcher_t
 
     playlist_t      *p_playlist;
 
-    vlc_thread_t    thread;
     vlc_mutex_t     lock;
-    bool            b_live, b_zombie;
+    vlc_cond_t      wait;
+    bool            b_live;
     int             i_art_policy;
     int             i_waiting;
     input_item_t    **pp_waiting;
@@ -72,8 +72,8 @@ playlist_fetcher_t *playlist_fetcher_New( playlist_t *p_playlist )
     vlc_object_attach( p_fetcher, p_playlist );
     p_fetcher->p_playlist = p_playlist;
     vlc_mutex_init( &p_fetcher->lock );
+    vlc_cond_init( &p_fetcher->wait );
     p_fetcher->b_live = false;
-    p_fetcher->b_zombie = false;
     p_fetcher->i_waiting = 0;
     p_fetcher->pp_waiting = NULL;
     p_fetcher->i_art_policy = var_GetInteger( p_playlist, "album-art" );
@@ -87,45 +87,39 @@ void playlist_fetcher_Push( playlist_fetcher_t *p_fetcher, input_item_t *p_item
     vlc_gc_incref( p_item );
 
     vlc_mutex_lock( &p_fetcher->lock );
-    if( p_fetcher->b_zombie ) /* FIXME: detach the thread */
-    {
-        vlc_join( p_fetcher->thread, NULL );
-        p_fetcher->b_zombie = false;
-    }
-
     INSERT_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting,
                  p_fetcher->i_waiting, p_item );
     if( !p_fetcher->b_live )
     {
-        if( vlc_clone( &p_fetcher->thread, Thread, p_fetcher,
-                       VLC_THREAD_PRIORITY_LOW ) )
+        vlc_thread_t th;
+
+        if( vlc_clone( &th, Thread, p_fetcher, VLC_THREAD_PRIORITY_LOW ) )
             msg_Err( p_fetcher, "cannot spawn secondary preparse thread" );
         else
+        {
+            vlc_detach( th );
             p_fetcher->b_live = true;
+        }
     }
     vlc_mutex_unlock( &p_fetcher->lock );
 }
 
 void playlist_fetcher_Delete( playlist_fetcher_t *p_fetcher )
 {
-    bool b_join;
-    /* Destroy the item meta-infos fetcher */
     vlc_mutex_lock( &p_fetcher->lock );
-    if( p_fetcher->b_live )
-    {
-        vlc_object_kill( p_fetcher );
-        vlc_cancel( p_fetcher->thread );
-    }
-    b_join = p_fetcher->b_live || p_fetcher->b_zombie;
-    vlc_mutex_unlock( &p_fetcher->lock );
-    if( b_join )
-        vlc_join( p_fetcher->thread, NULL );
-
+    /* Remove any left-over item, the fetcher will exit */
     while( p_fetcher->i_waiting > 0 )
-    {   /* Any left-over unparsed item? */
+    {
         vlc_gc_decref( p_fetcher->pp_waiting[0] );
         REMOVE_ELEM( p_fetcher->pp_waiting, p_fetcher->i_waiting, 0 );
     }
+
+    vlc_object_kill( p_fetcher );
+    while( p_fetcher->b_live )
+        vlc_cond_wait( &p_fetcher->wait, &p_fetcher->lock );
+    vlc_mutex_unlock( &p_fetcher->lock );
+
+    vlc_cond_destroy( &p_fetcher->wait );
     vlc_mutex_destroy( &p_fetcher->lock );
     vlc_object_release( p_fetcher );
 }
@@ -400,7 +394,7 @@ static void *Thread( void *p_data )
         else
         {
             p_fetcher->b_live = false;
-            p_fetcher->b_zombie = true;
+            vlc_cond_signal( &p_fetcher->wait );
         }
         vlc_mutex_unlock( &p_fetcher->lock );
 
@@ -408,7 +402,6 @@ static void *Thread( void *p_data )
             break;
 
         /* */
-        int canc = vlc_savecancel();
 
         /* Wait that the input item is preparsed if it is being played */
         WaitPreparsed( p_fetcher, p_item );
@@ -444,13 +437,6 @@ static void *Thread( void *p_data )
 
     end:
         vlc_gc_decref( p_item );
-
-        vlc_restorecancel( canc );
-
-        int i_activity = var_GetInteger( p_playlist, "activity" );
-        if( i_activity < 0 ) i_activity = 0;
-        /* Sleep at least 1ms and handle potential thread cancellation */
-        msleep( (i_activity+1) * 1000 );
     }
     return NULL;
 }