]> git.sesse.net Git - vlc/blob - src/playlist/thread.c
libvlccore: push threads cancellation down vlc_thread_create
[vlc] / src / playlist / thread.c
1 /*****************************************************************************
2  * thread.c : Playlist management functions
3  *****************************************************************************
4  * Copyright © 1999-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Clément Stenac <zorglub@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_es.h>
30 #include <vlc_input.h>
31 #include <vlc_interface.h>
32 #include <vlc_playlist.h>
33 #include "playlist_internal.h"
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static void* RunControlThread   ( vlc_object_t * );
39 static void* RunPreparse        ( vlc_object_t * );
40 static void* RunFetcher         ( vlc_object_t * );
41 static void PreparseDestructor  ( vlc_object_t * );
42 static void FetcherDestructor   ( vlc_object_t * );
43
44 /*****************************************************************************
45  * Main functions for the global thread
46  *****************************************************************************/
47
48 /**
49  * Create the main playlist thread
50  * Additionally to the playlist, this thread controls :
51  *    - Statistics
52  *    - VLM
53  * \param p_parent
54  * \return an object with a started thread
55  */
56 void __playlist_ThreadCreate( vlc_object_t *p_parent )
57 {
58     playlist_t *p_playlist = playlist_Create( p_parent );
59     if( !p_playlist ) return;
60
61     // Preparse
62     static const char ppname[] = "preparser";
63     p_playlist->p_preparse =
64         vlc_custom_create( p_playlist, sizeof( playlist_preparse_t ),
65                            VLC_OBJECT_GENERIC, ppname );
66     if( !p_playlist->p_preparse )
67     {
68         msg_Err( p_playlist, "unable to create preparser" );
69         vlc_object_release( p_playlist );
70         return;
71     }
72     p_playlist->p_preparse->psz_object_name = strdup( "preparser" );
73     p_playlist->p_preparse->i_waiting = 0;
74     p_playlist->p_preparse->pp_waiting = NULL;
75
76     vlc_object_set_destructor( p_playlist->p_preparse, PreparseDestructor );
77
78     vlc_object_attach( p_playlist->p_preparse, p_playlist );
79     if( vlc_thread_create( p_playlist->p_preparse, "preparser",
80                            RunPreparse, VLC_THREAD_PRIORITY_LOW, true ) )
81     {
82         msg_Err( p_playlist, "cannot spawn preparse thread" );
83         vlc_object_release( p_playlist->p_preparse );
84         return;
85     }
86
87     // Secondary Preparse
88     static const char fname[] = "fetcher";
89     p_playlist->p_fetcher =
90         vlc_custom_create( p_playlist, sizeof( playlist_fetcher_t ),
91                            VLC_OBJECT_GENERIC, fname );
92     if( !p_playlist->p_fetcher )
93     {
94         msg_Err( p_playlist, "unable to create secondary preparser" );
95         vlc_object_release( p_playlist );
96         return;
97     }
98     p_playlist->p_fetcher->psz_object_name = strdup( "fetcher" );
99     p_playlist->p_fetcher->i_waiting = 0;
100     p_playlist->p_fetcher->pp_waiting = NULL;
101     p_playlist->p_fetcher->i_art_policy = var_CreateGetInteger( p_playlist,
102                                                                 "album-art" );
103
104     vlc_object_set_destructor( p_playlist->p_fetcher, FetcherDestructor );
105
106     vlc_object_attach( p_playlist->p_fetcher, p_playlist );
107     if( vlc_thread_create( p_playlist->p_fetcher,
108                            "fetcher",
109                            RunFetcher,
110                            VLC_THREAD_PRIORITY_LOW, true ) )
111     {
112         msg_Err( p_playlist, "cannot spawn secondary preparse thread" );
113         vlc_object_release( p_playlist->p_fetcher );
114         return;
115     }
116
117     // Start the thread
118     if( vlc_thread_create( p_playlist, "playlist", RunControlThread,
119                            VLC_THREAD_PRIORITY_LOW, true ) )
120     {
121         msg_Err( p_playlist, "cannot spawn playlist thread" );
122         vlc_object_release( p_playlist );
123         return;
124     }
125
126     /* The object has been initialized, now attach it */
127     vlc_object_attach( p_playlist, p_parent );
128
129     return;
130 }
131
132 /**
133  * Run the main control thread itself
134  */
135 static void* RunControlThread ( vlc_object_t *p_this )
136 {
137     playlist_t *p_playlist = (playlist_t*)p_this;
138     /* Tell above that we're ready */
139     vlc_thread_ready( p_playlist );
140
141     int canc = vlc_savecancel ();
142     vlc_object_lock( p_playlist );
143     while( vlc_object_alive( p_playlist ) )
144     {
145         playlist_MainLoop( p_playlist );
146
147         /* The playlist lock has been unlocked, so we can't tell if
148          * someone has killed us in the meantime. Check now. */
149         if( !vlc_object_alive( p_playlist ) )
150             break;
151
152         if( p_playlist->b_cant_sleep )
153         {
154             /* 100 ms is an acceptable delay for playlist operations */
155             vlc_object_unlock( p_playlist );
156
157             msleep( INTF_IDLE_SLEEP*2 );
158
159             vlc_object_lock( p_playlist );
160         }
161         else
162         {
163             vlc_object_wait( p_playlist );
164         }
165     }
166     vlc_object_unlock( p_playlist );
167
168     playlist_LastLoop( p_playlist );
169     vlc_restorecancel (canc);
170     return NULL;
171 }
172
173 /*****************************************************************************
174  * Preparse-specific functions
175  *****************************************************************************/
176 static void* RunPreparse ( vlc_object_t *p_this )
177 {
178     playlist_preparse_t *p_obj = (playlist_preparse_t*)p_this;
179     int canc;
180
181     /* Tell above that we're ready */
182     vlc_thread_ready( p_obj );
183     canc = vlc_savecancel ();
184     playlist_PreparseLoop( p_obj );
185     vlc_restorecancel (canc);
186     return NULL;
187 }
188
189 static void* RunFetcher( vlc_object_t *p_this )
190 {
191     playlist_fetcher_t *p_obj = (playlist_fetcher_t *)p_this;
192     /* Tell above that we're ready */
193     vlc_thread_ready( p_obj );
194     int canc = vlc_savecancel ();
195     playlist_FetcherLoop( p_obj );
196     vlc_restorecancel (canc);
197     return NULL;
198 }
199
200 static void PreparseDestructor( vlc_object_t * p_this )
201 {
202     playlist_preparse_t * p_preparse = (playlist_preparse_t *)p_this;
203     free( p_preparse->pp_waiting );
204     msg_Dbg( p_this, "Destroyed" );
205 }
206
207 static void FetcherDestructor( vlc_object_t * p_this )
208 {
209     playlist_fetcher_t * p_fetcher = (playlist_fetcher_t *)p_this;
210     free( p_fetcher->pp_waiting );
211     msg_Dbg( p_this, "Destroyed" );
212 }