Changeset 20ad5a20371bd4b028c2fd8d6ae9a928878e5d06

Show
Ignore:
Timestamp:
07/09/08 13:06:14 (3 months ago)
Author:
Rémi Denis-Courmont <rdenis@simphalempin.com>
git-committer:
Rémi Denis-Courmont <rdenis@simphalempin.com> 1220785574 +0300
git-parent:

[28092982d6ddf47461484664e2ff3fe6eef46faa]

git-author:
Rémi Denis-Courmont <rdenis@simphalempin.com> 1220785574 +0300
Message:

Podcast: thread safety fixes

Variables callback can be (and usually are) invoked from other threads.
They cannot access the object without locking, and must be unregistered
before the object is destroyed (var_DelCallback() -> free(p_sys)).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • modules/services_discovery/podcast.c

    rc6d9fff r20ad5a2  
    3434#include <vlc_playlist.h> 
    3535#include <vlc_network.h> 
     36#include <assert.h> 
    3637 
    3738#include <errno.h>                                                 /* ENOMEM */ 
     
    8990    int i_urls; 
    9091 
     92    vlc_mutex_t lock; 
     93    vlc_cond_t  wait; 
    9194    bool b_update; 
    9295}; 
     
    115118    p_sys->i_input = 0; 
    116119    p_sys->pp_input = NULL; 
     120    vlc_mutex_init( &p_sys->lock ); 
     121    vlc_cond_init( &p_sys->wait ); 
    117122    p_sys->b_update = true; 
    118123 
     
    122127    /* Give us a name */ 
    123128    services_discovery_SetLocalizedName( p_sd, _("Podcasts") ); 
     129 
     130    /* Launch the callback associated with this variable */ 
     131    var_Create( p_sd, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT ); 
     132    var_AddCallback( p_sd, "podcast-urls", UrlsChange, p_sys ); 
    124133 
    125134    return VLC_SUCCESS; 
     
    134143    services_discovery_sys_t *p_sys  = p_sd->p_sys; 
    135144    int i; 
     145 
     146    var_DelCallback( p_sd, "podcast-urls", UrlsChange, p_sys ); 
     147    vlc_cond_destroy( &p_sys->wait ); 
     148    vlc_mutex_destroy( &p_sys->lock ); 
     149 
    136150    for( i = 0; i < p_sys->i_input; i++ ) 
    137151    { 
     
    156170    services_discovery_sys_t *p_sys  = p_sd->p_sys; 
    157171 
    158     /* Launch the callback associated with this variable */ 
    159     var_Create( p_sd, "podcast-urls", VLC_VAR_STRING | VLC_VAR_DOINHERIT ); 
    160     var_AddCallback( p_sd, "podcast-urls", UrlsChange, p_sys ); 
    161  
     172    vlc_mutex_lock( &p_sys->lock ); 
     173    mutex_cleanup_push( &p_sys->lock ); 
    162174    for( ;; ) 
    163175    { 
    164         /* FIXME: That's 2000 wake up per seconds too many. */ 
    165         msleep( 500 ); 
    166  
    167         int canc = vlc_savecancel (); /* <- FIXME: should not be needed */ 
    168         if( p_sys->b_update == true ) 
    169         { 
    170             msg_Dbg( p_sd, "Update required" ); 
    171             char* psz_urls = var_GetNonEmptyString( p_sd, "podcast-urls" ); 
    172             if( psz_urls != NULL ) 
    173                 ParseUrls( p_sd, psz_urls ); 
    174             free( psz_urls ); 
    175             p_sys->b_update = false; 
    176         } 
     176        while( !p_sys->b_update ) 
     177            vlc_cond_wait( &p_sys->wait, &p_sys->lock ); 
     178 
     179        int canc = vlc_savecancel (); 
     180        msg_Dbg( p_sd, "Update required" ); 
     181        char* psz_urls = var_GetNonEmptyString( p_sd, "podcast-urls" ); 
     182        if( psz_urls != NULL ) 
     183            ParseUrls( p_sd, psz_urls ); 
     184        free( psz_urls ); 
     185        p_sys->b_update = false; 
    177186 
    178187        for( int i = 0; i < p_sd->p_sys->i_input; i++ ) 
     
    190199        vlc_restorecancel (canc); 
    191200    } 
     201    vlc_cleanup_pop(); 
     202    assert(0); /* dead code */ 
    192203} 
    193204 
     
    199210    VLC_UNUSED(newval); 
    200211    services_discovery_sys_t *p_sys  = (services_discovery_sys_t *)p_data; 
     212 
     213    vlc_mutex_lock( &p_sys->lock ); 
    201214    p_sys->b_update = true; 
     215    vlc_cond_signal( &p_sys->wait ); 
     216    vlc_mutex_unlock( &p_sys->lock ); 
    202217    return VLC_SUCCESS; 
    203218}