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/>.
27 #include "tevent_internal.h"
28 #include "tevent_util.h"
30 #undef tevent_queue_add
31 #undef tevent_queue_add_entry
32 #undef tevent_queue_add_optimize_empty
34 struct tevent_queue_entry
{
35 struct tevent_queue_entry
*prev
, *next
;
36 struct tevent_queue
*queue
;
40 struct tevent_req
*req
;
41 struct tevent_context
*ev
;
43 tevent_queue_trigger_fn_t trigger
;
44 const char *trigger_name
;
54 struct tevent_immediate
*immediate
;
57 struct tevent_queue_entry
*list
;
60 static void tevent_queue_immediate_trigger(struct tevent_context
*ev
,
61 struct tevent_immediate
*im
,
64 static int tevent_queue_entry_destructor(struct tevent_queue_entry
*e
)
66 struct tevent_queue
*q
= e
->queue
;
72 tevent_trace_queue_callback(q
->list
->ev
, e
, TEVENT_EVENT_TRACE_DETACH
);
73 tevent_thread_call_depth_notify(TEVENT_CALL_FLOW_REQ_QUEUE_LEAVE
,
75 q
->list
->req
->internal
.call_depth
,
77 DLIST_REMOVE(q
->list
, e
);
88 if (q
->list
->triggered
) {
92 tevent_schedule_immediate(q
->immediate
,
94 tevent_queue_immediate_trigger
,
100 static int tevent_queue_destructor(struct tevent_queue
*q
)
105 struct tevent_queue_entry
*e
= q
->list
;
112 struct tevent_queue
*_tevent_queue_create(TALLOC_CTX
*mem_ctx
,
114 const char *location
)
116 struct tevent_queue
*queue
;
118 queue
= talloc_zero(mem_ctx
, struct tevent_queue
);
123 queue
->name
= talloc_strdup(queue
, name
);
128 queue
->immediate
= tevent_create_immediate(queue
);
129 if (!queue
->immediate
) {
134 queue
->location
= location
;
136 /* queue is running by default */
137 queue
->running
= true;
139 talloc_set_destructor(queue
, tevent_queue_destructor
);
143 static void tevent_queue_immediate_trigger(struct tevent_context
*ev
,
144 struct tevent_immediate
*im
,
147 struct tevent_queue
*q
=
148 talloc_get_type_abort(private_data
,
149 struct tevent_queue
);
159 tevent_trace_queue_callback(ev
, q
->list
,
160 TEVENT_EVENT_TRACE_BEFORE_HANDLER
);
161 /* Set the call depth of the request coming from the queue. */
162 tevent_thread_call_depth_notify(TEVENT_CALL_FLOW_REQ_QUEUE_TRIGGER
,
164 q
->list
->req
->internal
.call_depth
,
165 q
->list
->trigger_name
);
166 q
->list
->triggered
= true;
167 q
->list
->trigger(q
->list
->req
, q
->list
->private_data
);
170 static void tevent_queue_noop_trigger(struct tevent_req
*req
,
173 /* this is doing nothing but blocking the queue */
176 static struct tevent_queue_entry
*tevent_queue_add_internal(
177 struct tevent_queue
*queue
,
178 struct tevent_context
*ev
,
179 struct tevent_req
*req
,
180 tevent_queue_trigger_fn_t trigger
,
181 const char *trigger_name
,
185 struct tevent_queue_entry
*e
;
187 e
= talloc_zero(req
, struct tevent_queue_entry
);
193 * if there is no trigger, it is just a blocker
195 if (trigger
== NULL
) {
196 trigger
= tevent_queue_noop_trigger
;
202 e
->trigger
= trigger
;
203 e
->trigger_name
= trigger_name
;
204 e
->private_data
= private_data
;
206 if (queue
->length
> 0) {
208 * if there are already entries in the
209 * queue do not optimize.
211 allow_direct
= false;
214 if (req
->async
.fn
!= NULL
) {
216 * If the caller wants to optimize for the
217 * empty queue case, call the trigger only
218 * if there is no callback defined for the
221 allow_direct
= false;
224 DLIST_ADD_END(queue
->list
, e
);
226 talloc_set_destructor(e
, tevent_queue_entry_destructor
);
227 tevent_trace_queue_callback(ev
, e
, TEVENT_EVENT_TRACE_ATTACH
);
228 tevent_thread_call_depth_notify(TEVENT_CALL_FLOW_REQ_QUEUE_ENTER
,
230 req
->internal
.call_depth
,
233 if (!queue
->running
) {
237 if (queue
->list
->triggered
) {
242 * If allowed we directly call the trigger
243 * avoiding possible delays caused by
244 * an immediate event.
247 tevent_trace_queue_callback(ev
,
249 TEVENT_EVENT_TRACE_BEFORE_HANDLER
);
250 queue
->list
->triggered
= true;
251 queue
->list
->trigger(queue
->list
->req
,
252 queue
->list
->private_data
);
256 tevent_schedule_immediate(queue
->immediate
,
258 tevent_queue_immediate_trigger
,
264 bool tevent_queue_add(struct tevent_queue
*queue
,
265 struct tevent_context
*ev
,
266 struct tevent_req
*req
,
267 tevent_queue_trigger_fn_t trigger
,
270 return _tevent_queue_add(queue
, ev
, req
, trigger
, NULL
, private_data
);
273 bool _tevent_queue_add(struct tevent_queue
*queue
,
274 struct tevent_context
*ev
,
275 struct tevent_req
*req
,
276 tevent_queue_trigger_fn_t trigger
,
277 const char* trigger_name
,
280 struct tevent_queue_entry
*e
;
282 e
= tevent_queue_add_internal(queue
, ev
, req
,
283 trigger
, trigger_name
,
284 private_data
, false);
292 struct tevent_queue_entry
*tevent_queue_add_entry(
293 struct tevent_queue
*queue
,
294 struct tevent_context
*ev
,
295 struct tevent_req
*req
,
296 tevent_queue_trigger_fn_t trigger
,
299 return _tevent_queue_add_entry(queue
, ev
, req
,
304 struct tevent_queue_entry
*_tevent_queue_add_entry(
305 struct tevent_queue
*queue
,
306 struct tevent_context
*ev
,
307 struct tevent_req
*req
,
308 tevent_queue_trigger_fn_t trigger
,
309 const char* trigger_name
,
312 return tevent_queue_add_internal(queue
, ev
, req
,
313 trigger
, trigger_name
,
314 private_data
, false);
317 struct tevent_queue_entry
*tevent_queue_add_optimize_empty(
318 struct tevent_queue
*queue
,
319 struct tevent_context
*ev
,
320 struct tevent_req
*req
,
321 tevent_queue_trigger_fn_t trigger
,
324 return _tevent_queue_add_optimize_empty(queue
, ev
, req
,
329 struct tevent_queue_entry
*_tevent_queue_add_optimize_empty(
330 struct tevent_queue
*queue
,
331 struct tevent_context
*ev
,
332 struct tevent_req
*req
,
333 tevent_queue_trigger_fn_t trigger
,
334 const char* trigger_name
,
337 return tevent_queue_add_internal(queue
, ev
, req
,
338 trigger
, trigger_name
,
342 void tevent_queue_entry_untrigger(struct tevent_queue_entry
*entry
)
344 if (entry
->queue
->running
) {
348 if (entry
->queue
->list
!= entry
) {
352 entry
->triggered
= false;
355 void tevent_queue_start(struct tevent_queue
*queue
)
357 if (queue
->running
) {
358 /* already started */
362 queue
->running
= true;
368 if (queue
->list
->triggered
) {
372 tevent_schedule_immediate(queue
->immediate
,
374 tevent_queue_immediate_trigger
,
378 void tevent_queue_stop(struct tevent_queue
*queue
)
380 queue
->running
= false;
383 size_t tevent_queue_length(struct tevent_queue
*queue
)
385 return queue
->length
;
388 bool tevent_queue_running(struct tevent_queue
*queue
)
390 return queue
->running
;
393 struct tevent_queue_wait_state
{
397 static void tevent_queue_wait_trigger(struct tevent_req
*req
,
400 struct tevent_req
*tevent_queue_wait_send(TALLOC_CTX
*mem_ctx
,
401 struct tevent_context
*ev
,
402 struct tevent_queue
*queue
)
404 struct tevent_req
*req
;
405 struct tevent_queue_wait_state
*state
;
408 req
= tevent_req_create(mem_ctx
, &state
,
409 struct tevent_queue_wait_state
);
414 ok
= _tevent_queue_add(queue
, ev
, req
,
415 tevent_queue_wait_trigger
,
416 "tevent_queue_wait_trigger",
420 return tevent_req_post(req
, ev
);
426 static void tevent_queue_wait_trigger(struct tevent_req
*req
,
429 tevent_req_done(req
);
432 bool tevent_queue_wait_recv(struct tevent_req
*req
)
434 enum tevent_req_state state
;
437 if (tevent_req_is_error(req
, &state
, &err
)) {
438 tevent_req_received(req
);
442 tevent_req_received(req
);
446 void tevent_queue_entry_set_tag(struct tevent_queue_entry
*qe
, uint64_t tag
)
455 uint64_t tevent_queue_entry_get_tag(const struct tevent_queue_entry
*qe
)