diff options
author | Jelmer Vernooij <jelmer@samba.org> | 2009-03-20 16:40:09 +0100 |
---|---|---|
committer | Jelmer Vernooij <jelmer@samba.org> | 2009-03-20 16:40:09 +0100 |
commit | ca202cf464aec82e63be4b2160f394f56b8c195e (patch) | |
tree | 58b432b7d67c5cf3b35a16c7df9b028d39e3b3a5 /lib/tevent | |
parent | 44787565715f0622cc1d049854427d735ca1c14b (diff) | |
parent | 2de464a7658f91d2d01087080b984d52c3483426 (diff) | |
download | samba-ca202cf464aec82e63be4b2160f394f56b8c195e.tar.gz samba-ca202cf464aec82e63be4b2160f394f56b8c195e.tar.bz2 samba-ca202cf464aec82e63be4b2160f394f56b8c195e.zip |
Merge branch 'master' of ssh://git.samba.org/data/git/samba into displaysec
Diffstat (limited to 'lib/tevent')
-rw-r--r-- | lib/tevent/configure.ac | 2 | ||||
-rw-r--r-- | lib/tevent/libtevent.m4 | 3 | ||||
-rw-r--r-- | lib/tevent/tevent.c | 93 | ||||
-rw-r--r-- | lib/tevent/tevent.h | 41 | ||||
-rw-r--r-- | lib/tevent/tevent_epoll.c | 62 | ||||
-rw-r--r-- | lib/tevent/tevent_immediate.c | 139 | ||||
-rw-r--r-- | lib/tevent/tevent_internal.h | 53 | ||||
-rw-r--r-- | lib/tevent/tevent_queue.c | 99 | ||||
-rw-r--r-- | lib/tevent/tevent_req.c | 74 | ||||
-rw-r--r-- | lib/tevent/tevent_select.c | 60 | ||||
-rw-r--r-- | lib/tevent/tevent_standard.c | 69 |
11 files changed, 480 insertions, 215 deletions
diff --git a/lib/tevent/configure.ac b/lib/tevent/configure.ac index 69f65c6df7..171a4088ba 100644 --- a/lib/tevent/configure.ac +++ b/lib/tevent/configure.ac @@ -1,5 +1,5 @@ AC_PREREQ(2.50) -AC_INIT(tevent, 0.9.4) +AC_INIT(tevent, 0.9.5) AC_CONFIG_SRCDIR([tevent.c]) AC_CONFIG_HEADER(config.h) diff --git a/lib/tevent/libtevent.m4 b/lib/tevent/libtevent.m4 index c316823a71..20730b17d6 100644 --- a/lib/tevent/libtevent.m4 +++ b/lib/tevent/libtevent.m4 @@ -26,7 +26,8 @@ AC_SUBST(TEVENT_LIBS) TEVENT_CFLAGS="-I$teventdir" -TEVENT_OBJ="tevent.o tevent_fd.o tevent_timed.o tevent_signal.o tevent_debug.o tevent_util.o" +TEVENT_OBJ="tevent.o tevent_debug.o tevent_util.o" +TEVENT_OBJ="$TEVENT_OBJ tevent_fd.o tevent_timed.o tevent_immediate.o tevent_signal.o" TEVENT_OBJ="$TEVENT_OBJ tevent_req.o tevent_wakeup.o tevent_queue.o" TEVENT_OBJ="$TEVENT_OBJ tevent_standard.o tevent_select.o" diff --git a/lib/tevent/tevent.c b/lib/tevent/tevent.c index 867cfc08fe..0c02e46f3c 100644 --- a/lib/tevent/tevent.c +++ b/lib/tevent/tevent.c @@ -143,6 +143,7 @@ int tevent_common_context_destructor(struct tevent_context *ev) { struct tevent_fd *fd, *fn; struct tevent_timer *te, *tn; + struct tevent_immediate *ie, *in; struct tevent_signal *se, *sn; if (ev->pipe_fde) { @@ -162,6 +163,13 @@ int tevent_common_context_destructor(struct tevent_context *ev) DLIST_REMOVE(ev->timer_events, te); } + for (ie = ev->immediate_events; ie; ie = in) { + in = ie->next; + ie->event_ctx = NULL; + ie->cancel_fn = NULL; + DLIST_REMOVE(ev->immediate_events, ie); + } + for (se = ev->signal_events; se; se = sn) { sn = se->next; se->event_ctx = NULL; @@ -350,6 +358,47 @@ struct tevent_timer *_tevent_add_timer(struct tevent_context *ev, } /* + allocate an immediate event + return NULL on failure (memory allocation error) +*/ +struct tevent_immediate *_tevent_create_immediate(TALLOC_CTX *mem_ctx, + const char *location) +{ + struct tevent_immediate *im; + + im = talloc(mem_ctx, struct tevent_immediate); + if (im == NULL) return NULL; + + im->prev = NULL; + im->next = NULL; + im->event_ctx = NULL; + im->create_location = location; + im->handler = NULL; + im->private_data = NULL; + im->handler_name = NULL; + im->schedule_location = NULL; + im->cancel_fn = NULL; + im->additional_data = NULL; + + return im; +} + +/* + schedule an immediate event + return NULL on failure +*/ +void _tevent_schedule_immediate(struct tevent_immediate *im, + struct tevent_context *ev, + tevent_immediate_handler_t handler, + void *private_data, + const char *handler_name, + const char *location) +{ + ev->ops->schedule_immediate(im, ev, handler, private_data, + handler_name, location); +} + +/* add a signal event sa_flags are flags to sigaction(2) @@ -378,6 +427,14 @@ void tevent_loop_set_nesting_hook(struct tevent_context *ev, tevent_nesting_hook hook, void *private_data) { + if (ev->nesting.hook_fn && + (ev->nesting.hook_fn != hook || + ev->nesting.hook_private != private_data)) { + /* the way the nesting hook code is currently written + we cannot support two different nesting hooks at the + same time. */ + tevent_abort(ev, "tevent: Violation of nesting hook rules\n"); + } ev->nesting.hook_fn = hook; ev->nesting.hook_private = private_data; } @@ -411,6 +468,8 @@ int _tevent_loop_once(struct tevent_context *ev, const char *location) errno = ELOOP; return -1; } + } + if (ev->nesting.level > 0) { if (ev->nesting.hook_fn) { int ret2; ret2 = ev->nesting.hook_fn(ev, @@ -428,7 +487,7 @@ int _tevent_loop_once(struct tevent_context *ev, const char *location) ret = ev->ops->loop_once(ev, location); - if (ev->nesting.level > 1) { + if (ev->nesting.level > 0) { if (ev->nesting.hook_fn) { int ret2; ret2 = ev->nesting.hook_fn(ev, @@ -468,6 +527,8 @@ int _tevent_loop_until(struct tevent_context *ev, errno = ELOOP; return -1; } + } + if (ev->nesting.level > 0) { if (ev->nesting.hook_fn) { int ret2; ret2 = ev->nesting.hook_fn(ev, @@ -490,7 +551,7 @@ int _tevent_loop_until(struct tevent_context *ev, } } - if (ev->nesting.level > 1) { + if (ev->nesting.level > 0) { if (ev->nesting.hook_fn) { int ret2; ret2 = ev->nesting.hook_fn(ev, @@ -514,6 +575,34 @@ done: /* return on failure or (with 0) if all fd events are removed */ +int tevent_common_loop_wait(struct tevent_context *ev, + const char *location) +{ + /* + * loop as long as we have events pending + */ + while (ev->fd_events || + ev->timer_events || + ev->immediate_events || + ev->signal_events) { + int ret; + ret = _tevent_loop_once(ev, location); + if (ret != 0) { + tevent_debug(ev, TEVENT_DEBUG_FATAL, + "_tevent_loop_once() failed: %d - %s\n", + ret, strerror(errno)); + return ret; + } + } + + tevent_debug(ev, TEVENT_DEBUG_WARNING, + "tevent_common_loop_wait() out of events\n"); + return 0; +} + +/* + return on failure or (with 0) if all fd events are removed +*/ int _tevent_loop_wait(struct tevent_context *ev, const char *location) { return ev->ops->loop_wait(ev, location); diff --git a/lib/tevent/tevent.h b/lib/tevent/tevent.h index 4a3f51ae5e..6c5df6321a 100644 --- a/lib/tevent/tevent.h +++ b/lib/tevent/tevent.h @@ -37,6 +37,7 @@ struct tevent_context; struct tevent_ops; struct tevent_fd; struct tevent_timer; +struct tevent_immediate; struct tevent_signal; /* event handler types */ @@ -52,6 +53,9 @@ typedef void (*tevent_timer_handler_t)(struct tevent_context *ev, struct tevent_timer *te, struct timeval current_time, void *private_data); +typedef void (*tevent_immediate_handler_t)(struct tevent_context *ctx, + struct tevent_immediate *im, + void *private_data); typedef void (*tevent_signal_handler_t)(struct tevent_context *ev, struct tevent_signal *se, int signum, @@ -87,6 +91,21 @@ struct tevent_timer *_tevent_add_timer(struct tevent_context *ev, _tevent_add_timer(ev, mem_ctx, next_event, handler, private_data, \ #handler, __location__) +struct tevent_immediate *_tevent_create_immediate(TALLOC_CTX *mem_ctx, + const char *location); +#define tevent_create_immediate(mem_ctx) \ + _tevent_create_immediate(mem_ctx, __location__) + +void _tevent_schedule_immediate(struct tevent_immediate *im, + struct tevent_context *ctx, + tevent_immediate_handler_t handler, + void *private_data, + const char *handler_name, + const char *location); +#define tevent_schedule_immediate(im, ctx, handler, private_data) \ + _tevent_schedule_immediate(im, ctx, handler, private_data, \ + #handler, __location__); + struct tevent_signal *_tevent_add_signal(struct tevent_context *ev, TALLOC_CTX *mem_ctx, int signum, @@ -233,13 +252,22 @@ bool tevent_req_set_endtime(struct tevent_req *req, struct tevent_context *ev, struct timeval endtime); -void tevent_req_done(struct tevent_req *req); +void _tevent_req_done(struct tevent_req *req, + const char *location); +#define tevent_req_done(req) \ + _tevent_req_done(req, __location__) -bool tevent_req_error(struct tevent_req *req, - uint64_t error); +bool _tevent_req_error(struct tevent_req *req, + uint64_t error, + const char *location); +#define tevent_req_error(req, error) \ + _tevent_req_error(req, error, __location__) -bool tevent_req_nomem(const void *p, - struct tevent_req *req); +bool _tevent_req_nomem(const void *p, + struct tevent_req *req, + const char *location); +#define tevent_req_nomem(p, req) \ + _tevent_req_nomem(p, req, __location__) struct tevent_req *tevent_req_post(struct tevent_req *req, struct tevent_context *ev); @@ -295,8 +323,7 @@ bool tevent_queue_add(struct tevent_queue *queue, struct tevent_req *req, tevent_queue_trigger_fn_t trigger, void *private_data); -bool tevent_queue_start(struct tevent_queue *queue, - struct tevent_context *ev); +void tevent_queue_start(struct tevent_queue *queue); void tevent_queue_stop(struct tevent_queue *queue); size_t tevent_queue_length(struct tevent_queue *queue); diff --git a/lib/tevent/tevent_epoll.c b/lib/tevent/tevent_epoll.c index b63d299d94..7c7f389d5b 100644 --- a/lib/tevent/tevent_epoll.c +++ b/lib/tevent/tevent_epoll.c @@ -35,14 +35,6 @@ struct epoll_event_context { /* a pointer back to the generic event_context */ struct tevent_context *ev; - /* this is changed by the destructors for the fd event - type. It is used to detect event destruction by event - handlers, which means the code that is calling the event - handler needs to assume that the linked list is no longer - valid - */ - uint32_t destruction_count; - /* when using epoll this is the handle from epoll_create */ int epoll_fd; @@ -242,9 +234,8 @@ static void epoll_change_event(struct epoll_event_context *epoll_ev, struct teve static int epoll_event_loop(struct epoll_event_context *epoll_ev, struct timeval *tvalp) { int ret, i; -#define MAXEVENTS 32 +#define MAXEVENTS 1 struct epoll_event events[MAXEVENTS]; - uint32_t destruction_count = ++epoll_ev->destruction_count; int timeout = -1; if (epoll_ev->epoll_fd == -1) return -1; @@ -305,9 +296,7 @@ static int epoll_event_loop(struct epoll_event_context *epoll_ev, struct timeval if (events[i].events & EPOLLOUT) flags |= TEVENT_FD_WRITE; if (flags) { fde->handler(epoll_ev->ev, fde, flags, fde->private_data); - if (destruction_count != epoll_ev->destruction_count) { - break; - } + break; } } @@ -351,8 +340,6 @@ static int epoll_event_fd_destructor(struct tevent_fd *fde) epoll_check_reopen(epoll_ev); - epoll_ev->destruction_count++; - epoll_del_event(epoll_ev, fde); } @@ -417,6 +404,16 @@ static int epoll_event_loop_once(struct tevent_context *ev, const char *location struct epoll_event_context); struct timeval tval; + if (ev->signal_events && + tevent_common_check_signal(ev)) { + return 0; + } + + if (ev->immediate_events && + tevent_common_loop_immediate(ev)) { + return 0; + } + tval = tevent_common_loop_timer_delay(ev); if (tevent_timeval_is_zero(&tval)) { return 0; @@ -427,32 +424,17 @@ static int epoll_event_loop_once(struct tevent_context *ev, const char *location return epoll_event_loop(epoll_ev, &tval); } -/* - return on failure or (with 0) if all fd events are removed -*/ -static int epoll_event_loop_wait(struct tevent_context *ev, const char *location) -{ - struct epoll_event_context *epoll_ev = talloc_get_type(ev->additional_data, - struct epoll_event_context); - while (epoll_ev->ev->fd_events) { - if (epoll_event_loop_once(ev, location) != 0) { - break; - } - } - - return 0; -} - static const struct tevent_ops epoll_event_ops = { - .context_init = epoll_event_context_init, - .add_fd = epoll_event_add_fd, - .set_fd_close_fn= tevent_common_fd_set_close_fn, - .get_fd_flags = tevent_common_fd_get_flags, - .set_fd_flags = epoll_event_set_fd_flags, - .add_timer = tevent_common_add_timer, - .add_signal = tevent_common_add_signal, - .loop_once = epoll_event_loop_once, - .loop_wait = epoll_event_loop_wait, + .context_init = epoll_event_context_init, + .add_fd = epoll_event_add_fd, + .set_fd_close_fn = tevent_common_fd_set_close_fn, + .get_fd_flags = tevent_common_fd_get_flags, + .set_fd_flags = epoll_event_set_fd_flags, + .add_timer = tevent_common_add_timer, + .schedule_immediate = tevent_common_schedule_immediate, + .add_signal = tevent_common_add_signal, + .loop_once = epoll_event_loop_once, + .loop_wait = tevent_common_loop_wait, }; bool tevent_epoll_init(void) diff --git a/lib/tevent/tevent_immediate.c b/lib/tevent/tevent_immediate.c new file mode 100644 index 0000000000..1ac293e175 --- /dev/null +++ b/lib/tevent/tevent_immediate.c @@ -0,0 +1,139 @@ +/* + Unix SMB/CIFS implementation. + + common events code for immediate events + + Copyright (C) Stefan Metzmacher 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 "tevent.h" +#include "tevent_internal.h" +#include "tevent_util.h" + +static void tevent_common_immediate_cancel(struct tevent_immediate *im) +{ + if (!im->event_ctx) { + return; + } + + tevent_debug(im->event_ctx, TEVENT_DEBUG_TRACE, + "Cancel immediate event %p \"%s\"\n", + im, im->handler_name); + + /* let the backend free im->additional_data */ + if (im->cancel_fn) { + im->cancel_fn(im); + } + + DLIST_REMOVE(im->event_ctx->immediate_events, im); + im->event_ctx = NULL; + im->handler = NULL; + im->private_data = NULL; + im->handler_name = NULL; + im->schedule_location = NULL; + im->cancel_fn = NULL; + im->additional_data = NULL; + + talloc_set_destructor(im, NULL); +} + +/* + destroy an immediate event +*/ +static int tevent_common_immediate_destructor(struct tevent_immediate *im) +{ + tevent_common_immediate_cancel(im); + return 0; +} + +/* + * schedule an immediate event on + */ +void tevent_common_schedule_immediate(struct tevent_immediate *im, + struct tevent_context *ev, + tevent_immediate_handler_t handler, + void *private_data, + const char *handler_name, + const char *location) +{ + tevent_common_immediate_cancel(im); + + if (!handler) { + return; + } + + im->event_ctx = ev; + im->handler = handler; + im->private_data = private_data; + im->handler_name = handler_name; + im->schedule_location = location; + im->cancel_fn = NULL; + im->additional_data = NULL; + + DLIST_ADD_END(ev->immediate_events, im, struct tevent_immediate *); + talloc_set_destructor(im, tevent_common_immediate_destructor); + + tevent_debug(ev, TEVENT_DEBUG_TRACE, + "Schedule immediate event \"%s\": %p\n", + handler_name, im); +} + +/* + trigger the first immediate event and return true + if no event was triggered return false +*/ +bool tevent_common_loop_immediate(struct tevent_context *ev) +{ + struct tevent_immediate *im = ev->immediate_events; + tevent_immediate_handler_t handler; + void *private_data; + + if (!im) { + return false; + } + + tevent_debug(ev, TEVENT_DEBUG_TRACE, + "Run immediate event \"%s\": %p\n", + im->handler_name, im); + + /* + * remember the handler and then clear the event + * the handler might reschedule the event + */ + handler = im->handler; + private_data = im->private_data; + + DLIST_REMOVE(im->event_ctx->immediate_events, im); + im->event_ctx = NULL; + im->handler = NULL; + im->private_data = NULL; + im->handler_name = NULL; + im->schedule_location = NULL; + im->cancel_fn = NULL; + im->additional_data = NULL; + + talloc_set_destructor(im, NULL); + + handler(ev, im, private_data); + + return true; +} + diff --git a/lib/tevent/tevent_internal.h b/lib/tevent/tevent_internal.h index f10485398f..eebf767067 100644 --- a/lib/tevent/tevent_internal.h +++ b/lib/tevent/tevent_internal.h @@ -85,7 +85,17 @@ struct tevent_req { * * This for debugging only. */ - const char *location; + const char *create_location; + + /** + * @brief The location where the request was finished + * + * This uses the __location__ macro via the tevent_req_done(), + * tevent_req_error() or tevent_req_nomem() macro. + * + * This for debugging only. + */ + const char *finish_location; /** * @brief The external state - will be queried by the caller @@ -105,10 +115,10 @@ struct tevent_req { uint64_t error; /** - * @brief the timer event if tevent_req_post was used + * @brief the immediate event used by tevent_req_post * */ - struct tevent_timer *trigger; + struct tevent_immediate *trigger; /** * @brief the timer event if tevent_req_set_timeout was used @@ -143,6 +153,15 @@ struct tevent_ops { void *private_data, const char *handler_name, const char *location); + + /* immediate event functions */ + void (*schedule_immediate)(struct tevent_immediate *im, + struct tevent_context *ev, + tevent_immediate_handler_t handler, + void *private_data, + const char *handler_name, + const char *location); + /* signal functions */ struct tevent_signal *(*add_signal)(struct tevent_context *ev, TALLOC_CTX *mem_ctx, @@ -188,6 +207,21 @@ struct tevent_timer { void *additional_data; }; +struct tevent_immediate { + struct tevent_immediate *prev, *next; + struct tevent_context *event_ctx; + tevent_immediate_handler_t handler; + /* this is private for the specific handler */ + void *private_data; + /* this is for debugging only! */ + const char *handler_name; + const char *create_location; + const char *schedule_location; + /* this is private for the events_ops implementation */ + void (*cancel_fn)(struct tevent_immediate *im); + void *additional_data; +}; + struct tevent_signal { struct tevent_signal *prev, *next; struct tevent_context *event_ctx; @@ -222,6 +256,9 @@ struct tevent_context { /* list of timed events - used by common code */ struct tevent_timer *timer_events; + /* list of immediate events - used by common code */ + struct tevent_immediate *immediate_events; + /* list of signal events - used by common code */ struct tevent_signal *signal_events; @@ -247,6 +284,8 @@ struct tevent_context { bool tevent_register_backend(const char *name, const struct tevent_ops *ops); int tevent_common_context_destructor(struct tevent_context *ev); +int tevent_common_loop_wait(struct tevent_context *ev, + const char *location); int tevent_common_fd_destructor(struct tevent_fd *fde); struct tevent_fd *tevent_common_add_fd(struct tevent_context *ev, @@ -271,6 +310,14 @@ struct tevent_timer *tevent_common_add_timer(struct tevent_context *ev, const char *location); struct timeval tevent_common_loop_timer_delay(struct tevent_context *); +void tevent_common_schedule_immediate(struct tevent_immediate *im, + struct tevent_context *ev, + tevent_immediate_handler_t handler, + void *private_data, + const char *handler_name, + const char *location); +bool tevent_common_loop_immediate(struct tevent_context *ev); + struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev, TALLOC_CTX *mem_ctx, int signum, diff --git a/lib/tevent/tevent_queue.c b/lib/tevent/tevent_queue.c index 6c8fbe4f95..3715c35e4f 100644 --- a/lib/tevent/tevent_queue.c +++ b/lib/tevent/tevent_queue.c @@ -34,6 +34,7 @@ struct tevent_queue_entry { bool triggered; struct tevent_req *req; + struct tevent_context *ev; tevent_queue_trigger_fn_t trigger; void *private_data; @@ -44,12 +45,16 @@ struct tevent_queue { const char *location; bool running; - struct tevent_timer *timer; + struct tevent_immediate *immediate; size_t length; struct tevent_queue_entry *list; }; +static void tevent_queue_immediate_trigger(struct tevent_context *ev, + struct tevent_immediate *im, + void *private_data); + static int tevent_queue_entry_destructor(struct tevent_queue_entry *e) { struct tevent_queue *q = e->queue; @@ -61,14 +66,23 @@ static int tevent_queue_entry_destructor(struct tevent_queue_entry *e) DLIST_REMOVE(q->list, e); q->length--; - if (e->triggered && - q->running && - q->list) { - q->list->triggered = true; - q->list->trigger(q->list->req, - q->list->private_data); + if (!q->running) { + return 0; + } + + if (!q->list) { + return 0; + } + + if (q->list->triggered) { + return 0; } + tevent_schedule_immediate(q->immediate, + q->list->ev, + tevent_queue_immediate_trigger, + q); + return 0; } @@ -100,6 +114,11 @@ struct tevent_queue *_tevent_queue_create(TALLOC_CTX *mem_ctx, talloc_free(queue); return NULL; } + queue->immediate = tevent_create_immediate(queue); + if (!queue->immediate) { + talloc_free(queue); + return NULL; + } queue->location = location; @@ -110,16 +129,16 @@ struct tevent_queue *_tevent_queue_create(TALLOC_CTX *mem_ctx, return queue; } -static void tevent_queue_timer_start(struct tevent_context *ev, - struct tevent_timer *te, - struct timeval now, - void *private_data) +static void tevent_queue_immediate_trigger(struct tevent_context *ev, + struct tevent_immediate *im, + void *private_data) { struct tevent_queue *q = talloc_get_type(private_data, struct tevent_queue); - talloc_free(te); - q->timer = NULL; + if (!q->running) { + return; + } q->list->triggered = true; q->list->trigger(q->list->req, q->list->private_data); @@ -140,56 +159,56 @@ bool tevent_queue_add(struct tevent_queue *queue, e->queue = queue; e->req = req; + e->ev = ev; e->trigger = trigger; e->private_data = private_data; - if (queue->running && - !queue->timer && - !queue->list) { - queue->timer = tevent_add_timer(ev, queue, tevent_timeval_zero(), - tevent_queue_timer_start, - queue); - if (!queue->timer) { - talloc_free(e); - return false; - } - } - DLIST_ADD_END(queue->list, e, struct tevent_queue_entry *); queue->length++; talloc_set_destructor(e, tevent_queue_entry_destructor); + if (!queue->running) { + return true; + } + + if (queue->list->triggered) { + return true; + } + + tevent_schedule_immediate(queue->immediate, + queue->list->ev, + tevent_queue_immediate_trigger, + queue); + return true; } -bool tevent_queue_start(struct tevent_queue *queue, - struct tevent_context *ev) +void tevent_queue_start(struct tevent_queue *queue) { if (queue->running) { /* already started */ - return true; + return; } - if (!queue->timer && - queue->list) { - queue->timer = tevent_add_timer(ev, queue, tevent_timeval_zero(), - tevent_queue_timer_start, - queue); - if (!queue->timer) { - return false; - } + queue->running = true; + + if (!queue->list) { + return; } - queue->running = true; + if (queue->list->triggered) { + return; + } - return true; + tevent_schedule_immediate(queue->immediate, + queue->list->ev, + tevent_queue_immediate_trigger, + queue); } void tevent_queue_stop(struct tevent_queue *queue) { queue->running = false; - talloc_free(queue->timer); - queue->timer = NULL; } size_t tevent_queue_length(struct tevent_queue *queue) diff --git a/lib/tevent/tevent_req.c b/lib/tevent/tevent_req.c index 3832088b34..380a6388e2 100644 --- a/lib/tevent/tevent_req.c +++ b/lib/tevent/tevent_req.c @@ -43,7 +43,7 @@ char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx) return talloc_asprintf(mem_ctx, "tevent_req[%p/%s]: state[%d] error[%lld (0x%llX)] " " state[%s (%p)] timer[%p]", - req, req->internal.location, + req, req->internal.create_location, req->internal.state, (unsigned long long)req->internal.error, (unsigned long long)req->internal.error, @@ -95,8 +95,14 @@ struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx, return NULL; } req->internal.private_type = type; - req->internal.location = location; + req->internal.create_location = location; + req->internal.finish_location = NULL; req->internal.state = TEVENT_REQ_IN_PROGRESS; + req->internal.trigger = tevent_create_immediate(req); + if (!req->internal.trigger) { + talloc_free(req); + return NULL; + } data = talloc_size(req, data_size); if (data == NULL) { @@ -111,9 +117,12 @@ struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx, return req; } -static void tevent_req_finish(struct tevent_req *req, enum tevent_req_state state) +static void tevent_req_finish(struct tevent_req *req, + enum tevent_req_state state, + const char *location) { req->internal.state = state; + req->internal.finish_location = location; if (req->async.fn != NULL) { req->async.fn(req); } @@ -128,9 +137,10 @@ static void tevent_req_finish(struct tevent_req *req, enum tevent_req_state stat * function. */ -void tevent_req_done(struct tevent_req *req) +void _tevent_req_done(struct tevent_req *req, + const char *location) { - tevent_req_finish(req, TEVENT_REQ_DONE); + tevent_req_finish(req, TEVENT_REQ_DONE, location); } /** @@ -161,14 +171,16 @@ void tevent_req_done(struct tevent_req *req) * \endcode */ -bool tevent_req_error(struct tevent_req *req, uint64_t error) +bool _tevent_req_error(struct tevent_req *req, + uint64_t error, + const char *location) { if (error == 0) { return false; } req->internal.error = error; - tevent_req_finish(req, TEVENT_REQ_USER_ERROR); + tevent_req_finish(req, TEVENT_REQ_USER_ERROR, location); return true; } @@ -189,42 +201,39 @@ bool tevent_req_error(struct tevent_req *req, uint64_t error) * \endcode */ -bool tevent_req_nomem(const void *p, struct tevent_req *req) +bool _tevent_req_nomem(const void *p, + struct tevent_req *req, + const char *location) { if (p != NULL) { return false; } - tevent_req_finish(req, TEVENT_REQ_NO_MEMORY); + tevent_req_finish(req, TEVENT_REQ_NO_MEMORY, location); return true; } /** - * @brief Timed event callback + * @brief Immediate event callback * @param[in] ev Event context - * @param[in] te The timed event - * @param[in] now zero time + * @param[in] im The immediate event * @param[in] priv The async request to be finished */ static void tevent_req_trigger(struct tevent_context *ev, - struct tevent_timer *te, - struct timeval zero, + struct tevent_immediate *im, void *private_data) { struct tevent_req *req = talloc_get_type(private_data, struct tevent_req); - talloc_free(req->internal.trigger); - req->internal.trigger = NULL; - - tevent_req_finish(req, req->internal.state); + tevent_req_finish(req, req->internal.state, + req->internal.finish_location); } /** * @brief Finish a request before the caller had the change to set the callback * @param[in] req The finished request * @param[in] ev The tevent_context for the timed event - * @retval On success req will be returned, - * on failure req will be destroyed + * @retval req will be returned * * An implementation of an async request might find that it can either finish * the request without waiting for an external event, or it can't even start @@ -237,13 +246,8 @@ static void tevent_req_trigger(struct tevent_context *ev, struct tevent_req *tevent_req_post(struct tevent_req *req, struct tevent_context *ev) { - req->internal.trigger = tevent_add_timer(ev, req, tevent_timeval_zero(), - tevent_req_trigger, req); - if (!req->internal.trigger) { - talloc_free(req); - return NULL; - } - + tevent_schedule_immediate(req->internal.trigger, + ev, tevent_req_trigger, req); return req; } @@ -265,14 +269,11 @@ bool tevent_req_is_in_progress(struct tevent_req *req) */ void tevent_req_received(struct tevent_req *req) { - talloc_free(req->data); - req->data = NULL; + TALLOC_FREE(req->data); req->private_print = NULL; - talloc_free(req->internal.trigger); - req->internal.trigger = NULL; - talloc_free(req->internal.timer); - req->internal.timer = NULL; + TALLOC_FREE(req->internal.trigger); + TALLOC_FREE(req->internal.timer); req->internal.state = TEVENT_REQ_RECEIVED; } @@ -313,17 +314,16 @@ static void tevent_req_timedout(struct tevent_context *ev, struct tevent_req *req = talloc_get_type(private_data, struct tevent_req); - talloc_free(req->internal.timer); - req->internal.timer = NULL; + TALLOC_FREE(req->internal.timer); - tevent_req_finish(req, TEVENT_REQ_TIMED_OUT); + tevent_req_finish(req, TEVENT_REQ_TIMED_OUT, __FUNCTION__); } bool tevent_req_set_endtime(struct tevent_req *req, struct tevent_context *ev, struct timeval endtime) { - talloc_free(req->internal.timer); + TALLOC_FREE(req->internal.timer); req->internal.timer = tevent_add_timer(ev, req, endtime, tevent_req_timedout, diff --git a/lib/tevent/tevent_select.c b/lib/tevent/tevent_select.c index cdddb601c4..d97418991a 100644 --- a/lib/tevent/tevent_select.c +++ b/lib/tevent/tevent_select.c @@ -38,10 +38,6 @@ struct select_event_context { /* information for exiting from the event loop */ int exit_code; - - /* this is incremented when the loop over events causes something which - could change the events yet to be processed */ - uint32_t destruction_count; }; /* @@ -95,8 +91,6 @@ static int select_event_fd_destructor(struct tevent_fd *fde) if (select_ev->maxfd == fde->fd) { select_ev->maxfd = EVENT_INVALID_MAXFD; } - - select_ev->destruction_count++; } return tevent_common_fd_destructor(fde); @@ -138,7 +132,6 @@ static int select_event_loop_select(struct select_event_context *select_ev, stru fd_set r_fds, w_fds; struct tevent_fd *fde; int selrtn; - uint32_t destruction_count = ++select_ev->destruction_count; /* we maybe need to recalculate the maxfd */ if (select_ev->maxfd == EVENT_INVALID_MAXFD) { @@ -200,15 +193,13 @@ static int select_event_loop_select(struct select_event_context *select_ev, stru if (FD_ISSET(fde->fd, &w_fds)) flags |= TEVENT_FD_WRITE; if (flags) { fde->handler(select_ev->ev, fde, flags, fde->private_data); - if (destruction_count != select_ev->destruction_count) { - break; - } + break; } } } return 0; -} +} /* do a single event loop using the events defined in ev @@ -219,42 +210,35 @@ static int select_event_loop_once(struct tevent_context *ev, const char *locatio struct select_event_context); struct timeval tval; - tval = tevent_common_loop_timer_delay(ev); - if (tevent_timeval_is_zero(&tval)) { + if (ev->signal_events && + tevent_common_check_signal(ev)) { return 0; } - return select_event_loop_select(select_ev, &tval); -} - -/* - return on failure or (with 0) if all fd events are removed -*/ -static int select_event_loop_wait(struct tevent_context *ev, const char *location) -{ - struct select_event_context *select_ev = talloc_get_type(ev->additional_data, - struct select_event_context); - select_ev->exit_code = 0; + if (ev->immediate_events && + tevent_common_loop_immediate(ev)) { + return 0; + } - while (ev->fd_events && select_ev->exit_code == 0) { - if (select_event_loop_once(ev, location) != 0) { - break; - } + tval = tevent_common_loop_timer_delay(ev); + if (tevent_timeval_is_zero(&tval)) { + return 0; } - return select_ev->exit_code; + return select_event_loop_select(select_ev, &tval); } static const struct tevent_ops select_event_ops = { - .context_init = select_event_context_init, - .add_fd = select_event_add_fd, - .set_fd_close_fn= tevent_common_fd_set_close_fn, - .get_fd_flags = tevent_common_fd_get_flags, - .set_fd_flags = tevent_common_fd_set_flags, - .add_timer = tevent_common_add_timer, - .add_signal = tevent_common_add_signal, - .loop_once = select_event_loop_once, - .loop_wait = select_event_loop_wait, + .context_init = select_event_context_init, + .add_fd = select_event_add_fd, + .set_fd_close_fn = tevent_common_fd_set_close_fn, + .get_fd_flags = tevent_common_fd_get_flags, + .set_fd_flags = tevent_common_fd_set_flags, + .add_timer = tevent_common_add_timer, + .schedule_immediate = tevent_common_schedule_immediate, + .add_signal = tevent_common_add_signal, + .loop_once = select_event_loop_once, + .loop_wait = tevent_common_loop_wait, }; bool tevent_select_init(void) diff --git a/lib/tevent/tevent_standard.c b/lib/tevent/tevent_standard.c index 73a45e8c20..c3f8b36e84 100644 --- a/lib/tevent/tevent_standard.c +++ b/lib/tevent/tevent_standard.c @@ -48,14 +48,6 @@ struct std_event_context { /* information for exiting from the event loop */ int exit_code; - /* this is changed by the destructors for the fd event - type. It is used to detect event destruction by event - handlers, which means the code that is calling the event - handler needs to assume that the linked list is no longer - valid - */ - uint32_t destruction_count; - /* when using epoll this is the handle from epoll_create */ int epoll_fd; @@ -253,9 +245,8 @@ static void epoll_change_event(struct std_event_context *std_ev, struct tevent_f static int epoll_event_loop(struct std_event_context *std_ev, struct timeval *tvalp) { int ret, i; -#define MAXEVENTS 8 +#define MAXEVENTS 1 struct epoll_event events[MAXEVENTS]; - uint32_t destruction_count = ++std_ev->destruction_count; int timeout = -1; if (std_ev->epoll_fd == -1) return -1; @@ -316,9 +307,7 @@ static int epoll_event_loop(struct std_event_context *std_ev, struct timeval *tv if (events[i].events & EPOLLOUT) flags |= TEVENT_FD_WRITE; if (flags) { fde->handler(std_ev->ev, fde, flags, fde->private_data); - if (destruction_count != std_ev->destruction_count) { - break; - } + break; } } @@ -390,8 +379,6 @@ static int std_event_fd_destructor(struct tevent_fd *fde) std_ev->maxfd = EVENT_INVALID_MAXFD; } - std_ev->destruction_count++; - epoll_del_event(std_ev, fde); } @@ -459,7 +446,6 @@ static int std_event_loop_select(struct std_event_context *std_ev, struct timeva fd_set r_fds, w_fds; struct tevent_fd *fde; int selrtn; - uint32_t destruction_count = ++std_ev->destruction_count; /* we maybe need to recalculate the maxfd */ if (std_ev->maxfd == EVENT_INVALID_MAXFD) { @@ -521,9 +507,7 @@ static int std_event_loop_select(struct std_event_context *std_ev, struct timeva if (FD_ISSET(fde->fd, &w_fds)) flags |= TEVENT_FD_WRITE; if (flags) { fde->handler(std_ev->ev, fde, flags, fde->private_data); - if (destruction_count != std_ev->destruction_count) { - break; - } + break; } } } @@ -540,6 +524,16 @@ static int std_event_loop_once(struct tevent_context *ev, const char *location) struct std_event_context); struct timeval tval; + if (ev->signal_events && + tevent_common_check_signal(ev)) { + return 0; + } + + if (ev->immediate_events && + tevent_common_loop_immediate(ev)) { + return 0; + } + tval = tevent_common_loop_timer_delay(ev); if (tevent_timeval_is_zero(&tval)) { return 0; @@ -554,34 +548,17 @@ static int std_event_loop_once(struct tevent_context *ev, const char *location) return std_event_loop_select(std_ev, &tval); } -/* - return on failure or (with 0) if all fd events are removed -*/ -static int std_event_loop_wait(struct tevent_context *ev, const char *location) -{ - struct std_event_context *std_ev = talloc_get_type(ev->additional_data, - struct std_event_context); - std_ev->exit_code = 0; - - while (ev->fd_events && std_ev->exit_code == 0) { - if (std_event_loop_once(ev, location) != 0) { - break; - } - } - - return std_ev->exit_code; -} - static const struct tevent_ops std_event_ops = { - .context_init = std_event_context_init, - .add_fd = std_event_add_fd, - .set_fd_close_fn= tevent_common_fd_set_close_fn, - .get_fd_flags = tevent_common_fd_get_flags, - .set_fd_flags = std_event_set_fd_flags, - .add_timer = tevent_common_add_timer, - .add_signal = tevent_common_add_signal, - .loop_once = std_event_loop_once, - .loop_wait = std_event_loop_wait, + .context_init = std_event_context_init, + .add_fd = std_event_add_fd, + .set_fd_close_fn = tevent_common_fd_set_close_fn, + .get_fd_flags = tevent_common_fd_get_flags, + .set_fd_flags = std_event_set_fd_flags, + .add_timer = tevent_common_add_timer, + .schedule_immediate = tevent_common_schedule_immediate, + .add_signal = tevent_common_add_signal, + .loop_once = std_event_loop_once, + .loop_wait = tevent_common_loop_wait, }; |