summaryrefslogtreecommitdiffstats
path: root/lib/tevent/tevent_timed.c
blob: 67e39ed0dd7424c7160f6a052afaee2f3499a08d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
   Unix SMB/CIFS implementation.

   common events code for timed events

   Copyright (C) Andrew Tridgell	2003-2006
   Copyright (C) Stefan Metzmacher	2005-2009

     ** NOTE! The following LGPL license applies to the tevent
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"
#include "system/time.h"
#define TEVENT_DEPRECATED 1
#include "tevent.h"
#include "tevent_internal.h"
#include "tevent_util.h"

/**
  compare two timeval structures.
  Return -1 if tv1 < tv2
  Return 0 if tv1 == tv2
  Return 1 if tv1 > tv2
*/
int tevent_timeval_compare(const struct timeval *tv1, const struct timeval *tv2)
{
	if (tv1->tv_sec  > tv2->tv_sec)  return 1;
	if (tv1->tv_sec  < tv2->tv_sec)  return -1;
	if (tv1->tv_usec > tv2->tv_usec) return 1;
	if (tv1->tv_usec < tv2->tv_usec) return -1;
	return 0;
}

/**
  return a zero timeval
*/
struct timeval tevent_timeval_zero(void)
{
	struct timeval tv;
	tv.tv_sec = 0;
	tv.tv_usec = 0;
	return tv;
}

/**
  return a timeval for the current time
*/
struct timeval tevent_timeval_current(void)
{
	struct timeval tv;
	gettimeofday(&tv, NULL);
	return tv;
}

/**
  return a timeval struct with the given elements
*/
struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs)
{
	struct timeval tv;
	tv.tv_sec = secs;
	tv.tv_usec = usecs;
	return tv;
}

/**
  return the difference between two timevals as a timeval
  if tv1 comes after tv2, then return a zero timeval
  (this is *tv2 - *tv1)
*/
struct timeval tevent_timeval_until(const struct timeval *tv1,
				    const struct timeval *tv2)
{
	struct timeval t;
	if (tevent_timeval_compare(tv1, tv2) >= 0) {
		return tevent_timeval_zero();
	}
	t.tv_sec = tv2->tv_sec - tv1->tv_sec;
	if (tv1->tv_usec > tv2->tv_usec) {
		t.tv_sec--;
		t.tv_usec = 1000000 - (tv1->tv_usec - tv2->tv_usec);
	} else {
		t.tv_usec = tv2->tv_usec - tv1->tv_usec;
	}
	return t;
}

/**
  return true if a timeval is zero
*/
bool tevent_timeval_is_zero(const struct timeval *tv)
{
	return tv->tv_sec == 0 && tv->tv_usec == 0;
}

struct timeval tevent_timeval_add(const struct timeval *tv, uint32_t secs,
				  uint32_t usecs)
{
	struct timeval tv2 = *tv;
	tv2.tv_sec += secs;
	tv2.tv_usec += usecs;
	tv2.tv_sec += tv2.tv_usec / 1000000;
	tv2.tv_usec = tv2.tv_usec % 1000000;

	return tv2;
}

/**
  return a timeval in the future with a specified offset
*/
struct timeval tevent_timeval_current_ofs(uint32_t secs, uint32_t usecs)
{
	struct timeval tv = tevent_timeval_current();
	return tevent_timeval_add(&tv, secs, usecs);
}

/*
  destroy a timed event
*/
static int tevent_common_timed_destructor(struct tevent_timer *te)
{
	if (te->destroyed) {
		tevent_common_check_double_free(te, "tevent_timer double free");
		goto done;
	}
	te->destroyed = true;

	if (te->event_ctx == NULL) {
		return 0;
	}

	tevent_debug(te->event_ctx, TEVENT_DEBUG_TRACE,
		     "Destroying timer event %p \"%s\"\n",
		     te, te->handler_name);

	if (te->event_ctx->last_zero_timer == te) {
		te->event_ctx->last_zero_timer = DLIST_PREV(te);
	}

	tevent_trace_timer_callback(te->event_ctx, te, TEVENT_EVENT_TRACE_DETACH);
	DLIST_REMOVE(te->event_ctx->timer_events, te);

	te->event_ctx = NULL;
done:
	if (te->busy) {
		return -1;
	}
	te->wrapper = NULL;

	return 0;
}

