From 2c0f592ff411d52b7fb09af08b720599f4bf2074 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Mon, 29 Aug 2016 11:44:45 +0200 Subject: [PATCH] remove fd manager Take ref in _add_object --- pinos/Makefile.am | 1 - pinos/client/fdmanager.c | 367 --------------------------------------- pinos/client/fdmanager.h | 79 --------- pinos/client/pinos.h | 1 - pinos/server/client.c | 7 +- pinos/server/daemon.c | 6 +- 6 files changed, 6 insertions(+), 455 deletions(-) delete mode 100644 pinos/client/fdmanager.c delete mode 100644 pinos/client/fdmanager.h diff --git a/pinos/Makefile.am b/pinos/Makefile.am index 09196ce0d..9727f925d 100644 --- a/pinos/Makefile.am +++ b/pinos/Makefile.am @@ -185,7 +185,6 @@ libpinos_@PINOS_MAJORMINOR@_la_SOURCES = \ client/properties.h client/properties.c \ client/stream.h client/stream.c \ client/pinos.c client/pinos.h \ - client/fdmanager.c client/fdmanager.h \ client/ringbuffer.c client/ringbuffer.h \ client/subscribe.c client/subscribe.h diff --git a/pinos/client/fdmanager.c b/pinos/client/fdmanager.c deleted file mode 100644 index b7d67afa7..000000000 --- a/pinos/client/fdmanager.c +++ /dev/null @@ -1,367 +0,0 @@ -/* GStreamer - * Copyright (C) 2016 Wim Taymans - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, - * Boston, MA 02110-1335, USA. - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "fdmanager.h" - -struct _PinosFdManagerPrivate -{ - GMutex lock; - GHashTable *object_ids; - GHashTable *client_ids; - volatile gint id_counter; -}; - -#define PINOS_FD_MANAGER_GET_PRIVATE(obj) \ - (G_TYPE_INSTANCE_GET_PRIVATE ((obj), PINOS_TYPE_FD_MANAGER, PinosFdManagerPrivate)) - -G_DEFINE_TYPE (PinosFdManager, pinos_fd_manager, G_TYPE_OBJECT); - -static GMutex manager_lock; -static GHashTable *managers; - -typedef struct { - guint32 id; - gint refcount; - gpointer obj; - GDestroyNotify notify; -} ObjectId; - -static ObjectId * -object_id_new (guint32 id, gpointer obj, GDestroyNotify notify) -{ - ObjectId *oid; - - oid = g_slice_new (ObjectId); - oid->id = id; - oid->refcount = 1; - oid->obj = obj; - oid->notify = notify; - - return oid; -} - -static void -object_id_free (ObjectId *oid) -{ - g_assert (oid->refcount == 0); - g_slice_free (ObjectId, oid); -} - -typedef struct { - GList *ids; -} ClientIds; - -static ClientIds * -client_ids_new (ObjectId *oid) -{ - ClientIds *ids; - - ids = g_slice_new (ClientIds); - ids->ids = g_list_prepend (NULL, oid); - - return ids; -} - -static void -client_ids_free (ClientIds *ids) -{ - g_list_free (ids->ids); - g_slice_free (ClientIds, ids); -} - -/** - * pinos_fd_manager_get: - * @type: the manager type - * - * Get a manager of @type. There will be a single instance of a #PinosFdManager - * per @type. - * - * Returns: a new reference to the #PinosFdManager for @type. - */ -PinosFdManager * -pinos_fd_manager_get (const gchar *type) -{ - PinosFdManager *manager; - - g_return_val_if_fail (type != NULL, NULL); - - g_type_class_ref (PINOS_TYPE_FD_MANAGER); - - g_mutex_lock (&manager_lock); - manager = g_hash_table_lookup (managers, type); - if (manager == NULL) { - manager = g_object_new (PINOS_TYPE_FD_MANAGER, NULL); - g_hash_table_insert (managers, g_strdup (type), manager); - } - g_mutex_unlock (&manager_lock); - - return g_object_ref (manager); -} - -/** - * pinos_fd_manager_get_id: - * @manager: a #PinosFdManager - * - * Get the next available id from @manager - * - * Returns: the next unused id. - */ -guint32 -pinos_fd_manager_get_id (PinosFdManager *manager) -{ - PinosFdManagerPrivate *priv; - - g_return_val_if_fail (PINOS_IS_FD_MANAGER (manager), -1); - - priv = manager->priv; - - return (guint32) g_atomic_int_add (&priv->id_counter, 1); -} - -/** - * pinos_fd_manager_add: - * @manager: a #PinosFdManager - * @client: a client id - * @id: an id - * @object: a pointer to an object to manage - * @notify: callback to free @object - * - * Associate @object with @id for @client. @object will be kept alive until a - * pinos_fd_manager_remove() or pinos_fd_manager_remove_client() with - * the same id and client is made. - * - * Returns: %TRUE on success. - */ -gboolean -pinos_fd_manager_add (PinosFdManager *manager, - const gchar *client, guint32 id, - gpointer object, GDestroyNotify notify) -{ - PinosFdManagerPrivate *priv; - ObjectId *oid; - ClientIds *cids; - - g_return_val_if_fail (PINOS_IS_FD_MANAGER (manager), FALSE); - g_return_val_if_fail (client != NULL, FALSE); - g_return_val_if_fail (object != NULL, FALSE); - - priv = manager->priv; - - g_mutex_lock (&priv->lock); - /* get the id */ - oid = g_hash_table_lookup (priv->object_ids, GINT_TO_POINTER (id)); - if (oid == NULL) { - /* first time, create and add */ - oid = object_id_new (id, object, notify); - g_hash_table_insert (priv->object_ids, GINT_TO_POINTER (id), oid); - } else { - /* existed, check if the same object and notify*/ - if (oid->obj != object || oid->notify != notify) - goto wrong_object; - /* increment refcount */ - oid->refcount++; - } - /* find the client and add the id */ - cids = g_hash_table_lookup (priv->client_ids, client); - if (cids == NULL) { - cids = client_ids_new (oid); - g_hash_table_insert (priv->client_ids, g_strdup (client), cids); - } else { - /* add the object to the client */ - cids->ids = g_list_prepend (cids->ids, oid); - } - g_mutex_unlock (&priv->lock); - - return TRUE; - - /* ERRORS */ -wrong_object: - { - g_warning ("wrong object"); - g_mutex_unlock (&priv->lock); - return FALSE; - } -} -/** - * pinos_fd_manager_find: - * @manager: a #PinosFdManager - * @client: a client id - * @id: an id - * - * find the object associated with the id and client from @manager. - * - * Returns: the object or %NULL - */ -gpointer -pinos_fd_manager_find (PinosFdManager *manager, - const gchar *client, guint32 id) -{ - PinosFdManagerPrivate *priv; - ObjectId *oid; - ClientIds *cids; - - g_return_val_if_fail (PINOS_IS_FD_MANAGER (manager), FALSE); - g_return_val_if_fail (client != NULL, FALSE); - - priv = manager->priv; - - g_mutex_lock (&priv->lock); - oid = g_hash_table_lookup (priv->object_ids, GINT_TO_POINTER (id)); - if (oid) { - cids = g_hash_table_lookup (priv->client_ids, client); - if (cids) { - GList *find = g_list_find (cids->ids, oid); - if (find) - return oid->obj; - } - } - g_mutex_unlock (&priv->lock); - - return NULL; -} - -/** - * pinos_fd_manager_remove: - * @manager: a #PinosFdManager - * @client: a client id - * @id: an id - * - * Remove the id associated with client from @manager. - * - * Returns: %TRUE on success. - */ -gboolean -pinos_fd_manager_remove (PinosFdManager *manager, - const gchar *client, guint32 id) -{ - PinosFdManagerPrivate *priv; - ObjectId *oid; - ClientIds *cids; - - g_return_val_if_fail (PINOS_IS_FD_MANAGER (manager), FALSE); - g_return_val_if_fail (client != NULL, FALSE); - - priv = manager->priv; - - g_mutex_lock (&priv->lock); - oid = g_hash_table_lookup (priv->object_ids, GINT_TO_POINTER (id)); - if (oid) { - cids = g_hash_table_lookup (priv->client_ids, client); - if (cids) { - GList *find = g_list_find (cids->ids, oid); - - if (find) { - cids->ids = g_list_delete_link (cids->ids, find); - - oid->notify (oid->obj); - oid->refcount--; - if (oid->refcount == 0) { - g_hash_table_remove (priv->object_ids, GINT_TO_POINTER (id)); - } - } - } - } - g_mutex_unlock (&priv->lock); - - return TRUE; -} - -static void -remove_id (ObjectId *oid, PinosFdManager *manager) -{ - PinosFdManagerPrivate *priv = manager->priv; - - oid->notify (oid->obj); - oid->refcount--; - if (oid->refcount == 0) { - g_hash_table_remove (priv->object_ids, GINT_TO_POINTER (oid->id)); - } -} - -/** - * pinos_fd_manager_remove_all: - * @manager: a #PinosFdManager - * @client: a client id - * - * Remove all ids from @manager associated with @client. - * - * Returns: %TRUE on success. - */ -gboolean -pinos_fd_manager_remove_all (PinosFdManager *manager, - const gchar *client) -{ - PinosFdManagerPrivate *priv; - ClientIds *cids; - - g_return_val_if_fail (PINOS_IS_FD_MANAGER (manager), FALSE); - g_return_val_if_fail (client != NULL, FALSE); - - priv = manager->priv; - - g_mutex_lock (&priv->lock); - cids = g_hash_table_lookup (priv->client_ids, client); - if (cids) { - g_list_foreach (cids->ids, (GFunc) remove_id, manager); - g_hash_table_remove (priv->client_ids, client); - client_ids_free (cids); - } - g_mutex_unlock (&priv->lock); - - return TRUE; -} - -static void -pinos_fd_manager_finalize (GObject * object) -{ - PinosFdManager *mgr = PINOS_FD_MANAGER (object); - PinosFdManagerPrivate *priv = mgr->priv; - - g_hash_table_unref (priv->object_ids); - g_hash_table_unref (priv->client_ids); - - G_OBJECT_CLASS (pinos_fd_manager_parent_class)->finalize (object); -} - -static void -pinos_fd_manager_init (PinosFdManager * mgr) -{ - PinosFdManagerPrivate *priv = mgr->priv = PINOS_FD_MANAGER_GET_PRIVATE (mgr); - - g_mutex_init (&priv->lock); - priv->object_ids = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) object_id_free); - priv->client_ids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); -} - -static void -pinos_fd_manager_class_init (PinosFdManagerClass * klass) -{ - GObjectClass *gobject_class = G_OBJECT_CLASS (klass); - - gobject_class->finalize = pinos_fd_manager_finalize; - - g_type_class_add_private (klass, sizeof (PinosFdManagerPrivate)); - - g_mutex_init (&manager_lock); - managers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref); -} diff --git a/pinos/client/fdmanager.h b/pinos/client/fdmanager.h deleted file mode 100644 index 5332bfece..000000000 --- a/pinos/client/fdmanager.h +++ /dev/null @@ -1,79 +0,0 @@ -/* GStreamer - * Copyright (C) 2016 Wim Taymans - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ - -#ifndef __PINOS_FD_MANAGER_H__ -#define __PINOS_FD_MANAGER_H__ - -#include - -G_BEGIN_DECLS - -typedef struct _PinosFdManager PinosFdManager; -typedef struct _PinosFdManagerClass PinosFdManagerClass; -typedef struct _PinosFdManagerPrivate PinosFdManagerPrivate; - -#define PINOS_TYPE_FD_MANAGER (pinos_fd_manager_get_type()) -#define PINOS_FD_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),PINOS_TYPE_FD_MANAGER,PinosFdManager)) -#define PINOS_FD_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),PINOS_TYPE_FD_MANAGER,PinosFdManagerClass)) -#define PINOS_IS_FD_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),PINOS_TYPE_FD_MANAGER)) -#define PINOS_IS_FD_MANAGER_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),PINOS_TYPE_FD_MANAGER)) - -/** - * PinosFdManager: - * - * Object to manager fds - */ -struct _PinosFdManager -{ - GObject parent; - - PinosFdManagerPrivate *priv; -}; - -struct _PinosFdManagerClass -{ - GObjectClass parent_class; -}; - -GType pinos_fd_manager_get_type (void); - -#define PINOS_FD_MANAGER_DEFAULT "default" - -PinosFdManager * pinos_fd_manager_get (const gchar *type); - -guint32 pinos_fd_manager_get_id (PinosFdManager *manager); - -gboolean pinos_fd_manager_add (PinosFdManager *manager, - const gchar *client, - guint32 id, - gpointer object, - GDestroyNotify notify); -gboolean pinos_fd_manager_remove (PinosFdManager *manager, - const gchar *client, - guint32 id); -gpointer pinos_fd_manager_find (PinosFdManager *manager, - const gchar *client, - guint32 id); -gboolean pinos_fd_manager_remove_all (PinosFdManager *manager, - const gchar *client); - - -G_END_DECLS - -#endif /* __PINOS_FD_MANAGER_H__ */ diff --git a/pinos/client/pinos.h b/pinos/client/pinos.h index 243450e0c..6dfb679b6 100644 --- a/pinos/client/pinos.h +++ b/pinos/client/pinos.h @@ -22,7 +22,6 @@ #include #include -#include #include #include #include diff --git a/pinos/server/client.c b/pinos/server/client.c index c37d67dee..64aa197eb 100644 --- a/pinos/server/client.c +++ b/pinos/server/client.c @@ -34,7 +34,6 @@ struct _PinosClientPrivate gchar *object_path; PinosProperties *properties; - PinosFdManager *fdmanager; GList *objects; }; @@ -208,8 +207,6 @@ pinos_client_dispose (GObject * object) GList *copy; g_debug ("client %p: dispose", client); - pinos_fd_manager_remove_all (priv->fdmanager, priv->object_path); - copy = g_list_copy (priv->objects); g_list_free_full (copy, g_object_unref); @@ -231,7 +228,6 @@ pinos_client_finalize (GObject * object) g_free (priv->object_path); if (priv->properties) pinos_properties_free (priv->properties); - g_clear_object (&priv->fdmanager); G_OBJECT_CLASS (pinos_client_parent_class)->finalize (object); } @@ -339,7 +335,6 @@ pinos_client_init (PinosClient * client) priv->iface = pinos_client1_skeleton_new (); g_debug ("client %p: new", client); - priv->fdmanager = pinos_fd_manager_get (PINOS_FD_MANAGER_DEFAULT); } /** @@ -391,7 +386,7 @@ pinos_client_add_object (PinosClient *client, g_return_if_fail (G_IS_OBJECT (object)); priv = client->priv; - priv->objects = g_list_prepend (priv->objects, object); + priv->objects = g_list_prepend (priv->objects, g_object_ref (object)); } void diff --git a/pinos/server/daemon.c b/pinos/server/daemon.c index fc4bf554b..9b48e0086 100644 --- a/pinos/server/daemon.c +++ b/pinos/server/daemon.c @@ -169,6 +169,7 @@ handle_create_node (PinosDaemon1 *interface, g_debug ("daemon %p: added node %p with path %s", daemon, node, object_path); g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", object_path)); + g_object_unref (node); return TRUE; @@ -212,6 +213,7 @@ on_port_added (PinosNode *node, PinosPort *port, PinosClient *client) } link = pinos_link_new (pinos_node_get_daemon (node), port, target); pinos_client_add_object (client, G_OBJECT (link)); + g_object_unref (link); } } @@ -259,6 +261,7 @@ handle_create_client_node (PinosDaemon1 *interface, object_path = pinos_node_get_object_path (PINOS_NODE (node)); g_debug ("daemon %p: add client-node %p, %s", daemon, node, object_path); + g_object_unref (node); fdlist = g_unix_fd_list_new (); fdidx = g_unix_fd_list_append (fdlist, g_socket_get_fd (socket), &error); @@ -271,7 +274,8 @@ handle_create_client_node (PinosDaemon1 *interface, no_socket: { - g_debug ("daemon %p: could not create node %s", daemon, error->message); + g_debug ("daemon %p: could not create socket %s", daemon, error->message); + g_object_unref (node); goto exit_error; } exit_error: