tevent: Save 32 bytes of .text in tevent_req_create
[sfrench/samba-autobuild/.git] / lib / tevent / tevent_req.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 char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
31 {
32         return talloc_asprintf(mem_ctx,
33                                "tevent_req[%p/%s]: state[%d] error[%lld (0x%llX)] "
34                                " state[%s (%p)] timer[%p]",
35                                req, req->internal.create_location,
36                                req->internal.state,
37                                (unsigned long long)req->internal.error,
38                                (unsigned long long)req->internal.error,
39                                talloc_get_name(req->data),
40                                req->data,
41                                req->internal.timer
42                                );
43 }
44
45 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req)
46 {
47         if (!req->private_print) {
48                 return tevent_req_default_print(req, mem_ctx);
49         }
50
51         return req->private_print(req, mem_ctx);
52 }
53
54 static int tevent_req_destructor(struct tevent_req *req);
55
56 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
57                                     void *pdata,
58                                     size_t data_size,
59                                     const char *type,
60                                     const char *location)
61 {
62         struct tevent_req *req;
63         void **ppdata = (void **)pdata;
64         void *data;
65
66         req = talloc_pooled_object(
67                 mem_ctx, struct tevent_req, 2,
68                 sizeof(struct tevent_immediate) + data_size);
69         if (req == NULL) {
70                 return NULL;
71         }
72
73         *req = (struct tevent_req) {
74                 .internal.private_type          = type,
75                 .internal.create_location       = location,
76                 .internal.state                 = TEVENT_REQ_IN_PROGRESS,
77                 .internal.trigger               = tevent_create_immediate(req)
78         };
79
80         if (!req->internal.trigger) {
81                 talloc_free(req);
82                 return NULL;
83         }
84
85         data = talloc_zero_size(req, data_size);
86         if (data == NULL) {
87                 talloc_free(req);
88                 return NULL;
89         }
90         talloc_set_name_const(data, type);
91
92         req->data = data;
93
94         talloc_set_destructor(req, tevent_req_destructor);
95
96         *ppdata = data;
97         return req;
98 }
99
100 static int tevent_req_destructor(struct tevent_req *req)
101 {
102         tevent_req_received(req);
103         return 0;
104 }
105
106 void _tevent_req_notify_callback(struct tevent_req *req, const char *location)
107 {
108         req->internal.finish_location = location;
109         if (req->internal.defer_callback_ev) {
110                 (void)tevent_req_post(req, req->internal.defer_callback_ev);
111                 req->internal.defer_callback_ev = NULL;
112                 return;
113         }
114         if (req->async.fn != NULL) {
115                 req->async.fn(req);
116         }
117 }
118
119 static void tevent_req_cleanup(struct tevent_req *req)
120 {
121         if (req->private_cleanup.fn == NULL) {
122                 return;
123         }
124
125         if (req->private_cleanup.state >= req->internal.state) {
126                 /*
127                  * Don't call the cleanup_function multiple times for the same
128                  * state recursively
129                  */
130                 return;
131         }
132
133         req->private_cleanup.state = req->internal.state;
134         req->private_cleanup.fn(req, req->internal.state);
135 }
136
137 static void tevent_req_finish(struct tevent_req *req,
138                               enum tevent_req_state state,
139                               const char *location)
140 {
141         /*
142          * make sure we do not timeout after
143          * the request was already finished
144          */
145         TALLOC_FREE(req->internal.timer);
146
147         req->internal.state = state;
148         req->internal.finish_location = location;
149
150         tevent_req_cleanup(req);
151
152         _tevent_req_notify_callback(req, location);
153 }
154
155 void _tevent_req_done(struct tevent_req *req,
156                       const char *location)
157 {
158         tevent_req_finish(req, TEVENT_REQ_DONE, location);
159 }
160
161 bool _tevent_req_error(struct tevent_req *req,
162                        uint64_t error,
163                        const char *location)
164 {
165         if (error == 0) {
166                 return false;
167         }
168
169         req->internal.error = error;
170         tevent_req_finish(req, TEVENT_REQ_USER_ERROR, location);
171         return true;
172 }
173
174 void _tevent_req_oom(struct tevent_req *req, const char *location)
175 {
176         tevent_req_finish(req, TEVENT_REQ_NO_MEMORY, location);
177 }
178
179 bool _tevent_req_nomem(const void *p,
180                        struct tevent_req *req,
181                        const char *location)
182 {
183         if (p != NULL) {
184                 return false;
185         }
186         _tevent_req_oom(req, location);
187         return true;
188 }
189
190 /**
191  * @internal
192  *
193  * @brief Immediate event callback.
194  *
195  * @param[in]  ev       The event context to use.
196  *
197  * @param[in]  im       The immediate event.
198  *
199  * @param[in]  priv     The async request to be finished.
200  */
201 static void tevent_req_trigger(struct tevent_context *ev,
202                                struct tevent_immediate *im,
203                                void *private_data)
204 {
205         struct tevent_req *req =
206                 talloc_get_type_abort(private_data,
207                 struct tevent_req);
208
209         tevent_req_finish(req, req->internal.state,
210                           req->internal.finish_location);
211 }
212
213 struct tevent_req *tevent_req_post(struct tevent_req *req,
214                                    struct tevent_context *ev)
215 {
216         tevent_schedule_immediate(req->internal.trigger,
217                                   ev, tevent_req_trigger, req);
218         return req;
219 }
220
221 void tevent_req_defer_callback(struct tevent_req *req,
222                                struct tevent_context *ev)
223 {
224         req->internal.defer_callback_ev = ev;
225 }
226
227 bool tevent_req_is_in_progress(struct tevent_req *req)
228 {
229         if (req->internal.state == TEVENT_REQ_IN_PROGRESS) {
230                 return true;
231         }
232
233         return false;
234 }
235
236 void tevent_req_received(struct tevent_req *req)
237 {
238         talloc_set_destructor(req, NULL);
239
240         req->private_print = NULL;
241         req->private_cancel = NULL;
242
243         TALLOC_FREE(req->internal.trigger);
244         TALLOC_FREE(req->internal.timer);
245
246         req->internal.state = TEVENT_REQ_RECEIVED;
247
248         tevent_req_cleanup(req);
249
250         TALLOC_FREE(req->data);
251 }
252
253 bool tevent_req_poll(struct tevent_req *req,
254                      struct tevent_context *ev)
255 {
256         while (tevent_req_is_in_progress(req)) {
257                 int ret;
258
259                 ret = tevent_loop_once(ev);
260                 if (ret != 0) {
261                         return false;
262                 }
263         }
264
265         return true;
266 }
267
268 bool tevent_req_is_error(struct tevent_req *req, enum tevent_req_state *state,
269                         uint64_t *error)
270 {
271         if (req->internal.state == TEVENT_REQ_DONE) {
272                 return false;
273         }
274         if (req->internal.state == TEVENT_REQ_USER_ERROR) {
275                 *error = req->internal.error;
276         }
277         *state = req->internal.state;
278         return true;
279 }
280
281 static void tevent_req_timedout(struct tevent_context *ev,
282                                struct tevent_timer *te,
283                                struct timeval now,
284                                void *private_data)
285 {
286         struct tevent_req *req =
287                 talloc_get_type_abort(private_data,
288                 struct tevent_req);
289
290         TALLOC_FREE(req->internal.timer);
291
292         tevent_req_finish(req, TEVENT_REQ_TIMED_OUT, __FUNCTION__);
293 }
294
295 bool tevent_req_set_endtime(struct tevent_req *req,
296                             struct tevent_context *ev,
297                             struct timeval endtime)
298 {
299         TALLOC_FREE(req->internal.timer);
300
301         req->internal.timer = tevent_add_timer(ev, req, endtime,
302                                                tevent_req_timedout,
303                                                req);
304         if (tevent_req_nomem(req->internal.timer, req)) {
305                 return false;
306         }
307
308         return true;
309 }
310
311 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt)
312 {
313         req->async.fn = fn;
314         req->async.private_data = pvt;
315 }
316
317 void *_tevent_req_callback_data(struct tevent_req *req)
318 {
319         return req->async.private_data;
320 }
321
322 void *_tevent_req_data(struct tevent_req *req)
323 {
324         return req->data;
325 }
326
327 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn)
328 {
329         req->private_print = fn;
330 }
331
332 void tevent_req_set_cancel_fn(struct tevent_req *req, tevent_req_cancel_fn fn)
333 {
334         req->private_cancel = fn;
335 }
336
337 bool _tevent_req_cancel(struct tevent_req *req, const char *location)
338 {
339         if (req->private_cancel == NULL) {
340                 return false;
341         }
342
343         return req->private_cancel(req);
344 }
345
346 void tevent_req_set_cleanup_fn(struct tevent_req *req, tevent_req_cleanup_fn fn)
347 {
348         req->private_cleanup.state = req->internal.state;
349         req->private_cleanup.fn = fn;
350 }