[vlc-commits] dsm/sd: discover in a separate thread

Thomas Guillem git at videolan.org
Wed Dec 10 18:26:11 CET 2014


vlc | branch: master | Thomas Guillem <thomas at gllm.fr> | Wed Dec 10 18:19:51 2014 +0100| [3a71b317f58e3ef627b65ae16d1c3403aaf171f4] | committer: Jean-Baptiste Kempf

dsm/sd: discover in a separate thread

Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=3a71b317f58e3ef627b65ae16d1c3403aaf171f4
---

 modules/access/dsm/sd.c |   79 ++++++++++++++++++++++++++++++++---------------
 1 file changed, 54 insertions(+), 25 deletions(-)

diff --git a/modules/access/dsm/sd.c b/modules/access/dsm/sd.c
index 724a77e..c07ed6d 100644
--- a/modules/access/dsm/sd.c
+++ b/modules/access/dsm/sd.c
@@ -30,11 +30,18 @@
 # include "config.h"
 #endif
 
+#include <vlc_common.h>
+#include <vlc_atomic.h>
+#include <bdsm/bdsm.h>
+
 #include "common.h"
 
 struct services_discovery_sys_t
 {
-    netbios_ns      *ns;
+    netbios_ns      *p_ns;
+    vlc_thread_t    thread;
+
+    atomic_bool     stop;
 };
 
 int bdsm_sd_probe_Open (vlc_object_t *p_this)
@@ -47,26 +54,20 @@ 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;
+    services_discovery_sys_t *p_sys = p_sd->p_sys;
 
-    /* Let's create a NETBIOS name service object */
-    p_sys->ns = netbios_ns_new();
-    if( p_sys->ns == NULL )
-        goto error;
+    if( !netbios_ns_discover( p_sys->p_ns ) )
+        return NULL;
 
-    if( !netbios_ns_discover( p_sys->ns ) )
-        goto error;
+    if (atomic_load(&p_sys->stop))
+        return NULL;
 
-    for( ssize_t i = 0; i < netbios_ns_entry_count( p_sys->ns ); i++ )
+    for( ssize_t i = 0; i < netbios_ns_entry_count( p_sys->p_ns ); i++ )
     {
-        netbios_ns_entry *p_entry = netbios_ns_entry_at( p_sys->ns, i );
+        netbios_ns_entry *p_entry = netbios_ns_entry_at( p_sys->p_ns, i );
         char type = netbios_ns_entry_type( p_entry );
 
         if( type == 0x20 )
@@ -76,7 +77,7 @@ int bdsm_SdOpen (vlc_object_t *p_this)
             const char *name = netbios_ns_entry_name( p_entry );
 
             if( asprintf(&psz_mrl, "smb://%s", name) < 0 )
-                goto error;
+                return NULL;
 
             p_item = input_item_NewWithType( psz_mrl, name, 0, NULL,
                                              0, -1, ITEM_TYPE_NODE );
@@ -84,18 +85,37 @@ int bdsm_SdOpen (vlc_object_t *p_this)
 
             services_discovery_AddItem( p_sd, p_item, NULL );
 
-            free( psz_mrl );
         }
     }
+    return 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 = calloc (1, sizeof (*p_sys));
+
+    if( p_sys == NULL )
+        return VLC_ENOMEM;
+
+    p_sd->p_sys = p_sys;
+
+    p_sys->p_ns = netbios_ns_new();
+    if( p_sys->p_ns == NULL )
+        goto error;
+
+    atomic_store(&p_sys->stop, false);
+
+    if( vlc_clone( &p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW ) )
+    {
+        p_sys->thread = 0;
+        goto error;
+    }
 
     return VLC_SUCCESS;
 
     error:
-        if( p_sys->ns != NULL )
-            netbios_ns_destroy( p_sys->ns );
-        free( p_sys );
-        p_sd->p_sys = NULL;
-
+        bdsm_SdClose( p_this );
         return VLC_EGENERIC;
 }
 
@@ -107,7 +127,16 @@ void bdsm_SdClose (vlc_object_t *p_this)
     if( p_sys == NULL )
         return;
 
-    if( p_sys->ns != NULL )
-        netbios_ns_destroy( p_sys->ns );
+    if( p_sys->thread ) {
+        atomic_store(&p_sys->stop, true);
+
+        if( p_sys->p_ns )
+            netbios_ns_abort( p_sys->p_ns );
+        vlc_join( p_sys->thread, NULL );
+    }
+    if( p_sys->p_ns )
+        netbios_ns_destroy( p_sys->p_ns );
+
+    free( p_sys );
 }
 



More information about the vlc-commits mailing list