diff options
author | Ernestas Kulik <ernestask@gnome.org> | 2017-08-07 13:26:09 +0300 |
---|---|---|
committer | Carlos Soriano <csoriano@gnome.org> | 2018-01-20 00:24:04 +0100 |
commit | 88fbf36f64482d1c36d8658d65135070617fb8af (patch) | |
tree | 3f03c1826c9b1fd3df7c294ce5d66bf125746486 | |
parent | 381335146f5a0f64fab898bade23d9ce6a796988 (diff) | |
download | nautilus-88fbf36f64482d1c36d8658d65135070617fb8af.tar.gz |
Separate the file table
-rw-r--r-- | src-ng/meson.build | 2 | ||||
-rw-r--r-- | src-ng/nautilus-file-table.c | 85 | ||||
-rw-r--r-- | src-ng/nautilus-file-table.h | 32 | ||||
-rw-r--r-- | 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 81b7d021d..27399e6f3 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 000000000..8e3314609 --- /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 000000000..44c3b4451 --- /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 fd51873b9..8cac9b0ae 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; } |