2 Unix SMB/CIFS implementation.
3 Infrastructure for async requests
4 Copyright (C) Volker Lendecke 2008
5 Copyright (C) Stefan Metzmacher 2009
7 ** NOTE! The following LGPL license applies to the tevent
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 3 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include <atalk/tevent.h>
26 #include "tevent_internal.h"
27 #include "tevent_util.h"
29 struct tevent_queue_entry {
30 struct tevent_queue_entry *prev, *next;
31 struct tevent_queue *queue;
35 struct tevent_req *req;
36 struct tevent_context *ev;
38 tevent_queue_trigger_fn_t trigger;
47 struct tevent_immediate *immediate;
50 struct tevent_queue_entry *list;
53 static void tevent_queue_immediate_trigger(struct tevent_context *ev,
54 struct tevent_immediate *im,
57 static int tevent_queue_entry_destructor(struct tevent_queue_entry *e)
59 struct tevent_queue *q = e->queue;
65 DLIST_REMOVE(q->list, e);
76 if (q->list->triggered) {
80 tevent_schedule_immediate(q->immediate,
82 tevent_queue_immediate_trigger,
88 static int tevent_queue_destructor(struct tevent_queue *q)
93 struct tevent_queue_entry *e = q->list;
100 struct tevent_queue *_tevent_queue_create(TALLOC_CTX *mem_ctx,
102 const char *location)
104 struct tevent_queue *queue;
106 queue = talloc_zero(mem_ctx, struct tevent_queue);
111 queue->name = talloc_strdup(queue, name);
116 queue->immediate = tevent_create_immediate(queue);
117 if (!queue->immediate) {
122 queue->location = location;
124 /* queue is running by default */
125 queue->running = true;
127 talloc_set_destructor(queue, tevent_queue_destructor);
131 static void tevent_queue_immediate_trigger(struct tevent_context *ev,
132 struct tevent_immediate *im,
135 struct tevent_queue *q = talloc_get_type(private_data,
136 struct tevent_queue);
142 q->list->triggered = true;
143 q->list->trigger(q->list->req, q->list->private_data);
146 bool tevent_queue_add(struct tevent_queue *queue,
147 struct tevent_context *ev,
148 struct tevent_req *req,
149 tevent_queue_trigger_fn_t trigger,
152 struct tevent_queue_entry *e;
154 e = talloc_zero(req, struct tevent_queue_entry);
162 e->trigger = trigger;
163 e->private_data = private_data;
165 DLIST_ADD_END(queue->list, e, struct tevent_queue_entry *);
167 talloc_set_destructor(e, tevent_queue_entry_destructor);
169 if (!queue->running) {
173 if (queue->list->triggered) {
177 tevent_schedule_immediate(queue->immediate,
179 tevent_queue_immediate_trigger,
185 void tevent_queue_start(struct tevent_queue *queue)
187 if (queue->running) {
188 /* already started */
192 queue->running = true;
198 if (queue->list->triggered) {
202 tevent_schedule_immediate(queue->immediate,
204 tevent_queue_immediate_trigger,
208 void tevent_queue_stop(struct tevent_queue *queue)
210 queue->running = false;
213 size_t tevent_queue_length(struct tevent_queue *queue)
215 return queue->length;