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"
29 #define TEVENT_DEPRECATED 1
31 #include "tevent_internal.h"
32 #include "tevent_util.h"
35 compare two timeval structures.
36 Return -1 if tv1 < tv2
37 Return 0 if tv1 == tv2
40 int tevent_timeval_compare(const struct timeval
*tv1
, const struct timeval
*tv2
)
42 if (tv1
->tv_sec
> tv2
->tv_sec
) return 1;
43 if (tv1
->tv_sec
< tv2
->tv_sec
) return -1;
44 if (tv1
->tv_usec
> tv2
->tv_usec
) return 1;
45 if (tv1
->tv_usec
< tv2
->tv_usec
) return -1;
52 struct timeval
tevent_timeval_zero(void)
61 return a timeval for the current time
63 struct timeval
tevent_timeval_current(void)
66 gettimeofday(&tv
, NULL
);
71 return a timeval struct with the given elements
73 struct timeval
tevent_timeval_set(uint32_t secs
, uint32_t usecs
)
82 return the difference between two timevals as a timeval
83 if tv1 comes after tv2, then return a zero timeval
86 struct timeval
tevent_timeval_until(const struct timeval
*tv1
,
87 const struct timeval
*tv2
)
90 if (tevent_timeval_compare(tv1
, tv2
) >= 0) {
91 return tevent_timeval_zero();
93 t
.tv_sec
= tv2
->tv_sec
- tv1
->tv_sec
;
94 if (tv1
->tv_usec
> tv2
->tv_usec
) {
96 t
.tv_usec
= 1000000 - (tv1
->tv_usec
- tv2
->tv_usec
);
98 t
.tv_usec
= tv2
->tv_usec
- tv1
->tv_usec
;
104 return true if a timeval is zero
106 bool tevent_timeval_is_zero(const struct timeval
*tv
)
108 return tv
->tv_sec
== 0 && tv
->tv_usec
== 0;
111 struct timeval
tevent_timeval_add(const struct timeval
*tv
, uint32_t secs
,
114 struct timeval tv2
= *tv
;
116 tv2
.tv_usec
+= usecs
;
117 tv2
.tv_sec
+= tv2
.tv_usec
/ 1000000;
118 tv2
.tv_usec
= tv2
.tv_usec
% 1000000;
124 return a timeval in the future with a specified offset
126 struct timeval
tevent_timeval_current_ofs(uint32_t secs
, uint32_t usecs
)
128 struct timeval tv
= tevent_timeval_current();
129 return tevent_timeval_add(&tv
, secs
, usecs
);
133 destroy a timed event
135 static int tevent_common_timed_destructor(struct tevent_timer
*te
)
138 tevent_common_check_double_free(te
, "tevent_timer double free");
141 te
->destroyed
= true;
143 if (te
->event_ctx
== NULL
) {
147 TEVENT_DEBUG(te
->event_ctx
, TEVENT_DEBUG_TRACE
,
148 "Destroying timer event %p \"%s\"\n",
149 te
, te
->handler_name
);
151 if (te
->event_ctx
->last_zero_timer
== te
) {
152 te
->event_ctx
->last_zero_timer
= DLIST_PREV(te
);
155 tevent_trace_timer_callback(te
->event_ctx
, te
, TEVENT_EVENT_TRACE_DETACH
);
156 DLIST_REMOVE(te
->event_ctx
->timer_events
, te
);
158 te
->event_ctx
= NULL
;
168 static void tevent_common_insert_timer(struct tevent_context
*ev
,
169 struct tevent_timer
*te
,
172 struct tevent_timer
*prev_te
= NULL
;
175 tevent_abort(ev
, "tevent_timer use after free");
179 /* keep the list ordered */
180 if (optimize_zero
&& tevent_timeval_is_zero(&te
->next_event
)) {
182 * Some callers use zero tevent_timer
183 * instead of tevent_immediate events.
185 * As these can happen very often,
186 * we remember the last zero timer
189 prev_te
= ev
->last_zero_timer
;
190 ev
->last_zero_timer
= te
;
192 struct tevent_timer
*cur_te
;
195 * we traverse the list from the tail
196 * because it's much more likely that
197 * timers are added at the end of the list
199 for (cur_te
= DLIST_TAIL(ev
->timer_events
);
201 cur_te
= DLIST_PREV(cur_te
))
206 * if the new event comes before the current
207 * we continue searching
209 ret
= tevent_timeval_compare(&te
->next_event
,
210 &cur_te
->next_event
);
221 tevent_trace_timer_callback(te
->event_ctx
, te
, TEVENT_EVENT_TRACE_ATTACH
);
222 DLIST_ADD_AFTER(ev
->timer_events
, te
, prev_te
);
227 return NULL on failure (memory allocation error)
229 static struct tevent_timer
*tevent_common_add_timer_internal(
230 struct tevent_context
*ev
,
232 struct timeval next_event
,
233 tevent_timer_handler_t handler
,
235 const char *handler_name
,
236 const char *location
,
239 struct tevent_timer
*te
;
241 te
= talloc(mem_ctx
?mem_ctx
:ev
, struct tevent_timer
);
242 if (te
== NULL
) return NULL
;
244 *te
= (struct tevent_timer
) {
246 .next_event
= next_event
,
248 .private_data
= private_data
,
249 .handler_name
= handler_name
,
250 .location
= location
,
253 if (ev
->timer_events
== NULL
) {
254 ev
->last_zero_timer
= NULL
;
257 tevent_common_insert_timer(ev
, te
, optimize_zero
);
259 talloc_set_destructor(te
, tevent_common_timed_destructor
);
262 TEVENT_DEBUG(ev
, TEVENT_DEBUG_TRACE
,
263 "Added timed event \"%s\": %p\n",
268 struct tevent_timer
*tevent_common_add_timer(struct tevent_context
*ev
,
270 struct timeval next_event
,
271 tevent_timer_handler_t handler
,
273 const char *handler_name
,
274 const char *location
)
277 * do not use optimization, there are broken Samba
278 * versions which use tevent_common_add_timer()
279 * without using tevent_common_loop_timer_delay(),
280 * it just uses DLIST_REMOVE(ev->timer_events, te)
281 * and would leave ev->last_zero_timer behind.
283 return tevent_common_add_timer_internal(ev
, mem_ctx
, next_event
,
284 handler
, private_data
,
285 handler_name
, location
,
289 struct tevent_timer
*tevent_common_add_timer_v2(struct tevent_context
*ev
,
291 struct timeval next_event
,
292 tevent_timer_handler_t handler
,
294 const char *handler_name
,
295 const char *location
)
298 * Here we turn on last_zero_timer optimization
300 return tevent_common_add_timer_internal(ev
, mem_ctx
, next_event
,
301 handler
, private_data
,
302 handler_name
, location
,
306 void tevent_update_timer(struct tevent_timer
*te
, struct timeval next_event
)
308 struct tevent_context
*ev
= te
->event_ctx
;
310 if (ev
->last_zero_timer
== te
) {
311 te
->event_ctx
->last_zero_timer
= DLIST_PREV(te
);
313 tevent_trace_timer_callback(te
->event_ctx
, te
, TEVENT_EVENT_TRACE_DETACH
);
314 DLIST_REMOVE(ev
->timer_events
, te
);
316 te
->next_event
= next_event
;
319 * Not doing the zero_timer optimization. This is for new code
320 * that should know about immediates.
322 tevent_common_insert_timer(ev
, te
, false);
325 int tevent_common_invoke_timer_handler(struct tevent_timer
*te
,
326 struct timeval current_time
,
329 struct tevent_context
*handler_ev
= te
->event_ctx
;
331 if (removed
!= NULL
) {
335 if (te
->event_ctx
== NULL
) {
340 * We need to remove the timer from the list before calling the
341 * handler because in a semi-async inner event loop called from the
342 * handler we don't want to come across this event again -- vl
344 if (te
->event_ctx
->last_zero_timer
== te
) {
345 te
->event_ctx
->last_zero_timer
= DLIST_PREV(te
);
347 DLIST_REMOVE(te
->event_ctx
->timer_events
, te
);
349 TEVENT_DEBUG(te
->event_ctx
, TEVENT_DEBUG_TRACE
,
350 "Running timer event %p \"%s\"\n",
351 te
, te
->handler_name
);
354 * If the timed event was registered for a zero current_time,
355 * then we pass a zero timeval here too! To avoid the
356 * overhead of gettimeofday() calls.
358 * otherwise we pass the current time
361 if (te
->wrapper
!= NULL
) {
362 handler_ev
= te
->wrapper
->wrap_ev
;
364 tevent_wrapper_push_use_internal(handler_ev
, te
->wrapper
);
365 te
->wrapper
->ops
->before_timer_handler(
366 te
->wrapper
->wrap_ev
,
367 te
->wrapper
->private_state
,
368 te
->wrapper
->main_ev
,
375 tevent_trace_timer_callback(te
->event_ctx
, te
, TEVENT_EVENT_TRACE_BEFORE_HANDLER
);
376 te
->handler(handler_ev
, te
, current_time
, te
->private_data
);
377 if (te
->wrapper
!= NULL
) {
378 te
->wrapper
->ops
->after_timer_handler(
379 te
->wrapper
->wrap_ev
,
380 te
->wrapper
->private_state
,
381 te
->wrapper
->main_ev
,
387 tevent_wrapper_pop_use_internal(handler_ev
, te
->wrapper
);
391 TEVENT_DEBUG(te
->event_ctx
, TEVENT_DEBUG_TRACE
,
392 "Ending timer event %p \"%s\"\n",
393 te
, te
->handler_name
);
395 /* The callback was already called when freed from the handler. */
396 if (!te
->destroyed
) {
397 tevent_trace_timer_callback(te
->event_ctx
, te
, TEVENT_EVENT_TRACE_DETACH
);
401 te
->event_ctx
= NULL
;
402 talloc_set_destructor(te
, NULL
);
405 if (removed
!= NULL
) {
412 do a single event loop using the events defined in ev
414 return the delay until the next timed event,
415 or zero if a timed event was triggered
417 struct timeval
tevent_common_loop_timer_delay(struct tevent_context
*ev
)
419 struct timeval current_time
= tevent_timeval_zero();
420 struct tevent_timer
*te
= ev
->timer_events
;
424 /* have a default tick time of 30 seconds. This guarantees
425 that code that uses its own timeout checking will be
426 able to proceed eventually */
427 return tevent_timeval_set(30, 0);
431 * work out the right timeout for the next timed event
433 * avoid the syscall to gettimeofday() if the timed event should
434 * be triggered directly
436 * if there's a delay till the next timed event, we're done
437 * with just returning the delay
439 if (!tevent_timeval_is_zero(&te
->next_event
)) {
440 struct timeval delay
;
442 current_time
= tevent_timeval_current();
444 delay
= tevent_timeval_until(¤t_time
, &te
->next_event
);
445 if (!tevent_timeval_is_zero(&delay
)) {
451 * ok, we have a timed event that we'll process ...
453 ret
= tevent_common_invoke_timer_handler(te
, current_time
, NULL
);
455 tevent_abort(ev
, "tevent_common_invoke_timer_handler() failed");
458 return tevent_timeval_zero();
461 void tevent_timer_set_tag(struct tevent_timer
*te
, uint64_t tag
)
470 uint64_t tevent_timer_get_tag(const struct tevent_timer
*te
)