r20087: Ensure we clean up any random pending events we
[Samba/nascimento.git] / source3 / lib / events.c
blob66aefa3b52dfb1cea413ef3248403b6da4ec3d5d
1 /*
2 Unix SMB/CIFS implementation.
3 Timed event library.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Volker Lendecke 2005
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 static struct timed_event *timed_events;
26 static int timed_event_destructor(struct timed_event *te)
28 DEBUG(10, ("Destroying timed event %lx \"%s\"\n", (unsigned long)te,
29 te->event_name));
30 DLIST_REMOVE(timed_events, te);
31 return 0;
34 /****************************************************************************
35 Schedule a function for future calling, cancel with TALLOC_FREE().
36 It's the responsibility of the handler to call TALLOC_FREE() on the event
37 handed to it.
38 ****************************************************************************/
40 struct timed_event *add_timed_event(TALLOC_CTX *mem_ctx,
41 struct timeval when,
42 const char *event_name,
43 void (*handler)(struct timed_event *te,
44 const struct timeval *now,
45 void *private_data),
46 void *private_data)
48 struct timed_event *te, *last_te, *cur_te;
50 te = TALLOC_P(mem_ctx, struct timed_event);
51 if (te == NULL) {
52 DEBUG(0, ("talloc failed\n"));
53 return NULL;
56 te->when = when;
57 te->event_name = event_name;
58 te->handler = handler;
59 te->private_data = private_data;
61 /* keep the list ordered - this is NOT guarenteed as event times
62 may be changed after insertion */
63 last_te = NULL;
64 for (cur_te = timed_events; cur_te; cur_te = cur_te->next) {
65 /* if the new event comes before the current one break */
66 if (!timeval_is_zero(&cur_te->when) &&
67 timeval_compare(&te->when, &cur_te->when) < 0) {
68 break;
70 last_te = cur_te;
73 DLIST_ADD_AFTER(timed_events, te, last_te);
74 talloc_set_destructor(te, timed_event_destructor);
76 DEBUG(10, ("Added timed event \"%s\": %lx\n", event_name,
77 (unsigned long)te));
78 return te;
81 void run_events(void)
83 /* Run all events that are pending, not just one (as we
84 did previously. */
86 while (timed_events) {
87 struct timeval now;
88 GetTimeOfDay(&now);
90 if (timeval_compare(&now, &timed_events->when) < 0) {
91 /* Nothing to do yet */
92 DEBUG(11, ("run_events: Nothing to do\n"));
93 return;
96 DEBUG(10, ("Running event \"%s\" %lx\n", timed_events->event_name,
97 (unsigned long)timed_events));
99 timed_events->handler(timed_events, &now, timed_events->private_data);
103 struct timeval *get_timed_events_timeout(struct timeval *to_ret)
105 struct timeval now;
107 if (timed_events == NULL) {
108 return NULL;
111 now = timeval_current();
112 *to_ret = timeval_until(&now, &timed_events->when);
114 DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret->tv_sec,
115 (int)to_ret->tv_usec));
117 return to_ret;
120 int set_event_dispatch_time(const char *event_name, struct timeval when)
122 int num_events = 0;
123 struct timed_event *te;
125 for (te = timed_events; te; te = te->next) {
126 if (strcmp(event_name, te->event_name) == 0) {
127 te->when = when;
128 num_events++;
131 return num_events;
134 /* Returns 1 if event was found and cancelled, 0 otherwise. */
136 int cancel_named_event(const char *event_name)
138 struct timed_event *te;
140 for (te = timed_events; te; te = te->next) {
141 if (strcmp(event_name, te->event_name) == 0) {
142 TALLOC_FREE(te);
143 return 1;
146 return 0;