giomodule: Don’t mandatorily cache GIOModule implementations

While all of the current callers of _g_io_module_get_default() want to
cache the returned GObject for the lifetime of the process, that doesn’t
necessarily have to be the case, so let callers make that decision on a
case-by-case basis.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
This commit is contained in:
Philip Withnall 2019-01-23 16:07:51 +00:00 committed by Philip Withnall
parent b207965697
commit 22b924b64a
7 changed files with 121 additions and 31 deletions

View File

@ -884,6 +884,13 @@ try_implementation (const char *extension_point,
} }
} }
static void
weak_ref_free (GWeakRef *weak_ref)
{
g_weak_ref_clear (weak_ref);
g_free (weak_ref);
}
/** /**
* _g_io_module_get_default: * _g_io_module_get_default:
* @extension_point: the name of an extension point * @extension_point: the name of an extension point
@ -905,10 +912,11 @@ try_implementation (const char *extension_point,
* be called on each candidate implementation after construction, to * be called on each candidate implementation after construction, to
* check if it is actually usable or not. * check if it is actually usable or not.
* *
* The result is cached after it is generated the first time, and * The result is cached after it is generated the first time (but the cache does
* not keep a strong reference to the object), and
* the function is thread-safe. * the function is thread-safe.
* *
* Returns: (transfer none): an object implementing * Returns: (transfer full) (nullable): an object implementing
* @extension_point, or %NULL if there are no usable * @extension_point, or %NULL if there are no usable
* implementations. * implementations.
*/ */
@ -923,25 +931,33 @@ _g_io_module_get_default (const gchar *extension_point,
GList *l; GList *l;
GIOExtensionPoint *ep; GIOExtensionPoint *ep;
GIOExtension *extension = NULL, *preferred; GIOExtension *extension = NULL, *preferred;
gpointer impl; gpointer impl, value;
GWeakRef *impl_weak_ref = NULL;
g_rec_mutex_lock (&default_modules_lock); g_rec_mutex_lock (&default_modules_lock);
if (default_modules) if (default_modules)
{ {
gpointer key;
if (g_hash_table_lookup_extended (default_modules, extension_point, if (g_hash_table_lookup_extended (default_modules, extension_point,
&key, &impl)) NULL, &value))
{ {
/* Dont debug here, since were returning a cached object which was /* Dont debug here, since were returning a cached object which was
* already printed earlier. */ * already printed earlier. */
impl_weak_ref = value;
impl = g_weak_ref_get (impl_weak_ref);
/* If the object has been finalised (impl == NULL), fall through and
* instantiate a new one. */
if (impl != NULL)
{
g_rec_mutex_unlock (&default_modules_lock); g_rec_mutex_unlock (&default_modules_lock);
return impl; return g_steal_pointer (&impl);
}
} }
} }
else else
{ {
default_modules = g_hash_table_new (g_str_hash, g_str_equal); default_modules = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, (GDestroyNotify) weak_ref_free);
} }
_g_io_modules_ensure_loaded (); _g_io_modules_ensure_loaded ();
@ -993,9 +1009,18 @@ _g_io_module_get_default (const gchar *extension_point,
impl = NULL; impl = NULL;
done: done:
g_hash_table_insert (default_modules, if (impl_weak_ref == NULL)
g_strdup (extension_point), {
impl ? g_object_ref (impl) : NULL); impl_weak_ref = g_new0 (GWeakRef, 1);
g_weak_ref_init (impl_weak_ref, impl);
g_hash_table_insert (default_modules, g_strdup (extension_point),
g_steal_pointer (&impl_weak_ref));
}
else
{
g_weak_ref_set (impl_weak_ref, impl);
}
g_rec_mutex_unlock (&default_modules_lock); g_rec_mutex_unlock (&default_modules_lock);
if (impl != NULL) if (impl != NULL)
@ -1009,7 +1034,7 @@ _g_io_module_get_default (const gchar *extension_point,
g_debug ("%s: Failed to find default implementation for %s", g_debug ("%s: Failed to find default implementation for %s",
G_STRFUNC, extension_point); G_STRFUNC, extension_point);
return impl; return g_steal_pointer (&impl);
} }
G_LOCK_DEFINE_STATIC (registered_extensions); G_LOCK_DEFINE_STATIC (registered_extensions);

View File

