1 /*****************************************************************************
2 * thread.c : Playlist management functions
3 *****************************************************************************
4 * Copyright © 1999-2008 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
30 #include <vlc_input.h>
31 #include <vlc_interface.h>
32 #include <vlc_playlist.h>
33 #include "playlist_internal.h"
35 /*****************************************************************************
37 *****************************************************************************/
38 static void RunControlThread ( playlist_t * );
39 static void RunPreparse( playlist_preparse_t * );
40 static void RunFetcher( playlist_fetcher_t * );
42 /*****************************************************************************
43 * Main functions for the global thread
44 *****************************************************************************/
47 * Create the main playlist thread
48 * Additionally to the playlist, this thread controls :
52 * \return an object with a started thread
54 void __playlist_ThreadCreate( vlc_object_t *p_parent )
56 playlist_t *p_playlist = playlist_Create( p_parent );
57 if( !p_playlist ) return;
60 p_playlist->p_stats = (global_stats_t *)malloc( sizeof( global_stats_t ) );
61 if( !p_playlist->p_stats )
63 vlc_object_release( p_playlist );
66 vlc_mutex_init( p_playlist, &p_playlist->p_stats->lock );
67 p_playlist->p_stats_computer = NULL;
70 p_playlist->p_preparse = vlc_object_create( p_playlist,
71 sizeof( playlist_preparse_t ) );
72 if( !p_playlist->p_preparse )
74 msg_Err( p_playlist, "unable to create preparser" );
75 vlc_object_release( p_playlist );
78 p_playlist->p_preparse->i_waiting = 0;
79 p_playlist->p_preparse->pp_waiting = NULL;
81 vlc_object_attach( p_playlist->p_preparse, p_playlist );
82 if( vlc_thread_create( p_playlist->p_preparse, "preparser",
83 RunPreparse, VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
85 msg_Err( p_playlist, "cannot spawn preparse thread" );
86 vlc_object_detach( p_playlist->p_preparse );
87 vlc_object_release( p_playlist->p_preparse );
92 p_playlist->p_fetcher = vlc_object_create( p_playlist,
93 sizeof( playlist_fetcher_t ) );
94 if( !p_playlist->p_fetcher )
96 msg_Err( p_playlist, "unable to create secondary preparser" );
97 vlc_object_release( p_playlist );
100 p_playlist->p_fetcher->i_waiting = 0;
101 p_playlist->p_fetcher->p_waiting = NULL;
102 p_playlist->p_fetcher->b_fetch_meta = var_CreateGetInteger( p_playlist,
104 p_playlist->p_fetcher->i_art_policy = var_CreateGetInteger( p_playlist,
107 vlc_object_attach( p_playlist->p_fetcher, p_playlist );
108 if( vlc_thread_create( p_playlist->p_fetcher,
111 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
113 msg_Err( p_playlist, "cannot spawn secondary preparse thread" );
114 vlc_object_detach( p_playlist->p_fetcher );
115 vlc_object_release( p_playlist->p_fetcher );
120 if( vlc_thread_create( p_playlist, "playlist", RunControlThread,
121 VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
123 msg_Err( p_playlist, "cannot spawn playlist thread" );
124 vlc_object_release( p_playlist );
128 /* The object has been initialized, now attach it */
129 vlc_object_attach( p_playlist, p_parent );
135 * Destroy the playlist global thread.
137 * Deinits all things controlled by the playlist global thread
138 * \param p_playlist the playlist thread to destroy
139 * \return VLC_SUCCESS or an error
141 int playlist_ThreadDestroy( playlist_t * p_playlist )
143 // Tell playlist to go to last loop
144 vlc_object_kill( p_playlist );
147 if( p_playlist->p_preparse )
149 vlc_object_kill( p_playlist->p_preparse );
150 vlc_thread_join( p_playlist->p_preparse );
151 free( p_playlist->p_preparse->pp_waiting );
152 vlc_object_detach( p_playlist->p_preparse );
153 vlc_object_release( p_playlist->p_preparse );
157 if( p_playlist->p_fetcher )
159 vlc_object_kill( p_playlist->p_fetcher );
160 vlc_thread_join( p_playlist->p_fetcher );
161 free( p_playlist->p_fetcher->p_waiting );
162 vlc_object_detach( p_playlist->p_fetcher );
163 vlc_object_release( p_playlist->p_fetcher );
166 // Wait for thread to complete
167 vlc_thread_join( p_playlist );
170 vlc_mutex_destroy( &p_playlist->p_stats->lock );
171 if( p_playlist->p_stats )
172 free( p_playlist->p_stats );
174 playlist_Destroy( p_playlist );
180 * Run the main control thread itself
182 static void RunControlThread ( playlist_t *p_playlist )
186 /* Tell above that we're ready */
187 vlc_thread_ready( p_playlist );
188 while( !p_playlist->b_die )
192 playlist_MainLoop( p_playlist );
193 if( p_playlist->b_cant_sleep )
195 /* 100 ms is an acceptable delay for playlist operations */
196 msleep( INTF_IDLE_SLEEP*2 );
201 vlc_cond_wait( &p_playlist->object_wait, &p_playlist->object_lock );
205 playlist_LastLoop( p_playlist );
208 /*****************************************************************************
209 * Preparse-specific functions
210 *****************************************************************************/
211 static void RunPreparse ( playlist_preparse_t *p_obj )
213 /* Tell above that we're ready */
214 vlc_thread_ready( p_obj );
215 playlist_PreparseLoop( p_obj );
218 static void RunFetcher( playlist_fetcher_t *p_obj )
220 /* Tell above that we're ready */
221 vlc_thread_ready( p_obj );
222 playlist_FetcherLoop( p_obj );