eeb922fbbc80644421889e679420599b12bff2aa
[samba.git] / lib / tevent / tevent_queue.c
1 /*
2    Unix SMB/CIFS implementation.
3    Infrastructure for async requests
4    Copyright (C) Volker Lendecke 2008
5    Copyright (C) Stefan Metzmacher 2009
6
7      ** NOTE! The following LGPL license applies to the tevent
8      ** library. This does NOT imply that all of Samba is released
9      ** under the LGPL
10
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.
15
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.
20
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/>.
23 */
24
25 #include "replace.h"
26 #include "tevent.h"
27 #include "tevent_internal.h"
28 #include "tevent_util.h"
29
30 struct tevent_queue_entry {
31         struct tevent_queue_entry *prev, *next;
32         struct tevent_queue *queue;
33
34         bool triggered;
35
36         struct tevent_req *req;
37         struct tevent_context *ev;
38
39         tevent_queue_trigger_fn_t trigger;
40         void *private_data;
41 };
42
43 struct tevent_queue {
44         const char *name;
45         const char *location;
46
47         bool running;
48         struct tevent_immediate *immediate;
49
50         size_t length;
51         struct tevent_queue_entry *list;
52 };
53
54 static void tevent_queue_immediate_trigger(struct tevent_context *ev,
55                                            struct tevent_immediate *im,
56                                            void *private_data);
57
58 static int tevent_queue_entry_destructor(struct tevent_queue_entry *e)
59 {
60         struct tevent_queue *q = e->queue;
61
62         if (!q) {
63                 return 0;
64         }
65
66         DLIST_REMOVE(q->list, e);
67         q->length--;
68
69         if (!q->running) {
70                 return 0;
71         }
72
73         if (!q->list) {
74                 return 0;
75         }
76
77         if (q->list->triggered) {
78                 return 0;
79         }
80
81         tevent_schedule_immediate(q->immediate,
82                                   q->list->ev,
83                                   tevent_queue_immediate_trigger,
84                                   q);
85
86         return 0;
87 }
88
89 static int tevent_queue_destructor(struct tevent_queue *q)
90 {
91         q->running = false;
92
93         while (q->list) {
94                 struct tevent_queue_entry *e = q->list;
95                 talloc_free(e);
96         }
97
98         return 0;
99 }
100
101 struct tevent_queue *_tevent_queue_create(TALLOC_CTX *mem_ctx,
102                                           const char *name,
103                                           const char *location)
104 {
105         struct tevent_queue *queue;
106
107         queue = talloc_zero(mem_ctx, struct tevent_queue);
108         if (!queue) {
109                 return NULL;
110         }
111
112         queue->name = talloc_strdup(queue, name);
113         if (!queue->name) {
114                 talloc_free(queue);
115                 return NULL;
116         }
117         queue->immediate = tevent_create_immediate(queue);
118         if (!queue->immediate) {
119                 talloc_free(queue);
120                 return NULL;
121         }
122
123         queue->location = location;
124
125         /* queue is running by default */
126         queue->running = true;
127
128         talloc_set_destructor(queue, tevent_queue_destructor);
129         return queue;
130 }
131
132 static void tevent_queue_immediate_trigger(struct tevent_context *ev,
133                                            struct tevent_immediate *im,
134                                            void *private_data)
135 {
136         struct tevent_queue *q = talloc_get_type(private_data,
137                                   struct tevent_queue);
138
139         if (!q->running) {
140                 return;
141         }
142
143         if (!q->list) {
144                 return;
145         }
146
147         q->list->triggered = true;
148         q->list->trigger(q->list->req, q->list->private_data);
149 }
150
151 static struct tevent_queue_entry *tevent_queue_add_internal(
152                                         struct tevent_queue *queue,
153                                         struct tevent_context *ev,
154                                         struct tevent_req *req,
155                                         tevent_queue_trigger_fn_t trigger,
156                                         void *private_data,
157                                         bool allow_direct)
158 {
159         struct tevent_queue_entry *e;
160
161         e = talloc_zero(req, struct tevent_queue_entry);
162         if (e == NULL) {
163                 return NULL;
164         }
165
166         e->queue = queue;
167         e->req = req;
168         e->ev = ev;
169         e->trigger = trigger;
170         e->private_data = private_data;
171
172         /*
173          * if there is no trigger, it is just a blocker
174          */
175         if (trigger == NULL) {
176                 e->triggered = true;
177         }
178
179         if (queue->length > 0) {
180                 /*
181                  * if there are already entries in the
182                  * queue do not optimize.
183                  */
184                 allow_direct = false;
185         }
186
187         if (req->async.fn != NULL) {
188                 /*
189                  * If the callers wants to optimize for the
190                  * empty queue case, call the trigger only
191                  * if there is no callback defined for the
192                  * request yet.
193                  */
194                 allow_direct = false;
195         }
196
197         DLIST_ADD_END(queue->list, e, struct tevent_queue_entry *);
198         queue->length++;
199         talloc_set_destructor(e, tevent_queue_entry_destructor);
200
201         if (!queue->running) {
202                 return e;
203         }
204
205         if (queue->list->triggered) {
206                 return e;
207         }
208
209         /*
210          * If allowed we directly call the trigger
211          * avoiding possible delays caused by
212          * an immediate event.
213          */
214         if (allow_direct) {
215                 queue->list->triggered = true;
216                 queue->list->trigger(queue->list->req,
217                                      queue->list->private_data);
218                 return e;
219         }
220
221         tevent_schedule_immediate(queue->immediate,
222                                   queue->list->ev,
223                                   tevent_queue_immediate_trigger,
224                                   queue);
225
226         return e;
227 }
228
229 bool tevent_queue_add(struct tevent_queue *queue,
230                       struct tevent_context *ev,
231                       struct tevent_req *req,
232                       tevent_queue_trigger_fn_t trigger,
233                       void *private_data)
234 {
235         struct tevent_queue_entry *e;
236
237         e = tevent_queue_add_internal(queue, ev, req,
238                                       trigger, private_data, false);
239         if (e == NULL) {
240                 return false;
241         }
242
243         return true;
244 }
245
246 struct tevent_queue_entry *tevent_queue_add_entry(
247                                         struct tevent_queue *queue,
248                                         struct tevent_context *ev,
249                                         struct tevent_req *req,
250                                         tevent_queue_trigger_fn_t trigger,
251                                         void *private_data)
252 {
253         return tevent_queue_add_internal(queue, ev, req,
254                                          trigger, private_data, false);
255 }
256
257 struct tevent_queue_entry *tevent_queue_add_optimize_empty(
258                                         struct tevent_queue *queue,
259                                         struct tevent_context *ev,
260                                         struct tevent_req *req,
261                                         tevent_queue_trigger_fn_t trigger,
262                                         void *private_data)
263 {
264         return tevent_queue_add_internal(queue, ev, req,
265                                          trigger, private_data, true);
266 }
267
268 void tevent_queue_start(struct tevent_queue *queue)
269 {
270         if (queue->running) {
271                 /* already started */
272                 return;
273         }
274
275         queue->running = true;
276
277         if (!queue->list) {
278                 return;
279         }
280
281         if (queue->list->triggered) {
282                 return;
283         }
284
285         tevent_schedule_immediate(queue->immediate,
286                                   queue->list->ev,
287                                   tevent_queue_immediate_trigger,
288                                   queue);
289 }
290
291 void tevent_queue_stop(struct tevent_queue *queue)
292 {
293         queue->running = false;
294 }
295
296 size_t tevent_queue_length(struct tevent_queue *queue)
297 {
298         return queue->length;
299 }
300
301 bool tevent_queue_running(struct tevent_queue *queue)
302 {
303         return queue->running;
304 }