2 Unix SMB/CIFS implementation.
4 common events code for timed events
6 Copyright (C) Andrew Tridgell 2003-2006
7 Copyright (C) Stefan Metzmacher 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "system/time.h"
26 #include "tevent_internal.h"
27 #include "tevent_util.h"
30 compare two timeval structures.
31 Return -1 if tv1 < tv2
32 Return 0 if tv1 == tv2
35 static int ev_timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
37 if (tv1->tv_sec > tv2->tv_sec) return 1;
38 if (tv1->tv_sec < tv2->tv_sec) return -1;
39 if (tv1->tv_usec > tv2->tv_usec) return 1;
40 if (tv1->tv_usec < tv2->tv_usec) return -1;
47 static struct timeval ev_timeval_zero(void)
56 return a timeval for the current time
58 static struct timeval ev_timeval_current(void)
61 gettimeofday(&tv, NULL);
66 return a timeval struct with the given elements
68 static struct timeval ev_timeval_set(uint32_t secs, uint32_t usecs)
77 return the difference between two timevals as a timeval
78 if tv1 comes after tv2, then return a zero timeval
81 static struct timeval ev_timeval_until(const struct timeval *tv1,
82 const struct timeval *tv2)
85 if (ev_timeval_compare(tv1, tv2) >= 0) {
86 return ev_timeval_zero();
88 t.tv_sec = tv2->tv_sec - tv1->tv_sec;
89 if (tv1->tv_usec > tv2->tv_usec) {
91 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
93 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
99 return true if a timeval is zero
101 bool ev_timeval_is_zero(const struct timeval *tv)
103 return tv->tv_sec == 0 && tv->tv_usec == 0;
107 destroy a timed event
109 static int tevent_common_timed_destructor(struct tevent_timer *te)
111 struct tevent_context *ev = talloc_get_type(te->event_ctx,
112 struct tevent_context);
113 DLIST_REMOVE(ev->timer_events, te);
117 static int tevent_common_timed_deny_destructor(struct tevent_timer *te)
124 return NULL on failure (memory allocation error)
126 struct tevent_timer *tevent_common_add_timer(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
127 struct timeval next_event,
128 tevent_timer_handler_t handler,
130 const char *handler_name,
131 const char *location)
133 struct tevent_timer *te, *last_te, *cur_te;
135 te = talloc(mem_ctx?mem_ctx:ev, struct tevent_timer);
136 if (te == NULL) return NULL;
139 te->next_event = next_event;
140 te->handler = handler;
141 te->private_data = private_data;
142 te->handler_name = handler_name;
143 te->location = location;
144 te->additional_data = NULL;
146 /* keep the list ordered */
148 for (cur_te = ev->timer_events; cur_te; cur_te = cur_te->next) {
149 /* if the new event comes before the current one break */
150 if (ev_timeval_compare(&te->next_event, &cur_te->next_event) < 0) {
157 DLIST_ADD_AFTER(ev->timer_events, te, last_te);
159 talloc_set_destructor(te, tevent_common_timed_destructor);
165 do a single event loop using the events defined in ev
167 return the delay untill the next timed event,
168 or zero if a timed event was triggered
170 struct timeval tevent_common_loop_timer_delay(struct tevent_context *ev)
172 struct timeval current_time = ev_timeval_zero();
173 struct tevent_timer *te = ev->timer_events;
176 /* have a default tick time of 30 seconds. This guarantees
177 that code that uses its own timeout checking will be
178 able to proceeed eventually */
179 return ev_timeval_set(30, 0);
183 * work out the right timeout for the next timed event
185 * avoid the syscall to gettimeofday() if the timed event should
186 * be triggered directly
188 * if there's a delay till the next timed event, we're done
189 * with just returning the delay
191 if (!ev_timeval_is_zero(&te->next_event)) {
192 struct timeval delay;
194 current_time = ev_timeval_current();
196 delay = ev_timeval_until(¤t_time, &te->next_event);
197 if (!ev_timeval_is_zero(&delay)) {
203 * ok, we have a timed event that we'll process ...
206 /* deny the handler to free the event */
207 talloc_set_destructor(te, tevent_common_timed_deny_destructor);
209 /* We need to remove the timer from the list before calling the
210 * handler because in a semi-async inner event loop called from the
211 * handler we don't want to come across this event again -- vl */
212 DLIST_REMOVE(ev->timer_events, te);
215 * If the timed event was registered for a zero current_time,
216 * then we pass a zero timeval here too! To avoid the
217 * overhead of gettimeofday() calls.
219 * otherwise we pass the current time
221 te->handler(ev, te, current_time, te->private_data);
223 /* The destructor isn't necessary anymore, we've already removed the
224 * event from the list. */
225 talloc_set_destructor(te, NULL);
229 return ev_timeval_zero();