hal/hald callout.c,1.15.2.1,1.15.2.2

David Zeuthen david at freedesktop.org
Mon Jan 10 22:45:02 PST 2005


Update of /cvs/hal/hal/hald
In directory gabe:/tmp/cvs-serv26782/hald

Modified Files:
      Tag: hal-0_4-stable-branch
	callout.c 
Log Message:
2005-01-11  David Zeuthen  <davidz at redhat.com>

	* hald/callout.c: Simplify a lot more by demanding that callouts
	are run sequentially - which they are anyway since everything is
	serialized. Make a mental note to review and stress test this in
	the morning.



Index: callout.c
===================================================================
RCS file: /cvs/hal/hal/hald/callout.c,v
retrieving revision 1.15.2.1
retrieving revision 1.15.2.2
diff -u -d -r1.15.2.1 -r1.15.2.2
--- callout.c	11 Jan 2005 05:56:56 -0000	1.15.2.1
+++ callout.c	11 Jan 2005 06:45:00 -0000	1.15.2.2
@@ -58,72 +58,31 @@
 	gboolean last_of_device;
 } Callout;
 
-static void process_callouts (void);
+static void process_next_callout (void);
 
 /* Callouts that still needing to be processed
  *
  * Key: HalDevice  Value: pointer to GSList of Callouts */
-static GHashTable *pending_callouts = NULL;
-
-/* List of Callouts currently being processed */
-static GSList *active_callouts = NULL;
+static GSList *pending_callouts = NULL;
 
-static gboolean processing_callouts = FALSE;
+static Callout *active_callout = NULL;
 
 static void
 add_pending_callout (HalDevice *device, Callout *callout)
 {
-	GSList **clist = NULL;
-
-	if (pending_callouts == NULL)
-		pending_callouts = g_hash_table_new (NULL, NULL);
-	else
-		clist = g_hash_table_lookup (pending_callouts, device);
-
-	if (clist == NULL) {
-		clist = g_new0 (GSList *, 1);
-		g_hash_table_insert (pending_callouts, device, clist);
-	}
-
-	*clist = g_slist_append (*clist, callout);
-}
-
-static void
-get_device (gpointer key, gpointer value, gpointer user_data)
-{
-	HalDevice **device = user_data;
-
-	if (*device == NULL)
-		*device = (HalDevice *) key;
+	pending_callouts = g_slist_append (pending_callouts, callout);
 }
 
 static Callout *
-pop_pending_callout (gboolean *last_of_device)
+pop_pending_callout (void)
 {
-	GSList **clist;
-	HalDevice *device = NULL;
 	Callout *callout;
 
 	if (pending_callouts == NULL)
 		return NULL;
 
-	/* Hmm, not sure of a better way to do this... */
-	g_hash_table_foreach (pending_callouts, get_device, &device);
-
-	clist = g_hash_table_lookup (pending_callouts, device);
-
-	if (clist == NULL)
-		return NULL;
-
-	callout = (Callout *) (*clist)->data;
-	*clist = g_slist_remove (*clist, callout);
-
-	if (*clist == NULL) {
-		g_hash_table_remove (pending_callouts, device);
-		g_free (clist);
-		*last_of_device = TRUE;
-	} else
-		*last_of_device = FALSE;
+	callout = (Callout *) pending_callouts->data;
+	pending_callouts = g_slist_remove (pending_callouts, callout);
 
 	return callout;
 }
@@ -185,8 +144,6 @@
 	gchar data[1];
 	GError *err = NULL;
 	pid_t child_pid;
-	Callout *callout;
-	GSList *it;
 
 	/* Empty the pipe; one character per dead child */
 	if (G_IO_STATUS_NORMAL != 
@@ -213,40 +170,26 @@
 
 		HAL_INFO (("Child pid %d terminated", child_pid));
 
-	
-		/* Now find the corresponding Callout object */
-		callout = NULL;
-		for (it=g_slist_nth(active_callouts, 0); 
-		     it != NULL; 
-		     it = g_slist_next(it)) {
-			Callout *callout_it = (Callout *) it->data;
-			if (callout_it->pid == child_pid) {
-				callout = callout_it;
-				break;
-			}
-		}
-
-		if (callout == NULL) {
+		if (active_callout->pid != child_pid) {
 			/* this should never happen */
 			HAL_ERROR (("Cannot find callout for terminated "
 				    "child with pid %d", child_pid));
 			goto out;
 		}
 
-
-		/* remove element from active_callouts list */
-		active_callouts = g_slist_delete_link (active_callouts,
-							       it);
-
-		if (callout->last_of_device)
-			hal_device_callouts_finished (callout->device);
+		if (active_callout->last_of_device) {
+			hal_device_callouts_finished (active_callout->device);
+			HAL_INFO (("fooo!"));
+		}
 		
-		g_free (callout->filename);
-		g_strfreev (callout->envp);
-		g_object_unref (callout->device);
-		g_free (callout);
+		g_free (active_callout->filename);
+		g_strfreev (active_callout->envp);
+		g_object_unref (active_callout->device);
+		g_free (active_callout);
+
+		active_callout = NULL;
 		
-		process_callouts ();
+		process_next_callout ();
 	}
 	
 out:
@@ -254,10 +197,9 @@
 }
 
 static void
