From 1533f1722de96f814c5e9de9aebc2892eb4bf882 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Thu, 28 Jul 2011 10:45:22 +0200 Subject: tevent: add tevent_queue_add_entry() and tevent_queue_add_optimize_empty() This adds more flexible handling for the add operation: - It allows the caller to remove a tevent_req from the queue by calling talloc_free() on the returned tevent_queue_entry. - It allows the caller to optimize for the empty queue case, where it the caller wants to avoid the delay caused by the immediate event. metze --- lib/tevent/tevent_queue.c | 57 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) (limited to 'lib/tevent/tevent_queue.c') diff --git a/lib/tevent/tevent_queue.c b/lib/tevent/tevent_queue.c index 7737b3e814..4750675802 100644 --- a/lib/tevent/tevent_queue.c +++ b/lib/tevent/tevent_queue.c @@ -149,7 +149,8 @@ static struct tevent_queue_entry *tevent_queue_add_internal( struct tevent_context *ev, struct tevent_req *req, tevent_queue_trigger_fn_t trigger, - void *private_data) + void *private_data, + bool allow_direct) { struct tevent_queue_entry *e; @@ -171,6 +172,24 @@ static struct tevent_queue_entry *tevent_queue_add_internal( e->triggered = true; } + if (queue->length > 0) { + /* + * if there are already entries in the + * queue do not optimize. + */ + allow_direct = false; + } + + if (req->async.fn != NULL) { + /* + * If the callers wants to optimize for the + * empty queue case, call the trigger only + * if there is no callback defined for the + * request yet. + */ + allow_direct = false; + } + DLIST_ADD_END(queue->list, e, struct tevent_queue_entry *); queue->length++; talloc_set_destructor(e, tevent_queue_entry_destructor); @@ -183,6 +202,18 @@ static struct tevent_queue_entry *tevent_queue_add_internal( return e; } + /* + * If allowed we directly call the trigger + * avoiding possible delays caused by + * an immediate event. + */ + if (allow_direct) { + queue->list->triggered = true; + queue->list->trigger(queue->list->req, + queue->list->private_data); + return e; + } + tevent_schedule_immediate(queue->immediate, queue->list->ev, tevent_queue_immediate_trigger, @@ -200,7 +231,7 @@ bool tevent_queue_add(struct tevent_queue *queue, struct tevent_queue_entry *e; e = tevent_queue_add_internal(queue, ev, req, - trigger, private_data); + trigger, private_data, false); if (e == NULL) { return false; } @@ -208,6 +239,28 @@ bool tevent_queue_add(struct tevent_queue *queue, return true; } +struct tevent_queue_entry *tevent_queue_add_entry( + struct tevent_queue *queue, + struct tevent_context *ev, + struct tevent_req *req, + tevent_queue_trigger_fn_t trigger, + void *private_data) +{ + return tevent_queue_add_internal(queue, ev, req, + trigger, private_data, false); +} + +struct tevent_queue_entry *tevent_queue_add_optimize_empty( + struct tevent_queue *queue, + struct tevent_context *ev, + struct tevent_req *req, + tevent_queue_trigger_fn_t trigger, + void *private_data) +{ + return tevent_queue_add_internal(queue, ev, req, + trigger, private_data, true); +} + void tevent_queue_start(struct tevent_queue *queue) { if (queue->running) { -- cgit