tevent_request (3) - Linux Manuals
NAME
The tevent request functions. -
A tevent_req represents an asynchronous computation.
Typedefs
typedef void(* tevent_req_fn )(struct tevent_req *req)
A tevent request callback function.
typedef char *(* tevent_req_print_fn )(struct tevent_req *req, TALLOC_CTX *ctx)
The print function which can be set for a tevent async request.
typedef bool(* tevent_req_cancel_fn )(struct tevent_req *req)
A typedef for a cancel function for a tevent request.
typedef void(* tevent_req_cleanup_fn )(struct tevent_req *req, enum tevent_req_state req_state)
A typedef for a cleanup function for a tevent request.
Enumerations
enum tevent_req_state { TEVENT_REQ_INIT, TEVENT_REQ_IN_PROGRESS, TEVENT_REQ_DONE, TEVENT_REQ_USER_ERROR, TEVENT_REQ_TIMED_OUT, TEVENT_REQ_NO_MEMORY, TEVENT_REQ_RECEIVED }
An async request moves from TEVENT_REQ_INIT to TEVENT_REQ_IN_PROGRESS.
Functions
void tevent_req_set_callback (struct tevent_req *req, tevent_req_fn fn, void *pvt)
Set an async request callback.
void * tevent_req_callback_data (struct tevent_req *req,#type)
Get the private data cast to the given type for a callback from a tevent request structure.
void * tevent_req_callback_data_void (struct tevent_req *req)
Get the private data for a callback from a tevent request structure.
void * tevent_req_data (struct tevent_req *req,#type)
Get the private data from a tevent request structure.
void tevent_req_set_print_fn (struct tevent_req *req, tevent_req_print_fn fn)
This function sets a print function for the given request.
char * tevent_req_default_print (struct tevent_req *req, TALLOC_CTX *mem_ctx)
The default print function for creating debug messages.
char * tevent_req_print (TALLOC_CTX *mem_ctx, struct tevent_req *req)
Print an tevent_req structure in debug messages.
void tevent_req_set_cancel_fn (struct tevent_req *req, tevent_req_cancel_fn fn)
This function sets a cancel function for the given tevent request.
bool tevent_req_cancel (struct tevent_req *req)
Try to cancel the given tevent request.
void tevent_req_set_cleanup_fn (struct tevent_req *req, tevent_req_cleanup_fn fn)
This function sets a cleanup function for the given tevent request.
struct tevent_req * tevent_req_create (TALLOC_CTX *mem_ctx, void **pstate,#type)
Create an async tevent request.
bool tevent_req_set_endtime (struct tevent_req *req, struct tevent_context *ev, struct timeval endtime)
Set a timeout for an async request.
void tevent_req_notify_callback (struct tevent_req *req)
Call the notify callback of the given tevent request manually.
void tevent_req_done (struct tevent_req *req)
An async request has successfully finished.
bool tevent_req_error (struct tevent_req *req, uint64_t error)
An async request has seen an error.
bool tevent_req_nomem (const void *p, struct tevent_req *req)
Helper function for nomem check.
void tevent_req_oom (struct tevent_req *req)
Indicate out of memory to a request.
struct tevent_req * tevent_req_post (struct tevent_req *req, struct tevent_context *ev)
Finish a request before the caller had the change to set the callback.
void tevent_req_defer_callback (struct tevent_req *req, struct tevent_context *ev)
Finish multiple requests within one function.
bool tevent_req_is_in_progress (struct tevent_req *req)
Check if the given request is still in progress.
bool tevent_req_poll (struct tevent_req *req, struct tevent_context *ev)
Actively poll for the given request to finish.
bool tevent_req_is_error (struct tevent_req *req, enum tevent_req_state *state, uint64_t *error)
Get the tevent request state and the actual error set by tevent_req_error.
void tevent_req_received (struct tevent_req *req)
Use as the last action of a _recv() function.
struct tevent_req * tevent_wakeup_send (TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct timeval wakeup_time)
Create a tevent subrequest at a given time.
bool tevent_wakeup_recv (struct tevent_req *req)
Check if the wakeup has been correctly executed.
Detailed Description
A tevent_req represents an asynchronous computation.
The tevent_req group of API calls is the recommended way of programming async computations within tevent. In particular the file descriptor (tevent_add_fd) and timer (tevent_add_timed) events are considered too low-level to be used in larger computations. To read and write from and to sockets, Samba provides two calls on top of tevent_add_fd: tstream_read_packet_send/recv and tstream_writev_send/recv. These requests are much easier to compose than the low-level event handlers called from tevent_add_fd.
A lot of the simplicity tevent_req has brought to the notoriously hairy async programming came via a set of conventions that every async computation programmed should follow. One central piece of these conventions is the naming of routines and variables.
Every async computation needs a name (sensibly called 'computation' down from here). From this name quite a few naming conventions are derived.
Every computation that requires local state needs a
* struct computation_state { * int local_var; * }; *
An async computation is started by a computation_send function. When it is finished, its result can be received by a computation_recv function. For an example how to set up an async computation, see the code example in the documentation for tevent_req_create() and tevent_req_post(). The prototypes for _send and _recv functions should follow some conventions:
The 'int' result of computation_recv() depends on the result the sync version of the function would have, 'int' is just an example here.
Another important piece of the conventions is that the program flow is interrupted as little as possible. Because a blocking sub-computation requires that the flow needs to continue in a separate function that is the logical sequel of some computation, it should lexically follow sending off the blocking sub-computation. Setting the callback function via tevent_req_set_callback() requires referencing a function lexically below the call to tevent_req_set_callback(), forward declarations are required. A lot of the async computations thus begin with a sequence of declarations such as
It really helps readability a lot to do these forward declarations, because the lexically sequential program flow makes the async computations almost as clear to read as a normal, sync program flow.
It is up to the user of the async computation to talloc_free it after it has finished. If an async computation should be aborted, the tevent_req structure can be talloc_free'ed. After it has finished, it should talloc_free'ed by the API user.
A typedef for a cancel function for a tevent request.
Parameters:
Returns:
A typedef for a cleanup function for a tevent request.
Parameters:
A tevent request callback function.
Parameters:
The print function which can be set for a tevent async request.
Parameters:
Returns:
Example:
An async request moves from TEVENT_REQ_INIT to TEVENT_REQ_IN_PROGRESS. All other states are valid after a request has finished.
Enumerator
Get the private data cast to the given type for a callback from a tevent request structure.
Parameters:
Returns:
Get the private data for a callback from a tevent request structure.
Parameters:
Returns:
Try to cancel the given tevent request. This function can be used to cancel the given request.
It is only possible to cancel a request when the implementation has registered a cancel function via the tevent_req_set_cancel_fn().
Parameters:
Returns:
Note:
Create an async tevent request. The new async request will be initialized in state TEVENT_REQ_IN_PROGRESS.
Tevent_req_create() allocates and zeros the state variable as a talloc child of its result. The state variable should be used as the talloc parent for all temporary variables that are allocated during the async computation. This way, when the user of the async computation frees the request, the state as a talloc child will be free'd along with all the temporary variables hanging off the state.
Parameters:
Returns:
Get the private data from a tevent request structure. When the tevent_req has been created by tevent_req_create, the result of tevent_req_data() is the state variable created by tevent_req_create() as a child of the req.
Parameters:
Returns:
The default print function for creating debug messages. The function should not be used by users of the async API, but custom print function can use it and append custom text to the string.
Parameters:
Returns:
Finish multiple requests within one function. Normally tevent_req_notify_callback() and all wrappers (e.g. tevent_req_done() and tevent_req_error()) need to be the last thing an event handler should call. This is because the callback is likely to destroy the context of the current function.
If a function wants to notify more than one caller, it is dangerous if it just triggers multiple callbacks in a row. With tevent_req_defer_callback() it is possible to set an event context that will be used to defer the callback via an immediate event (similar to tevent_req_post()).
Parameters:
Returns:
An async request has successfully finished. This function is to be used by implementors of async requests. When a request is successfully finished, this function calls the user's completion function.
Parameters:
An async request has seen an error. This function is to be used by implementors of async requests. When a request can not successfully completed, the implementation should call this function with the appropriate status code.
If error is 0 the function returns false and does nothing more.
Parameters:
Returns:
Get the tevent request state and the actual error set by tevent_req_error.
Parameters:
Returns:
See Also:
Check if the given request is still in progress. It is typically used by sync wrapper functions.
Parameters:
Returns:
Helper function for nomem check. Convenience helper to easily check alloc failure within a callback implementing the next step of an async request.
Parameters:
Call the notify callback of the given tevent request manually.
Parameters:
See Also:
Indicate out of memory to a request.
Parameters:
Actively poll for the given request to finish. This function is typically used by sync wrapper functions.
Parameters:
Returns:
Note:
Finish a request before the caller had the change to set the callback. An implementation of an async request might find that it can either finish the request without waiting for an external event, or it can not even start the engine. To present the illusion of a callback to the user of the API, the implementation can call this helper function which triggers an immediate event. This way the caller can use the same calling conventions, independent of whether the request was actually deferred.
Parameters:
Returns:
Print an tevent_req structure in debug messages. This function should be used by callers of the async API.
Parameters:
Returns:
Use as the last action of a _recv() function. This function destroys the attached private data.
Parameters:
Set an async request callback. See the documentation of tevent_req_post() for an example how this is supposed to be used.
Parameters:
This function sets a cancel function for the given tevent request. This function can be used to setup a cancel function for the given request. This will be triggered if the tevent_req_cancel() function was called on the given request.
Parameters:
This function sets a cleanup function for the given tevent request. This function can be used to setup a cleanup function for the given request. This will be triggered when the tevent_req_done() or tevent_req_error() function was called, before notifying the callers callback function, and also before scheduling the deferred trigger.
This might be useful if more than one tevent_req belong together and need to finish both requests at the same time.
The cleanup function is able to call tevent_req_done() or tevent_req_error() recursively, the cleanup function is only triggered the first time.
The cleanup function is also called by tevent_req_received() (possibly triggered from tevent_req_destructor()) before destroying the private data of the tevent_req.
Parameters:
Set a timeout for an async request.
Parameters:
Returns:
This function sets a print function for the given request. This function can be used to setup a print function for the given request. This will be triggered if the tevent_req_print() function was called on the given request.
Parameters:
Note:
Check if the wakeup has been correctly executed. This function needs to be called in the callback function set after calling tevent_wakeup_send().
Parameters:
Returns:
See Also:
Create a tevent subrequest at a given time. The idea is that always the same syntax for tevent requests.
Parameters:
Returns:
Example:
See Also:
Generated automatically by Doxygen for tevent from the source code.
* struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
* struct tevent_req *ev,
* ... further args);
* int computation_recv(struct tevent_req *req, ... further output args);
*
* static void computation_step1_done(struct tevent_req *subreq);
* static void computation_step2_done(struct tevent_req *subreq);
* static void computation_step3_done(struct tevent_req *subreq);
*
Typedef Documentation
typedef bool(* tevent_req_cancel_fn)(struct tevent_req *req)
typedef void(* tevent_req_cleanup_fn)(struct tevent_req *req, enum tevent_req_state req_state)
req_state The current tevent_req_state.
typedef void(* tevent_req_fn)(struct tevent_req *req)
typedef char*(* tevent_req_print_fn)(struct tevent_req *req, TALLOC_CTX *ctx)
ctx A talloc memory context which can be uses to allocate memory.
* static char *my_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
* {
* struct my_data *data = tevent_req_data(req, struct my_data);
* char *result;
*
* result = tevent_req_default_print(mem_ctx, req);
* if (result == NULL) {
* return NULL;
* }
*
* return talloc_asprintf_append_buffer(result, "foo=%d, bar=%d",
* data->foo, data->bar);
* }
*
Enumeration Type Documentation
enum tevent_req_state
Function Documentation
void* tevent_req_callback_data (struct tevent_req *req, #type)
* static void computation_done(struct tevent_req *subreq) {
* struct tevent_req *req = tevent_req_callback_data(subreq, struct tevent_req);
* struct computation_state *state = tevent_req_data(req, struct computation_state);
* .... more things, eventually maybe call tevent_req_done(req);
* }
*
type The type of the private callback data to get.
void* tevent_req_callback_data_void (struct tevent_req *req)
req The structure to get the data from.
bool tevent_req_cancel (struct tevent_req *req)
struct tevent_req* tevent_req_create (TALLOC_CTX *mem_ctx, void **pstate, #type)
* struct tevent_req *req;
* struct computation_state *state;
* req = tevent_req_create(mem_ctx, &state, struct computation_state);
*
pstate Pointer to the private request state.
type The name of the request.
void* tevent_req_data (struct tevent_req *req, #type)
type The type of the private data
char* tevent_req_default_print (struct tevent_req *req, TALLOC_CTX *mem_ctx)
mem_ctx The memory context for the result.
void tevent_req_defer_callback (struct tevent_req *req, struct tevent_context *ev)
* struct complete_state {
* struct tevent_context *ev;
*
* struct tevent_req **reqs;
* };
*
* void complete(struct complete_state *state)
* {
* size_t i, c = talloc_array_length(state->reqs);
*
* for (i=0; i < c; i++) {
* tevent_req_defer_callback(state->reqs[i], state->ev);
* tevent_req_done(state->reqs[i]);
* }
* }
*
ev The tevent_context for the immediate event.
void tevent_req_done (struct tevent_req *req)
bool tevent_req_error (struct tevent_req *req, uint64_terror)
error The error code.
* int error = first_function();
* if (tevent_req_error(req, error)) {
* return;
* }
*
* error = second_function();
* if (tevent_req_error(req, error)) {
* return;
* }
*
* tevent_req_done(req);
* return;
*
bool tevent_req_is_error (struct tevent_req *req, enum tevent_req_state *state, uint64_t *error)
* int computation_recv(struct tevent_req *req, uint64_t *perr)
* {
* enum tevent_req_state state;
* uint64_t err;
* if (tevent_req_is_error(req, &state, &err)) {
* *perr = err;
* return -1;
* }
* return 0;
* }
*
state A pointer to store the tevent request error state.
error A pointer to store the error set by tevent_req_error().
bool tevent_req_is_in_progress (struct tevent_req *req)
bool tevent_req_nomem (const void *p, struct tevent_req *req)
req The request being processed.
* p = talloc(mem_ctx, bla);
* if (tevent_req_nomem(p, req)) {
* return;
* }
*
void tevent_req_notify_callback (struct tevent_req *req)
void tevent_req_oom (struct tevent_req *req)
bool tevent_req_poll (struct tevent_req *req, struct tevent_context *ev)
ev The tevent_context to be used.
* req = tstream_writev_queue_send(mem_ctx,
* ev_ctx,
* tstream,
* send_queue,
* iov, 2);
* ok = tevent_req_poll(req, tctx->ev);
* rc = tstream_writev_queue_recv(req, &sys_errno);
* TALLOC_FREE(req);
*
struct tevent_req* tevent_req_post (struct tevent_req *req, struct tevent_context *ev)
* struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
* struct tevent_context *ev)
* {
* struct tevent_req *req, *subreq;
* struct computation_state *state;
* req = tevent_req_create(mem_ctx, &state, struct computation_state);
* if (req == NULL) {
* return NULL;
* }
* subreq = subcomputation_send(state, ev);
* if (tevent_req_nomem(subreq, req)) {
* return tevent_req_post(req, ev);
* }
* tevent_req_set_callback(subreq, computation_done, req);
* return req;
* }
*
ev The tevent_context for the immediate event.
char* tevent_req_print (TALLOC_CTX *mem_ctx, struct tevent_req *req)
req The request to be printed.
void tevent_req_received (struct tevent_req *req)
void tevent_req_set_callback (struct tevent_req *req, tevent_req_fnfn, void *pvt)
fn The callback function to set.
pvt A pointer to private data to pass to the async request callback.
void tevent_req_set_cancel_fn (struct tevent_req *req, tevent_req_cancel_fnfn)
fn A pointer to the cancel function.
void tevent_req_set_cleanup_fn (struct tevent_req *req, tevent_req_cleanup_fnfn)
fn A pointer to the cancel function.
bool tevent_req_set_endtime (struct tevent_req *req, struct tevent_context *ev, struct timevalendtime)
ev The event context to use for the timer.
endtime The endtime of the request.
void tevent_req_set_print_fn (struct tevent_req *req, tevent_req_print_fnfn)
fn A pointer to the print function
bool tevent_wakeup_recv (struct tevent_req *req)
struct tevent_req* tevent_wakeup_send (TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct timevalwakeup_time)
ev The event handle to setup the request.
wakeup_time The time to wakeup and execute the request.
* static void my_callback_wakeup_done(tevent_req *subreq)
* {
* struct tevent_req *req = tevent_req_callback_data(subreq,
* struct tevent_req);
* bool ok;
*
* ok = tevent_wakeup_recv(subreq);
* TALLOC_FREE(subreq);
* if (!ok) {
* tevent_req_error(req, -1);
* return;
* }
* ...
* }
*
* subreq = tevent_wakeup_send(mem_ctx, ev, wakeup_time);
* if (tevent_req_nomem(subreq, req)) {
* return false;
* }
* tevent_set_callback(subreq, my_callback_wakeup_done, req);
*
Author