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-2009
9 ** NOTE! The following LGPL license applies to the tevent
10 ** library. This does NOT imply that all of Samba is released
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "system/time.h"
30 #include "tevent_internal.h"
31 #include "tevent_util.h"
34 compare two timeval structures.
35 Return -1 if tv1 < tv2
36 Return 0 if tv1 == tv2
39 static int ev_timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
41 if (tv1->tv_sec > tv2->tv_sec) return 1;
42 if (tv1->tv_sec < tv2->tv_sec) return -1;
43 if (tv1->tv_usec > tv2->tv_usec) return 1;
44 if (tv1->tv_usec < tv2->tv_usec) return -1;
51 struct timeval ev_timeval_zero(void)
60 return a timeval for the current time
62 static struct timeval ev_timeval_current(void)
65 gettimeofday(&tv, NULL);
70 return a timeval struct with the given elements
72 static struct timeval ev_timeval_set(uint32_t secs, uint32_t usecs)
81 return the difference between two timevals as a timeval
82 if tv1 comes after tv2, then return a zero timeval
85 static struct timeval ev_timeval_until(const struct timeval *tv1,
86 const struct timeval *tv2)
89 if (ev_timeval_compare(tv1, tv2) >= 0) {
90 return ev_timeval_zero();
92 t.tv_sec = tv2->tv_sec - tv1->tv_sec;
93 if (tv1->tv_usec > tv2->tv_usec) {
95 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
97 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
103 return true if a timeval is zero
105 bool ev_timeval_is_zero(const struct timeval *tv)
107 return tv->tv_sec == 0 && tv->tv_usec == 0;
111 destroy a timed event
113 static int tevent_common_timed_destructor(struct tevent_timer *te)
115 tevent_debug(te->event_ctx, TEVENT_DEBUG_TRACE,
116 "Destroying timer event %p \"%s\"\n",
117 te, te->handler_name);
120 DLIST_REMOVE(te->event_ctx->timer_events, te);
126 static int tevent_common_timed_deny_destructor(struct tevent_timer *te)
133 return NULL on failure (memory allocation error)
135 struct tevent_timer *tevent_common_add_timer(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
136 struct timeval next_event,
137 tevent_timer_handler_t handler,
139 const char *handler_name,
140 const char *location)
142 struct tevent_timer *te, *last_te, *cur_te;
144 te = talloc(mem_ctx?mem_ctx:ev, struct tevent_timer);
145 if (te == NULL) return NULL;
148 te->next_event = next_event;
149 te->handler = handler;
150 te->private_data = private_data;
151 te->handler_name = handler_name;
152 te->location = location;
153 te->additional_data = NULL;
155 /* keep the list ordered */
157 for (cur_te = ev->timer_events; cur_te; cur_te = cur_te->next) {
158 /* if the new event comes before the current one break */
159 if (ev_timeval_compare(&te->next_event, &cur_te->next_event) < 0) {
166 DLIST_ADD_AFTER(ev->timer_events, te, last_te);
168 talloc_set_destructor(te, tevent_common_timed_destructor);
170 tevent_debug(ev, TEVENT_DEBUG_TRACE,
171 "Added timed event \"%s\": %p\n",
177 do a single event loop using the events defined in ev
179 return the delay untill the next timed event,
180 or zero if a timed event was triggered
182 struct timeval tevent_common_loop_timer_delay(struct tevent_context *ev)
184 struct timeval current_time = ev_timeval_zero();
185 struct tevent_timer *te = ev->timer_events;
188 /* have a default tick time of 30 seconds. This guarantees
189 that code that uses its own timeout checking will be
190 able to proceeed eventually */
191 return ev_timeval_set(30, 0);
195 * work out the right timeout for the next timed event
197 * avoid the syscall to gettimeofday() if the timed event should
198 * be triggered directly
200 * if there's a delay till the next timed event, we're done
201 * with just returning the delay
203 if (!ev_timeval_is_zero(&te->next_event)) {
204 struct timeval delay;
206 current_time = ev_timeval_current();
208 delay = ev_timeval_until(¤t_time, &te->next_event);
209 if (!ev_timeval_is_zero(&delay)) {
215 * ok, we have a timed event that we'll process ...
218 /* deny the handler to free the event */
219 talloc_set_destructor(te, tevent_common_timed_deny_destructor);
221 /* We need to remove the timer from the list before calling the
222 * handler because in a semi-async inner event loop called from the
223 * handler we don't want to come across this event again -- vl */
224 DLIST_REMOVE(ev->timer_events, te);
227 * If the timed event was registered for a zero current_time,
228 * then we pass a zero timeval here too! To avoid the
229 * overhead of gettimeofday() calls.
231 * otherwise we pass the current time
233 te->handler(ev, te, current_time, te->private_data);
235 /* The destructor isn't necessary anymore, we've already removed the
236 * event from the list. */
237 talloc_set_destructor(te, NULL);
239 tevent_debug(te->event_ctx, TEVENT_DEBUG_TRACE,
240 "Ending timer event %p \"%s\"\n",
241 te, te->handler_name);
245 return ev_timeval_zero();