@ -77,6 +77,7 @@ enum {
}; };
static guint signals[LAST_SIGNAL] = { 0 }; static guint signals[LAST_SIGNAL] = { 0 };
static GNetworkMonitor *network_monitor_default_singleton = NULL; /* (owned) (atomic) */
/** /**
* g_network_monitor_get_default: * g_network_monitor_get_default:
@ -91,9 +92,18 @@ static guint signals[LAST_SIGNAL] = { 0 };
GNetworkMonitor * GNetworkMonitor *
g_network_monitor_get_default (void) g_network_monitor_get_default (void)
{ {
return _g_io_module_get_default (G_NETWORK_MONITOR_EXTENSION_POINT_NAME, if (g_once_init_enter (&network_monitor_default_singleton))
{
GNetworkMonitor *singleton;
singleton = _g_io_module_get_default (G_NETWORK_MONITOR_EXTENSION_POINT_NAME,
"GIO_USE_NETWORK_MONITOR", "GIO_USE_NETWORK_MONITOR",
NULL); NULL);
g_once_init_leave (&network_monitor_default_singleton, singleton);
}
return network_monitor_default_singleton;
} }
/** /**

View File

@ -67,6 +67,8 @@ g_proxy_resolver_default_init (GProxyResolverInterface *iface)
{ {
} }
static GProxyResolver *proxy_resolver_default_singleton = NULL; /* (owned) (atomic) */
/** /**
* g_proxy_resolver_get_default: * g_proxy_resolver_get_default:
* *
@ -80,9 +82,18 @@ g_proxy_resolver_default_init (GProxyResolverInterface *iface)
GProxyResolver * GProxyResolver *
g_proxy_resolver_get_default (void) g_proxy_resolver_get_default (void)
{ {
return _g_io_module_get_default (G_PROXY_RESOLVER_EXTENSION_POINT_NAME, if (g_once_init_enter (&proxy_resolver_default_singleton))
{
GProxyResolver *singleton;
singleton = _g_io_module_get_default (G_PROXY_RESOLVER_EXTENSION_POINT_NAME,
"GIO_USE_PROXY_RESOLVER", "GIO_USE_PROXY_RESOLVER",
(GIOModuleVerifyFunc)g_proxy_resolver_is_supported); (GIOModuleVerifyFunc) g_proxy_resolver_is_supported);
g_once_init_leave (&proxy_resolver_default_singleton, singleton);
}
return proxy_resolver_default_singleton;
} }
/** /**

View File

@ -992,6 +992,12 @@ g_settings_backend_verify (gpointer impl)
return TRUE; return TRUE;
} }
/* We need to cache the default #GSettingsBackend for the entire process
* lifetime, especially if the backend is #GMemorySettingsBackend: it needs to
* keep the in-memory settings around even while there are no #GSettings
* instances alive. */
static GSettingsBackend *settings_backend_default_singleton = NULL; /* (owned) (atomic) */
/** /**
* g_settings_backend_get_default: * g_settings_backend_get_default:
* *
@ -1010,12 +1016,18 @@ g_settings_backend_verify (gpointer impl)
GSettingsBackend * GSettingsBackend *
g_settings_backend_get_default (void) g_settings_backend_get_default (void)
{ {
GSettingsBackend *backend; if (g_once_init_enter (&settings_backend_default_singleton))
{
GSettingsBackend *singleton;
backend = _g_io_module_get_default (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME, singleton = _g_io_module_get_default (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
"GSETTINGS_BACKEND", "GSETTINGS_BACKEND",
g_settings_backend_verify); g_settings_backend_verify);
return g_object_ref (backend);
g_once_init_leave (&settings_backend_default_singleton, singleton);
}
return g_object_ref (settings_backend_default_singleton);
} }
/*< private > /*< private >

View File

@ -93,6 +93,8 @@ g_tls_backend_default_init (GTlsBackendInterface *iface)
{ {
} }
static GTlsBackend *tls_backend_default_singleton = NULL; /* (owned) (atomic) */
/** /**
* g_tls_backend_get_default: * g_tls_backend_get_default:
* *
@ -106,8 +108,18 @@ g_tls_backend_default_init (GTlsBackendInterface *iface)
GTlsBackend * GTlsBackend *
g_tls_backend_get_default (void) g_tls_backend_get_default (void)
{ {
return _g_io_module_get_default (G_TLS_BACKEND_EXTENSION_POINT_NAME, if (g_once_init_enter (&tls_backend_default_singleton))
"GIO_USE_TLS", NULL); {
GTlsBackend *singleton;
singleton = _g_io_module_get_default (G_TLS_BACKEND_EXTENSION_POINT_NAME,
"GIO_USE_TLS",
NULL);
g_once_init_leave (&tls_backend_default_singleton, singleton);
}
return tls_backend_default_singleton;
} }
/** /**

View File

@ -337,6 +337,8 @@ g_vfs_parse_name (GVfs *vfs,
return (* class->parse_name) (vfs, parse_name); return (* class->parse_name) (vfs, parse_name);
} }
static GVfs *vfs_default_singleton = NULL; /* (owned) (atomic) */
/** /**
* g_vfs_get_default: * g_vfs_get_default:
* *
@ -350,9 +352,19 @@ g_vfs_get_default (void)
{ {
if (GLIB_PRIVATE_CALL (g_check_setuid) ()) if (GLIB_PRIVATE_CALL (g_check_setuid) ())
return g_vfs_get_local (); return g_vfs_get_local ();
return _g_io_module_get_default (G_VFS_EXTENSION_POINT_NAME,
if (g_once_init_enter (&vfs_default_singleton))
{
GVfs *singleton;
singleton = _g_io_module_get_default (G_VFS_EXTENSION_POINT_NAME,
"GIO_USE_VFS", "GIO_USE_VFS",
(GIOModuleVerifyFunc)g_vfs_is_active); (GIOModuleVerifyFunc) g_vfs_is_active);
g_once_init_leave (&vfs_default_singleton, singleton);
}
return vfs_default_singleton;
} }
/** /**

View File

@ -421,6 +421,14 @@
fun:_g_io_module_get_default* fun:_g_io_module_get_default*
} }
{
g-io-module-default-singleton-weak-ref
Memcheck:Leak
fun:calloc
...
fun:_g_io_module_get_default
}
{ {
g-get-language-names-malloc g-get-language-names-malloc
Memcheck:Leak Memcheck:Leak