]> git.sesse.net Git - vlc/commitdiff
Protect XInitThreads() with a global lock
authorRémi Denis-Courmont <remi@remlab.net>
Thu, 3 Jun 2010 20:55:27 +0000 (23:55 +0300)
committerRémi Denis-Courmont <remi@remlab.net>
Thu, 3 Jun 2010 20:55:45 +0000 (23:55 +0300)
include/vlc_threads.h
include/vlc_xlib.h [new file with mode: 0644]
modules/audio_output/pulse.c
modules/codec/avcodec/vaapi.c
modules/gui/hildon/maemo.c
modules/gui/qt4/qt4.cpp
modules/gui/skins2/x11/x11_factory.cpp
modules/video_output/xcb/glx.c
src/Makefile.am
src/misc/threads.c

index 5d9ab65cb2456877a92f7486f3e0ba88e58cb3b9..3770e2fa0f401e7b6f0760e52fa1451c98028f9a 100644 (file)
@@ -445,6 +445,8 @@ class vlc_mutex_locker
 enum {
    VLC_AVCODEC_MUTEX = 0,
    VLC_GCRYPT_MUTEX,
+   VLC_XLIB_MUTEX,
+   /* Insert new entry HERE */
    VLC_MAX_MUTEX
 };
 
diff --git a/include/vlc_xlib.h b/include/vlc_xlib.h
new file mode 100644 (file)
index 0000000..4e3632c
--- /dev/null
@@ -0,0 +1,41 @@
+/*****************************************************************************
+ * vlc_xlib.h: initialization of Xlib
+ *****************************************************************************
+ * Copyright (C) 2010 Rémi Denis-Courmont
+ *
+ * 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.
+ *****************************************************************************/
+
+#ifndef VLC_XLIB_H
+# define VLC_XLIB_H 1
+
+# include <X11/Xlib.h>
+
+static inline bool vlc_xlib_init (vlc_object_t *obj)
+{
+    bool ok = false;
+
+    if (var_InheritBool (obj, "xlib"))
+    {
+        /* XInitThreads() can be called multiple times,
+         * but it is not reentrant. */
+        vlc_global_lock (VLC_XLIB_MUTEX);
+        ok = XInitThreads () != 0;
+        vlc_global_unlock (VLC_XLIB_MUTEX);
+    }
+    return ok;
+}
+
+#endif
index a064ff3aec0ef5307a143a16a3bd1d2a74255e7c..e7c74508dc16be118d82d360378070424fa0481a 100644 (file)
 #include <vlc_cpu.h>
 
 #include <pulse/pulseaudio.h>
-#ifdef HAVE_X11_XLIB_H
-# include <X11/Xlib.h>
+#ifdef X_DISPLAY_MISSING
+# error Xlib required due to PulseAudio bug 799!
 #endif
+#include <vlc_xlib.h>
 
 #include <assert.h>
 
@@ -121,12 +122,9 @@ static int Open ( vlc_object_t *p_this )
     struct pa_buffer_attr a;
     struct pa_channel_map map;
 
-#ifdef X_DISPLAY_MISSING
-# error Xlib required due to PulseAudio bug 799!
-#else
-    if( !var_InheritBool( p_this, "xlib" ) || !XInitThreads() )
+    if( !vlc_xlib_init( p_this ) )
         return VLC_EGENERIC;
-#endif
+
     /* Allocate structures */
     p_aout->output.p_sys = p_sys = calloc( 1, sizeof( aout_sys_t ) );
     if( p_sys == NULL )
