<html><head></head><body><div class="gmail_quote">Le 1 juillet 2017 11:22:25 GMT+02:00, "Hugo Beauzée-Luyssen" <hugo@beauzee.fr> a écrit :<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<pre class="k9mail">This API can be used to store an instance in the core, removing the use<br />for some special shared libraries/shared variables without refcounting<br />---<br /> include/vlc_instance_holder.h | 74 +++++++++++++++++<br /> include/vlc_threads.h | 1 +<br /> src/<a href="http://Makefile.am">Makefile.am</a> | 4 +-<br /> src/libvlc.c | 4 +<br /> src/libvlc.h | 2 +<br /> src/libvlccore.sym | 3 +<br /> src/misc/instance_holder.c | 181 ++++++++++++++++++++++++++++++++++++++++++<br /> src/misc/threads.c | 1 +<br /> 8 files changed, 269 insertions(+), 1 deletion(-)<br /> create mode 100644 include/vlc_instance_holder.h<br /> create mode 100644 src/misc/instance_holder.c<br /><br />diff --git a/include/vlc_instance_holder.h b/include/vlc_instance_holder.h<br />new file mode 100644<br />index 0000000000..72f21e1e95<br />--- /dev/null<br />+++ b/include/vlc_instance_holder.h<br />@@ -0,0 +1,74 @@<br />+/*****************************************************************************<br />+ * vlc_instance_holder.h: Provide functions to hold a unique refcounted opaque<br />+ * instance.<br />+ *****************************************************************************<br />+ * Copyright (C) 2004-2016 VLC authors, VideoLAN and VideoLabs<br />+ *<br />+ * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr><br />+ *<br />+ * This program is free software; you can redistribute it and/or modify it<br />+ * under the terms of the GNU Lesser General Public License as published by<br />+ * the Free Software Foundation; either version 2.1 of the License, or<br />+ * (at your option) any later version.<br />+ *<br />+ * This program is distributed in the hope that it will be useful,<br />+ * but WITHOUT ANY WARRANTY; without even the implied warranty of<br />+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br />+ * GNU Lesser General Public License for more details.<br />+ *<br />+ * You should have received a copy of the GNU Lesser General Public License<br />+ * along with this program; if not, write to the Free Software Foundation,<br />+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.<br />+ *****************************************************************************/<br />+<br />+#ifndef VLC_INSTANCE_HOLDER_H<br />+#define VLC_INSTANCE_HOLDER_H<br />+<br />+#include <vlc_common.h><br />+<br />+/**<br />+ * Will acquire the instance with the given name.<br />+ * \param p_obj A vlc_object_t instance<br />+ * \param psz_name The instance name<br />+ * \param pf_constructor Will be invoked to create the instance when the provided<br />+ * name doesn't refer to an existing instance. If the<br />+ * constructor function pointer is NULL, the instance won't<br />+ * be created.<br />+ * \param pf_destructor Will be invoked to destroy the instance when its<br />+ * refount reaches 0. If the opaque context was allocated<br />+ * the destructor is expected to release it as well. This<br />+ * can be NULL.<br />+ * \param p_opaque An opaque context that will be owned by the instance<br />+ * holder. It is expected to be release by pf_destructor.<br />+ * This can be NULL.<br />+ */<br />+VLC_API void* vlc_instance_holder_acquire( vlc_object_t* p_obj,<br />+ const char* psz_name, void* (*pf_constructor)( void* ),<br />+ void (*pf_destructor)( void*, void* ), void* p_opaque );<br />+<br />+/**<br />+ * Return an existing instance, or NULL.<br />+ *<br />+ * This is equivalent to calling vlc_instance_holder_acquire with a<br />+ * NULL constructor, a NULL destructor, and a NULL context.<br />+ * This is guaranteed not to create a new instance.<br />+ * If an instance is returned, its refcount will be incremented.<br />+ * \param p_obj A vlc_object_t<br />+ * \param psz_name The instance name<br />+ */<br />+VLC_API void* vlc_instance_holder_get( vlc_object_t* p_obj,<br />+ const char* psz_name );<br />+<br />+/**<br />+ * Decrease the instance refcount.<br />+ *<br />+ * When the refcount reaches 0, the destructor provided upon first creation will<br />+ * be invoked.<br />+ */<br />+VLC_API void vlc_instance_holder_release( vlc_object_t* p_obj,<br />+ const char* psz_name );<br />+<br />+int libvlc_InstanceHolderCreate( libvlc_int_t* p_libvlc );<br />+void libvlc_InstanceHolderRelease( libvlc_int_t* p_libvlc );<br />+<br />+#endif // VLC_INSTANCE_HOLDER_H<br />diff --git a/include/vlc_threads.h b/include/vlc_threads.h<br />index 960d458bb2..55b5753234 100644<br />--- a/include/vlc_threads.h<br />+++ b/include/vlc_threads.h<br />@@ -1053,6 +1053,7 @@ enum<br /> VLC_XLIB_MUTEX,<br /> VLC_MOSAIC_MUTEX,<br /> VLC_HIGHLIGHT_MUTEX,<br />+ VLC_INSTANCE_HOLDER_MUTEX,<br /> #ifdef _WIN32<br /> VLC_MTA_MUTEX,<br /> #endif<br />diff --git a/src/<a href="http://Makefile.am">Makefile.am</a> b/src/<a href="http://Makefile.am">Makefile.am</a><br />index c93a4021ee..24970b0bec 100644<br />--- a/src/<a href="http://Makefile.am">Makefile.am</a><br />+++ b/src/<a href="http://Makefile.am">Makefile.am</a><br />@@ -57,6 +57,7 @@ pluginsinclude_HEADERS = \<br /> ../include/vlc_inhibit.h \<br /> ../include/vlc_input.h \<br /> ../include/vlc_input_item.h \<br />+ ../include/vlc_instance_holder.h \<br /> ../include/vlc_interface.h \<br /> ../include/vlc_keys.h \<br /> ../include/vlc_keystore.h \<br />@@ -348,7 +349,8 @@ libvlccore_la_SOURCES = \<br /> misc/fingerprinter.c \<br /> misc/text_style.c \<br /> misc/subpicture.c \<br />- misc/subpicture.h<br />+ misc/subpicture.h \<br />+ misc/instance_holder.c<br /> libvlccore_la_LIBADD = $(LIBS_libvlccore) \<br /> ../compat/<a href="http://libcompat.la">libcompat.la</a> \<br /> $(LTLIBINTL) $(LTLIBICONV) \<br />diff --git a/src/libvlc.c b/src/libvlc.c<br />index c7e7b9013d..46e97cfd04 100644<br />--- a/src/libvlc.c<br />+++ b/src/libvlc.c<br />@@ -61,6 +61,7 @@<br /> #include <vlc_cpu.h><br /> #include <vlc_url.h><br /> #include <vlc_modules.h><br />+#include <vlc_instance_holder.h><br /> <br /> #include "libvlc.h"<br /> #include "playlist/playlist_internal.h"<br />@@ -225,6 +226,8 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,<br /> goto error;<br /> if( libvlc_InternalKeystoreInit( p_libvlc ) != VLC_SUCCESS )<br /> msg_Warn( p_libvlc, "memory keystore init failed" );<br />+ if ( libvlc_InstanceHolderCreate( p_libvlc ) != VLC_SUCCESS )<br />+ goto error;<br /> <br /> vlc_CPU_dump( VLC_OBJECT(p_libvlc) );<br /> <br />@@ -390,6 +393,7 @@ void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )<br /> msg_Dbg( p_libvlc, "removing all interfaces" );<br /> intf_DestroyAll( p_libvlc );<br /> <br />+ libvlc_InstanceHolderRelease( p_libvlc );<br /> libvlc_InternalDialogClean( p_libvlc );<br /> libvlc_InternalKeystoreClean( p_libvlc );<br /> <br />diff --git a/src/libvlc.h b/src/libvlc.h<br />index 299795c549..4866e0da5f 100644<br />--- a/src/libvlc.h<br />+++ b/src/libvlc.h<br />@@ -177,6 +177,7 @@ void vlc_objres_remove(vlc_object_t *obj, void *data,<br /> */<br /> typedef struct vlc_dialog_provider vlc_dialog_provider;<br /> typedef struct vlc_keystore vlc_keystore;<br />+typedef struct vlc_instance_holder vlc_instance_holder;<br /> <br /> typedef struct libvlc_priv_t<br /> {<br />@@ -190,6 +191,7 @@ typedef struct libvlc_priv_t<br /> vlm_t *p_vlm; ///< the VLM singleton (or NULL)<br /> vlc_dialog_provider *p_dialog_provider; ///< dialog provider<br /> vlc_keystore *p_memory_keystore; ///< memory keystore<br />+ vlc_instance_holder *p_instance_holder; ///< Store global opaque instances<br /> struct playlist_t *playlist; ///< Playlist for interfaces<br /> struct playlist_preparser_t *parser; ///< Input item meta data handler<br /> struct vlc_actions *actions; ///< Hotkeys handler<br />diff --git a/src/libvlccore.sym b/src/libvlccore.sym<br />index 58a54bb0f0..2d809d0b39 100644<br />--- a/src/libvlccore.sym<br />+++ b/src/libvlccore.sym<br />@@ -755,3 +755,6 @@ vlc_rd_get_names<br /> vlc_rd_new<br /> vlc_rd_release<br /> vlc_rd_probe_add<br />+vlc_instance_holder_acquire<br />+vlc_instance_holder_release<br />+vlc_instance_holder_get<br />diff --git a/src/misc/instance_holder.c b/src/misc/instance_holder.c<br />new file mode 100644<br />index 0000000000..c236366fcc<br />--- /dev/null<br />+++ b/src/misc/instance_holder.c<br />@@ -0,0 +1,181 @@<br />+/*****************************************************************************<br />+ * instance_holder.c: Holds a unique refcounted opaque instance.<br />+ *****************************************************************************<br />+ * Copyright (C) 2004-2016 VLC authors, VideoLAN and VideoLabs<br />+ *<br />+ * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr><br />+ *<br />+ * This program is free software; you can redistribute it and/or modify it<br />+ * under the terms of the GNU Lesser General Public License as published by<br />+ * the Free Software Foundation; either version 2.1 of the License, or<br />+ * (at your option) any later version.<br />+ *<br />+ * This program is distributed in the hope that it will be useful,<br />+ * but WITHOUT ANY WARRANTY; without even the implied warranty of<br />+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br />+ * GNU Lesser General Public License for more details.<br />+ *<br />+ * You should have received a copy of the GNU Lesser General Public License<br />+ * along with this program; if not, write to the Free Software Foundation,<br />+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.<br />+ *****************************************************************************/<br />+<br />+#ifdef HAVE_CONFIG_H<br />+# include "config.h"<br />+#endif<br />+<br />+#include <vlc_instance_holder.h><br />+#include <vlc_threads.h><br />+#include "libvlc.h"<br />+<br />+#include <assert.h><br />+#ifdef HAVE_SEARCH_H<br />+# include <search.h><br />+#endif<br />+<br />+typedef struct instance<br />+{<br />+ char* psz_name;<br />+ void* p_instance;<br />+ void* p_opaque;<br />+ unsigned int i_refcount;<br />+ void (*pf_destructor)( void*, void* );<br />+ vlc_mutex_t lock;<br />+} instance;<br />+<br />+typedef struct vlc_instance_holder<br />+{<br />+ void* p_root;<br />+} vlc_instance_holder;<br />+<br />+static vlc_instance_holder* instance_holder_create( void )<br />+{<br />+ vlc_instance_holder* p_holder = malloc( sizeof( *p_holder ) );<br />+ if( unlikely( p_holder == NULL ) )<br />+ return NULL;<br />+ p_holder->p_root = NULL;<br />+ return p_holder;<br />+}<br />+<br />+int libvlc_InstanceHolderCreate( libvlc_int_t* p_libvlc )<br />+{<br />+ libvlc_priv_t* p_priv = libvlc_priv( p_libvlc );<br />+ p_priv->p_instance_holder = instance_holder_create();<br />+ if( unlikely( p_priv->p_instance_holder == NULL ) )<br />+ return VLC_ENOMEM;<br />+ return VLC_SUCCESS;<br />+}<br />+<br />+void libvlc_InstanceHolderRelease( libvlc_int_t* p_libvlc )<br />+{<br />+ libvlc_priv_t* p_priv = libvlc_priv( p_libvlc );<br />+ // We expect all instances to have been released when the instance holder<br />+ // gets released.<br />+ assert( p_priv->p_instance_holder->p_root == NULL );<br />+ free( p_priv->p_instance_holder );<br />+ p_priv->p_instance_holder = NULL;<br />+}<br />+<br />+static int instance_key_compare( const void* l, const void* r )<br />+{<br />+ const instance* p_left = l, *p_right = r;<br />+ return strcmp( p_left->psz_name, p_right->psz_name );<br />+}<br />+<br />+void* vlc_instance_holder_acquire( vlc_object_t* p_obj,<br />+ const char* psz_name, void*(*pf_constructor)( void* ),<br />+ void(*pf_destructor)( void*, void* ), void* p_opaque )<br />+{<br />+ vlc_instance_holder* p_holder = libvlc_priv( p_obj->obj.libvlc )->p_instance_holder;<br />+ assert( p_holder != NULL );<br />+ instance* p_instance = malloc( sizeof( *p_instance ) );<br />+ if( unlikely( p_instance == NULL ) )<br />+ return NULL;<br />+ /* Simply copy the pointer to the string until we know if the variable<br />+ * was created or not: */<br />+ p_instance->psz_name = (char*)psz_name;<br />+ vlc_global_lock( VLC_INSTANCE_HOLDER_MUTEX );<br />+ void** pp_key = tsearch( p_instance, &p_holder->p_root,<br />+ &instance_key_compare );<br />+ /* If the instance just got created, fully initialize it */<br />+ if( pp_key != NULL && *pp_key == p_instance )<br />+ {<br />+ p_instance->psz_name = strdup( psz_name );<br />+ if( p_instance->psz_name != NULL )<br />+ {<br />+ if ( pf_constructor == NULL )<br />+ {<br />+ tdelete( p_instance, &p_holder->p_root, &instance_key_compare );<br />+ free( p_instance );<br />+ vlc_global_unlock( VLC_INSTANCE_HOLDER_MUTEX );<br />+ return NULL;<br />+ }<br />+ p_instance->p_instance = pf_constructor( p_opaque );<br />+ }<br />+ if( p_instance->psz_name == NULL || p_instance->p_instance == NULL )<br />+ {<br />+ tdelete( p_instance, &p_holder->p_root, &instance_key_compare );<br />+ free( p_instance );<br />+ vlc_global_unlock( VLC_INSTANCE_HOLDER_MUTEX );<br />+ return NULL;<br />+ }<br />+ vlc_mutex_init( &p_instance->lock );<br />+ p_instance->i_refcount = 1;<br />+ p_instance->pf_destructor = pf_destructor;<br />+ p_instance->p_opaque = p_opaque;<br />+ }<br />+ vlc_global_unlock( VLC_INSTANCE_HOLDER_MUTEX );<br />+<br />+ // Failed to create a new instance:<br />+ if( unlikely( pp_key == NULL ) )<br />+ return NULL;<br />+ // Got an already existing instance back:<br />+ if( *pp_key == p_instance )<br />+ return p_instance->p_instance;<br />+ // The instance was created:<br />+ free( p_instance );<br />+ instance* p_existing_instance = (instance*)*pp_key;<br />+ vlc_mutex_lock( &p_existing_instance->lock );<br />+ p_existing_instance->i_refcount++;<br />+ vlc_mutex_unlock( &p_existing_instance->lock );<br />+ return p_existing_instance->p_instance;<br />+}<br />+<br />+void* vlc_instance_holder_get( vlc_object_t* p_obj,<br />+ const char* psz_name )<br />+{<br />+ return vlc_instance_holder_acquire( p_obj, psz_name, NULL,<br />+ NULL, NULL );<br />+}<br />+<br />+void vlc_instance_holder_release( vlc_object_t* p_obj,<br />+ const char *psz_name )<br />+{<br />+ vlc_instance_holder* p_holder = libvlc_priv( p_obj->obj.libvlc )->p_instance_holder;<br />+ assert( p_holder != NULL );<br />+ instance inst = { .psz_name = (char*)psz_name };<br />+ vlc_global_lock( VLC_INSTANCE_HOLDER_MUTEX );<br />+ instance** pp_inst = tfind( &inst, &p_holder->p_root, &instance_key_compare );<br />+ instance* p_inst = NULL;<br />+ bool b_delete = false;<br />+ if( pp_inst != NULL )<br />+ {<br />+ p_inst = *pp_inst;<br />+ vlc_mutex_lock( &p_inst->lock );<br />+ if( --p_inst->i_refcount == 0 )<br />+ {<br />+ tdelete( p_inst, &p_holder->p_root, &instance_key_compare );<br />+ b_delete = true;<br />+ }<br />+ vlc_mutex_unlock( &p_inst->lock );<br />+ }<br />+ vlc_global_unlock( VLC_INSTANCE_HOLDER_MUTEX );<br />+ if( b_delete == true )<br />+ {<br />+ if( p_inst->pf_destructor != NULL )<br />+ p_inst->pf_destructor( p_inst->p_instance, p_inst->p_opaque );<br />+ free( p_inst->psz_name );<br />+ vlc_mutex_destroy( &p_inst->lock );<br />+ free( p_inst );<br />+ }<br />+}<br />diff --git a/src/misc/threads.c b/src/misc/threads.c<br />index 3d099db6cb..f0d709e300 100644<br />--- a/src/misc/threads.c<br />+++ b/src/misc/threads.c<br />@@ -37,6 +37,7 @@ void vlc_global_mutex (unsigned n, bool acquire)<br /> VLC_STATIC_MUTEX,<br /> VLC_STATIC_MUTEX,<br /> VLC_STATIC_MUTEX,<br />+ VLC_STATIC_MUTEX,<br /> #ifdef _WIN32<br /> VLC_STATIC_MUTEX, // For MTA holder<br /> #endif</pre></blockquote></div><br clear="all">In general, singletons should be avoided. And when they are used, they should be static to whatever source module needs them and wrapped with function calls. VDPAU already solves this just as well without leaking anything to the core, for instance. Qt also has quite a few singletons that wouldn't benefit from this.<br>
<br>
So I can't really see the point.<br>
<br>
Also this will deadlock if a singleton depends on another one.<br>
<br>
Furthermore using a name is really pointlessly slow and intricate as opposed to a global or static variable.<br>
<br>
And depending on VLC object cannot be right, since there may be multiple independent vlc instances in a process.<br>
<br>
-- <br>
Rémi Denis-Courmont<br>
Typed on an inconvenient virtual keyboard</body></html>