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(void *p
)
28 struct timed_event
*te
= talloc_get_type_abort(p
, struct timed_event
);
29 DEBUG(10, ("Destroying timed event %lx \"%s\"\n", (unsigned long)te
,
31 DLIST_REMOVE(timed_events
, te
);
35 /****************************************************************************
36 Schedule a function for future calling, cancel with TALLOC_FREE().
37 It's the responsibility of the handler to call TALLOC_FREE() on the event
39 ****************************************************************************/
41 struct timed_event
*add_timed_event(TALLOC_CTX
*mem_ctx
,
43 const char *event_name
,
44 void (*handler
)(struct timed_event
*te
,
45 const struct timeval
*now
,
49 struct timed_event
*te
, *last_te
, *cur_te
;
51 te
= TALLOC_P(mem_ctx
, struct timed_event
);
53 DEBUG(0, ("talloc failed\n"));
58 te
->event_name
= event_name
;
59 te
->handler
= handler
;
60 te
->private_data
= private_data
;
62 /* keep the list ordered */
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
,
85 if (timed_events
== NULL
) {
86 /* No syscall if there are no events */
87 DEBUG(11, ("run_events: No events\n"));
93 if (timeval_compare(&now
, &timed_events
->when
) < 0) {
94 /* Nothing to do yet */
95 DEBUG(11, ("run_events: Nothing to do\n"));
99 DEBUG(10, ("Running event \"%s\" %lx\n", timed_events
->event_name
,
100 (unsigned long)timed_events
));
102 timed_events
->handler(timed_events
, &now
, timed_events
->private_data
);
106 struct timeval
*get_timed_events_timeout(struct timeval
*to_ret
, time_t default_to
)
110 if (timed_events
== NULL
) {
111 if (default_to
== (time_t)-1) {
114 *to_ret
= timeval_set(default_to
, 0);
118 now
= timeval_current();
119 *to_ret
= timeval_until(&now
, &timed_events
->when
);
121 DEBUG(10, ("timed_events_timeout: %d/%d\n", (int)to_ret
->tv_sec
,
122 (int)to_ret
->tv_usec
));