index de793acaa12662605f56423a6f498357ce0b5d7f..5f676d870d78861a3ac70a4d7787ccdda89ed2e7 100644 (file)
@@ -464,7 +464,12 @@ static void Delete( vlc_va_t *p_external )
 /* */
 vlc_va_t *vlc_va_NewVaapi( int i_codec_id )
 {
-    if( !XInitThreads() )
+    bool fail;
+
+    vlc_global_lock( VLC_XLIB_MUTEX );
+    fail = !XInitThreads();
+    vlc_global_unlock( VLC_XLIB_MUTEX )
+    if( unlikely(fail) )
         return NULL;
 
     vlc_va_vaapi_t *p_va = calloc( 1, sizeof(*p_va) );
index 640069d55e7cca4549aa56c8b1ee1f21a06f6af3..401860f8453a743410b7da4b16031e53c9ef9797 100644 (file)
@@ -30,6 +30,7 @@
 #include <vlc_plugin.h>
 #include <vlc_interface.h>
 #include <vlc_vout_window.h>
+#include <vlc_xlib.h>
 
 #include <hildon/hildon-program.h>
 #include <hildon/hildon-banner.h>
@@ -80,7 +81,7 @@ static int Open( vlc_object_t *p_this )
     intf_sys_t *p_sys;
     vlc_value_t val;
 
-    if( !var_InheritBool( p_this, "xlib" ) || !XInitThreads() )
+    if( !vlc_xlib_init( p_this ) )
         return VLC_EGENERIC;
 
     /* Allocate instance and initialize some members */
index 6d13f5bcec57ba18ede305691ced066146548c1a..daa7b5fe5ed98cec942250edeff1f98ab2b88cb7 100644 (file)
@@ -39,7 +39,7 @@
 #include "util/qvlcapp.hpp"     /* QVLCApplication definition */
 
 #ifdef Q_WS_X11
- #include <X11/Xlib.h>
+ #include <vlc_xlib.h>
 #endif
 
 #include "../../../share/vlc32x32.xpm"
@@ -283,7 +283,7 @@ static int Open( vlc_object_t *p_this, bool isDialogProvider )
     intf_thread_t *p_intf = (intf_thread_t *)p_this;
 
 #ifdef Q_WS_X11
-    if( !var_InheritBool( p_this, "xlib" ) || !XInitThreads() )
+    if( !vlc_xlib_init( p_this ) )
         return VLC_EGENERIC;
 
     char *display = var_CreateGetNonEmptyString( p_intf, "x11-display" );
index 5d09402ba99324738a2d375d9f344e79d46103b0..22c329b4ca2109782b134617d192621fb21e825d 100644 (file)
@@ -28,6 +28,7 @@
 #include <dirent.h>
 #include <sys/stat.h>
 #include <X11/Xlib.h>
+#include <vlc_xlib.h>
 
 #include "x11_factory.hpp"
 #include "x11_display.hpp"
@@ -57,7 +58,7 @@ X11Factory::~X11Factory()
 bool X11Factory::init()
 {
     // make sure xlib is safe-thread
-    if( !var_InheritBool( getIntf(), "xlib" ) || !XInitThreads() )
+    if( !vlc_xlib_init( getIntf() ) )
     {
         msg_Err( getIntf(), "initializing xlib for multi-threading failed" );
         return false;
index 7598cd10f3b3ec02e60f83df3c7bf73eb4efbb9a..7d148f19320ea16cdda497286f8b6e041acbc7b2 100644 (file)
@@ -34,6 +34,7 @@
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
+#include <vlc_xlib.h>
 #include <vlc_vout_display.h>
 #include <vlc_vout_opengl.h>
 #include "../opengl.h"
@@ -203,7 +204,7 @@ static int CreateWindow (vout_display_t *vd, xcb_connection_t *conn,
  */
 static int Open (vlc_object_t *obj)
 {
-    if (!var_InheritBool (obj, "xlib") || !XInitThreads ())
+    if (!vlc_xlib_init (obj))
         return VLC_EGENERIC;
 
     vout_display_t *vd = (vout_display_t *)obj;
index 9bc9f31786f4fadfcdc6c8d709369020dc3a269f..68aaf26c2edf070483e01743aef50fc8a05d1a92 100644 (file)
@@ -107,6 +107,7 @@ pluginsinclude_HEADERS = \
        ../include/vlc_vout_osd.h \
        ../include/vlc_vout_window.h \
        ../include/vlc_xml.h \
+       ../include/vlc_xlib.h \
        $(NULL)
 
 noinst_HEADERS = \
index ba93c1bb275af8309116bfc3c61df5d0acd22c02..fd84ad7270cc94de9779719290817947eacda60b 100644 (file)
@@ -241,6 +241,7 @@ void vlc_global_mutex (unsigned n, bool acquire)
     static vlc_mutex_t locks[] = {
         VLC_STATIC_MUTEX,
         VLC_STATIC_MUTEX,
+        VLC_STATIC_MUTEX,
     };
     assert (n < (sizeof (locks) / sizeof (locks[0])));
     vlc_mutex_t *lock = locks + n;