thread.c 4.2 KB
Newer Older
1
/*****************************************************************************
2
 * thread.c : Playlist management functions
3
 *****************************************************************************
4
 * Copyright © 1999-2008 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * $Id$
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *          Clément Stenac <zorglub@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
24 25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include <vlc_common.h>
Jean-Paul Saman's avatar
Jean-Paul Saman committed
29
#include <vlc_es.h>
30
#include <vlc_input.h>
Clément Stenac's avatar
Clément Stenac committed
31 32
#include <vlc_interface.h>
#include <vlc_playlist.h>
33
#include "playlist_internal.h"
34 35 36 37

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
38
static void* RunControlThread   ( vlc_object_t * );
39 40 41 42 43 44

/*****************************************************************************
 * Main functions for the global thread
 *****************************************************************************/

/**
45
 * Create the main playlist threads.
46 47 48 49 50 51
 * Additionally to the playlist, this thread controls :
 *    - Statistics
 *    - VLM
 * \param p_parent
 * \return an object with a started thread
 */
52
void playlist_Activate( playlist_t *p_playlist )
53
{
54 55
    /* */
    playlist_private_t *p_sys = pl_priv(p_playlist);
56

57 58 59 60
    /* Fetcher */
    p_sys->p_fetcher = playlist_fetcher_New( p_playlist );
    if( !p_sys->p_fetcher )
        msg_Err( p_playlist, "cannot create playlist fetcher" );
61

62 63 64 65 66 67
    /* Preparse */
    p_sys->p_preparser = playlist_preparser_New( p_playlist, p_sys->p_fetcher );
    if( !p_sys->p_preparser )
        msg_Err( p_playlist, "cannot create playlist preparser" );

    /* Start the playlist thread */
68
    if( vlc_thread_create( p_playlist, "playlist", RunControlThread,
69
                           VLC_THREAD_PRIORITY_LOW, false ) )
70 71 72
    {
        msg_Err( p_playlist, "cannot spawn playlist thread" );
    }
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    msg_Err( p_playlist, "Activated" );
}

void playlist_Deactivate( playlist_t *p_playlist )
{
    /* */
    playlist_private_t *p_sys = pl_priv(p_playlist);

    msg_Err( p_playlist, "Deactivate" );
    vlc_object_kill( p_playlist );
    vlc_thread_join( p_playlist );

    if( p_sys->p_preparser )
        playlist_preparser_Delete( p_sys->p_preparser );
    if( p_sys->p_fetcher )
        playlist_fetcher_Delete( p_sys->p_fetcher );
89

90 91 92 93
    /* The NULL are there only to assert in playlist destructor */
    p_sys->p_preparser = NULL;
    p_sys->p_fetcher = NULL;
    msg_Err( p_playlist, "Deactivated" );
94 95 96 97 98
}

/**
 * Run the main control thread itself
 */
99
static void* RunControlThread ( vlc_object_t *p_this )
100
{
101
    playlist_t *p_playlist = (playlist_t*)p_this;
102

103
    int canc = vlc_savecancel ();
104 105
    vlc_object_lock( p_playlist );
    while( vlc_object_alive( p_playlist ) )
106
    {
107
        playlist_MainLoop( p_playlist );
108

109 110 111 112 113
        /* The playlist lock has been unlocked, so we can't tell if
         * someone has killed us in the meantime. Check now. */
        if( !vlc_object_alive( p_playlist ) )
            break;

114
        if( pl_priv(p_playlist)->b_cant_sleep )
115 116
        {
            /* 100 ms is an acceptable delay for playlist operations */
Laurent Aimar's avatar
Laurent Aimar committed
117 118
            vlc_object_unlock( p_playlist );

119
            msleep( INTF_IDLE_SLEEP*2 );
Laurent Aimar's avatar
Laurent Aimar committed
120 121

            vlc_object_lock( p_playlist );
122 123 124
        }
        else
        {
125
            vlc_object_wait( p_playlist );
126
        }
127
    }
128 129
    vlc_object_unlock( p_playlist );

130
    playlist_LastLoop( p_playlist );
131
    vlc_restorecancel (canc);
132
    return NULL;
133
}