]> git.sesse.net Git - vlc/blob - src/playlist/thread.c
Preparser: use normal thread API
[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* RunFetcher         ( vlc_object_t * );
40 static void FetcherDestructor   ( vlc_object_t * );
41
42 /*****************************************************************************
43  * Main functions for the global thread
44  *****************************************************************************/
45
46 /**
47  * Create the main playlist thread
48  * Additionally to the playlist, this thread controls :
49  *    - Statistics
50  *    - VLM
51  * \param p_parent
52  * \return an object with a started thread
53  */
54 void __playlist_ThreadCreate( vlc_object_t *p_parent )
55 {
56     playlist_t *p_playlist = playlist_Create( p_parent );
57     if( !p_playlist ) return;
58
59     // Preparse
60     playlist_preparse_t *p_preparse = &p_playlist->p->preparse;
61     vlc_mutex_init (&p_preparse->lock);
62     vlc_cond_init (&p_preparse->wait);
63     p_preparse->i_waiting = 0;
64     p_preparse->pp_waiting = NULL;
65
66     if( vlc_clone( &p_preparse->thread, playlist_PreparseLoop, p_preparse,
67                    VLC_THREAD_PRIORITY_LOW ) )
68     {
69         msg_Err( p_playlist, "cannot spawn preparse thread" );
70         p_preparse->up = false;
71         return;
72     }
73     p_preparse->up = true;
74
75     // Secondary Preparse
76     static const char fname[] = "fetcher";
77     p_playlist->p->p_fetcher =
78         vlc_custom_create( p_playlist, sizeof( playlist_fetcher_t ),
79                            VLC_OBJECT_GENERIC, fname );
80     if( !p_playlist->p->p_fetcher )
81     {
82         msg_Err( p_playlist, "unable to create secondary preparser" );
83         vlc_object_release( p_playlist );
84         return;
85     }
86     p_playlist->p->p_fetcher->i_waiting = 0;
87     p_playlist->p->p_fetcher->pp_waiting = NULL;
88     p_playlist->p->p_fetcher->i_art_policy = var_CreateGetInteger( p_playlist,
89                                                                 "album-art" );
90
91     vlc_object_set_destructor( p_playlist->p->p_fetcher, FetcherDestructor );
92
93     vlc_object_attach( p_playlist->p->p_fetcher, p_playlist );
94     if( vlc_thread_create( p_playlist->p->p_fetcher,
95                            "fetcher",
96                            RunFetcher,
97                            VLC_THREAD_PRIORITY_LOW, false ) )
98     {
99         msg_Err( p_playlist, "cannot spawn secondary preparse thread" );
100         vlc_object_release( p_playlist->p->p_fetcher );
101         return;
102     }
103
104     // Start the thread
105     if( vlc_thread_create( p_playlist, "playlist", RunControlThread,
106                            VLC_THREAD_PRIORITY_LOW, false ) )
107     {
108         msg_Err( p_playlist, "cannot spawn playlist thread" );
109         vlc_object_release( p_playlist );
110         return;
111     }
112
113     /* The object has been initialized, now attach it */
114     vlc_object_attach( p_playlist, p_parent );
115
116     return;
117 }
118
119 /**
120  * Run the main control thread itself
121  */
122 static void* RunControlThread ( vlc_object_t *p_this )
123 {
124     playlist_t *p_playlist = (playlist_t*)p_this;
125
126     int canc = vlc_savecancel ();
127     vlc_object_lock( p_playlist );
128     while( vlc_object_alive( p_playlist ) )
129     {
130         playlist_MainLoop( p_playlist );
131
132         /* The playlist lock has been unlocked, so we can't tell if
133          * someone has killed us in the meantime. Check now. */
134         if( !vlc_object_alive( p_playlist ) )
135             break;
136
137         if( p_playlist->b_cant_sleep )
138         {
139             /* 100 ms is an acceptable delay for playlist operations */
140             vlc_object_unlock( p_playlist );
141
142             msleep( INTF_IDLE_SLEEP*2 );
143
144             vlc_object_lock( p_playlist );
145         }
146         else
147         {
148             vlc_object_wait( p_playlist );
149         }
150     }
151     vlc_object_unlock( p_playlist );
152
153     playlist_LastLoop( p_playlist );
154     vlc_restorecancel (canc);
155     return NULL;
156 }
157
158 static void* RunFetcher( vlc_object_t *p_this )
159 {
160     playlist_fetcher_t *p_obj = (playlist_fetcher_t *)p_this;
161     int canc = vlc_savecancel ();
162     playlist_FetcherLoop( p_obj );
163     vlc_restorecancel (canc);
164     return NULL;
165 }
166
167 static void FetcherDestructor( vlc_object_t * p_this )
168 {
169     playlist_fetcher_t * p_fetcher = (playlist_fetcher_t *)p_this;
170     free( p_fetcher->pp_waiting );
171     msg_Dbg( p_this, "Destroyed" );
172 }