Split up async_req into a generic and a NTSTATUS specific part
[Samba/ekacnet.git] / lib / async_req / async_req.c
blob1b9fc5517bebd78b07fbb4bf0adde38edeedbc43
1 /*
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/>.
20 #include "includes.h"
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"
26 #ifndef TALLOC_FREE
27 #define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
28 #endif
30 /**
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));
51 /**
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);
65 if (result == NULL) {
66 return NULL;
68 result->state = ASYNC_REQ_IN_PROGRESS;
69 result->print = async_req_print;
70 return result;
73 /**
74 * @brief An async request has successfully finished
75 * @param[in] req The finished request
77 * async_req_done is to be used by implementors of async requests. When a
78 * request is successfully finished, this function calls the user's completion
79 * function.
82 void async_req_done(struct async_req *req)
84 req->error = 0;
85 req->state = ASYNC_REQ_DONE;
86 if (req->async.fn != NULL) {
87 req->async.fn(req);
91 /**
92 * @brief An async request has seen an error
93 * @param[in] req The request with an error
94 * @param[in] status The error code
96 * async_req_done is to be used by implementors of async requests. When a
97 * request can not successfully completed, the implementation should call this
98 * function with the appropriate status code.
101 void async_req_error(struct async_req *req, uint32_t error)
103 req->error = error;
104 req->state = ASYNC_REQ_ERROR;
105 if (req->async.fn != NULL) {
106 req->async.fn(req);
111 * @brief Timed event callback
112 * @param[in] ev Event context
113 * @param[in] te The timed event
114 * @param[in] now zero time
115 * @param[in] priv The async request to be finished
118 static void async_trigger(struct tevent_context *ev, struct tevent_timer *te,
119 struct timeval now, void *priv)
121 struct async_req *req = talloc_get_type_abort(priv, struct async_req);
123 TALLOC_FREE(te);
124 if (req->error == 0) {
125 async_req_done(req);
127 else {
128 async_req_error(req, req->error);
133 * @brief Finish a request before it started processing
134 * @param[in] req The finished request
135 * @param[in] status The success code
137 * An implementation of an async request might find that it can either finish
138 * the request without waiting for an external event, or it can't even start
139 * the engine. To present the illusion of a callback to the user of the API,
140 * the implementation can call this helper function which triggers an
141 * immediate timed event. This way the caller can use the same calling
142 * conventions, independent of whether the request was actually deferred.
145 bool async_post_error(struct async_req *req, struct tevent_context *ev,
146 uint32_t error)
148 req->error = error;
150 if (tevent_add_timer(ev, req, timeval_zero(),
151 async_trigger, req) == NULL) {
152 return false;
154 return true;
157 bool async_req_is_error(struct async_req *req, uint32_t *error)
159 if (req->state < ASYNC_REQ_DONE) {
160 return true;
162 if (req->state == ASYNC_REQ_ERROR) {
163 *error = req->error;
164 return true;
166 return false;
169 static void async_req_timedout(struct tevent_context *ev,
170 struct tevent_timer *te,
171 struct timeval now,
172 void *priv)
174 struct async_req *req = talloc_get_type_abort(
175 priv, struct async_req);
176 TALLOC_FREE(te);
177 async_req_nterror(req, NT_STATUS_IO_TIMEOUT);
180 bool async_req_set_timeout(struct async_req *req, struct tevent_context *ev,
181 struct timeval to)
183 return (tevent_add_timer(ev, req,
184 timeval_current_ofs(to.tv_sec, to.tv_usec),
185 async_req_timedout, req)
186 != NULL);
189 struct async_req *async_wait_send(TALLOC_CTX *mem_ctx,
190 struct tevent_context *ev,
191 struct timeval to)
193 struct async_req *result;
195 result = async_req_new(mem_ctx);
196 if (result == NULL) {
197 return result;
199 if (!async_req_set_timeout(result, ev, to)) {
200 TALLOC_FREE(result);
201 return NULL;
203 return result;
206 bool async_wait_recv(struct async_req *req)
208 return true;
211 struct async_queue_entry {
212 struct async_queue_entry *prev, *next;
213 struct async_req_queue *queue;
214 struct async_req *req;
215 void (*trigger)(struct async_req *req);
218 struct async_req_queue {
219 struct async_queue_entry *queue;
222 struct async_req_queue *async_req_queue_init(TALLOC_CTX *mem_ctx)
224 return talloc_zero(mem_ctx, struct async_req_queue);
227 static int async_queue_entry_destructor(struct async_queue_entry *e)
229 struct async_req_queue *queue = e->queue;
231 DLIST_REMOVE(queue->queue, e);
233 if (queue->queue != NULL) {
234 queue->queue->trigger(queue->queue->req);
237 return 0;
240 static void async_req_immediate_trigger(struct tevent_context *ev,
241 struct tevent_timer *te,
242 struct timeval now,
243 void *priv)
245 struct async_queue_entry *e = talloc_get_type_abort(
246 priv, struct async_queue_entry);
248 TALLOC_FREE(te);
249 e->trigger(e->req);
252 bool async_req_enqueue(struct async_req_queue *queue, struct tevent_context *ev,
253 struct async_req *req,
254 void (*trigger)(struct async_req *req))
256 struct async_queue_entry *e;
257 bool busy;
259 busy = (queue->queue != NULL);
261 e = talloc(req, struct async_queue_entry);
262 if (e == NULL) {
263 return false;
266 e->req = req;
267 e->trigger = trigger;
268 e->queue = queue;
270 DLIST_ADD_END(queue->queue, e, struct async_queue_entry *);
271 talloc_set_destructor(e, async_queue_entry_destructor);
273 if (!busy) {
274 struct tevent_timer *te;
276 te = tevent_add_timer(ev, e, timeval_zero(),
277 async_req_immediate_trigger,
279 if (te == NULL) {
280 TALLOC_FREE(e);
281 return false;
285 return true;
288 bool _async_req_setup(TALLOC_CTX *mem_ctx, struct async_req **preq,
289 void *pstate, size_t state_size, const char *typename)
291 struct async_req *req;
292 void **ppstate = (void **)pstate;
293 void *state;
295 req = async_req_new(mem_ctx);
296 if (req == NULL) {
297 return false;
299 state = talloc_size(req, state_size);
300 if (state == NULL) {
301 TALLOC_FREE(req);
302 return false;
304 talloc_set_name_const(state, typename);
305 req->private_data = state;
307 *preq = req;
308 *ppstate = state;
310 return true;