r13676: have to return a value from a non-void function
[Samba.git] / source / lib / events.c
blob133752c78e40380533e44b6829ab96f86b381274
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(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,
30 te->event_name));
31 DLIST_REMOVE(timed_events, te);
32 return 0;
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
38 handed to it.
39 ****************************************************************************/
41 struct timed_event *add_timed_event(TALLOC_CTX *mem_ctx,
42 struct timeval when,
43 const char *event_name,
44 void (*handler)(struct timed_event *te,
45 const struct timeval *now,
46 void *private_data),
47 void *private_data)
49 struct timed_event *te, *last_te, *cur_te;
51 te = TALLOC_P(mem_ctx, struct timed_event);
52 if (te == NULL) {
53 DEBUG(0, ("talloc failed\n"));
54 return NULL;
57 te->when = when;
58 te->event_name = event_name;
59 te->handler = handler;
60 te->private_data = private_data;
62 /* keep the list ordered */
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 struct timeval now;
85 if (timed_events == NULL) {
86 /* No syscall if there are no events */
87 DEBUG(11, ("run_events: No events\n"));
88 return;
91 GetTimeOfDay(&now);
93 if (timeval_compare(&now, &timed_events->when) < 0) {
94 /* Nothing to do yet */
95 DEBUG(11, ("run_events: Nothing to do\n"));
96 return;
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);
103 return;
106 struct timeval *get_timed_events_timeout(struct timeval *to_ret, time_t default_to)
108 struct timeval now;
110 if (timed_events == NULL) {
111 if (default_to == (time_t)-1) {
112 return NULL;
114 *to_ret = timeval_set(default_to, 0);
115 return to_ret;
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));
124 return to_ret;