- A tevent_req represents an asynchronous computation.
typedef void(* tevent_req_fn) (struct tevent_req *subreq)
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.
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.
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_reset_endtime (struct tevent_req *req)
Reset the timer set by tevent_req_set_endtime.
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 a chance 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.
bool tevent_req_set_profile (struct tevent_req *req)
Mark a tevent_req for profiling.
const struct tevent_req_profile * tevent_req_get_profile (struct tevent_req *req)
Get the a request's profile for inspection.
struct tevent_req_profile * tevent_req_move_profile (struct tevent_req *req, TALLOC_CTX *mem_ctx)
Move the profile out of a request.
void tevent_req_profile_get_name (const struct tevent_req_profile *profile, const char **req_name)
Get a profile description.
void tevent_req_profile_get_start (const struct tevent_req_profile *profile, const char **start_location, struct timeval *start_time)
Get a profile's start event data.
void tevent_req_profile_get_stop (const struct tevent_req_profile *profile, const char **stop_location, struct timeval *stop_time)
Get a profile's stop event data.
void tevent_req_profile_get_status (const struct tevent_req_profile *profile, pid_t *pid, enum tevent_req_state *state, uint64_t *user_error)
Get a profile's result data.
const struct tevent_req_profile * tevent_req_profile_get_subprofiles (const struct tevent_req_profile *profile)
Retrieve the first subreq's profile from a profile.
const struct tevent_req_profile * tevent_req_profile_next (const struct tevent_req_profile *profile)
Walk the chain of subreqs.
struct tevent_req_profile * tevent_req_profile_create (TALLOC_CTX *mem_ctx)
Create a fresh tevent_req_profile.
bool tevent_req_profile_set_name (struct tevent_req_profile *profile, const char *name)
Set a profile's name.
bool tevent_req_profile_set_start (struct tevent_req_profile *profile, const char *start_location, struct timeval start_time)
Set a profile's start event.
bool tevent_req_profile_set_stop (struct tevent_req_profile *profile, const char *stop_location, struct timeval stop_time)
Set a profile's stop event.
void tevent_req_profile_set_status (struct tevent_req_profile *profile, pid_t pid, enum tevent_req_state state, uint64_t user_error)
Set a profile's exit status.
void tevent_req_profile_append_sub (struct tevent_req_profile *parent_profile, struct tevent_req_profile **sub_profile)
Add a subprofile to a profile.
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.
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; };
Even if no local variables are required, such a state struct should be created containing a dummy variable. Quite a few helper functions and macros (for example tevent_req_create()) assume such a state struct.
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:
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);
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
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);
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.
tevent_req variable naming conventions:
The name of the variable pointing to the tevent_req structure returned by a _send() function SHOULD be named differently between implementation and caller.
From the point of view of the implementation (of the _send() and _recv() functions) the variable returned by tevent_req_create() is always called req.
While the caller of the _send() function should use subreq to hold the result.
See also
tevent_req_fn()
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:
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); }
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.
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); }
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.
struct tevent_req *req; struct computation_state *state; req = tevent_req_create(mem_ctx, &state, struct computation_state);
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()).
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]); } }
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
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;
Get the a request's profile for inspection.
Parameters
Returns
Get the tevent request state and the actual error set by tevent_req_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; }
Parameters
Returns
See also
Check if the given request is still in progress. It is typically used by sync wrapper functions.
Parameters
Returns
Move the profile out of a request. This function detaches the request's profile from the request, so that the profile can outlive the request in a _recv function.
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
p = talloc(mem_ctx, bla); if (tevent_req_nomem(p, req)) { return; }
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
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);
Finish a request before the caller had a chance 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.
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; }
Parameters
Returns
Print an tevent_req structure in debug messages. This function should be used by callers of the async API.
Parameters
Returns
Add a subprofile to a profile.
Parameters
Create a fresh tevent_req_profile.
Parameters
Returns
Get a profile description.
Parameters
Get a profile's start event data.
Parameters
Get a profile's result data.
Parameters
Get a profile's stop event data.
Parameters
Retrieve the first subreq's profile from a profile.
Parameters
Returns
Walk the chain of subreqs.
Parameters
Returns
Set a profile's name.
Parameters
Returns
Set a profile's start event.
Parameters
Returns
Set a profile's exit status.
Parameters
Set a profile's stop event.
Parameters
Returns
Use as the last action of a _recv() function. This function destroys the attached private data.
Parameters
Reset the timer set by tevent_req_set_endtime.
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. On failure, 'req' is already set to state TEVENT_REQ_NO_MEMORY.
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
Mark a tevent_req for profiling. This will turn on profiling for this tevent_req an all subreqs that are directly started as helper requests off this tevent_req. subreqs are chained by walking up the talloc_parent hierarchy at a subreq's tevent_req_create. This means to get the profiling chain right the subreq that needs to be profiled as part of this tevent_req's profile must be a talloc child of the requests state variable.
Parameters
Returns
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:
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);
See also
Generated automatically by Doxygen for tevent from the source code.