2 Unix SMB/CIFS implementation.
3 Infrastructure for async requests
4 Copyright (C) Volker Lendecke 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "lib/tevent/tevent.h"
22 #include "lib/talloc/talloc.h"
23 #include "lib/util/dlinklist.h"
24 #include "lib/async_req/async_req.h"
27 #define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
31 * @brief Print an async_req structure
32 * @param[in] mem_ctx The memory context for the result
33 * @param[in] req The request to be printed
34 * @retval Text representation of req
36 * This is a default print function for async requests. Implementations should
37 * override this with more specific information.
39 * This function should not be used by async API users, this is non-static
40 * only to allow implementations to easily provide default information in
41 * their specific functions.
44 char *async_req_print(TALLOC_CTX *mem_ctx, struct async_req *req)
46 return talloc_asprintf(mem_ctx, "async_req: state=%d, error=%d, "
47 "priv=%s", req->state, (int)req->error,
48 talloc_get_name(req->private_data));
52 * @brief Create an async request
53 * @param[in] mem_ctx The memory context for the result
54 * @param[in] ev The event context this async request will be driven by
55 * @retval A new async request
57 * The new async request will be initialized in state ASYNC_REQ_IN_PROGRESS
60 struct async_req *async_req_new(TALLOC_CTX *mem_ctx)
62 struct async_req *result;
64 result = talloc_zero(mem_ctx, struct async_req);
68 result->state = ASYNC_REQ_IN_PROGRESS;
69 result->print = async_req_print;
73 static void async_req_finish(struct async_req *req, enum async_req_state state)
76 if (req->async.fn != NULL) {
82 * @brief An async request has successfully finished
83 * @param[in] req The finished request
85 * async_req_done is to be used by implementors of async requests. When a
86 * request is successfully finished, this function calls the user's completion
90 void async_req_done(struct async_req *req)
92 async_req_finish(req, ASYNC_REQ_DONE);
96 * @brief An async request has seen an error
97 * @param[in] req The request with an error
98 * @param[in] error The error code
100 * async_req_done is to be used by implementors of async requests. When a
101 * request can not successfully completed, the implementation should call this
102 * function with the appropriate status code.
105 void async_req_error(struct async_req *req, uint64_t error)
108 async_req_finish(req, ASYNC_REQ_USER_ERROR);
112 * @brief Timed event callback
113 * @param[in] ev Event context
114 * @param[in] te The timed event
115 * @param[in] now zero time
116 * @param[in] priv The async request to be finished
119 static void async_trigger(struct tevent_context *ev, struct tevent_timer *te,
120 struct timeval now, void *priv)
122 struct async_req *req = talloc_get_type_abort(priv, struct async_req);
125 if (req->error == 0) {
129 async_req_error(req, req->error);
134 * @brief Helper function for nomem check
135 * @param[in] p The pointer to be checked
136 * @param[in] req The request being processed
138 * Convenience helper to easily check alloc failure within a callback
139 * implementing the next step of an async request.
141 * Call pattern would be
143 * p = talloc(mem_ctx, bla);
144 * if (async_req_ntnomem(p, req)) {
150 bool async_req_nomem(const void *p, struct async_req *req)
155 async_req_finish(req, ASYNC_REQ_NO_MEMORY);
160 * @brief Finish a request before it started processing
161 * @param[in] req The finished request
162 * @param[in] status The success code
164 * An implementation of an async request might find that it can either finish
165 * the request without waiting for an external event, or it can't even start
166 * the engine. To present the illusion of a callback to the user of the API,
167 * the implementation can call this helper function which triggers an
168 * immediate timed event. This way the caller can use the same calling
169 * conventions, independent of whether the request was actually deferred.
172 bool async_post_error(struct async_req *req, struct tevent_context *ev,
177 if (tevent_add_timer(ev, req, tevent_timeval_zero(),
178 async_trigger, req) == NULL) {
184 bool async_req_is_error(struct async_req *req, enum async_req_state *state,
187 if (req->state == ASYNC_REQ_DONE) {
190 if (req->state == ASYNC_REQ_USER_ERROR) {
197 static void async_req_timedout(struct tevent_context *ev,
198 struct tevent_timer *te,
202 struct async_req *req = talloc_get_type_abort(priv, struct async_req);
204 async_req_finish(req, ASYNC_REQ_TIMED_OUT);
207 bool async_req_set_timeout(struct async_req *req, struct tevent_context *ev,
210 return (tevent_add_timer(
212 tevent_timeval_current_ofs(to.tv_sec, to.tv_usec),
213 async_req_timedout, req)
217 struct async_req *async_wait_send(TALLOC_CTX *mem_ctx,
218 struct tevent_context *ev,
221 struct async_req *result;
223 result = async_req_new(mem_ctx);
224 if (result == NULL) {
227 if (!async_req_set_timeout(result, ev, to)) {
234 bool async_wait_recv(struct async_req *req)
239 struct async_queue_entry {
240 struct async_queue_entry *prev, *next;
241 struct async_req_queue *queue;
242 struct async_req *req;
243 void (*trigger)(struct async_req *req);
246 struct async_req_queue {
247 struct async_queue_entry *queue;
250 struct async_req_queue *async_req_queue_init(TALLOC_CTX *mem_ctx)
252 return talloc_zero(mem_ctx, struct async_req_queue);
255 static int async_queue_entry_destructor(struct async_queue_entry *e)
257 struct async_req_queue *queue = e->queue;
259 DLIST_REMOVE(queue->queue, e);
261 if (queue->queue != NULL) {
262 queue->queue->trigger(queue->queue->req);
268 static void async_req_immediate_trigger(struct tevent_context *ev,
269 struct tevent_timer *te,
273 struct async_queue_entry *e = talloc_get_type_abort(
274 priv, struct async_queue_entry);
280 bool async_req_enqueue(struct async_req_queue *queue, struct tevent_context *ev,
281 struct async_req *req,
282 void (*trigger)(struct async_req *req))
284 struct async_queue_entry *e;
287 busy = (queue->queue != NULL);
289 e = talloc(req, struct async_queue_entry);
295 e->trigger = trigger;
298 DLIST_ADD_END(queue->queue, e, struct async_queue_entry *);
299 talloc_set_destructor(e, async_queue_entry_destructor);
302 struct tevent_timer *te;
304 te = tevent_add_timer(ev, e, tevent_timeval_zero(),
305 async_req_immediate_trigger,
316 bool _async_req_setup(TALLOC_CTX *mem_ctx, struct async_req **preq,
317 void *pstate, size_t state_size, const char *typename)
319 struct async_req *req;
320 void **ppstate = (void **)pstate;
323 req = async_req_new(mem_ctx);
327 state = talloc_size(req, state_size);
332 talloc_set_name_const(state, typename);
333 req->private_data = state;