svn commit: samba r20912 - in branches: SAMBA_3_0/source/lib SAMBA_3_0_24/source/lib

jra at samba.org jra at samba.org
Fri Jan 19 19:41:35 GMT 2007


Author: jra
Date: 2007-01-19 19:41:34 +0000 (Fri, 19 Jan 2007)
New Revision: 20912

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=20912

Log:
Ensure the list always remains sorted even when
moving events around.
Jeremy.

Modified:
   branches/SAMBA_3_0/source/lib/events.c
   branches/SAMBA_3_0_24/source/lib/events.c


Changeset:
Modified: branches/SAMBA_3_0/source/lib/events.c
===================================================================
--- branches/SAMBA_3_0/source/lib/events.c	2007-01-19 18:13:53 UTC (rev 20911)
+++ branches/SAMBA_3_0/source/lib/events.c	2007-01-19 19:41:34 UTC (rev 20912)
@@ -69,13 +69,35 @@
 }
 
 /****************************************************************************
+ Add te by time.
+****************************************************************************/
+
+static void add_event_by_time(struct timed_event *te)
+{
+	struct timed_event *last_te, *cur_te;
+
+	/* Keep the list ordered by time. We must preserve this. */
+	last_te = NULL;
+	for (cur_te = timed_events; cur_te; cur_te = cur_te->next) {
+		/* if the new event comes before the current one break */
+		if (!timeval_is_zero(&cur_te->when) &&
+				timeval_compare(&te->when, &cur_te->when) < 0) {
+			break;
+		}
+		last_te = cur_te;
+	}
+
+	DLIST_ADD_AFTER(timed_events, te, last_te);
+}
+
+/****************************************************************************
  Schedule a function for future calling, cancel with TALLOC_FREE().
  It's the responsibility of the handler to call TALLOC_FREE() on the event
  handed to it.
 ****************************************************************************/
 
 struct timed_event *event_add_timed(struct event_context *event_ctx,
-				    TALLOC_CTX *mem_ctx,
+				TALLOC_CTX *mem_ctx,
 				struct timeval when,
 				const char *event_name,
 				void (*handler)(struct event_context *event_ctx,
@@ -84,7 +106,7 @@
 						void *private_data),
 				void *private_data)
 {
-	struct timed_event *te, *last_te, *cur_te;
+	struct timed_event *te;
 
 	te = TALLOC_P(mem_ctx, struct timed_event);
 	if (te == NULL) {
@@ -98,19 +120,8 @@
 	te->handler = handler;
 	te->private_data = private_data;
 
-	/* keep the list ordered - this is NOT guarenteed as event times
-	   may be changed after insertion */
-	last_te = NULL;
-	for (cur_te = event_ctx->timed_events; cur_te; cur_te = cur_te->next) {
-		/* if the new event comes before the current one break */
-		if (!timeval_is_zero(&cur_te->when)
-		    && timeval_compare(&te->when, &cur_te->when) < 0) {
-			break;
-		}
-		last_te = cur_te;
-	}
+	add_event_by_time(te);
 
-	DLIST_ADD_AFTER(event_ctx->timed_events, te, last_te);
 	talloc_set_destructor(te, timed_event_destructor);
 
 	DEBUG(10, ("Added timed event \"%s\": %lx\n", event_name,
@@ -293,16 +304,17 @@
 int set_event_dispatch_time(struct event_context *event_ctx,
 			    const char *event_name, struct timeval when)
 {
-	int num_events = 0;
 	struct timed_event *te;
 
 	for (te = event_ctx->timed_events; te; te = te->next) {
 		if (strcmp(event_name, te->event_name) == 0) {
+			DLIST_REMOVE(timed_events, te);
 			te->when = when;
-			num_events++;
+			add_event_by_time(te);
+			return 1;
 		}
 	}
-	return num_events;
+	return 0;
 }
 
 /* Returns 1 if event was found and cancelled, 0 otherwise. */

Modified: branches/SAMBA_3_0_24/source/lib/events.c
===================================================================
--- branches/SAMBA_3_0_24/source/lib/events.c	2007-01-19 18:13:53 UTC (rev 20911)
+++ branches/SAMBA_3_0_24/source/lib/events.c	2007-01-19 19:41:34 UTC (rev 20912)
@@ -32,6 +32,28 @@
 }
 
 /****************************************************************************
+  Add te by time.
+****************************************************************************/
+
+static void add_event_by_time(struct timed_event *te)
+{
+	struct timed_event *last_te, *cur_te;
+
+	/* Keep the list ordered by time. We must preserve this. */
+	last_te = NULL;
+	for (cur_te = timed_events; cur_te; cur_te = cur_te->next) {
+		/* if the new event comes before the current one break */
+		if (!timeval_is_zero(&cur_te->when) &&
+				timeval_compare(&te->when, &cur_te->when) < 0) {
+			break;
+		}
+		last_te = cur_te;
+	}
+
+	DLIST_ADD_AFTER(timed_events, te, last_te);
+}
+
+/****************************************************************************
  Schedule a function for future calling, cancel with TALLOC_FREE().
  It's the responsibility of the handler to call TALLOC_FREE() on the event
  handed to it.
@@ -45,7 +67,7 @@
 						void *private_data),
 				void *private_data)
 {
-	struct timed_event *te, *last_te, *cur_te;
+	struct timed_event *te;
 
 	te = TALLOC_P(mem_ctx, struct timed_event);
 	if (te == NULL) {
@@ -58,19 +80,8 @@
 	te->handler = handler;
 	te->private_data = private_data;
 
-	/* keep the list ordered - this is NOT guarenteed as event times
-	   may be changed after insertion */
-	last_te = NULL;
-	for (cur_te = timed_events; cur_te; cur_te = cur_te->next) {
-		/* if the new event comes before the current one break */
-		if (!timeval_is_zero(&cur_te->when) &&
-				timeval_compare(&te->when, &cur_te->when) < 0) {
-			break;
-		}
-		last_te = cur_te;
-	}
+	add_event_by_time(te);
 
-	DLIST_ADD_AFTER(timed_events, te, last_te);
 	talloc_set_destructor(te, timed_event_destructor);
 
 	DEBUG(10, ("Added timed event \"%s\": %lx\n", event_name,
@@ -117,18 +128,24 @@
 	return to_ret;
 }
 
+/****************************************************************************
+ Move a function within the list. Keep the list sorted by time.
+****************************************************************************/
+
 int set_event_dispatch_time(const char *event_name, struct timeval when)
 {
-	int num_events = 0;
 	struct timed_event *te;
 
 	for (te = timed_events; te; te = te->next) {
 		if (strcmp(event_name, te->event_name) == 0) {
+			DLIST_REMOVE(timed_events, te);
 			te->when = when;
-			num_events++;
+			add_event_by_time(te);
+			return 1;
 		}
 	}
-	return num_events;
+
+	return 0;
 }
 
 /* Returns 1 if event was found and cancelled, 0 otherwise. */



More information about the samba-cvs mailing list