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
;
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
82 void async_req_done(struct async_req
*req
)
85 req
->state
= ASYNC_REQ_DONE
;
86 if (req
->async
.fn
!= NULL
) {
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
)
104 req
->state
= ASYNC_REQ_ERROR
;
105 if (req
->async
.fn
!= NULL
) {
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
);
124 if (req
->error
== 0) {
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
,
150 if (tevent_add_timer(ev
, req
, timeval_zero(),
151 async_trigger
, req
) == NULL
) {
157 bool async_req_is_error(struct async_req
*req
, uint32_t *error
)
159 if (req
->state
< ASYNC_REQ_DONE
) {
162 if (req
->state
== ASYNC_REQ_ERROR
) {
169 static void async_req_timedout(struct tevent_context
*ev
,
170 struct tevent_timer
*te
,
174 struct async_req
*req
= talloc_get_type_abort(
175 priv
, struct async_req
);
177 async_req_nterror(req
, NT_STATUS_IO_TIMEOUT
);
180 bool async_req_set_timeout(struct async_req
*req
, struct tevent_context
*ev
,
183 return (tevent_add_timer(ev
, req
,
184 timeval_current_ofs(to
.tv_sec
, to
.tv_usec
),
185 async_req_timedout
, req
)
189 struct async_req
*async_wait_send(TALLOC_CTX
*mem_ctx
,
190 struct tevent_context
*ev
,
193 struct async_req
*result
;
195 result
= async_req_new(mem_ctx
);
196 if (result
== NULL
) {
199 if (!async_req_set_timeout(result
, ev
, to
)) {
206 bool async_wait_recv(struct async_req
*req
)
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
);
240 static void async_req_immediate_trigger(struct tevent_context
*ev
,
241 struct tevent_timer
*te
,
245 struct async_queue_entry
*e
= talloc_get_type_abort(
246 priv
, struct async_queue_entry
);
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
;
259 busy
= (queue
->queue
!= NULL
);
261 e
= talloc(req
, struct async_queue_entry
);
267 e
->trigger
= trigger
;
270 DLIST_ADD_END(queue
->queue
, e
, struct async_queue_entry
*);
271 talloc_set_destructor(e
, async_queue_entry_destructor
);
274 struct tevent_timer
*te
;
276 te
= tevent_add_timer(ev
, e
, timeval_zero(),
277 async_req_immediate_trigger
,
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
;
295 req
= async_req_new(mem_ctx
);
299 state
= talloc_size(req
, state_size
);
304 talloc_set_name_const(state
, typename
);
305 req
->private_data
= state
;