-process_callouts (void)
+process_next_callout (void)
 {
 	Callout *callout;
-	gboolean last_of_device;
 	char *argv[3];
 	GError *err = NULL;
 	int num_props;
@@ -284,16 +226,15 @@
 		have_installed_sigchild_handler = TRUE;
 	}
 
-	if (pending_callouts == NULL ||
-	    g_hash_table_size (pending_callouts) == 0) {
-		processing_callouts = FALSE;
+next_callout:
+	if (active_callout != NULL)
 		return;
-	}
 
-	processing_callouts = TRUE;
+	callout = pop_pending_callout ();
+	if (callout == NULL)
+		return;
 
-	callout = pop_pending_callout (&last_of_device);
-	callout->last_of_device = last_of_device;
+	active_callout = callout;
 
 	argv[0] = callout->filename;
 
@@ -329,8 +270,6 @@
 	 */
 	callout->envp[callout->envp_index] = NULL;
 
-	active_callouts = g_slist_append (active_callouts, callout);
-
 	HAL_INFO (("Invoking %s/%s", callout->working_dir, argv[0]));
 
 	if (!g_spawn_async (callout->working_dir, argv, callout->envp,
@@ -339,9 +278,12 @@
 		HAL_WARNING (("Couldn't invoke %s: %s", argv[0],
 			      err->message));
 		g_error_free (err);
+		active_callout = NULL;
+		goto next_callout;
+	} else {
+		HAL_INFO (("Child pid %d for %s", callout->pid, argv[0]));
 	}
 
-	HAL_INFO (("Child pid %d for %s", callout->pid, argv[0]));
 }
 
 void
@@ -351,6 +293,7 @@
 	GError *err = NULL;
 	const char *filename;
 	gboolean any_callouts = FALSE;
+	Callout *callout;
 
 	/* Directory doesn't exist.  This isn't an error, just exit
 	 * quietly. */
@@ -366,9 +309,9 @@
 		goto finish;
 	}
 
+	callout = NULL;
 	while ((filename = g_dir_read_name (dir)) != NULL) {
 		char *full_filename;
-		Callout *callout;
 		int num_props;
 		int i;
 
@@ -417,10 +360,13 @@
 		any_callouts = TRUE;
 	}
 
+	if (callout != NULL)
+		callout->last_of_device = TRUE;
+
 	g_dir_close (dir);
 
 	if (any_callouts)
-		process_callouts ();
+		process_next_callout ();
 
 finish:
 	/*
@@ -437,6 +383,7 @@
 {
 	GDir *dir;
 	GError *err = NULL;
+	Callout *callout;
 	const char *filename;
 
 	/* Directory doesn't exist.  This isn't an error, just exit
@@ -453,9 +400,9 @@
 		return;
 	}
 
+	callout = NULL;
 	while ((filename = g_dir_read_name (dir)) != NULL) {
 		char *full_filename;
-		Callout *callout;
 		int num_props;
 		int i;
 
@@ -503,9 +450,12 @@
 		add_pending_callout (callout->device, callout);
 	}
 
+	if (callout != NULL)
+		callout->last_of_device = TRUE;
+
 	g_dir_close (dir);
 
-	process_callouts ();
+	process_next_callout ();
 }
 
 void
@@ -513,6 +463,7 @@
 {
 	GDir *dir;
 	GError *err = NULL;
+	Callout *callout;
 	const char *filename;
 
 	/* Directory doesn't exist.  This isn't an error, just exit
@@ -529,9 +480,9 @@
 		return;
 	}
 
+	callout = NULL;
 	while ((filename = g_dir_read_name (dir)) != NULL) {
 		char *full_filename, *value;
-		Callout *callout;
 		int num_props;
 		int i;
 
@@ -555,6 +506,8 @@
 		callout->action = CALLOUT_MODIFY;
 		callout->device = g_object_ref (device);
 
+		callout->last_of_device = FALSE;
+
 		num_props = hal_device_num_properties (device);
 
 		value = hal_device_property_to_string (device, key);
@@ -583,7 +536,11 @@
 		g_free (value);
 	}
 
+	if (callout != NULL)
+		callout->last_of_device = TRUE;
+
+
 	g_dir_close (dir);
 
-	process_callouts ();
+	process_next_callout ();
 }




More information about the hal-commit mailing list