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 char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
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,
37 (unsigned long long)req->internal.error,
38 (unsigned long long)req->internal.error,
39 talloc_get_name(req->data),
45 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req)
47 if (!req->private_print) {
48 return tevent_req_default_print(req, mem_ctx);
51 return req->private_print(req, mem_ctx);
54 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
60 struct tevent_req *req;
61 void **ppdata = (void **)pdata;
64 req = talloc_zero(mem_ctx, struct tevent_req);
68 req->internal.private_type = type;
69 req->internal.create_location = location;
70 req->internal.finish_location = NULL;
71 req->internal.state = TEVENT_REQ_IN_PROGRESS;
72 req->internal.trigger = tevent_create_immediate(req);
73 if (!req->internal.trigger) {
78 data = talloc_zero_size(req, data_size);
83 talloc_set_name_const(data, type);
91 void _tevent_req_notify_callback(struct tevent_req *req, const char *location)
93 req->internal.finish_location = location;
94 if (req->async.fn != NULL) {
99 static void tevent_req_finish(struct tevent_req *req,
100 enum tevent_req_state state,
101 const char *location)
103 req->internal.state = state;
104 _tevent_req_notify_callback(req, location);
107 void _tevent_req_done(struct tevent_req *req,
108 const char *location)
110 tevent_req_finish(req, TEVENT_REQ_DONE, location);
113 bool _tevent_req_error(struct tevent_req *req,
115 const char *location)
121 req->internal.error = error;
122 tevent_req_finish(req, TEVENT_REQ_USER_ERROR, location);
126 bool _tevent_req_nomem(const void *p,
127 struct tevent_req *req,
128 const char *location)
133 tevent_req_finish(req, TEVENT_REQ_NO_MEMORY, location);
140 * @brief Immediate event callback.
142 * @param[in] ev The event context to use.
144 * @param[in] im The immediate event.
146 * @param[in] priv The async request to be finished.
148 static void tevent_req_trigger(struct tevent_context *ev,
149 struct tevent_immediate *im,
152 struct tevent_req *req = talloc_get_type(private_data,
155 tevent_req_finish(req, req->internal.state,
156 req->internal.finish_location);
159 struct tevent_req *tevent_req_post(struct tevent_req *req,
160 struct tevent_context *ev)
162 tevent_schedule_immediate(req->internal.trigger,
163 ev, tevent_req_trigger, req);
167 bool tevent_req_is_in_progress(struct tevent_req *req)
169 if (req->internal.state == TEVENT_REQ_IN_PROGRESS) {
176 void tevent_req_received(struct tevent_req *req)
178 TALLOC_FREE(req->data);
179 req->private_print = NULL;
181 TALLOC_FREE(req->internal.trigger);
182 TALLOC_FREE(req->internal.timer);
184 req->internal.state = TEVENT_REQ_RECEIVED;
187 bool tevent_req_poll(struct tevent_req *req,
188 struct tevent_context *ev)
190 while (tevent_req_is_in_progress(req)) {
193 ret = tevent_loop_once(ev);
202 bool tevent_req_is_error(struct tevent_req *req, enum tevent_req_state *state,
205 if (req->internal.state == TEVENT_REQ_DONE) {
208 if (req->internal.state == TEVENT_REQ_USER_ERROR) {
209 *error = req->internal.error;
211 *state = req->internal.state;
215 static void tevent_req_timedout(struct tevent_context *ev,
216 struct tevent_timer *te,
220 struct tevent_req *req = talloc_get_type(private_data,
223 TALLOC_FREE(req->internal.timer);
225 tevent_req_finish(req, TEVENT_REQ_TIMED_OUT, __FUNCTION__);
228 bool tevent_req_set_endtime(struct tevent_req *req,
229 struct tevent_context *ev,
230 struct timeval endtime)
232 TALLOC_FREE(req->internal.timer);
234 req->internal.timer = tevent_add_timer(ev, req, endtime,
237 if (tevent_req_nomem(req->internal.timer, req)) {
244 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt)
247 req->async.private_data = pvt;
250 void *_tevent_req_callback_data(struct tevent_req *req)
252 return req->async.private_data;
255 void *_tevent_req_data(struct tevent_req *req)
260 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn)
262 req->private_print = fn;
265 void tevent_req_set_cancel_fn(struct tevent_req *req, tevent_req_cancel_fn fn)
267 req->private_cancel = fn;
270 bool _tevent_req_cancel(struct tevent_req *req, const char *location)
272 if (req->private_cancel == NULL) {
276 return req->private_cancel(req);