[nautilus/wip/ernestask/tasks: 47/49] Separate the file table



commit f6ec84f02581e357b4c4ea5e3cedb0d8bfb18837
Author: Ernestas Kulik <ernestask gnome org>
Date:   Mon Aug 7 13:26:09 2017 +0300

    Separate the file table

 src-ng/meson.build           |    2 +
 src-ng/nautilus-file-table.c |   85 ++++++++++++++++++++++++++++++++++++++++++
 src-ng/nautilus-file-table.h |   32 ++++++++++++++++
 src-ng/nautilus-file.c       |   45 +++++-----------------
 4 files changed, 129 insertions(+), 35 deletions(-)
---
diff --git a/src-ng/meson.build b/src-ng/meson.build
index 81b7d02..27399e6 100644
--- a/src-ng/meson.build
+++ b/src-ng/meson.build
@@ -19,6 +19,8 @@ nautilus_ng_sources = ['nautilus-task.c',
                        'nautilus-file-changes.h',
                        'nautilus-signal-utilities.c',
                        'nautilus-signal-utilities.h',
+                       'nautilus-file-table.c',
+                       'nautilus-file-table.h',
                        'main.c']
 
 nautilus_ng_dependencies = [gio, glib]
diff --git a/src-ng/nautilus-file-table.c b/src-ng/nautilus-file-table.c
new file mode 100644
index 0000000..8e33146
--- /dev/null
+++ b/src-ng/nautilus-file-table.c
@@ -0,0 +1,85 @@
+/* Copyright (C) 2017 Ernestas Kulik <ernestask gnome org>
+ *
+ * This file is part of Nautilus.
+ *
+ * Nautilus 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.
+ *
+ * Nautilus 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 Nautilus.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "nautilus-file-table.h"
+
+static GMutex      mutex;
+
+static gpointer
+create_hash_table (gpointer data)
+{
+    (void) data;
+
+    return g_hash_table_new_full (g_file_hash, (GEqualFunc) g_file_equal,
+                                  g_object_unref, NULL);
+}
+
+static GHashTable *
+get_hash_table (void)
+{
+    static GOnce once = G_ONCE_INIT;
+
+    g_once (&once, create_hash_table, NULL);
+
+    g_assert (once.retval != NULL);
+
+    return once.retval;
+}
+
+gboolean
+nautilus_file_table_insert (GFile        *location,
+                            NautilusFile *instance)
+{
+    gboolean success;
+
+    g_mutex_lock (&mutex);
+
+    success = g_hash_table_insert (get_hash_table (), location, instance);
+
+    g_mutex_unlock (&mutex);
+
+    return success;
+}
+
+gboolean
+nautilus_file_table_remove (GFile *location)
+{
+    gboolean success;
+
+    g_mutex_lock (&mutex);
+
+    success = g_hash_table_remove (get_hash_table (), location);
+
+    g_mutex_unlock (&mutex);
+
+    return success;
+}
+
+NautilusFile *
+nautilus_file_table_lookup (GFile *location)
+{
+    gpointer instance;
+
+    g_mutex_lock (&mutex);
+
+    instance = g_hash_table_lookup (get_hash_table (), location);
+
+    g_mutex_unlock (&mutex);
+
+    return instance;
+}
diff --git a/src-ng/nautilus-file-table.h b/src-ng/nautilus-file-table.h
new file mode 100644
index 0000000..44c3b44
--- /dev/null
+++ b/src-ng/nautilus-file-table.h
@@ -0,0 +1,32 @@
+/* Copyright (C) 2017 Ernestas Kulik <ernestask gnome org>
+ *
+ * This file is part of Nautilus.
+ *
+ * Nautilus 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.
+ *
+ * Nautilus 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 Nautilus.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef NAUTILUS_FILE_TABLE_H_INCLUDED
+#define NAUTILUS_FILE_TABLE_H_INCLUDED
+
+#include "nautilus-file.h"
+
+#include <gio/gio.h>
+
+gboolean nautilus_file_table_insert (GFile        *location,
+                                     NautilusFile *instance);
+gboolean nautilus_file_table_remove (GFile        *location);
+
+NautilusFile *nautilus_file_table_lookup (GFile *location);
+
+#endif
diff --git a/src-ng/nautilus-file.c b/src-ng/nautilus-file.c
index fd51873..8cac9b0 100644
--- a/src-ng/nautilus-file.c
+++ b/src-ng/nautilus-file.c
@@ -20,6 +20,7 @@
 
 #include "nautilus-cache.h"
 #include "nautilus-directory.h"
+#include "nautilus-file-table.h"
 #include "nautilus-task-manager.h"
 #include "tasks/nautilus-attribute-task.h"
 
@@ -53,8 +54,6 @@ enum
 
 static GParamSpec *properties[N_PROPERTIES] = { NULL };
 static guint       signals[LAST_SIGNAL]     = { 0 };
-static GHashTable *files                    = NULL;
-static GMutex      files_mutex;
 
 static GObject *
 constructor (GType                  type,
@@ -74,16 +73,7 @@ constructor (GType                  type,
 
     g_assert (location != NULL);
 
-    g_mutex_lock (&files_mutex);
-
-    if (files == NULL)
-    {
-        files = g_hash_table_new_full (g_file_hash, (GEqualFunc) g_file_equal,
-                                       g_object_unref, NULL);
-    }
-
-
-    instance = g_hash_table_lookup (files, location);
+    instance = nautilus_file_table_lookup (location);
     if (instance != NULL)
     {
         instance = g_object_ref (instance);
@@ -96,11 +86,9 @@ constructor (GType                  type,
         instance = parent_class->constructor (type, n_construct_properties,
                                               construct_properties);
 
-        g_assert (g_hash_table_insert (files, location, instance));
+        g_assert (nautilus_file_table_insert (location, instance));
     }
 
-    g_mutex_unlock (&files_mutex);
-
     return instance;
 }
 
@@ -136,9 +124,7 @@ finalize (GObject *object)
 
     priv = nautilus_file_get_instance_private (NAUTILUS_FILE (object));
 
-    g_mutex_lock (&files_mutex);
-    g_hash_table_remove (files, priv->location);
-    g_mutex_unlock (&files_mutex);
+    g_assert (nautilus_file_table_remove (priv->location));
 
     G_OBJECT_CLASS (nautilus_file_parent_class)->finalize (object);
 }
@@ -155,15 +141,11 @@ renamed (NautilusFile *file,
                (gpointer) file, (gpointer) priv->location,
                (gpointer) new_location);
 
-    g_mutex_lock (&files_mutex);
-
-    g_hash_table_remove (files, priv->location);
+    g_assert (nautilus_file_table_remove (priv->location));
 
     priv->location = g_object_ref (new_location);
 
-    g_assert (g_hash_table_insert (files, new_location, file));
-
-    g_mutex_unlock (&files_mutex);
+    g_assert (nautilus_file_table_insert (new_location, file));
 
     nautilus_cache_item_invalidate (priv->cache, priv->cache_items[INFO],
                                     FALSE);
@@ -326,23 +308,16 @@ nautilus_file_query_info (NautilusFile             *file,
 NautilusFile *
 nautilus_file_get_existing (GFile *location)
 {
-    NautilusFile *file = NULL;
+    NautilusFile *file;
 
     g_return_val_if_fail (G_IS_FILE (location), NULL);
 
-    g_mutex_lock (&files_mutex);
-
-    if (files != NULL)
+    file = nautilus_file_table_lookup (location);
+    if (file != NULL)
     {
-        file = g_hash_table_lookup (files, location);
-        if (file != NULL)
-        {
-            file = g_object_ref (file);
-        }
+        file = g_object_ref (file);
     }
 
-    g_mutex_unlock (&files_mutex);
-
     return file;
 }
 


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