[vlc-commits] win32: Add a vlc_mta_holder

Hugo Beauzée-Luyssen git at videolan.org
Mon Jun 26 13:27:13 CEST 2017


vlc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Wed Jun 14 10:27:32 2017 +0200| [50313aa964c2cc7bb0543587537c57d93fd3a08f] | committer: Hugo Beauzée-Luyssen

win32: Add a vlc_mta_holder

This small helper will hold on to the MTA to ensure it won't be
destroyed between two calls to CoInitializeEx.
This will help us not leaking our TA configurations to libvlc/libvlccore
threads

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

 include/vlc_threads.h  |   3 ++
 src/Makefile.am        |   2 +-
 src/misc/threads.c     |   3 ++
 src/win32/mta_holder.h | 121 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 128 insertions(+), 1 deletion(-)

diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index 9911d2eacf..960d458bb2 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -1053,6 +1053,9 @@ enum
    VLC_XLIB_MUTEX,
    VLC_MOSAIC_MUTEX,
    VLC_HIGHLIGHT_MUTEX,
+#ifdef _WIN32
+   VLC_MTA_MUTEX,
+#endif
    /* Insert new entry HERE */
    VLC_MAX_MUTEX
 };
diff --git a/src/Makefile.am b/src/Makefile.am
index 87cf8e3e29..c93a4021ee 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -468,7 +468,7 @@ libvlccore_la_LDFLAGS = \
 libvlccore_la_DEPENDENCIES = libvlccore.sym
 if HAVE_WIN32
 libvlccore_la_DEPENDENCIES += libvlc_win32_rc.$(OBJEXT)
-libvlccore_la_LDFLAGS += -Wl,libvlc_win32_rc.$(OBJEXT) -avoid-version -Wc,-static
+libvlccore_la_LDFLAGS += -Wl,libvlc_win32_rc.$(OBJEXT) -avoid-version -Wc,-static $(LIBCOM)
 endif
 if HAVE_OS2
 libvlccore_la_LDFLAGS += -avoid-version
diff --git a/src/misc/threads.c b/src/misc/threads.c
index dc8b89f702..3d099db6cb 100644
--- a/src/misc/threads.c
+++ b/src/misc/threads.c
@@ -37,6 +37,9 @@ void vlc_global_mutex (unsigned n, bool acquire)
         VLC_STATIC_MUTEX,
         VLC_STATIC_MUTEX,
         VLC_STATIC_MUTEX,
+#ifdef _WIN32
+        VLC_STATIC_MUTEX, // For MTA holder
+#endif
     };
     static_assert (VLC_MAX_MUTEX == (sizeof (locks) / sizeof (locks[0])),
                    "Wrong number of global mutexes");
diff --git a/src/win32/mta_holder.h b/src/win32/mta_holder.h
new file mode 100644
index 0000000000..1733c5f9a7
--- /dev/null
+++ b/src/win32/mta_holder.h
@@ -0,0 +1,121 @@
+/*****************************************************************************
+ * mta_holder.c: Hold a MTA from another thread
+ *****************************************************************************
+ * Copyright (C) 2002-2017 the VideoLAN and AUTHORS
+ *
+ * Author: Hugo Beauzée-Luyssen <hugo at beauzee.fr>
+ *
+ * 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 MTA_HOLDER_H
+# define MTA_HOLDER_H
+
+#include <vlc_common.h>
+
+#include <assert.h>
+#include <windows.h>
+#include <objbase.h>
+
+typedef struct vlc_mta_holder
+{
+    vlc_thread_t thread;
+    int          i_refcount;
+    vlc_sem_t    ready_sem;
+    vlc_sem_t    release_sem;
+} vlc_mta_holder;
+
+static inline void* MtaMainLoop( void* opaque )
+{
+    vlc_mta_holder* p_mta = (vlc_mta_holder*)opaque;
+    CoInitializeEx( NULL, COINIT_MULTITHREADED );
+
+    vlc_sem_post( &p_mta->ready_sem );
+
+    vlc_sem_wait( &p_mta->release_sem );
+
+    CoUninitialize();
+    return NULL;
+}
+
+/**
+ * Ensure an MTA context will be available until vlc_mta_release gets called.
+ *
+ * In the background, this will create a thread that does nothing but to keep the MTA
+ * refcount greater than 0.
+ *
+ * This is usefull in order not to commit a thread to a specific concurrency model.
+ * This function is win32 specific.
+ */
+static inline bool vlc_mta_acquire( vlc_object_t *p_parent )
+{
+    vlc_global_lock( VLC_MTA_MUTEX );
+    vlc_mta_holder* p_mta = (vlc_mta_holder*)var_CreateGetAddress( p_parent->obj.libvlc, "mta-holder" );
+    if ( p_mta == NULL )
+    {
+        p_mta = (vlc_mta_holder*)malloc( sizeof( *p_mta ) );
+        if ( unlikely( p_mta == NULL ) )
+        {
+            vlc_global_unlock( VLC_MTA_MUTEX );
+            return false;
+        }
+        vlc_sem_init( &p_mta->ready_sem, 0 );
+        vlc_sem_init( &p_mta->release_sem, 0 );
+        p_mta->i_refcount = 1;
+        if ( vlc_clone( &p_mta->thread, MtaMainLoop, p_mta, VLC_THREAD_PRIORITY_LOW ) )
+        {
+            vlc_sem_destroy( &p_mta->release_sem );
+            vlc_sem_destroy( &p_mta->ready_sem );
+            free( p_mta );
+            p_mta = NULL;
+            vlc_global_unlock( VLC_MTA_MUTEX );
+            return false;
+        }
+        var_SetAddress( p_parent->obj.libvlc, "mta-holder", p_mta );
+        vlc_sem_wait( &p_mta->ready_sem );
+    }
+    else
+        ++p_mta->i_refcount;
+    vlc_global_unlock( VLC_MTA_MUTEX );
+    return true;
+}
+
+/**
+ * Releases a reference to the MTA holder.
+ *
+ * When its refcount reaches 0, the thread created by
+ */
+static inline void vlc_mta_release( vlc_object_t* p_parent )
+{
+    vlc_global_lock( VLC_MTA_MUTEX );
+    vlc_mta_holder *p_mta = (vlc_mta_holder*)var_InheritAddress( p_parent->obj.libvlc, "mta-holder" );
+    assert( p_mta != NULL );
+    int i_refcount = --p_mta->i_refcount;
+    if ( i_refcount == 0 )
+        var_SetAddress( p_parent->obj.libvlc, "mta-holder", NULL );
+    vlc_global_unlock( VLC_MTA_MUTEX );
+    if ( i_refcount == 0 )
+    {
+        vlc_sem_post( &p_mta->release_sem );
+
+        vlc_join( p_mta->thread, NULL );
+
+        vlc_sem_destroy( &p_mta->release_sem );
+        vlc_sem_destroy( &p_mta->ready_sem );
+        free( p_mta );
+    }
+}
+
+#endif



More information about the vlc-commits mailing list