[vlc-commits] commit: Protect XInitThreads() with a global lock ( Rémi Denis-Courmont )

git at videolan.org git at videolan.org
Sat Jun 5 12:35:10 CEST 2010


vlc/vlc-1.1 | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Fri Jun  4 00:00:51 2010 +0300| [de4477801bbd7f3402c3d3c9ad5666b7a0f1b32c] | committer: Rémi Denis-Courmont 

Protect XInitThreads() with a global lock

(cherry picked from commit ddfbaea04ac415ce036350e3f2244b6f6bb881dc)

Conflicts:

	include/vlc_threads.h
	src/misc/threads.c

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

 include/vlc_threads.h                  |    2 +
 include/vlc_xlib.h                     |   41 ++++++++++++++++++++++++++++++++
 modules/audio_output/pulse.c           |   12 ++++-----
 modules/codec/avcodec/vaapi.c          |    7 ++++-
 modules/gui/hildon/maemo.c             |    3 +-
 modules/gui/qt4/qt4.cpp                |    4 +-
 modules/gui/skins2/x11/x11_factory.cpp |    5 +++-
 modules/video_output/xcb/glx.c         |    3 +-
 src/Makefile.am                        |    1 +
 src/misc/threads.c                     |    1 +
 10 files changed, 66 insertions(+), 13 deletions(-)

diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index 88226cd..d7844ee 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -431,6 +431,8 @@ class vlc_mutex_locker
 #endif
 
 enum {
+   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
index 0000000..4e3632c
--- /dev/null
+++ b/include/vlc_xlib.h
@@ -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
diff --git a/modules/audio_output/pulse.c b/modules/audio_output/pulse.c
index a064ff3..e7c7450 100644
--- a/modules/audio_output/pulse.c
+++ b/modules/audio_output/pulse.c
@@ -34,9 +34,10 @@
 #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 )
diff --git a/modules/codec/avcodec/vaapi.c b/modules/codec/avcodec/vaapi.c
index c7a4f94..cd891d1 100644
--- a/modules/codec/avcodec/vaapi.c
+++ b/modules/codec/avcodec/vaapi.c
@@ -461,7 +461,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) );
diff --git a/modules/gui/hildon/maemo.c b/modules/gui/hildon/maemo.c
index 640069d..401860f 100644
--- a/modules/gui/hildon/maemo.c
+++ b/modules/gui/hildon/maemo.c
@@ -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 */
diff --git a/modules/gui/qt4/qt4.cpp b/modules/gui/qt4/qt4.cpp
index 6d13f5b..daa7b5f 100644
--- a/modules/gui/qt4/qt4.cpp
+++ b/modules/gui/qt4/qt4.cpp
@@ -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" );
diff --git a/modules/gui/skins2/x11/x11_factory.cpp b/modules/gui/skins2/x11/x11_factory.cpp
index 5d09402..97ab4c9 100644
--- a/modules/gui/skins2/x11/x11_factory.cpp
+++ b/modules/gui/skins2/x11/x11_factory.cpp
@@ -40,6 +40,9 @@
 
 #include "../src/generic_window.hpp"
 
+#include <vlc_common.h>
+#include <vlc_xlib.h>
+
 X11Factory::X11Factory( intf_thread_t *pIntf ): OSFactory( pIntf ),
     m_pDisplay( NULL ), m_pTimerLoop( NULL ), m_dirSep( "/" )
 {
@@ -57,7 +60,7 @@ X11Factory::~X11Factory()
 bool X11Factory::init()
 {
     // make sure xlib is safe-thread
-    if( !var_InheritBool( getIntf(), "xlib" ) || !XInitThreads() )
+    if( !vlc_xlib_init( VLC_OBJECT(getIntf()) ) )
     {
         msg_Err( getIntf(), "initializing xlib for multi-threading failed" );
         return false;
diff --git a/modules/video_output/xcb/glx.c b/modules/video_output/xcb/glx.c
index 0ac24fd..46f8a90 100644
--- a/modules/video_output/xcb/glx.c
+++ b/modules/video_output/xcb/glx.c
@@ -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"
@@ -207,7 +208,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;
diff --git a/src/Makefile.am b/src/Makefile.am
index b8d8b41..5ca53f8 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -104,6 +104,7 @@ pluginsinclude_HEADERS = \
 	../include/vlc_vout_opengl.h \
 	../include/vlc_vout_window.h \
 	../include/vlc_xml.h \
+	../include/vlc_xlib.h \
 	$(NULL)
 
 noinst_HEADERS = \
diff --git a/src/misc/threads.c b/src/misc/threads.c
index 04e83e8..b73eb30 100644
--- a/src/misc/threads.c
+++ b/src/misc/threads.c
@@ -239,6 +239,7 @@ void vlc_thread_cancel (vlc_object_t *obj)
 void vlc_global_mutex (unsigned n, bool acquire)
 {
     static vlc_mutex_t locks[] = {
+        VLC_STATIC_MUTEX,
     };
     assert (n < (sizeof (locks) / sizeof (locks[0])));
     vlc_mutex_t *lock = locks + n;



More information about the vlc-commits mailing list