mirror of
https://github.com/samba-team/samba.git
synced 2025-02-03 13:47:25 +03:00
6c6c5927a1
- if someone adds a timed_event with a zero timeval we now avoid serval gettimeofday() calls and the event handler doesn't get the current time when it's called, instead we also pass a zero timeval - this also makes sure multiple timed events with a zero timeval are processed in the order there're added. the little benchmark shows that processing 2000000 directly timed events is now much faster, while avoiding syscalls at all! > time ./evtest (with the old code) real 0m6.388s user 0m1.740s sys 0m4.632s > time ./evtest (with the new code) real 0m1.498s user 0m1.496s sys 0m0.004s metze@SERNOX:~/devel/samba/4.0/samba4-ci/source> cat evtest.c #include <stdio.h> #include <stdint.h> #include <sys/time.h> #include <talloc.h> #include <events.h> static void dummy_fde_handler(struct event_context *ev_ctx, struct fd_event *fde, uint16_t flags, void *private_data) { } static void timeout_handler(struct event_context *ev, struct timed_event *te, struct timeval tval, void *private_data) { uint32_t *countp = (uint32_t *)private_data; (*countp)++; if (*countp > 2000000) exit(0); event_add_timed(ev, ev, tval, timeout_handler, countp); } int main(void) { struct event_context *ev; struct timeval tval = { 0, 0 }; uint32_t count = 0; ev = event_context_init(NULL); event_add_fd(ev, ev, 0, 0, dummy_fde_handler, NULL); event_add_timed(ev, ev, tval, timeout_handler, &count); return event_loop_wait(ev); } (This used to be commit 4db64b4ce2320b88d648078cbf86385f6fb44f1f)
153 lines
4.1 KiB
C
153 lines
4.1 KiB
C
/*
|
|
Unix SMB/CIFS implementation.
|
|
|
|
common events code for timed events
|
|
|
|
Copyright (C) Andrew Tridgell 2003-2006
|
|
Copyright (C) Stefan Metzmacher 2005
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
*/
|
|
|
|
#include "includes.h"
|
|
#include "system/filesys.h"
|
|
#include "system/select.h"
|
|
#include "lib/util/dlinklist.h"
|
|
#include "lib/events/events.h"
|
|
#include "lib/events/events_internal.h"
|
|
|
|
/*
|
|
destroy a timed event
|
|
*/
|
|
static int common_event_timed_destructor(struct timed_event *te)
|
|
{
|
|
struct event_context *ev = talloc_get_type(te->event_ctx,
|
|
struct event_context);
|
|
DLIST_REMOVE(ev->timed_events, te);
|
|
return 0;
|
|
}
|
|
|
|
static int common_event_timed_deny_destructor(struct timed_event *te)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
/*
|
|
add a timed event
|
|
return NULL on failure (memory allocation error)
|
|
*/
|
|
struct timed_event *common_event_add_timed(struct event_context *ev, TALLOC_CTX *mem_ctx,
|
|
struct timeval next_event,
|
|
event_timed_handler_t handler,
|
|
void *private_data)
|
|
{
|
|
struct timed_event *te, *last_te, *cur_te;
|
|
|
|
te = talloc(mem_ctx?mem_ctx:ev, struct timed_event);
|
|
if (te == NULL) return NULL;
|
|
|
|
te->event_ctx = ev;
|
|
te->next_event = next_event;
|
|
te->handler = handler;
|
|
te->private_data = private_data;
|
|
te->additional_data = NULL;
|
|
|
|
/* keep the list ordered */
|
|
last_te = NULL;
|
|
for (cur_te = ev->timed_events; cur_te; cur_te = cur_te->next) {
|
|
/* if the new event comes before the current one break */
|
|
if (timeval_compare(&te->next_event, &cur_te->next_event) < 0) {
|
|
break;
|
|
}
|
|
|
|
last_te = cur_te;
|
|
}
|
|
|
|
DLIST_ADD_AFTER(ev->timed_events, te, last_te);
|
|
|
|
talloc_set_destructor(te, common_event_timed_destructor);
|
|
|
|
return te;
|
|
}
|
|
|
|
/*
|
|
do a single event loop using the events defined in ev
|
|
|
|
return the delay untill the next timed event,
|
|
or zero if a timed event was triggered
|
|
*/
|
|
struct timeval common_event_loop_timer_delay(struct event_context *ev)
|
|
{
|
|
struct timeval current_time = timeval_zero();
|
|
struct timed_event *te = ev->timed_events;
|
|
|
|
if (!te) {
|
|
/* have a default tick time of 30 seconds. This guarantees
|
|
that code that uses its own timeout checking will be
|
|
able to proceeed eventually */
|
|
return 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 (!timeval_is_zero(&te->next_event)) {
|
|
struct timeval delay;
|
|
|
|
current_time = timeval_current();
|
|
|
|
delay = timeval_until(¤t_time, &te->next_event);
|
|
if (!timeval_is_zero(&delay)) {
|
|
return delay;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* ok, we have a timed event that we'll process ...
|
|
*/
|
|
|
|
/* deny the handler to free the event */
|
|
talloc_set_destructor(te, common_event_timed_deny_destructor);
|
|
|
|
/* 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 */
|
|
DLIST_REMOVE(ev->timed_events, te);
|
|
|
|
/*
|
|
* 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->handler(ev, te, current_time, te->private_data);
|
|
|
|
/* The destructor isn't necessary anymore, we've already removed the
|
|
* event from the list. */
|
|
talloc_set_destructor(te, NULL);
|
|
|
|
talloc_free(te);
|
|
|
|
return timeval_zero();
|
|
}
|
|
|