[vlc-devel] [RFC PATCH 7/8] dsm/sd: discover in a separate thread

Thomas Guillem thomas at gllm.fr
Thu Nov 27 12:00:17 CET 2014


---
 modules/access/dsm/sd.c | 52 ++++++++++++++++++++++++++++++-------------------
 1 file changed, 32 insertions(+), 20 deletions(-)

diff --git a/modules/access/dsm/sd.c b/modules/access/dsm/sd.c
index 9b581f6..8c4cddf 100644
--- a/modules/access/dsm/sd.c
+++ b/modules/access/dsm/sd.c
@@ -35,6 +35,7 @@
 struct services_discovery_sys_t
 {
     netbios_ns      *ns;
+    vlc_thread_t    thread;
 };
 
 int bdsm_sd_probe_Open (vlc_object_t *p_this)
@@ -47,26 +48,22 @@ int bdsm_sd_probe_Open (vlc_object_t *p_this)
     return VLC_PROBE_CONTINUE;
 }
 
-int bdsm_SdOpen (vlc_object_t *p_this)
+static void *Run( void *data )
 {
-    services_discovery_t *p_sd = (services_discovery_t *)p_this;
-    services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
-
-    if( p_sys == NULL )
-        return VLC_ENOMEM;
-    p_sd->p_sys = p_sys;
+    services_discovery_t *p_sd = data;
+    netbios_ns *ns;
 
     /* Let's create a NETBIOS name service object */
-    p_sys->ns = netbios_ns_new();
-    if( p_sys->ns == NULL )
+    ns = netbios_ns_new();
+    if( ns == NULL )
         goto error;
 
-    if( !netbios_ns_discover( p_sys->ns ) )
+    if( !netbios_ns_discover( ns ) )
         goto error;
 
-    for( ssize_t i = 0; i < netbios_ns_entry_count( p_sys->ns ); i++ )
+    for( ssize_t i = 0; i < netbios_ns_entry_count( ns ); i++ )
     {
-        netbios_ns_entry *p_entry = netbios_ns_entry_at( p_sys->ns, i );
+        netbios_ns_entry *p_entry = netbios_ns_entry_at( ns, i );
 
         if( p_entry->type == 0x20 )
         {
@@ -85,16 +82,30 @@ int bdsm_SdOpen (vlc_object_t *p_this)
             free( psz_mrl );
         }
     }
+    error:
+        if( ns != NULL )
+            netbios_ns_destroy( ns );
 
-    return VLC_SUCCESS;
+        return NULL;
+}
 
-    error:
-        if( p_sys->ns != NULL )
-            netbios_ns_destroy( p_sys->ns );
-        free( p_sys );
-        p_sd->p_sys = NULL;
+int bdsm_SdOpen (vlc_object_t *p_this)
+{
+    services_discovery_t *p_sd = (services_discovery_t *)p_this;
+    services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
+
+    if( p_sys == NULL )
+        return VLC_ENOMEM;
+
+    p_sd->p_sys = p_sys;
 
+    if( vlc_clone( &p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW ) )
+    {
+        free(p_sys);
         return VLC_EGENERIC;
+    }
+
+    return VLC_SUCCESS;
 }
 
 void bdsm_SdClose (vlc_object_t *p_this)
@@ -105,7 +116,8 @@ void bdsm_SdClose (vlc_object_t *p_this)
     if( p_sys == NULL )
         return;
 
-    if( p_sys->ns != NULL )
-        netbios_ns_destroy( p_sys->ns );
+    vlc_join( p_sys->thread, NULL );
+
+    free( p_sys );
 }
 
-- 
2.1.3




More information about the vlc-devel mailing list