diff options
author | Jeremy Allison <jra@samba.org> | 2007-01-19 19:41:34 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 12:17:19 -0500 |
commit | 6fee874ab420b4c406e38b5e189a6d6ea8594fc9 (patch) | |
tree | 03b9e49f857167812807267c32ceb87ab0e37bfe /source/lib | |
parent | e635bad00ecf083c34da339e3616c945a140e478 (diff) | |
download | samba-6fee874ab420b4c406e38b5e189a6d6ea8594fc9.tar.gz samba-6fee874ab420b4c406e38b5e189a6d6ea8594fc9.tar.xz samba-6fee874ab420b4c406e38b5e189a6d6ea8594fc9.zip |
r20912: Ensure the list always remains sorted even when
moving events around.
Jeremy.
Diffstat (limited to 'source/lib')
-rw-r--r-- | source/lib/events.c | 46 |
1 files changed, 29 insertions, 17 deletions
diff --git a/source/lib/events.c b/source/lib/events.c index ef52faef01f..ab38a85a53e 100644 --- a/source/lib/events.c +++ b/source/lib/events.c @@ -69,13 +69,35 @@ static int timed_event_destructor(struct timed_event *te) } /**************************************************************************** + 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 @@ struct timed_event *event_add_timed(struct event_context *event_ctx, 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 @@ struct timed_event *event_add_timed(struct event_context *event_ctx, 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 @@ struct event_context *event_context_init(TALLOC_CTX *mem_ctx) 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. */ |