static void tevent_common_insert_timer(struct tevent_context *ev,
				       struct tevent_timer *te,
				       bool optimize_zero)
{
	struct tevent_timer *prev_te = NULL;

	if (te->destroyed) {
		tevent_abort(ev, "tevent_timer use after free");
		return;
	}

	/* keep the list ordered */
	if (optimize_zero && tevent_timeval_is_zero(&te->next_event)) {
		/*
		 * Some callers use zero tevent_timer
		 * instead of tevent_immediate events.
		 *
		 * As these can happen very often,
		 * we remember the last zero timer
		 * in the list.
		 */
		prev_te = ev->last_zero_timer;
		ev->last_zero_timer = te;
	} else {
		struct tevent_timer *cur_te;

		/*
		 * we traverse the list from the tail
		 * because it's much more likely that
		 * timers are added at the end of the list
		 */
		for (cur_te = DLIST_TAIL(ev->timer_events);
		     cur_te != NULL;
		     cur_te = DLIST_PREV(cur_te))
		{
			int ret;

			/*
			 * if the new event comes before the current
			 * we continue searching
			 */
			ret = tevent_timeval_compare(&te->next_event,
						     &cur_te->next_event);
			if (ret < 0) {
				continue;
			}

			break;
		}

		prev_te = cur_te;
	}

	tevent_trace_timer_callback(te->event_ctx, te, TEVENT_EVENT_TRACE_ATTACH);
	DLIST_ADD_AFTER(ev->timer_events, te, prev_te);
}

/*
  add a timed event
  return NULL on failure (memory allocation error)
*/
static struct tevent_timer *tevent_common_add_timer_internal(
					struct tevent_context *ev,
					TALLOC_CTX *mem_ctx,
					struct timeval next_event,
					tevent_timer_handler_t handler,
					void *private_data,
					const char *handler_name,
					const char *location,
					bool optimize_zero)
{
	struct tevent_timer *te;

	te = talloc(mem_ctx?mem_ctx:ev, struct tevent_timer);
	if (te == NULL) return NULL;

	*te = (struct tevent_timer) {
		.event_ctx	= ev,
		.next_event	= next_event,
		.handler	= handler,
		.private_data	= private_data,
		.handler_name	= handler_name,
		.location	= location,
	};

	if (ev->timer_events == NULL) {
		ev->last_zero_timer = NULL;
	}

	tevent_common_insert_timer(ev, te, optimize_zero);

	talloc_set_destructor(te, tevent_common_timed_destructor);


	tevent_debug(ev, TEVENT_DEBUG_TRACE,
		     "Added timed event \"%s\": %p\n",
		     handler_name, te);
	return te;
}

struct tevent_timer *tevent_common_add_timer(struct tevent_context *ev,
					     TALLOC_CTX *mem_ctx,
					     struct timeval next_event,
					     tevent_timer_handler_t handler,
					     void *private_data,
					     const char *handler_name,
					     const char *location)
{
	/*
	 * do not use optimization, there are broken Samba
	 * versions which use tevent_common_add_timer()
	 * without using tevent_common_loop_timer_delay(),
	 * it just uses DLIST_REMOVE(ev->timer_events, te)
	 * and would leave ev->last_zero_timer behind.
	 */
	return tevent_common_add_timer_internal(ev, mem_ctx, next_event,
						handler, private_data,
						handler_name, location,
						false);
}

struct tevent_timer *tevent_common_add_timer_v2(struct tevent_context *ev,
						TALLOC_CTX *mem_ctx,
					        struct timeval next_event,
					        tevent_timer_handler_t handler,
					        void *private_data,
					        const char *handler_name,
					        const char *location)
{
	/*
	 * Here we turn on last_zero_timer optimization
	 */
	return tevent_common_add_timer_internal(ev, mem_ctx, next_event,
						handler, private_data,
						handler_name, location,
						true);
}

void tevent_update_timer(struct tevent_timer *te, struct timeval next_event)
{
	struct tevent_context *ev = te->event_ctx;

	if (ev->last_zero_timer == te) {
		te->event_ctx->last_zero_timer = DLIST_PREV(te);
	}
	tevent_trace_timer_callback(te->event_ctx, te, TEVENT_EVENT_TRACE_DETACH);
	DLIST_REMOVE(ev->timer_events, te);

	te->next_event = next_event;

	/*
	 * Not doing the zero_timer optimization. This is for new code
	 * that should know about immediates.
	 */
	tevent_common_insert_timer(ev, te, false);
}

