Make struct tevent_req opaque
[Samba/ekacnet.git] / lib / tevent / tevent_req.c
blob9b3e00ec8fa7bb97a982726a42ca7615a6849612
1 /*
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
9 ** under the LGPL
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 "replace.h"
26 #include "tevent.h"
27 #include "tevent_internal.h"
28 #include "tevent_util.h"
30 /**
31 * @brief The default print function for creating debug messages
32 * @param[in] req The request to be printed
33 * @param[in] mem_ctx The memory context for the result
34 * @retval Text representation of req
36 * The function should not be used by users of the asynx API,
37 * but custom print function can use it and append custom text
38 * to the string.
41 char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
43 return talloc_asprintf(mem_ctx,
44 "tevent_req[%p/%s]: state[%d] error[%lld (0x%llX)] "
45 " state[%s (%p)] timer[%p]",
46 req, req->internal.location,
47 req->internal.state,
48 (unsigned long long)req->internal.error,
49 (unsigned long long)req->internal.error,
50 talloc_get_name(req->data),
51 req->data,
52 req->internal.timer
56 /**
57 * @brief Print an tevent_req structure in debug messages
58 * @param[in] mem_ctx The memory context for the result
59 * @param[in] req The request to be printed
60 * @retval Text representation of req
62 * This function should be used by callers of the async API
65 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req)
67 if (!req->private_print) {
68 return tevent_req_default_print(req, mem_ctx);
71 return req->private_print(req, mem_ctx);
74 /**
75 * @brief Create an async request
76 * @param[in] mem_ctx The memory context for the result
77 * @param[in] ev The event context this async request will be driven by
78 * @retval A new async request
80 * The new async request will be initialized in state ASYNC_REQ_IN_PROGRESS
83 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
84 void *pdata,
85 size_t data_size,
86 const char *type,
87 const char *location)
89 struct tevent_req *req;
90 void **ppdata = (void **)pdata;
91 void *data;
93 req = talloc_zero(mem_ctx, struct tevent_req);
94 if (req == NULL) {
95 return NULL;
97 req->internal.private_type = type;
98 req->internal.location = location;
99 req->internal.state = TEVENT_REQ_IN_PROGRESS;
101 data = talloc_size(req, data_size);
102 if (data == NULL) {
103 talloc_free(req);
104 return NULL;
106 talloc_set_name_const(data, type);
108 req->data = data;
110 *ppdata = data;
111 return req;
114 static void tevent_req_finish(struct tevent_req *req, enum tevent_req_state state)
116 req->internal.state = state;
117 if (req->async.fn != NULL) {
118 req->async.fn(req);
123 * @brief An async request has successfully finished
124 * @param[in] req The finished request
126 * async_req_done is to be used by implementors of async requests. When a
127 * request is successfully finished, this function calls the user's completion
128 * function.
131 void tevent_req_done(struct tevent_req *req)
133 tevent_req_finish(req, TEVENT_REQ_DONE);
137 * @brief An async request has seen an error
138 * @param[in] req The request with an error
139 * @param[in] error The error code
141 * tevent_req_done is to be used by implementors of async requests. When a
142 * request can not successfully completed, the implementation should call this
143 * function with the appropriate status code.
145 * If error is 0 the function returns false and does nothing more.
147 * Call pattern would be
148 * \code
149 * int error = first_function();
150 * if (tevent_req_error(req, error)) {
151 * return;
154 * error = second_function();
155 * if (tevent_req_error(req, error)) {
156 * return;
159 * tevent_req_done(req);
160 * return;
161 * \endcode
164 bool tevent_req_error(struct tevent_req *req, uint64_t error)
166 if (error == 0) {
167 return false;
170 req->internal.error = error;
171 tevent_req_finish(req, TEVENT_REQ_USER_ERROR);
172 return true;
176 * @brief Helper function for nomem check
177 * @param[in] p The pointer to be checked
178 * @param[in] req The request being processed
180 * Convenience helper to easily check alloc failure within a callback
181 * implementing the next step of an async request.
183 * Call pattern would be
184 * \code
185 * p = talloc(mem_ctx, bla);
186 * if (tevent_req_nomem(p, req)) {
187 * return;
189 * \endcode
192 bool tevent_req_nomem(const void *p, struct tevent_req *req)
194 if (p != NULL) {
195 return false;
197 tevent_req_finish(req, TEVENT_REQ_NO_MEMORY);
198 return true;
202 * @brief Timed event callback
203 * @param[in] ev Event context
204 * @param[in] te The timed event
205 * @param[in] now zero time
206 * @param[in] priv The async request to be finished
208 static void tevent_req_trigger(struct tevent_context *ev,
209 struct tevent_timer *te,
210 struct timeval zero,
211 void *private_data)
213 struct tevent_req *req = talloc_get_type(private_data,
214 struct tevent_req);
216 talloc_free(req->internal.trigger);
217 req->internal.trigger = NULL;
219 tevent_req_finish(req, req->internal.state);
223 * @brief Finish a request before the caller had the change to set the callback
224 * @param[in] req The finished request
225 * @param[in] ev The tevent_context for the timed event
226 * @retval On success req will be returned,
227 * on failure req will be destroyed
229 * An implementation of an async request might find that it can either finish
230 * the request without waiting for an external event, or it can't even start
231 * the engine. To present the illusion of a callback to the user of the API,
232 * the implementation can call this helper function which triggers an
233 * immediate timed event. This way the caller can use the same calling
234 * conventions, independent of whether the request was actually deferred.
237 struct tevent_req *tevent_req_post(struct tevent_req *req,
238 struct tevent_context *ev)
240 req->internal.trigger = tevent_add_timer(ev, req, tevent_timeval_zero(),
241 tevent_req_trigger, req);
242 if (!req->internal.trigger) {
243 talloc_free(req);
244 return NULL;
247 return req;
250 bool tevent_req_is_in_progress(struct tevent_req *req)
252 if (req->internal.state == TEVENT_REQ_IN_PROGRESS) {
253 return true;
256 return false;
259 bool tevent_req_poll(struct tevent_req *req,
260 struct tevent_context *ev)
262 while (tevent_req_is_in_progress(req)) {
263 int ret;
265 ret = tevent_loop_once(ev);
266 if (ret != 0) {
267 return false;
271 return true;
274 bool tevent_req_is_error(struct tevent_req *req, enum tevent_req_state *state,
275 uint64_t *error)
277 if (req->internal.state == TEVENT_REQ_DONE) {
278 return false;
280 if (req->internal.state == TEVENT_REQ_USER_ERROR) {
281 *error = req->internal.error;
283 *state = req->internal.state;
284 return true;
287 static void tevent_req_timedout(struct tevent_context *ev,
288 struct tevent_timer *te,
289 struct timeval now,
290 void *private_data)
292 struct tevent_req *req = talloc_get_type(private_data,
293 struct tevent_req);
295 talloc_free(req->internal.timer);
296 req->internal.timer = NULL;
298 tevent_req_finish(req, TEVENT_REQ_TIMED_OUT);
301 bool tevent_req_set_endtime(struct tevent_req *req,
302 struct tevent_context *ev,
303 struct timeval endtime)
305 talloc_free(req->internal.timer);
307 req->internal.timer = tevent_add_timer(ev, req, endtime,
308 tevent_req_timedout,
309 req);
310 if (tevent_req_nomem(req->internal.timer, req)) {
311 return false;
314 return true;
317 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt)
319 req->async.fn = fn;
320 req->async.private_data = pvt;
323 void *_tevent_req_callback_data(struct tevent_req *req)
325 return req->async.private_data;
328 void *_tevent_req_data(struct tevent_req *req)
330 return req->data;
333 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn)
335 req->private_print = fn;