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 int tevent_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
tevent_timeval_zero(void)
60 return a timeval for the current time
62 struct timeval
tevent_timeval_current(void)
65 gettimeofday(&tv
, NULL
);
70 return a timeval struct with the given elements
72 struct timeval
tevent_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 struct timeval
tevent_timeval_until(const struct timeval
*tv1
,
86 const struct timeval
*tv2
)
89 if (tevent_timeval_compare(tv1
, tv2
) >= 0) {
90 return tevent_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 tevent_timeval_is_zero(const struct timeval
*tv
)
107 return tv
->tv_sec
== 0 && tv
->tv_usec
== 0;
110 struct timeval
tevent_timeval_add(const struct timeval
*tv
, uint32_t secs
,
113 struct timeval tv2
= *tv
;
115 tv2
.tv_usec
+= usecs
;
116 tv2
.tv_sec
+= tv2
.tv_usec
/ 1000000;
117 tv2
.tv_usec
= tv2
.tv_usec
% 1000000;
123 return a timeval in the future with a specified offset
125 struct timeval
tevent_timeval_current_ofs(uint32_t secs
, uint32_t usecs
)
127 struct timeval tv
= tevent_timeval_current();
128 return tevent_timeval_add(&tv
, secs
, usecs
);
132 destroy a timed event
134 static int tevent_common_timed_destructor(struct tevent_timer
*te
)
137 tevent_common_check_double_free(te
, "tevent_timer double free");
140 te
->destroyed
= true;
142 if (te
->event_ctx
== NULL
) {
146 tevent_debug(te
->event_ctx
, TEVENT_DEBUG_TRACE
,
147 "Destroying timer event %p \"%s\"\n",
148 te
, te
->handler_name
);
150 if (te
->event_ctx
->last_zero_timer
== te
) {
151 te
->event_ctx
->last_zero_timer
= DLIST_PREV(te
);
153 DLIST_REMOVE(te
->event_ctx
->timer_events
, te
);
155 te
->event_ctx
= NULL
;
165 static void tevent_common_insert_timer(struct tevent_context
*ev
,
166 struct tevent_timer
*te
,
169 struct tevent_timer
*prev_te
= NULL
;
172 tevent_abort(ev
, "tevent_timer use after free");
176 /* keep the list ordered */
177 if (optimize_zero
&& tevent_timeval_is_zero(&te
->next_event
)) {
179 * Some callers use zero tevent_timer
180 * instead of tevent_immediate events.
182 * As these can happen very often,
183 * we remember the last zero timer
186 prev_te
= ev
->last_zero_timer
;
187 ev
->last_zero_timer
= te
;
189 struct tevent_timer
*cur_te
;
192 * we traverse the list from the tail
193 * because it's much more likely that
194 * timers are added at the end of the list
196 for (cur_te
= DLIST_TAIL(ev
->timer_events
);
198 cur_te
= DLIST_PREV(cur_te
))
203 * if the new event comes before the current
204 * we continue searching
206 ret
= tevent_timeval_compare(&te
->next_event
,
207 &cur_te
->next_event
);
218 DLIST_ADD_AFTER(ev
->timer_events
, te
, prev_te
);
223 return NULL on failure (memory allocation error)
225 static struct tevent_timer
*tevent_common_add_timer_internal(
226 struct tevent_context
*ev
,
228 struct timeval next_event
,
229 tevent_timer_handler_t handler
,
231 const char *handler_name
,
232 const char *location
,
235 struct tevent_timer
*te
;
237 te
= talloc(mem_ctx
?mem_ctx
:ev
, struct tevent_timer
);
238 if (te
== NULL
) return NULL
;
240 *te
= (struct tevent_timer
) {
242 .next_event
= next_event
,
244 .private_data
= private_data
,
245 .handler_name
= handler_name
,
246 .location
= location
,
249 if (ev
->timer_events
== NULL
) {
250 ev
->last_zero_timer
= NULL
;
253 tevent_common_insert_timer(ev
, te
, optimize_zero
);
255 talloc_set_destructor(te
, tevent_common_timed_destructor
);
257 tevent_debug(ev
, TEVENT_DEBUG_TRACE
,
258 "Added timed event \"%s\": %p\n",
263 struct tevent_timer
*tevent_common_add_timer(struct tevent_context
*ev
,
265 struct timeval next_event
,
266 tevent_timer_handler_t handler
,
268 const char *handler_name
,
269 const char *location
)
272 * do not use optimization, there are broken Samba
273 * versions which use tevent_common_add_timer()
274 * without using tevent_common_loop_timer_delay(),
275 * it just uses DLIST_REMOVE(ev->timer_events, te)
276 * and would leave ev->last_zero_timer behind.
278 return tevent_common_add_timer_internal(ev
, mem_ctx
, next_event
,
279 handler
, private_data
,
280 handler_name
, location
,
284 struct tevent_timer
*tevent_common_add_timer_v2(struct tevent_context
*ev
,
286 struct timeval next_event
,
287 tevent_timer_handler_t handler
,
289 const char *handler_name
,
290 const char *location
)
293 * Here we turn on last_zero_timer optimization
295 return tevent_common_add_timer_internal(ev
, mem_ctx
, next_event
,
296 handler
, private_data
,
297 handler_name
, location
,
301 void tevent_update_timer(struct tevent_timer
*te
, struct timeval next_event
)
303 struct tevent_context
*ev
= te
->event_ctx
;
305 if (ev
->last_zero_timer
== te
) {
306 te
->event_ctx
->last_zero_timer
= DLIST_PREV(te
);
308 DLIST_REMOVE(ev
->timer_events
, te
);
310 te
->next_event
= next_event
;
313 * Not doing the zero_timer optimization. This is for new code
314 * that should know about immediates.
316 tevent_common_insert_timer(ev
, te
, false);
319 int tevent_common_invoke_timer_handler(struct tevent_timer
*te
,
320 struct timeval current_time
,
323 struct tevent_context
*handler_ev
= te
->event_ctx
;
325 if (removed
!= NULL
) {
329 if (te
->event_ctx
== NULL
) {
334 * We need to remove the timer from the list before calling the
335 * handler because in a semi-async inner event loop called from the
336 * handler we don't want to come across this event again -- vl
338 if (te
->event_ctx
->last_zero_timer
== te
) {
339 te
->event_ctx
->last_zero_timer
= DLIST_PREV(te
);
341 DLIST_REMOVE(te
->event_ctx
->timer_events
, te
);
343 tevent_debug(te
->event_ctx
, TEVENT_DEBUG_TRACE
,
344 "Running timer event %p \"%s\"\n",
345 te
, te
->handler_name
);
348 * If the timed event was registered for a zero current_time,
349 * then we pass a zero timeval here too! To avoid the
350 * overhead of gettimeofday() calls.
352 * otherwise we pass the current time
355 if (te
->wrapper
!= NULL
) {
356 handler_ev
= te
->wrapper
->wrap_ev
;
358 tevent_wrapper_push_use_internal(handler_ev
, te
->wrapper
);
359 te
->wrapper
->ops
->before_timer_handler(
360 te
->wrapper
->wrap_ev
,
361 te
->wrapper
->private_state
,
362 te
->wrapper
->main_ev
,
369 te
->handler(handler_ev
, te
, current_time
, te
->private_data
);
370 if (te
->wrapper
!= NULL
) {
371 te
->wrapper
->ops
->after_timer_handler(
372 te
->wrapper
->wrap_ev
,
373 te
->wrapper
->private_state
,
374 te
->wrapper
->main_ev
,
380 tevent_wrapper_pop_use_internal(handler_ev
, te
->wrapper
);
384 tevent_debug(te
->event_ctx
, TEVENT_DEBUG_TRACE
,
385 "Ending timer event %p \"%s\"\n",
386 te
, te
->handler_name
);
389 te
->event_ctx
= NULL
;
390 talloc_set_destructor(te
, NULL
);
393 if (removed
!= NULL
) {
400 do a single event loop using the events defined in ev
402 return the delay until the next timed event,
403 or zero if a timed event was triggered
405 struct timeval
tevent_common_loop_timer_delay(struct tevent_context
*ev
)
407 struct timeval current_time
= tevent_timeval_zero();
408 struct tevent_timer
*te
= ev
->timer_events
;
412 /* have a default tick time of 30 seconds. This guarantees
413 that code that uses its own timeout checking will be
414 able to proceed eventually */
415 return tevent_timeval_set(30, 0);
419 * work out the right timeout for the next timed event
421 * avoid the syscall to gettimeofday() if the timed event should
422 * be triggered directly
424 * if there's a delay till the next timed event, we're done
425 * with just returning the delay
427 if (!tevent_timeval_is_zero(&te
->next_event
)) {
428 struct timeval delay
;
430 current_time
= tevent_timeval_current();
432 delay
= tevent_timeval_until(¤t_time
, &te
->next_event
);
433 if (!tevent_timeval_is_zero(&delay
)) {
439 * ok, we have a timed event that we'll process ...
441 ret
= tevent_common_invoke_timer_handler(te
, current_time
, NULL
);
443 tevent_abort(ev
, "tevent_common_invoke_timer_handler() failed");
446 return tevent_timeval_zero();