The tevent request functions.(3) | tevent | The tevent request functions.(3) |
NAME¶
The tevent request functions. -
A tevent_req represents an asynchronous computation.
Typedefs¶
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.
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_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. 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. struct tevent_req_profile *
tevent_req_profile_get_subprofiles (const struct tevent_req_profile
*profile)
Retrieve the first subreq's profile from a profile. 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.
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; };
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()
Typedef Documentation¶
typedef bool(* tevent_req_cancel_fn)(struct tevent_req *req)¶
A typedef for a cancel function for a tevent request.
Parameters:
Returns:
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.
Parameters:
req_state The current tevent_req_state.
typedef void(* tevent_req_fn)(struct tevent_req *subreq)¶
A tevent request callback function.
Parameters:
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.
Parameters:
ctx A talloc memory context which can be uses to allocate memory.
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); }
Enumeration Type Documentation¶
enum tevent_req_state¶
An async request moves from TEVENT_REQ_INIT to TEVENT_REQ_IN_PROGRESS. All other states are valid after a request has finished.
Enumerator
- TEVENT_REQ_INIT
- We are creating the request.
- TEVENT_REQ_IN_PROGRESS
- We are waiting the request to complete.
- TEVENT_REQ_DONE
- The request is finished successfully.
- TEVENT_REQ_USER_ERROR
- A user error has occurred. The user error has been indicated by tevent_req_error(), it can be retrieved via tevent_req_is_error().
- TEVENT_REQ_TIMED_OUT
- Request timed out after the timeout set by tevent_req_set_endtime.
- TEVENT_REQ_NO_MEMORY
- An internal allocation has failed, or tevent_req_nomem has been given a NULL pointer as the first argument.
- TEVENT_REQ_RECEIVED
- The request has been received by the caller. No further action is valid.
Function Documentation¶
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.
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:
type The type of the private callback data to get.
Returns:
void* tevent_req_callback_data_void (struct tevent_req *req)¶
Get the private data for a callback from a tevent request structure.
Parameters:
Returns:
bool tevent_req_cancel (struct tevent_req *req)¶
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:
struct tevent_req* tevent_req_create (TALLOC_CTX *mem_ctx, void **pstate, #type)¶
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:
pstate Pointer to the private request state.
type The name of the request.
Returns:
void* tevent_req_data (struct tevent_req *req, #type)¶
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:
type The type of the private data
Returns:
char* tevent_req_default_print (struct tevent_req *req, TALLOC_CTX *mem_ctx)¶
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:
mem_ctx The memory context for the result.
Returns:
void tevent_req_defer_callback (struct tevent_req *req, struct tevent_context *ev)¶
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:
ev The tevent_context for the immediate event.
Returns:
void tevent_req_done (struct tevent_req *req)¶
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:
bool tevent_req_error (struct tevent_req *req, uint64_terror)¶
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:
error The error code.
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;
struct tevent_req_profile* tevent_req_get_profile (struct tevent_req *req)¶
Get the a request's profile for inspection.
Parameters:
Returns:
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.
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:
state A pointer to store the tevent request error state.
error A pointer to store the error set by tevent_req_error().
Returns:
See Also:
bool tevent_req_is_in_progress (struct tevent_req *req)¶
Check if the given request is still in progress. It is typically used by sync wrapper functions.
Parameters:
Returns:
struct tevent_req_profile* tevent_req_move_profile (struct tevent_req *req, TALLOC_CTX *mem_ctx)¶
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:
mem_ctx The new talloc context for the profile
Returns:
bool tevent_req_nomem (const void *p, struct tevent_req *req)¶
Helper function for nomem check. Convenience helper to easily check alloc failure within a callback implementing the next step of an async request.
Parameters:
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)¶
Call the notify callback of the given tevent request manually.
Parameters:
See Also:
void tevent_req_oom (struct tevent_req *req)¶
Indicate out of memory to a request.
Parameters:
bool tevent_req_poll (struct tevent_req *req, struct tevent_context *ev)¶
Actively poll for the given request to finish. This function is typically used by sync wrapper functions.
Parameters:
ev The tevent_context to be used.
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);
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. 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:
ev The tevent_context for the immediate event.
Returns:
char* tevent_req_print (TALLOC_CTX *mem_ctx, struct tevent_req *req)¶
Print an tevent_req structure in debug messages. This function should be used by callers of the async API.
Parameters:
req The request to be printed.
Returns:
void tevent_req_profile_append_sub (struct tevent_req_profile *parent_profile, struct tevent_req_profile **sub_profile)¶
Add a subprofile to a profile.
Parameters:
sub_profile The subreqs profile profile to be added
struct tevent_req_profile* tevent_req_profile_create (TALLOC_CTX *mem_ctx)¶
Create a fresh tevent_req_profile.
Parameters:
Returns:
void tevent_req_profile_get_name (const struct tevent_req_profile *profile, const char **req_name)¶
Get a profile description.
Parameters:
req_name The name of the request (state's name)
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.
Parameters:
start_location The location where this event started
start_time The time this event started
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.
Parameters:
state The status the profile's tevent_req finished with
user_error The user error of the profile's tevent_req
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.
Parameters:
stop_location The location where this event stopped
stop_time The time this event stopped
struct tevent_req_profile* tevent_req_profile_get_subprofiles (const struct tevent_req_profile *profile)¶
Retrieve the first subreq's profile from a profile.
Parameters:
Returns:
struct tevent_req_profile* tevent_req_profile_next (const struct tevent_req_profile *profile)¶
Walk the chain of subreqs.
Parameters:
Returns:
bool tevent_req_profile_set_name (struct tevent_req_profile *profile, const char *name)¶
Set a profile's name.
Parameters:
name The new name for the profile
Returns:
bool tevent_req_profile_set_start (struct tevent_req_profile *profile, const char *start_location, struct timevalstart_time)¶
Set a profile's start event.
Parameters:
start_location The new start location
start_time The new start time
Returns:
void tevent_req_profile_set_status (struct tevent_req_profile *profile, pid_tpid, enum tevent_req_statestate, uint64_tuser_error)¶
Set a profile's exit status.
Parameters:
pid The process where this profile was taken
state The status the profile's tevent_req finished with
user_error The user error of the profile's tevent_req
bool tevent_req_profile_set_stop (struct tevent_req_profile *profile, const char *stop_location, struct timevalstop_time)¶
Set a profile's stop event.
Parameters:
stop_location The new stop location
stop_time The new stop time
Returns:
void tevent_req_received (struct tevent_req *req)¶
Use as the last action of a _recv() function. This function destroys the attached private data.
Parameters:
void tevent_req_reset_endtime (struct tevent_req *req)¶
Reset the timer set by tevent_req_set_endtime.
Parameters:
void tevent_req_set_callback (struct tevent_req *req, tevent_req_fnfn, void *pvt)¶
Set an async request callback. See the documentation of tevent_req_post() for an example how this is supposed to be used.
Parameters:
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)¶
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:
fn A pointer to the cancel function.
void tevent_req_set_cleanup_fn (struct tevent_req *req, tevent_req_cleanup_fnfn)¶
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:
fn A pointer to the cancel function.
bool tevent_req_set_endtime (struct tevent_req *req, struct tevent_context *ev, struct timevalendtime)¶
Set a timeout for an async request. On failure, 'req' is already set to state TEVENT_REQ_NO_MEMORY.
Parameters:
ev The event context to use for the timer.
endtime The endtime of the request.
Returns:
void tevent_req_set_print_fn (struct tevent_req *req, tevent_req_print_fnfn)¶
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:
fn A pointer to the print function
Note:
bool tevent_req_set_profile (struct tevent_req *req)¶
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:
bool tevent_wakeup_recv (struct tevent_req *req)¶
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:
struct tevent_req* tevent_wakeup_send (TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct timevalwakeup_time)¶
Create a tevent subrequest at a given time. The idea is that always the same syntax for tevent requests.
Parameters:
ev The event handle to setup the request.
wakeup_time The time to wakeup and execute the request.
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:
Author¶
Generated automatically by Doxygen for tevent from the source code.
Thu Apr 2 2020 | Version 0.9.8 |