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
);
154 DLIST_REMOVE(te
->event_ctx
->timer_events
, te
);
156 te
->event_ctx
= NULL
;
166 static void tevent_common_insert_timer(struct tevent_context
*ev
,
167 struct tevent_timer
*te
,
170 struct tevent_timer
*prev_te
= NULL
;
173 tevent_abort(ev
, "tevent_timer use after free");
177 /* keep the list ordered */
178 if (optimize_zero
&& tevent_timeval_is_zero(&te
->next_event
)) {
180 * Some callers use zero tevent_timer
181 * instead of tevent_immediate events.
183 * As these can happen very often,
184 * we remember the last zero timer
187 prev_te
= ev
->last_zero_timer
;
188 ev
->last_zero_timer
= te
;
190 struct tevent_timer
*cur_te
;
193 * we traverse the list from the tail
194 * because it's much more likely that
195 * timers are added at the end of the list
197 for (cur_te
= DLIST_TAIL(ev
->timer_events
);
199 cur_te
= DLIST_PREV(cur_te
))
204 * if the new event comes before the current
205 * we continue searching
207 ret
= tevent_timeval_compare(&te
->next_event
,
208 &cur_te
->next_event
);
219 DLIST_ADD_AFTER(ev
->timer_events
, te
, prev_te
);
224 return NULL on failure (memory allocation error)
226 static struct tevent_timer
*tevent_common_add_timer_internal(
227 struct tevent_context
*ev
,
229 struct timeval next_event
,
230 tevent_timer_handler_t handler
,
232 const char *handler_name
,
233 const char *location
,
236 struct tevent_timer
*te
;
238 te
= talloc(mem_ctx
?mem_ctx
:ev
, struct tevent_timer
);
239 if (te
== NULL
) return NULL
;
241 *te
= (struct tevent_timer
) {
243 .next_event
= next_event
,
245 .private_data
= private_data
,
246 .handler_name
= handler_name
,
247 .location
= location
,
250 if (ev
->timer_events
== NULL
) {
251 ev
->last_zero_timer
= NULL
;
254 tevent_common_insert_timer(ev
, te
, optimize_zero
);
256 talloc_set_destructor(te
, tevent_common_timed_destructor
);
258 tevent_debug(ev
, TEVENT_DEBUG_TRACE
,
259 "Added timed event \"%s\": %p\n",
264 struct tevent_timer
*tevent_common_add_timer(struct tevent_context
*ev
,
266 struct timeval next_event
,
267 tevent_timer_handler_t handler
,
269 const char *handler_name
,
270 const char *location
)
273 * do not use optimization, there are broken Samba
274 * versions which use tevent_common_add_timer()
275 * without using tevent_common_loop_timer_delay(),
276 * it just uses DLIST_REMOVE(ev->timer_events, te)
277 * and would leave ev->last_zero_timer behind.
279 return tevent_common_add_timer_internal(ev
, mem_ctx
, next_event
,
280 handler
, private_data
,
281 handler_name
, location
,
285 struct tevent_timer
*tevent_common_add_timer_v2(struct tevent_context
*ev
,
287 struct timeval next_event
,
288 tevent_timer_handler_t handler
,
290 const char *handler_name
,
291 const char *location
)
294 * Here we turn on last_zero_timer optimization
296 return tevent_common_add_timer_internal(ev
, mem_ctx
, next_event
,
297 handler
, private_data
,
298 handler_name
, location
,
302 void tevent_update_timer(struct tevent_timer
*te
, struct timeval next_event
)
304 struct tevent_context
*ev
= te
->event_ctx
;
306 if (ev
->last_zero_timer
== te
) {
307 te
->event_ctx
->last_zero_timer
= DLIST_PREV(te
);
309 DLIST_REMOVE(ev
->timer_events
, te
);
311 te
->next_event
= next_event
;
314 * Not doing the zero_timer optimization. This is for new code
315 * that should know about immediates.
317 tevent_common_insert_timer(ev
, te
, false);
320 int tevent_common_invoke_timer_handler(struct tevent_timer
*te
,
321 struct timeval current_time
,
324 struct tevent_context
*handler_ev
= te
->event_ctx
;
326 if (removed
!= NULL
) {
330 if (te
->event_ctx
== NULL
) {
335 * We need to remove the timer from the list before calling the
336 * handler because in a semi-async inner event loop called from the
337 * handler we don't want to come across this event again -- vl
339 if (te
->event_ctx
->last_zero_timer
== te
) {
340 te
->event_ctx
->last_zero_timer
= DLIST_PREV(te
);
342 DLIST_REMOVE(te
->event_ctx
->timer_events
, te
);
344 tevent_debug(te
->event_ctx
, TEVENT_DEBUG_TRACE
,
345 "Running timer event %p \"%s\"\n",
346 te
, te
->handler_name
);
349 * If the timed event was registered for a zero current_time,
350 * then we pass a zero timeval here too! To avoid the
351 * overhead of gettimeofday() calls.
353 * otherwise we pass the current time
356 if (te
->wrapper
!= NULL
) {
357 handler_ev
= te
->wrapper
->wrap_ev
;
359 tevent_wrapper_push_use_internal(handler_ev
, te
->wrapper
);
360 te
->wrapper
->ops
->before_timer_handler(
361 te
->wrapper
->wrap_ev
,
362 te
->wrapper
->private_state
,
363 te
->wrapper
->main_ev
,
370 te
->handler(handler_ev
, te
, current_time
, te
->private_data
);
371 if (te
->wrapper
!= NULL
) {
372 te
->wrapper
->ops
->after_timer_handler(
373 te
->wrapper
->wrap_ev
,
374 te
->wrapper
->private_state
,
375 te
->wrapper
->main_ev
,
381 tevent_wrapper_pop_use_internal(handler_ev
, te
->wrapper
);
385 tevent_debug(te
->event_ctx
, TEVENT_DEBUG_TRACE
,
386 "Ending timer event %p \"%s\"\n",
387 te
, te
->handler_name
);
390 te
->event_ctx
= NULL
;
391 talloc_set_destructor(te
, NULL
);
394 if (removed
!= NULL
) {
401 do a single event loop using the events defined in ev
403 return the delay until the next timed event,
404 or zero if a timed event was triggered
406 struct timeval
tevent_common_loop_timer_delay(struct tevent_context
*ev
)
408 struct timeval current_time
= tevent_timeval_zero();
409 struct tevent_timer
*te
= ev
->timer_events
;
413 /* have a default tick time of 30 seconds. This guarantees
414 that code that uses its own timeout checking will be
415 able to proceed eventually */
416 return tevent_timeval_set(30, 0);
420 * work out the right timeout for the next timed event
422 * avoid the syscall to gettimeofday() if the timed event should
423 * be triggered directly
425 * if there's a delay till the next timed event, we're done
426 * with just returning the delay
428 if (!tevent_timeval_is_zero(&te
->next_event
)) {
429 struct timeval delay
;
431 current_time
= tevent_timeval_current();
433 delay
= tevent_timeval_until(¤t_time
, &te
->next_event
);
434 if (!tevent_timeval_is_zero(&delay
)) {
440 * ok, we have a timed event that we'll process ...
442 ret
= tevent_common_invoke_timer_handler(te
, current_time
, NULL
);
444 tevent_abort(ev
, "tevent_common_invoke_timer_handler() failed");
447 return tevent_timeval_zero();