int tevent_common_invoke_timer_handler(struct tevent_timer *te,
				       struct timeval current_time,
				       bool *removed)
{
	struct tevent_context *handler_ev = te->event_ctx;

	if (removed != NULL) {
		*removed = false;
	}

	if (te->event_ctx == NULL) {
		return 0;
	}

	/*
	 * We need to remove the timer from the list before calling the
	 * handler because in a semi-async inner event loop called from the
	 * handler we don't want to come across this event again -- vl
	 */
	if (te->event_ctx->last_zero_timer == te) {
		te->event_ctx->last_zero_timer = DLIST_PREV(te);
	}
	DLIST_REMOVE(te->event_ctx->timer_events, te);

	tevent_debug(te->event_ctx, TEVENT_DEBUG_TRACE,
		     "Running timer event %p \"%s\"\n",
		     te, te->handler_name);

	/*
	 * If the timed event was registered for a zero current_time,
	 * then we pass a zero timeval here too! To avoid the
	 * overhead of gettimeofday() calls.
	 *
	 * otherwise we pass the current time
	 */
	te->busy = true;
	if (te->wrapper != NULL) {
		handler_ev = te->wrapper->wrap_ev;

		tevent_wrapper_push_use_internal(handler_ev, te->wrapper);
		te->wrapper->ops->before_timer_handler(
					te->wrapper->wrap_ev,
					te->wrapper->private_state,
					te->wrapper->main_ev,
					te,
					te->next_event,
					current_time,
					te->handler_name,
					te->location);
	}
	tevent_trace_timer_callback(te->event_ctx, te, TEVENT_EVENT_TRACE_BEFORE_HANDLER);
	te->handler(handler_ev, te, current_time, te->private_data);
	if (te->wrapper != NULL) {
		te->wrapper->ops->after_timer_handler(
					te->wrapper->wrap_ev,
					te->wrapper->private_state,
					te->wrapper->main_ev,
					te,
					te->next_event,
					current_time,
					te->handler_name,
					te->location);
		tevent_wrapper_pop_use_internal(handler_ev, te->wrapper);
	}
	te->busy = false;

	tevent_debug(te->event_ctx, TEVENT_DEBUG_TRACE,
		     "Ending timer event %p \"%s\"\n",
		     te, te->handler_name);

	/* The callback was already called when freed from the handler. */
	if (!te->destroyed) {
		tevent_trace_timer_callback(te->event_ctx, te, TEVENT_EVENT_TRACE_DETACH);
	}

	te->wrapper = NULL;
	te->event_ctx = NULL;
	talloc_set_destructor(te, NULL);
	TALLOC_FREE(te);

	if (removed != NULL) {
		*removed = true;
	}

	return 0;
}
/*
  do a single event loop using the events defined in ev

  return the delay until the next timed event,
  or zero if a timed event was triggered
*/
struct timeval tevent_common_loop_timer_delay(struct tevent_context *ev)
{
	struct timeval current_time = tevent_timeval_zero();
	struct tevent_timer *te = ev->timer_events;
	int ret;

	if (!te) {
		/* have a default tick time of 30 seconds. This guarantees
		   that code that uses its own timeout checking will be
		   able to proceed eventually */
		return tevent_timeval_set(30, 0);
	}

	/*
	 * work out the right timeout for the next timed event
	 *
	 * avoid the syscall to gettimeofday() if the timed event should
	 * be triggered directly
	 *
	 * if there's a delay till the next timed event, we're done
	 * with just returning the delay
	 */
	if (!tevent_timeval_is_zero(&te->next_event)) {
		struct timeval delay;

		current_time = tevent_timeval_current();

		delay = tevent_timeval_until(&current_time, &te->next_event);
		if (!tevent_timeval_is_zero(&delay)) {
			return delay;
		}
	}

	/*
	 * ok, we have a timed event that we'll process ...
	 */
	ret = tevent_common_invoke_timer_handler(te, current_time, NULL);
	if (ret != 0) {
		tevent_abort(ev, "tevent_common_invoke_timer_handler() failed");
	}

	return tevent_timeval_zero();
}

void tevent_timer_set_tag(struct tevent_timer *te, uint64_t tag)
{
	if (te == NULL) {
		return;
	}

	te->tag = tag;
}

uint64_t tevent_timer_get_tag(const struct tevent_timer *te)
{
	if (te == NULL) {
		return 0;
	}

	return te->tag;
}