librpc: Remove unused RPC_NDR_SCERPC
[Samba.git] / lib / tevent / tevent_timed.c
bloba78d286a18714444f4327437d28c462441a2e392
1 /*
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
11 ** under the LGPL
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/>.
27 #include "replace.h"
28 #include "system/time.h"
29 #define TEVENT_DEPRECATED 1
30 #include "tevent.h"
31 #include "tevent_internal.h"
32 #include "tevent_util.h"
34 /**
35 compare two timeval structures.
36 Return -1 if tv1 < tv2
37 Return 0 if tv1 == tv2
38 Return 1 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;
46 return 0;
49 /**
50 return a zero timeval
52 struct timeval tevent_timeval_zero(void)
54 struct timeval tv;
55 tv.tv_sec = 0;
56 tv.tv_usec = 0;
57 return tv;
60 /**
61 return a timeval for the current time
63 struct timeval tevent_timeval_current(void)
65 struct timeval tv;
66 gettimeofday(&tv, NULL);
67 return tv;
70 /**
71 return a timeval struct with the given elements
73 struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs)
75 struct timeval tv;
76 tv.tv_sec = secs;
77 tv.tv_usec = usecs;
78 return tv;
81 /**
82 return the difference between two timevals as a timeval
83 if tv1 comes after tv2, then return a zero timeval
84 (this is *tv2 - *tv1)
86 struct timeval tevent_timeval_until(const struct timeval *tv1,
87 const struct timeval *tv2)
89 struct timeval t;
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) {
95 t.tv_sec--;
96 t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
97 } else {
98 t.tv_usec = tv2->tv_usec - tv1->tv_usec;
100 return t;
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,
112 uint32_t usecs)
114 struct timeval tv2 = *tv;
115 tv2.tv_sec += secs;
116 tv2.tv_usec += usecs;
117 tv2.tv_sec += tv2.tv_usec / 1000000;
118 tv2.tv_usec = tv2.tv_usec % 1000000;
120 return tv2;
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)
137 if (te->destroyed) {
138 tevent_common_check_double_free(te, "tevent_timer double free");
139 goto done;
141 te->destroyed = true;
143 if (te->event_ctx == NULL) {
144 return 0;
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;
157 done:
158 if (te->busy) {
159 return -1;
161 te->wrapper = NULL;
163 return 0;
166 static void tevent_common_insert_timer(struct tevent_context *ev,
167 struct tevent_timer *te,
168 bool optimize_zero)
170 struct tevent_timer *prev_te = NULL;
172 if (te->destroyed) {
173 tevent_abort(ev, "tevent_timer use after free");
174 return;
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
185 * in the list.
187 prev_te = ev->last_zero_timer;
188 ev->last_zero_timer = te;
189 } else {
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);
198 cur_te != NULL;
199 cur_te = DLIST_PREV(cur_te))
201 int ret;
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);
209 if (ret < 0) {
210 continue;
213 break;
216 prev_te = cur_te;
219 DLIST_ADD_AFTER(ev->timer_events, te, prev_te);
223 add a timed event
224 return NULL on failure (memory allocation error)
226 static struct tevent_timer *tevent_common_add_timer_internal(
227 struct tevent_context *ev,
228 TALLOC_CTX *mem_ctx,
229 struct timeval next_event,
230 tevent_timer_handler_t handler,
231 void *private_data,
232 const char *handler_name,
233 const char *location,
234 bool optimize_zero)
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) {
242 .event_ctx = ev,
243 .next_event = next_event,
244 .handler = handler,
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",
260 handler_name, te);
261 return te;
264 struct tevent_timer *tevent_common_add_timer(struct tevent_context *ev,
265 TALLOC_CTX *mem_ctx,
266 struct timeval next_event,
267 tevent_timer_handler_t handler,
268 void *private_data,
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,
282 false);
285 struct tevent_timer *tevent_common_add_timer_v2(struct tevent_context *ev,
286 TALLOC_CTX *mem_ctx,
287 struct timeval next_event,
288 tevent_timer_handler_t handler,
289 void *private_data,
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,
299 true);
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,
322 bool *removed)
324 struct tevent_context *handler_ev = te->event_ctx;
326 if (removed != NULL) {
327 *removed = false;
330 if (te->event_ctx == NULL) {
331 return 0;
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
355 te->busy = true;
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,
365 te->next_event,
366 current_time,
367 te->handler_name,
368 te->location);
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,
377 te->next_event,
378 current_time,
379 te->handler_name,
380 te->location);
381 tevent_wrapper_pop_use_internal(handler_ev, te->wrapper);
383 te->busy = false;
385 tevent_debug(te->event_ctx, TEVENT_DEBUG_TRACE,
386 "Ending timer event %p \"%s\"\n",
387 te, te->handler_name);
389 te->wrapper = NULL;
390 te->event_ctx = NULL;
391 talloc_set_destructor(te, NULL);
392 TALLOC_FREE(te);
394 if (removed != NULL) {
395 *removed = true;
398 return 0;
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;
410 int ret;
412 if (!te) {
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(&current_time, &te->next_event);
434 if (!tevent_timeval_is_zero(&delay)) {
435 return delay;
440 * ok, we have a timed event that we'll process ...
442 ret = tevent_common_invoke_timer_handler(te, current_time, NULL);
443 if (ret != 0) {
444 tevent_abort(ev, "tevent_common_invoke_timer_handler() failed");
447 return tevent_timeval_zero();