[tepl] AmtkActionInfoCentralStore: add private function to unref singleton



commit 42eafbdf280b637e36439ad35691157fa8b429bc
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Jul 29 13:33:13 2017 +0200

    AmtkActionInfoCentralStore: add private function to unref singleton
    
    The singleton is implemented in the same way as in Devhelp (for example
    DhSettings).

 amtk/amtk-action-info-central-store.c |   27 +++++++++++++++++++++++----
 amtk/amtk-action-info-central-store.h |    3 +++
 2 files changed, 26 insertions(+), 4 deletions(-)
---
diff --git a/amtk/amtk-action-info-central-store.c b/amtk/amtk-action-info-central-store.c
index 8ef76fd..753a2fb 100644
--- a/amtk/amtk-action-info-central-store.c
+++ b/amtk/amtk-action-info-central-store.c
@@ -57,6 +57,8 @@ struct _AmtkActionInfoCentralStorePrivate
        GHashTable *hash_table;
 };
 
+static AmtkActionInfoCentralStore *singleton = NULL;
+
 G_DEFINE_TYPE_WITH_PRIVATE (AmtkActionInfoCentralStore, amtk_action_info_central_store, G_TYPE_OBJECT)
 
 static void
@@ -66,6 +68,11 @@ amtk_action_info_central_store_finalize (GObject *object)
 
        g_hash_table_unref (central_store->priv->hash_table);
 
+       if (singleton == central_store)
+       {
+               singleton = NULL;
+       }
+
        G_OBJECT_CLASS (amtk_action_info_central_store_parent_class)->finalize (object);
 }
 
@@ -97,14 +104,26 @@ amtk_action_info_central_store_init (AmtkActionInfoCentralStore *central_store)
 AmtkActionInfoCentralStore *
 amtk_action_info_central_store_get_instance (void)
 {
-       static AmtkActionInfoCentralStore *instance = NULL;
+       if (G_UNLIKELY (singleton == NULL))
+       {
+               singleton = g_object_new (AMTK_TYPE_ACTION_INFO_CENTRAL_STORE, NULL);
+       }
 
-       if (G_UNLIKELY (instance == NULL))
+       return singleton;
+}
+
+void
+_amtk_action_info_central_store_unref_singleton (void)
+{
+       if (singleton != NULL)
        {
-               instance = g_object_new (AMTK_TYPE_ACTION_INFO_CENTRAL_STORE, NULL);
+               g_object_unref (singleton);
        }
 
-       return instance;
+       /* singleton is not set to NULL here, it is set to NULL in
+        * amtk_action_info_central_store_finalize() (i.e. when we are sure that
+        * the ref count reaches 0).
+        */
 }
 
 void
diff --git a/amtk/amtk-action-info-central-store.h b/amtk/amtk-action-info-central-store.h
index 7f2a861..a2a561c 100644
--- a/amtk/amtk-action-info-central-store.h
+++ b/amtk/amtk-action-info-central-store.h
@@ -65,6 +65,9 @@ G_GNUC_INTERNAL
 void           _amtk_action_info_central_store_add             (AmtkActionInfoCentralStore *central_store,
                                                                 AmtkActionInfo             *info);
 
+G_GNUC_INTERNAL
+void           _amtk_action_info_central_store_unref_singleton (void);
+
 G_END_DECLS
 
 #endif /* AMTK_ACTION_INFO_CENTRAL_STORE_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]