2 Unix SMB/CIFS implementation.
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.
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
,
30 DLIST_REMOVE(timed_events
, te
);
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
38 ****************************************************************************/
40 struct timed_event
*add_timed_event(TALLOC_CTX
*mem_ctx
,
42 const char *event_name
,
43 void (*handler
)(struct timed_event
*te
,
44 const struct timeval
*now
,
48 struct timed_event
*te
, *last_te
, *cur_te
;
50 te
= TALLOC_P(mem_ctx
, struct timed_event
);
52 DEBUG(0, ("talloc failed\n"));
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 */
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) {
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
,
83 /* Run all events that are pending, not just one (as we
86 while (timed_events
) {
90 if (timeval_compare(&now
, &timed_events
->when
) < 0) {
91 /* Nothing to do yet */
92 DEBUG(11, ("run_events: Nothing to do\n"));
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
)
107 if (timed_events
== 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
));
120 int set_event_dispatch_time(const char *event_name
, struct timeval when
)
123 struct timed_event
*te
;
125 for (te
= timed_events
; te
; te
= te
->next
) {
126 if (strcmp(event_name
, te
->event_name
) == 0) {
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) {