[glib: 1/2] Add cache to g_unix_mount_points_get()




commit 37b39c525fa158e6501dc43d05daa5476b392728
Author: rim <rozhuk im gmail com>
Date:   Wed Feb 16 10:53:40 2022 +0000

    Add cache to g_unix_mount_points_get()

 gio/gunixmounts.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
---
diff --git a/gio/gunixmounts.c b/gio/gunixmounts.c
index ecfa61de8..9c8ef5d66 100644
--- a/gio/gunixmounts.c
+++ b/gio/gunixmounts.c
@@ -1666,6 +1666,14 @@ g_unix_mount_for (const char *file_path,
   return entry;
 }
 
+static gpointer
+copy_mount_point_cb (gconstpointer src,
+                     gpointer      data)
+{
+  GUnixMountPoint *src_mount_point = (GUnixMountPoint *) src;
+  return g_unix_mount_point_copy (src_mount_point);
+}
+
 /**
  * g_unix_mount_points_get:
  * @time_read: (out) (optional): guint64 to contain a timestamp.
@@ -1681,10 +1689,29 @@ g_unix_mount_for (const char *file_path,
 GList *
 g_unix_mount_points_get (guint64 *time_read)
 {
+  static GList *mnt_pts_last = NULL;
+  static guint64 time_read_last = 0;
+  GList *mnt_pts = NULL;
+  guint64 time_read_now;
+  G_LOCK_DEFINE_STATIC (unix_mount_points);
+
+  G_LOCK (unix_mount_points);
+
+  time_read_now = get_mount_points_timestamp ();
+  if (time_read_now != time_read_last || mnt_pts_last == NULL)
+    {
+      time_read_last = time_read_now;
+      g_list_free_full (mnt_pts_last, (GDestroyNotify) g_unix_mount_point_free);
+      mnt_pts_last = _g_get_unix_mount_points ();
+    }
+  mnt_pts = g_list_copy_deep (mnt_pts_last, copy_mount_point_cb, NULL);
+
+  G_UNLOCK (unix_mount_points);
+
   if (time_read)
-    *time_read = get_mount_points_timestamp ();
+    *time_read = time_read_now;
 
-  return _g_get_unix_mount_points ();
+  return mnt_pts;
 }
 
 /**


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