[tracker/timer: 1/2] libtracker-common: Use per-client timeouts in tracker-dbus



commit 97880cefa0169c7cac9e0e332f85b9c86a2a63a8
Author: Jürg Billeter <j bitron ch>
Date:   Tue Oct 26 11:33:13 2010 +0200

    libtracker-common: Use per-client timeouts in tracker-dbus
    
    This prevents unnecessary wakeups.

 src/libtracker-common/tracker-dbus.c |   57 +++++++++-------------------------
 1 files changed, 15 insertions(+), 42 deletions(-)
---
diff --git a/src/libtracker-common/tracker-dbus.c b/src/libtracker-common/tracker-dbus.c
index 1f0aac4..b73e234 100644
--- a/src/libtracker-common/tracker-dbus.c
+++ b/src/libtracker-common/tracker-dbus.c
@@ -33,9 +33,6 @@
  */
 #define CLIENT_CLEAN_UP_TIME  300
 
-/* How often we check for stale client cache (in seconds) */
-#define CLIENT_CLEAN_UP_CHECK 60
-
 struct TrackerDBusRequestHandler {
 	TrackerDBusRequestFunc new;
 	TrackerDBusRequestFunc done;
@@ -47,6 +44,7 @@ typedef struct {
 	gchar *binary;
 	gulong pid;
 	GTimeVal last_time;
+	guint clean_up_id;
 } ClientData;
 
 typedef struct {
@@ -66,10 +64,9 @@ static gboolean client_lookup_enabled;
 static DBusGConnection *freedesktop_connection;
 static DBusGProxy *freedesktop_proxy;
 static GHashTable *clients;
-static guint clients_clean_up_id;
 
 static void     client_data_free    (gpointer data);
-static gboolean clients_clean_up_cb (gpointer data);
+static gboolean client_clean_up_cb (gpointer data);
 
 static void
 request_handler_call_for_new (guint request_id)
@@ -145,8 +142,6 @@ clients_init (void)
 	                                 g_str_equal,
 	                                 NULL,
 	                                 client_data_free);
-	clients_clean_up_id =
-		g_timeout_add_seconds (CLIENT_CLEAN_UP_CHECK, clients_clean_up_cb, NULL);
 
 	return TRUE;
 }
@@ -164,11 +159,6 @@ clients_shutdown (void)
 		freedesktop_connection = NULL;
 	}
 
-	if (clients_clean_up_id != 0) {
-		g_source_remove (clients_clean_up_id);
-		clients_clean_up_id = 0;
-	}
-
 	if (clients) {
 		g_hash_table_unref (clients);
 		clients = NULL;
@@ -186,6 +176,8 @@ client_data_free (gpointer data)
 		return;
 	}
 
+	g_source_remove (cd->clean_up_id);
+
 	g_free (cd->sender);
 	g_free (cd->binary);
 
@@ -249,44 +241,22 @@ client_data_new (gchar *sender)
 }
 
 static gboolean
-clients_clean_up_cb (gpointer data)
+client_clean_up_cb (gpointer data)
 {
-	GTimeVal now;
-	GHashTableIter iter;
-	gpointer key, value;
-
-	g_get_current_time (&now);
-
-	g_hash_table_iter_init (&iter, clients);
-	while (g_hash_table_iter_next (&iter, &key, &value)) {
-		ClientData *cd;
-		glong diff;
+	ClientData *cd;
 
-		cd = value;
+	cd = data;
 
-		diff = now.tv_sec - cd->last_time.tv_sec;
-
-		/* 5 Minutes */
-		if (diff >= CLIENT_CLEAN_UP_TIME) {
-			g_debug ("Removing D-Bus client data for '%s' with id:'%s'",
-			         cd->binary, cd->sender);
-			g_hash_table_iter_remove (&iter);
-		}
-	}
+	g_debug ("Removing D-Bus client data for '%s' with id:'%s'",
+	         cd->binary, cd->sender);
+	g_hash_table_remove (clients, cd->sender);
 
 	if (g_hash_table_size (clients) < 1) {
-		/* This must be before the clients_shutdown which will
-		 * attempt to clean up the the timeout too.
-		 */
-		clients_clean_up_id = 0;
-
-		/* Clean everything else up. */
+		/* Clean everything up. */
 		clients_shutdown ();
-
-		return FALSE;
 	}
 
-	return TRUE;
+	return FALSE;
 }
 
 static ClientData *
@@ -321,8 +291,11 @@ client_get_for_context (DBusGMethodInvocation *context)
 		g_hash_table_insert (clients, sender, cd);
 	} else {
 		g_free (sender);
+		g_source_remove (cd->clean_up_id);
 	}
 
+	cd->clean_up_id = g_timeout_add_seconds (CLIENT_CLEAN_UP_TIME, client_clean_up_cb, cd);
+
 	g_get_current_time (&cd->last_time);
 
 	return cd;



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