s4-torture: fix some build warnings in rpc samr test.
[Samba.git] / lib / tevent / tevent.h
blobc54cbe2133649df5830ea0fd61f766fafd7d64d4
1 /*
2 Unix SMB/CIFS implementation.
4 generalised event loop handling
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Stefan Metzmacher 2005-2009
8 Copyright (C) Volker Lendecke 2008
10 ** NOTE! The following LGPL license applies to the tevent
11 ** library. This does NOT imply that all of Samba is released
12 ** under the LGPL
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 3 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #ifndef __TEVENT_H__
29 #define __TEVENT_H__
31 #include <stdint.h>
32 #include <talloc.h>
33 #include <sys/time.h>
34 #include <stdbool.h>
36 struct tevent_context;
37 struct tevent_ops;
38 struct tevent_fd;
39 struct tevent_timer;
40 struct tevent_immediate;
41 struct tevent_signal;
43 /**
44 * @defgroup tevent The tevent API
46 * The tevent low-level API
48 * This API provides the public interface to manage events in the tevent
49 * mainloop. Functions are provided for managing low-level events such
50 * as timer events, fd events and signal handling.
52 * @{
55 /* event handler types */
56 /**
57 * Called when a file descriptor monitored by tevent has
58 * data to be read or written on it.
60 typedef void (*tevent_fd_handler_t)(struct tevent_context *ev,
61 struct tevent_fd *fde,
62 uint16_t flags,
63 void *private_data);
65 /**
66 * Called when tevent is ceasing the monitoring of a file descriptor.
68 typedef void (*tevent_fd_close_fn_t)(struct tevent_context *ev,
69 struct tevent_fd *fde,
70 int fd,
71 void *private_data);
73 /**
74 * Called when a tevent timer has fired.
76 typedef void (*tevent_timer_handler_t)(struct tevent_context *ev,
77 struct tevent_timer *te,
78 struct timeval current_time,
79 void *private_data);
81 /**
82 * Called when a tevent immediate event is invoked.
84 typedef void (*tevent_immediate_handler_t)(struct tevent_context *ctx,
85 struct tevent_immediate *im,
86 void *private_data);
88 /**
89 * Called after tevent detects the specified signal.
91 typedef void (*tevent_signal_handler_t)(struct tevent_context *ev,
92 struct tevent_signal *se,
93 int signum,
94 int count,
95 void *siginfo,
96 void *private_data);
98 /**
99 * @brief Create a event_context structure.
101 * This must be the first events call, and all subsequent calls pass this
102 * event_context as the first element. Event handlers also receive this as
103 * their first argument.
105 * @param[in] mem_ctx The memory context to use.
107 * @return An allocated tevent context, NULL on error.
109 * @see tevent_context_init()
111 struct tevent_context *tevent_context_init(TALLOC_CTX *mem_ctx);
114 * @brief Create a event_context structure and select a specific backend.
116 * This must be the first events call, and all subsequent calls pass this
117 * event_context as the first element. Event handlers also receive this as
118 * their first argument.
120 * @param[in] mem_ctx The memory context to use.
122 * @param[in] name The name of the backend to use.
124 * @return An allocated tevent context, NULL on error.
126 struct tevent_context *tevent_context_init_byname(TALLOC_CTX *mem_ctx, const char *name);
129 * @brief Create a custom event context
131 * @param[in] mem_ctx The memory context to use.
132 * @param[in] ops The function pointer table of the backend.
133 * @param[in] additional_data The additional/private data to this instance
135 * @return An allocated tevent context, NULL on error.
138 struct tevent_context *tevent_context_init_ops(TALLOC_CTX *mem_ctx,
139 const struct tevent_ops *ops,
140 void *additional_data);
143 * @brief List available backends.
145 * @param[in] mem_ctx The memory context to use.
147 * @return A string vector with a terminating NULL element, NULL
148 * on error.
150 const char **tevent_backend_list(TALLOC_CTX *mem_ctx);
153 * @brief Set the default tevent backend.
155 * @param[in] backend The name of the backend to set.
157 void tevent_set_default_backend(const char *backend);
159 #ifdef DOXYGEN
161 * @brief Add a file descriptor based event.
163 * @param[in] ev The event context to work on.
165 * @param[in] mem_ctx The talloc memory context to use.
167 * @param[in] fd The file descriptor to base the event on.
169 * @param[in] flags #TEVENT_FD_READ or #TEVENT_FD_WRITE
171 * @param[in] handler The callback handler for the event.
173 * @param[in] private_data The private data passed to the callback handler.
175 * @return The file descriptor based event, NULL on error.
177 * @note To cancel the monitoring of a file descriptor, call talloc_free()
178 * on the object returned by this function.
180 struct tevent_fd *tevent_add_fd(struct tevent_context *ev,
181 TALLOC_CTX *mem_ctx,
182 int fd,
183 uint16_t flags,
184 tevent_fd_handler_t handler,
185 void *private_data);
186 #else
187 struct tevent_fd *_tevent_add_fd(struct tevent_context *ev,
188 TALLOC_CTX *mem_ctx,
189 int fd,
190 uint16_t flags,
191 tevent_fd_handler_t handler,
192 void *private_data,
193 const char *handler_name,
194 const char *location);
195 #define tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
196 _tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data, \
197 #handler, __location__)
198 #endif
200 #ifdef DOXYGEN
202 * @brief Add a timed event
204 * @param[in] ev The event context to work on.
206 * @param[in] mem_ctx The talloc memory context to use.
208 * @param[in] next_event Timeval specifying the absolute time to fire this
209 * event. This is not an offset.
211 * @param[in] handler The callback handler for the event.
213 * @param[in] private_data The private data passed to the callback handler.
215 * @return The newly-created timer event, or NULL on error.
217 * @note To cancel a timer event before it fires, call talloc_free() on the
218 * event returned from this function. This event is automatically
219 * talloc_free()-ed after its event handler files, if it hasn't been freed yet.
221 * @note Unlike some mainloops, tevent timers are one-time events. To set up
222 * a recurring event, it is necessary to call tevent_add_timer() again during
223 * the handler processing.
225 * @note Due to the internal mainloop processing, a timer set to run
226 * immediately will do so after any other pending timers fire, but before
227 * any further file descriptor or signal handling events fire. Callers should
228 * not rely on this behavior!
230 struct tevent_timer *tevent_add_timer(struct tevent_context *ev,
231 TALLOC_CTX *mem_ctx,
232 struct timeval next_event,
233 tevent_timer_handler_t handler,
234 void *private_data);
235 #else
236 struct tevent_timer *_tevent_add_timer(struct tevent_context *ev,
237 TALLOC_CTX *mem_ctx,
238 struct timeval next_event,
239 tevent_timer_handler_t handler,
240 void *private_data,
241 const char *handler_name,
242 const char *location);
243 #define tevent_add_timer(ev, mem_ctx, next_event, handler, private_data) \
244 _tevent_add_timer(ev, mem_ctx, next_event, handler, private_data, \
245 #handler, __location__)
246 #endif
248 #ifdef DOXYGEN
250 * Initialize an immediate event object
252 * This object can be used to trigger an event to occur immediately after
253 * returning from the current event (before any other event occurs)
255 * @param[in] mem_ctx The talloc memory context to use as the parent
257 * @return An empty tevent_immediate object. Use tevent_schedule_immediate
258 * to populate and use it.
260 * @note Available as of tevent 0.9.8
262 struct tevent_immediate *tevent_create_immediate(TALLOC_CTX *mem_ctx);
263 #else
264 struct tevent_immediate *_tevent_create_immediate(TALLOC_CTX *mem_ctx,
265 const char *location);
266 #define tevent_create_immediate(mem_ctx) \
267 _tevent_create_immediate(mem_ctx, __location__)
268 #endif
270 #ifdef DOXYGEN
273 * Schedule an event for immediate execution. This event will occur
274 * immediately after returning from the current event (before any other
275 * event occurs)
277 * @param[in] im The tevent_immediate object to populate and use
278 * @param[in] ctx The tevent_context to run this event
279 * @param[in] handler The event handler to run when this event fires
280 * @param[in] private_data Data to pass to the event handler
282 void tevent_schedule_immediate(struct tevent_immediate *im,
283 struct tevent_context *ctx,
284 tevent_immediate_handler_t handler,
285 void *private_data);
286 #else
287 void _tevent_schedule_immediate(struct tevent_immediate *im,
288 struct tevent_context *ctx,
289 tevent_immediate_handler_t handler,
290 void *private_data,
291 const char *handler_name,
292 const char *location);
293 #define tevent_schedule_immediate(im, ctx, handler, private_data) \
294 _tevent_schedule_immediate(im, ctx, handler, private_data, \
295 #handler, __location__);
296 #endif
298 #ifdef DOXYGEN
300 * @brief Add a tevent signal handler
302 * tevent_add_signal() creates a new event for handling a signal the next
303 * time through the mainloop. It implements a very simple traditional signal
304 * handler whose only purpose is to add the handler event into the mainloop.
306 * @param[in] ev The event context to work on.
308 * @param[in] mem_ctx The talloc memory context to use.
310 * @param[in] signum The signal to trap
312 * @param[in] handler The callback handler for the signal.
314 * @param[in] sa_flags sigaction flags for this signal handler.
316 * @param[in] private_data The private data passed to the callback handler.
318 * @return The newly-created signal handler event, or NULL on error.
320 * @note To cancel a signal handler, call talloc_free() on the event returned
321 * from this function.
323 * @see tevent_num_signals, tevent_sa_info_queue_count
325 struct tevent_signal *tevent_add_signal(struct tevent_context *ev,
326 TALLOC_CTX *mem_ctx,
327 int signum,
328 int sa_flags,
329 tevent_signal_handler_t handler,
330 void *private_data);
331 #else
332 struct tevent_signal *_tevent_add_signal(struct tevent_context *ev,
333 TALLOC_CTX *mem_ctx,
334 int signum,
335 int sa_flags,
336 tevent_signal_handler_t handler,
337 void *private_data,
338 const char *handler_name,
339 const char *location);
340 #define tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
341 _tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data, \
342 #handler, __location__)
343 #endif
346 * @brief the number of supported signals
348 * This returns value of the configure time TEVENT_NUM_SIGNALS constant.
350 * The 'signum' argument of tevent_add_signal() must be less than
351 * TEVENT_NUM_SIGNALS.
353 * @see tevent_add_signal
355 size_t tevent_num_signals(void);
358 * @brief the number of pending realtime signals
360 * This returns value of TEVENT_SA_INFO_QUEUE_COUNT.
362 * The tevent internals remember the last TEVENT_SA_INFO_QUEUE_COUNT
363 * siginfo_t structures for SA_SIGINFO signals. If the system generates
364 * more some signals get lost.
366 * @see tevent_add_signal
368 size_t tevent_sa_info_queue_count(void);
370 #ifdef DOXYGEN
372 * @brief Pass a single time through the mainloop
374 * This will process any appropriate signal, immediate, fd and timer events
376 * @param[in] ev The event context to process
378 * @return Zero on success, nonzero if an internal error occurred
380 int tevent_loop_once(struct tevent_context *ev);
381 #else
382 int _tevent_loop_once(struct tevent_context *ev, const char *location);
383 #define tevent_loop_once(ev) \
384 _tevent_loop_once(ev, __location__)
385 #endif
387 #ifdef DOXYGEN
389 * @brief Run the mainloop
391 * The mainloop will run until there are no events remaining to be processed
393 * @param[in] ev The event context to process
395 * @return Zero if all events have been processed. Nonzero if an internal
396 * error occurred.
398 int tevent_loop_wait(struct tevent_context *ev);
399 #else
400 int _tevent_loop_wait(struct tevent_context *ev, const char *location);
401 #define tevent_loop_wait(ev) \
402 _tevent_loop_wait(ev, __location__)
403 #endif
407 * Assign a function to run when a tevent_fd is freed
409 * This function is a destructor for the tevent_fd. It does not automatically
410 * close the file descriptor. If this is the desired behavior, then it must be
411 * performed by the close_fn.
413 * @param[in] fde File descriptor event on which to set the destructor
414 * @param[in] close_fn Destructor to execute when fde is freed
416 void tevent_fd_set_close_fn(struct tevent_fd *fde,
417 tevent_fd_close_fn_t close_fn);
420 * Automatically close the file descriptor when the tevent_fd is freed
422 * This function calls close(fd) internally.
424 * @param[in] fde File descriptor event to auto-close
426 void tevent_fd_set_auto_close(struct tevent_fd *fde);
429 * Return the flags set on this file descriptor event
431 * @param[in] fde File descriptor event to query
433 * @return The flags set on the event. See #TEVENT_FD_READ and
434 * #TEVENT_FD_WRITE
436 uint16_t tevent_fd_get_flags(struct tevent_fd *fde);
439 * Set flags on a file descriptor event
441 * @param[in] fde File descriptor event to set
442 * @param[in] flags Flags to set on the event. See #TEVENT_FD_READ and
443 * #TEVENT_FD_WRITE
445 void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
448 * Query whether tevent supports signal handling
450 * @param[in] ev An initialized tevent context
452 * @return True if this platform and tevent context support signal handling
454 bool tevent_signal_support(struct tevent_context *ev);
456 void tevent_set_abort_fn(void (*abort_fn)(const char *reason));
458 /* bits for file descriptor event flags */
461 * Monitor a file descriptor for write availability
463 #define TEVENT_FD_READ 1
465 * Monitor a file descriptor for data to be read
467 #define TEVENT_FD_WRITE 2
470 * Convenience function for declaring a tevent_fd writable
472 #define TEVENT_FD_WRITEABLE(fde) \
473 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_WRITE)
476 * Convenience function for declaring a tevent_fd readable
478 #define TEVENT_FD_READABLE(fde) \
479 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_READ)
482 * Convenience function for declaring a tevent_fd non-writable
484 #define TEVENT_FD_NOT_WRITEABLE(fde) \
485 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_WRITE)
488 * Convenience function for declaring a tevent_fd non-readable
490 #define TEVENT_FD_NOT_READABLE(fde) \
491 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_READ)
494 * Debug level of tevent
496 enum tevent_debug_level {
497 TEVENT_DEBUG_FATAL,
498 TEVENT_DEBUG_ERROR,
499 TEVENT_DEBUG_WARNING,
500 TEVENT_DEBUG_TRACE
504 * @brief The tevent debug callbac.
506 * @param[in] context The memory context to use.
508 * @param[in] level The debug level.
510 * @param[in] fmt The format string.
512 * @param[in] ap The arguments for the format string.
514 typedef void (*tevent_debug_fn)(void *context,
515 enum tevent_debug_level level,
516 const char *fmt,
517 va_list ap) PRINTF_ATTRIBUTE(3,0);
520 * Set destination for tevent debug messages
522 * @param[in] ev Event context to debug
523 * @param[in] debug Function to handle output printing
524 * @param[in] context The context to pass to the debug function.
526 * @return Always returns 0 as of version 0.9.8
528 * @note Default is to emit no debug messages
530 int tevent_set_debug(struct tevent_context *ev,
531 tevent_debug_fn debug,
532 void *context);
535 * Designate stderr for debug message output
537 * @param[in] ev Event context to debug
539 * @note This function will only output TEVENT_DEBUG_FATAL, TEVENT_DEBUG_ERROR
540 * and TEVENT_DEBUG_WARNING messages. For TEVENT_DEBUG_TRACE, please define a
541 * function for tevent_set_debug()
543 int tevent_set_debug_stderr(struct tevent_context *ev);
545 enum tevent_trace_point {
547 * Corresponds to a trace point just before waiting
549 TEVENT_TRACE_BEFORE_WAIT,
551 * Corresponds to a trace point just after waiting
553 TEVENT_TRACE_AFTER_WAIT,
554 #define TEVENT_HAS_LOOP_ONCE_TRACE_POINTS 1
556 * Corresponds to a trace point just before calling
557 * the loop_once() backend function.
559 TEVENT_TRACE_BEFORE_LOOP_ONCE,
561 * Corresponds to a trace point right after the
562 * loop_once() backend function has returned.
564 TEVENT_TRACE_AFTER_LOOP_ONCE,
567 typedef void (*tevent_trace_callback_t)(enum tevent_trace_point,
568 void *private_data);
571 * Register a callback to be called at certain trace points
573 * @param[in] ev Event context
574 * @param[in] cb Trace callback
575 * @param[in] private_data Data to be passed to callback
577 * @note The callback will be called at trace points defined by
578 * tevent_trace_point. Call with NULL to reset.
580 void tevent_set_trace_callback(struct tevent_context *ev,
581 tevent_trace_callback_t cb,
582 void *private_data);
585 * Retrieve the current trace callback
587 * @param[in] ev Event context
588 * @param[out] cb Registered trace callback
589 * @param[out] private_data Registered data to be passed to callback
591 * @note This can be used to allow one component that wants to
592 * register a callback to respect the callback that another component
593 * has already registered.
595 void tevent_get_trace_callback(struct tevent_context *ev,
596 tevent_trace_callback_t *cb,
597 void *private_data);
600 * @}
604 * @defgroup tevent_request The tevent request functions.
605 * @ingroup tevent
607 * A tevent_req represents an asynchronous computation.
609 * The tevent_req group of API calls is the recommended way of
610 * programming async computations within tevent. In particular the
611 * file descriptor (tevent_add_fd) and timer (tevent_add_timed) events
612 * are considered too low-level to be used in larger computations. To
613 * read and write from and to sockets, Samba provides two calls on top
614 * of tevent_add_fd: read_packet_send/recv and writev_send/recv. These
615 * requests are much easier to compose than the low-level event
616 * handlers called from tevent_add_fd.
618 * A lot of the simplicity tevent_req has brought to the notoriously
619 * hairy async programming came via a set of conventions that every
620 * async computation programmed should follow. One central piece of
621 * these conventions is the naming of routines and variables.
623 * Every async computation needs a name (sensibly called "computation"
624 * down from here). From this name quite a few naming conventions are
625 * derived.
627 * Every computation that requires local state needs a
628 * @code
629 * struct computation_state {
630 * int local_var;
631 * };
632 * @endcode
633 * Even if no local variables are required, such a state struct should
634 * be created containing a dummy variable. Quite a few helper
635 * functions and macros (for example tevent_req_create()) assume such
636 * a state struct.
638 * An async computation is started by a computation_send
639 * function. When it is finished, its result can be received by a
640 * computation_recv function. For an example how to set up an async
641 * computation, see the code example in the documentation for
642 * tevent_req_create() and tevent_req_post(). The prototypes for _send
643 * and _recv functions should follow some conventions:
645 * @code
646 * struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
647 * struct tevent_req *ev,
648 * ... further args);
649 * int computation_recv(struct tevent_req *req, ... further output args);
650 * @endcode
652 * The "int" result of computation_recv() depends on the result the
653 * sync version of the function would have, "int" is just an example
654 * here.
656 * Another important piece of the conventions is that the program flow
657 * is interrupted as little as possible. Because a blocking
658 * sub-computation requires that the flow needs to continue in a
659 * separate function that is the logical sequel of some computation,
660 * it should lexically follow sending off the blocking
661 * sub-computation. Setting the callback function via
662 * tevent_req_set_callback() requires referencing a function lexically
663 * below the call to tevent_req_set_callback(), forward declarations
664 * are required. A lot of the async computations thus begin with a
665 * sequence of declarations such as
667 * @code
668 * static void computation_step1_done(struct tevent_req *subreq);
669 * static void computation_step2_done(struct tevent_req *subreq);
670 * static void computation_step3_done(struct tevent_req *subreq);
671 * @endcode
673 * It really helps readability a lot to do these forward declarations,
674 * because the lexically sequential program flow makes the async
675 * computations almost as clear to read as a normal, sync program
676 * flow.
678 * It is up to the user of the async computation to talloc_free it
679 * after it has finished. If an async computation should be aborted,
680 * the tevent_req structure can be talloc_free'ed. After it has
681 * finished, it should talloc_free'ed by the API user.
683 * @{
687 * An async request moves from TEVENT_REQ_INIT to
688 * TEVENT_REQ_IN_PROGRESS. All other states are valid after a request
689 * has finished.
691 enum tevent_req_state {
693 * We are creating the request
695 TEVENT_REQ_INIT,
697 * We are waiting the request to complete
699 TEVENT_REQ_IN_PROGRESS,
701 * The request is finished successfully
703 TEVENT_REQ_DONE,
705 * A user error has occurred. The user error has been
706 * indicated by tevent_req_error(), it can be retrieved via
707 * tevent_req_is_error().
709 TEVENT_REQ_USER_ERROR,
711 * Request timed out after the timeout set by tevent_req_set_endtime.
713 TEVENT_REQ_TIMED_OUT,
715 * An internal allocation has failed, or tevent_req_nomem has
716 * been given a NULL pointer as the first argument.
718 TEVENT_REQ_NO_MEMORY,
720 * The request has been received by the caller. No further
721 * action is valid.
723 TEVENT_REQ_RECEIVED
727 * @brief An async request
729 struct tevent_req;
732 * @brief A tevent request callback function.
734 * @param[in] req The tevent async request which executed this callback.
736 typedef void (*tevent_req_fn)(struct tevent_req *req);
739 * @brief Set an async request callback.
741 * See the documentation of tevent_req_post() for an example how this
742 * is supposed to be used.
744 * @param[in] req The async request to set the callback.
746 * @param[in] fn The callback function to set.
748 * @param[in] pvt A pointer to private data to pass to the async request
749 * callback.
751 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt);
753 #ifdef DOXYGEN
755 * @brief Get the private data cast to the given type for a callback from
756 * a tevent request structure.
758 * @code
759 * static void computation_done(struct tevent_req *subreq) {
760 * struct tevent_req *req = tevent_req_callback_data(subreq, struct tevent_req);
761 * struct computation_state *state = tevent_req_data(req, struct computation_state);
762 * .... more things, eventually maybe call tevent_req_done(req);
764 * @endcode
766 * @param[in] req The structure to get the callback data from.
768 * @param[in] type The type of the private callback data to get.
770 * @return The type casted private data set NULL if not set.
772 void *tevent_req_callback_data(struct tevent_req *req, #type);
773 #else
774 void *_tevent_req_callback_data(struct tevent_req *req);
775 #define tevent_req_callback_data(_req, _type) \
776 talloc_get_type_abort(_tevent_req_callback_data(_req), _type)
777 #endif
779 #ifdef DOXYGEN
781 * @brief Get the private data for a callback from a tevent request structure.
783 * @param[in] req The structure to get the callback data from.
785 * @param[in] req The structure to get the data from.
787 * @return The private data or NULL if not set.
789 void *tevent_req_callback_data_void(struct tevent_req *req);
790 #else
791 #define tevent_req_callback_data_void(_req) \
792 _tevent_req_callback_data(_req)
793 #endif
795 #ifdef DOXYGEN
797 * @brief Get the private data from a tevent request structure.
799 * When the tevent_req has been created by tevent_req_create, the
800 * result of tevent_req_data() is the state variable created by
801 * tevent_req_create() as a child of the req.
803 * @param[in] req The structure to get the private data from.
805 * @param[in] type The type of the private data
807 * @return The private data or NULL if not set.
809 void *tevent_req_data(struct tevent_req *req, #type);
810 #else
811 void *_tevent_req_data(struct tevent_req *req);
812 #define tevent_req_data(_req, _type) \
813 talloc_get_type_abort(_tevent_req_data(_req), _type)
814 #endif
817 * @brief The print function which can be set for a tevent async request.
819 * @param[in] req The tevent async request.
821 * @param[in] ctx A talloc memory context which can be uses to allocate
822 * memory.
824 * @return An allocated string buffer to print.
826 * Example:
827 * @code
828 * static char *my_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
830 * struct my_data *data = tevent_req_data(req, struct my_data);
831 * char *result;
833 * result = tevent_req_default_print(mem_ctx, req);
834 * if (result == NULL) {
835 * return NULL;
838 * return talloc_asprintf_append_buffer(result, "foo=%d, bar=%d",
839 * data->foo, data->bar);
841 * @endcode
843 typedef char *(*tevent_req_print_fn)(struct tevent_req *req, TALLOC_CTX *ctx);
846 * @brief This function sets a print function for the given request.
848 * This function can be used to setup a print function for the given request.
849 * This will be triggered if the tevent_req_print() function was
850 * called on the given request.
852 * @param[in] req The request to use.
854 * @param[in] fn A pointer to the print function
856 * @note This function should only be used for debugging.
858 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn);
861 * @brief The default print function for creating debug messages.
863 * The function should not be used by users of the async API,
864 * but custom print function can use it and append custom text
865 * to the string.
867 * @param[in] req The request to be printed.
869 * @param[in] mem_ctx The memory context for the result.
871 * @return Text representation of request.
874 char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx);
877 * @brief Print an tevent_req structure in debug messages.
879 * This function should be used by callers of the async API.
881 * @param[in] mem_ctx The memory context for the result.
883 * @param[in] req The request to be printed.
885 * @return Text representation of request.
887 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req);
890 * @brief A typedef for a cancel function for a tevent request.
892 * @param[in] req The tevent request calling this function.
894 * @return True if the request could be canceled, false if not.
896 typedef bool (*tevent_req_cancel_fn)(struct tevent_req *req);
899 * @brief This function sets a cancel function for the given tevent request.
901 * This function can be used to setup a cancel function for the given request.
902 * This will be triggered if the tevent_req_cancel() function was
903 * called on the given request.
905 * @param[in] req The request to use.
907 * @param[in] fn A pointer to the cancel function.
909 void tevent_req_set_cancel_fn(struct tevent_req *req, tevent_req_cancel_fn fn);
911 #ifdef DOXYGEN
913 * @brief Try to cancel the given tevent request.
915 * This function can be used to cancel the given request.
917 * It is only possible to cancel a request when the implementation
918 * has registered a cancel function via the tevent_req_set_cancel_fn().
920 * @param[in] req The request to use.
922 * @return This function returns true is the request is cancelable,
923 * othererwise false is returned.
925 * @note Even if the function returns true, the caller need to wait
926 * for the function to complete normally.
927 * Only the _recv() function of the given request indicates
928 * if the request was really canceled.
930 bool tevent_req_cancel(struct tevent_req *req);
931 #else
932 bool _tevent_req_cancel(struct tevent_req *req, const char *location);
933 #define tevent_req_cancel(req) \
934 _tevent_req_cancel(req, __location__)
935 #endif
938 * @brief A typedef for a cleanup function for a tevent request.
940 * @param[in] req The tevent request calling this function.
942 * @param[in] req_state The current tevent_req_state.
945 typedef void (*tevent_req_cleanup_fn)(struct tevent_req *req,
946 enum tevent_req_state req_state);
949 * @brief This function sets a cleanup function for the given tevent request.
951 * This function can be used to setup a cleanup function for the given request.
952 * This will be triggered when the tevent_req_done() or tevent_req_error()
953 * function was called, before notifying the callers callback function,
954 * and also before scheduling the deferred trigger.
956 * This might be useful if more than one tevent_req belong together
957 * and need to finish both requests at the same time.
959 * The cleanup function is able to call tevent_req_done() or tevent_req_error()
960 * recursively, the cleanup function is only triggered the first time.
962 * The cleanup function is also called by tevent_req_received()
963 * (possibly triggered from tevent_req_destructor()) before destroying
964 * the private data of the tevent_req.
966 * @param[in] req The request to use.
968 * @param[in] fn A pointer to the cancel function.
970 void tevent_req_set_cleanup_fn(struct tevent_req *req, tevent_req_cleanup_fn fn);
972 #ifdef DOXYGEN
974 * @brief Create an async tevent request.
976 * The new async request will be initialized in state TEVENT_REQ_IN_PROGRESS.
978 * @code
979 * struct tevent_req *req;
980 * struct computation_state *state;
981 * req = tevent_req_create(mem_ctx, &state, struct computation_state);
982 * @endcode
984 * Tevent_req_create() allocates and zeros the state variable as a talloc
985 * child of its result. The state variable should be used as the talloc
986 * parent for all temporary variables that are allocated during the async
987 * computation. This way, when the user of the async computation frees
988 * the request, the state as a talloc child will be free'd along with
989 * all the temporary variables hanging off the state.
991 * @param[in] mem_ctx The memory context for the result.
992 * @param[in] pstate Pointer to the private request state.
993 * @param[in] type The name of the request.
995 * @return A new async request. NULL on error.
997 struct tevent_req *tevent_req_create(TALLOC_CTX *mem_ctx,
998 void **pstate, #type);
999 #else
1000 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
1001 void *pstate,
1002 size_t state_size,
1003 const char *type,
1004 const char *location);
1006 #define tevent_req_create(_mem_ctx, _pstate, _type) \
1007 _tevent_req_create((_mem_ctx), (_pstate), sizeof(_type), \
1008 #_type, __location__)
1009 #endif
1012 * @brief Set a timeout for an async request.
1014 * @param[in] req The request to set the timeout for.
1016 * @param[in] ev The event context to use for the timer.
1018 * @param[in] endtime The endtime of the request.
1020 * @return True if succeeded, false if not.
1022 bool tevent_req_set_endtime(struct tevent_req *req,
1023 struct tevent_context *ev,
1024 struct timeval endtime);
1026 #ifdef DOXYGEN
1028 * @brief Call the notify callback of the given tevent request manually.
1030 * @param[in] req The tevent request to call the notify function from.
1032 * @see tevent_req_set_callback()
1034 void tevent_req_notify_callback(struct tevent_req *req);
1035 #else
1036 void _tevent_req_notify_callback(struct tevent_req *req, const char *location);
1037 #define tevent_req_notify_callback(req) \
1038 _tevent_req_notify_callback(req, __location__)
1039 #endif
1041 #ifdef DOXYGEN
1043 * @brief An async request has successfully finished.
1045 * This function is to be used by implementors of async requests. When a
1046 * request is successfully finished, this function calls the user's completion
1047 * function.
1049 * @param[in] req The finished request.
1051 void tevent_req_done(struct tevent_req *req);
1052 #else
1053 void _tevent_req_done(struct tevent_req *req,
1054 const char *location);
1055 #define tevent_req_done(req) \
1056 _tevent_req_done(req, __location__)
1057 #endif
1059 #ifdef DOXYGEN
1061 * @brief An async request has seen an error.
1063 * This function is to be used by implementors of async requests. When a
1064 * request can not successfully completed, the implementation should call this
1065 * function with the appropriate status code.
1067 * If error is 0 the function returns false and does nothing more.
1069 * @param[in] req The request with an error.
1071 * @param[in] error The error code.
1073 * @return On success true is returned, false if error is 0.
1075 * @code
1076 * int error = first_function();
1077 * if (tevent_req_error(req, error)) {
1078 * return;
1081 * error = second_function();
1082 * if (tevent_req_error(req, error)) {
1083 * return;
1086 * tevent_req_done(req);
1087 * return;
1088 * @endcode
1090 bool tevent_req_error(struct tevent_req *req,
1091 uint64_t error);
1092 #else
1093 bool _tevent_req_error(struct tevent_req *req,
1094 uint64_t error,
1095 const char *location);
1096 #define tevent_req_error(req, error) \
1097 _tevent_req_error(req, error, __location__)
1098 #endif
1100 #ifdef DOXYGEN
1102 * @brief Helper function for nomem check.
1104 * Convenience helper to easily check alloc failure within a callback
1105 * implementing the next step of an async request.
1107 * @param[in] p The pointer to be checked.
1109 * @param[in] req The request being processed.
1111 * @code
1112 * p = talloc(mem_ctx, bla);
1113 * if (tevent_req_nomem(p, req)) {
1114 * return;
1116 * @endcode
1118 bool tevent_req_nomem(const void *p,
1119 struct tevent_req *req);
1120 #else
1121 bool _tevent_req_nomem(const void *p,
1122 struct tevent_req *req,
1123 const char *location);
1124 #define tevent_req_nomem(p, req) \
1125 _tevent_req_nomem(p, req, __location__)
1126 #endif
1128 #ifdef DOXYGEN
1130 * @brief Indicate out of memory to a request
1132 * @param[in] req The request being processed.
1134 void tevent_req_oom(struct tevent_req *req);
1135 #else
1136 void _tevent_req_oom(struct tevent_req *req,
1137 const char *location);
1138 #define tevent_req_oom(req) \
1139 _tevent_req_oom(req, __location__)
1140 #endif
1143 * @brief Finish a request before the caller had the change to set the callback.
1145 * An implementation of an async request might find that it can either finish
1146 * the request without waiting for an external event, or it can not even start
1147 * the engine. To present the illusion of a callback to the user of the API,
1148 * the implementation can call this helper function which triggers an
1149 * immediate event. This way the caller can use the same calling
1150 * conventions, independent of whether the request was actually deferred.
1152 * @code
1153 * struct tevent_req *computation_send(TALLOC_CTX *mem_ctx,
1154 * struct tevent_context *ev)
1156 * struct tevent_req *req, *subreq;
1157 * struct computation_state *state;
1158 * req = tevent_req_create(mem_ctx, &state, struct computation_state);
1159 * if (req == NULL) {
1160 * return NULL;
1162 * subreq = subcomputation_send(state, ev);
1163 * if (tevent_req_nomem(subreq, req)) {
1164 * return tevent_req_post(req, ev);
1166 * tevent_req_set_callback(subreq, computation_done, req);
1167 * return req;
1169 * @endcode
1171 * @param[in] req The finished request.
1173 * @param[in] ev The tevent_context for the immediate event.
1175 * @return The given request will be returned.
1177 struct tevent_req *tevent_req_post(struct tevent_req *req,
1178 struct tevent_context *ev);
1181 * @brief Finish multiple requests within one function
1183 * Normally tevent_req_notify_callback() and all wrappers
1184 * (e.g. tevent_req_done() and tevent_req_error())
1185 * need to be the last thing an event handler should call.
1186 * This is because the callback is likely to destroy the
1187 * context of the current function.
1189 * If a function wants to notify more than one caller,
1190 * it is dangerous if it just triggers multiple callbacks
1191 * in a row. With tevent_req_defer_callback() it is possible
1192 * to set an event context that will be used to defer the callback
1193 * via an immediate event (similar to tevent_req_post()).
1195 * @code
1196 * struct complete_state {
1197 * struct tevent_context *ev;
1199 * struct tevent_req **reqs;
1200 * };
1202 * void complete(struct complete_state *state)
1204 * size_t i, c = talloc_array_length(state->reqs);
1206 * for (i=0; i < c; i++) {
1207 * tevent_req_defer_callback(state->reqs[i], state->ev);
1208 * tevent_req_done(state->reqs[i]);
1211 * @endcode
1213 * @param[in] req The finished request.
1215 * @param[in] ev The tevent_context for the immediate event.
1217 * @return The given request will be returned.
1219 void tevent_req_defer_callback(struct tevent_req *req,
1220 struct tevent_context *ev);
1223 * @brief Check if the given request is still in progress.
1225 * It is typically used by sync wrapper functions.
1227 * @param[in] req The request to poll.
1229 * @return The boolean form of "is in progress".
1231 bool tevent_req_is_in_progress(struct tevent_req *req);
1234 * @brief Actively poll for the given request to finish.
1236 * This function is typically used by sync wrapper functions.
1238 * @param[in] req The request to poll.
1240 * @param[in] ev The tevent_context to be used.
1242 * @return On success true is returned. If a critical error has
1243 * happened in the tevent loop layer false is returned.
1244 * This is not the return value of the given request!
1246 * @note This should only be used if the given tevent context was created by the
1247 * caller, to avoid event loop nesting.
1249 * @code
1250 * req = tstream_writev_queue_send(mem_ctx,
1251 * ev_ctx,
1252 * tstream,
1253 * send_queue,
1254 * iov, 2);
1255 * ok = tevent_req_poll(req, tctx->ev);
1256 * rc = tstream_writev_queue_recv(req, &sys_errno);
1257 * TALLOC_FREE(req);
1258 * @endcode
1260 bool tevent_req_poll(struct tevent_req *req,
1261 struct tevent_context *ev);
1264 * @brief Get the tevent request state and the actual error set by
1265 * tevent_req_error.
1267 * @code
1268 * int computation_recv(struct tevent_req *req, uint64_t *perr)
1270 * enum tevent_req_state state;
1271 * uint64_t err;
1272 * if (tevent_req_is_error(req, &state, &err)) {
1273 * *perr = err;
1274 * return -1;
1276 * return 0;
1278 * @endcode
1280 * @param[in] req The tevent request to get the error from.
1282 * @param[out] state A pointer to store the tevent request error state.
1284 * @param[out] error A pointer to store the error set by tevent_req_error().
1286 * @return True if the function could set error and state, false
1287 * otherwise.
1289 * @see tevent_req_error()
1291 bool tevent_req_is_error(struct tevent_req *req,
1292 enum tevent_req_state *state,
1293 uint64_t *error);
1296 * @brief Use as the last action of a _recv() function.
1298 * This function destroys the attached private data.
1300 * @param[in] req The finished request.
1302 void tevent_req_received(struct tevent_req *req);
1305 * @brief Create a tevent subrequest at a given time.
1307 * The idea is that always the same syntax for tevent requests.
1309 * @param[in] mem_ctx The talloc memory context to use.
1311 * @param[in] ev The event handle to setup the request.
1313 * @param[in] wakeup_time The time to wakeup and execute the request.
1315 * @return The new subrequest, NULL on error.
1317 * Example:
1318 * @code
1319 * static void my_callback_wakeup_done(tevent_req *subreq)
1321 * struct tevent_req *req = tevent_req_callback_data(subreq,
1322 * struct tevent_req);
1323 * bool ok;
1325 * ok = tevent_wakeup_recv(subreq);
1326 * TALLOC_FREE(subreq);
1327 * if (!ok) {
1328 * tevent_req_error(req, -1);
1329 * return;
1331 * ...
1333 * @endcode
1335 * @code
1336 * subreq = tevent_wakeup_send(mem_ctx, ev, wakeup_time);
1337 * if (tevent_req_nomem(subreq, req)) {
1338 * return false;
1340 * tevent_set_callback(subreq, my_callback_wakeup_done, req);
1341 * @endcode
1343 * @see tevent_wakeup_recv()
1345 struct tevent_req *tevent_wakeup_send(TALLOC_CTX *mem_ctx,
1346 struct tevent_context *ev,
1347 struct timeval wakeup_time);
1350 * @brief Check if the wakeup has been correctly executed.
1352 * This function needs to be called in the callback function set after calling
1353 * tevent_wakeup_send().
1355 * @param[in] req The tevent request to check.
1357 * @return True on success, false otherwise.
1359 * @see tevent_wakeup_recv()
1361 bool tevent_wakeup_recv(struct tevent_req *req);
1363 /* @} */
1366 * @defgroup tevent_helpers The tevent helper functiions
1367 * @ingroup tevent
1369 * @todo description
1371 * @{
1375 * @brief Compare two timeval values.
1377 * @param[in] tv1 The first timeval value to compare.
1379 * @param[in] tv2 The second timeval value to compare.
1381 * @return 0 if they are equal.
1382 * 1 if the first time is greater than the second.
1383 * -1 if the first time is smaller than the second.
1385 int tevent_timeval_compare(const struct timeval *tv1,
1386 const struct timeval *tv2);
1389 * @brief Get a zero timval value.
1391 * @return A zero timval value.
1393 struct timeval tevent_timeval_zero(void);
1396 * @brief Get a timeval value for the current time.
1398 * @return A timval value with the current time.
1400 struct timeval tevent_timeval_current(void);
1403 * @brief Get a timeval structure with the given values.
1405 * @param[in] secs The seconds to set.
1407 * @param[in] usecs The microseconds to set.
1409 * @return A timeval structure with the given values.
1411 struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs);
1414 * @brief Get the difference between two timeval values.
1416 * @param[in] tv1 The first timeval.
1418 * @param[in] tv2 The second timeval.
1420 * @return A timeval structure with the difference between the
1421 * first and the second value.
1423 struct timeval tevent_timeval_until(const struct timeval *tv1,
1424 const struct timeval *tv2);
1427 * @brief Check if a given timeval structure is zero.
1429 * @param[in] tv The timeval to check if it is zero.
1431 * @return True if it is zero, false otherwise.
1433 bool tevent_timeval_is_zero(const struct timeval *tv);
1436 * @brief Add the given amount of time to a timeval structure.
1438 * @param[in] tv The timeval structure to add the time.
1440 * @param[in] secs The seconds to add to the timeval.
1442 * @param[in] usecs The microseconds to add to the timeval.
1444 * @return The timeval structure with the new time.
1446 struct timeval tevent_timeval_add(const struct timeval *tv, uint32_t secs,
1447 uint32_t usecs);
1450 * @brief Get a timeval in the future with a specified offset from now.
1452 * @param[in] secs The seconds of the offset from now.
1454 * @param[in] usecs The microseconds of the offset from now.
1456 * @return A timval with the given offset in the future.
1458 struct timeval tevent_timeval_current_ofs(uint32_t secs, uint32_t usecs);
1460 /* @} */
1464 * @defgroup tevent_queue The tevent queue functions
1465 * @ingroup tevent
1467 * A tevent_queue is used to queue up async requests that must be
1468 * serialized. For example writing buffers into a socket must be
1469 * serialized. Writing a large lump of data into a socket can require
1470 * multiple write(2) or send(2) system calls. If more than one async
1471 * request is outstanding to write large buffers into a socket, every
1472 * request must individually be completed before the next one begins,
1473 * even if multiple syscalls are required.
1475 * Take a look at @ref tevent_queue_tutorial for more details.
1476 * @{
1479 struct tevent_queue;
1480 struct tevent_queue_entry;
1482 #ifdef DOXYGEN
1484 * @brief Create and start a tevent queue.
1486 * @param[in] mem_ctx The talloc memory context to allocate the queue.
1488 * @param[in] name The name to use to identify the queue.
1490 * @return An allocated tevent queue on success, NULL on error.
1492 * @see tevent_queue_start()
1493 * @see tevent_queue_stop()
1495 struct tevent_queue *tevent_queue_create(TALLOC_CTX *mem_ctx,
1496 const char *name);
1497 #else
1498 struct tevent_queue *_tevent_queue_create(TALLOC_CTX *mem_ctx,
1499 const char *name,
1500 const char *location);
1502 #define tevent_queue_create(_mem_ctx, _name) \
1503 _tevent_queue_create((_mem_ctx), (_name), __location__)
1504 #endif
1507 * @brief A callback trigger function run by the queue.
1509 * @param[in] req The tevent request the trigger function is executed on.
1511 * @param[in] private_data The private data pointer specified by
1512 * tevent_queue_add().
1514 * @see tevent_queue_add()
1515 * @see tevent_queue_add_entry()
1516 * @see tevent_queue_add_optimize_empty()
1518 typedef void (*tevent_queue_trigger_fn_t)(struct tevent_req *req,
1519 void *private_data);
1522 * @brief Add a tevent request to the queue.
1524 * @param[in] queue The queue to add the request.
1526 * @param[in] ev The event handle to use for the request.
1528 * @param[in] req The tevent request to add to the queue.
1530 * @param[in] trigger The function triggered by the queue when the request
1531 * is called. Since tevent 0.9.14 it's possible to
1532 * pass NULL, in order to just add a "blocker" to the
1533 * queue.
1535 * @param[in] private_data The private data passed to the trigger function.
1537 * @return True if the request has been successfully added, false
1538 * otherwise.
1540 bool tevent_queue_add(struct tevent_queue *queue,
1541 struct tevent_context *ev,
1542 struct tevent_req *req,
1543 tevent_queue_trigger_fn_t trigger,
1544 void *private_data);
1547 * @brief Add a tevent request to the queue.
1549 * The request can be removed from the queue by calling talloc_free()
1550 * (or a similar function) on the returned queue entry. This
1551 * is the only difference to tevent_queue_add().
1553 * @param[in] queue The queue to add the request.
1555 * @param[in] ev The event handle to use for the request.
1557 * @param[in] req The tevent request to add to the queue.
1559 * @param[in] trigger The function triggered by the queue when the request
1560 * is called. Since tevent 0.9.14 it's possible to
1561 * pass NULL, in order to just add a "blocker" to the
1562 * queue.
1564 * @param[in] private_data The private data passed to the trigger function.
1566 * @return a pointer to the tevent_queue_entry if the request
1567 * has been successfully added, NULL otherwise.
1569 * @see tevent_queue_add()
1570 * @see tevent_queue_add_optimize_empty()
1572 struct tevent_queue_entry *tevent_queue_add_entry(
1573 struct tevent_queue *queue,
1574 struct tevent_context *ev,
1575 struct tevent_req *req,
1576 tevent_queue_trigger_fn_t trigger,
1577 void *private_data);
1580 * @brief Add a tevent request to the queue using a possible optimization.
1582 * This tries to optimize for the empty queue case and may calls
1583 * the trigger function directly. This is the only difference compared
1584 * to tevent_queue_add_entry().
1586 * The caller needs to be prepared that the trigger function has
1587 * already called tevent_req_notify_callback(), tevent_req_error(),
1588 * tevent_req_done() or a similar function.
1590 * The request can be removed from the queue by calling talloc_free()
1591 * (or a similar function) on the returned queue entry.
1593 * @param[in] queue The queue to add the request.
1595 * @param[in] ev The event handle to use for the request.
1597 * @param[in] req The tevent request to add to the queue.
1599 * @param[in] trigger The function triggered by the queue when the request
1600 * is called. Since tevent 0.9.14 it's possible to
1601 * pass NULL, in order to just add a "blocker" to the
1602 * queue.
1604 * @param[in] private_data The private data passed to the trigger function.
1606 * @return a pointer to the tevent_queue_entry if the request
1607 * has been successfully added, NULL otherwise.
1609 * @see tevent_queue_add()
1610 * @see tevent_queue_add_entry()
1612 struct tevent_queue_entry *tevent_queue_add_optimize_empty(
1613 struct tevent_queue *queue,
1614 struct tevent_context *ev,
1615 struct tevent_req *req,
1616 tevent_queue_trigger_fn_t trigger,
1617 void *private_data);
1620 * @brief Start a tevent queue.
1622 * The queue is started by default.
1624 * @param[in] queue The queue to start.
1626 void tevent_queue_start(struct tevent_queue *queue);
1629 * @brief Stop a tevent queue.
1631 * The queue is started by default.
1633 * @param[in] queue The queue to stop.
1635 void tevent_queue_stop(struct tevent_queue *queue);
1638 * @brief Get the length of the queue.
1640 * @param[in] queue The queue to get the length from.
1642 * @return The number of elements.
1644 size_t tevent_queue_length(struct tevent_queue *queue);
1647 * @brief Is the tevent queue running.
1649 * The queue is started by default.
1651 * @param[in] queue The queue.
1653 * @return Wether the queue is running or not..
1655 bool tevent_queue_running(struct tevent_queue *queue);
1658 * @brief Create a tevent subrequest that waits in a tevent_queue
1660 * The idea is that always the same syntax for tevent requests.
1662 * @param[in] mem_ctx The talloc memory context to use.
1664 * @param[in] ev The event handle to setup the request.
1666 * @param[in] queue The queue to wait in.
1668 * @return The new subrequest, NULL on error.
1670 * @see tevent_queue_wait_recv()
1672 struct tevent_req *tevent_queue_wait_send(TALLOC_CTX *mem_ctx,
1673 struct tevent_context *ev,
1674 struct tevent_queue *queue);
1677 * @brief Check if we no longer need to wait in the queue.
1679 * This function needs to be called in the callback function set after calling
1680 * tevent_queue_wait_send().
1682 * @param[in] req The tevent request to check.
1684 * @return True on success, false otherwise.
1686 * @see tevent_queue_wait_send()
1688 bool tevent_queue_wait_recv(struct tevent_req *req);
1690 typedef int (*tevent_nesting_hook)(struct tevent_context *ev,
1691 void *private_data,
1692 uint32_t level,
1693 bool begin,
1694 void *stack_ptr,
1695 const char *location);
1696 #ifdef TEVENT_DEPRECATED
1697 #ifndef _DEPRECATED_
1698 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
1699 #define _DEPRECATED_ __attribute__ ((deprecated))
1700 #else
1701 #define _DEPRECATED_
1702 #endif
1703 #endif
1704 void tevent_loop_allow_nesting(struct tevent_context *ev) _DEPRECATED_;
1705 void tevent_loop_set_nesting_hook(struct tevent_context *ev,
1706 tevent_nesting_hook hook,
1707 void *private_data) _DEPRECATED_;
1708 int _tevent_loop_until(struct tevent_context *ev,
1709 bool (*finished)(void *private_data),
1710 void *private_data,
1711 const char *location) _DEPRECATED_;
1712 #define tevent_loop_until(ev, finished, private_data) \
1713 _tevent_loop_until(ev, finished, private_data, __location__)
1714 #endif
1716 int tevent_re_initialise(struct tevent_context *ev);
1718 /* @} */
1721 * @defgroup tevent_ops The tevent operation functions
1722 * @ingroup tevent
1724 * The following structure and registration functions are exclusively
1725 * needed for people writing and pluggin a different event engine.
1726 * There is nothing useful for normal tevent user in here.
1727 * @{
1730 struct tevent_ops {
1731 /* context init */
1732 int (*context_init)(struct tevent_context *ev);
1734 /* fd_event functions */
1735 struct tevent_fd *(*add_fd)(struct tevent_context *ev,
1736 TALLOC_CTX *mem_ctx,
1737 int fd, uint16_t flags,
1738 tevent_fd_handler_t handler,
1739 void *private_data,
1740 const char *handler_name,
1741 const char *location);
1742 void (*set_fd_close_fn)(struct tevent_fd *fde,
1743 tevent_fd_close_fn_t close_fn);
1744 uint16_t (*get_fd_flags)(struct tevent_fd *fde);
1745 void (*set_fd_flags)(struct tevent_fd *fde, uint16_t flags);
1747 /* timed_event functions */
1748 struct tevent_timer *(*add_timer)(struct tevent_context *ev,
1749 TALLOC_CTX *mem_ctx,
1750 struct timeval next_event,
1751 tevent_timer_handler_t handler,
1752 void *private_data,
1753 const char *handler_name,
1754 const char *location);
1756 /* immediate event functions */
1757 void (*schedule_immediate)(struct tevent_immediate *im,
1758 struct tevent_context *ev,
1759 tevent_immediate_handler_t handler,
1760 void *private_data,
1761 const char *handler_name,
1762 const char *location);
1764 /* signal functions */
1765 struct tevent_signal *(*add_signal)(struct tevent_context *ev,
1766 TALLOC_CTX *mem_ctx,
1767 int signum, int sa_flags,
1768 tevent_signal_handler_t handler,
1769 void *private_data,
1770 const char *handler_name,
1771 const char *location);
1773 /* loop functions */
1774 int (*loop_once)(struct tevent_context *ev, const char *location);
1775 int (*loop_wait)(struct tevent_context *ev, const char *location);
1778 bool tevent_register_backend(const char *name, const struct tevent_ops *ops);
1780 /* @} */
1783 * @defgroup tevent_compat The tevent compatibility functions
1784 * @ingroup tevent
1786 * The following definitions are usueful only for compatibility with the
1787 * implementation originally developed within the samba4 code and will be
1788 * soon removed. Please NEVER use in new code.
1790 * @todo Ignore it?
1792 * @{
1795 #ifdef TEVENT_COMPAT_DEFINES
1797 #define event_context tevent_context
1798 #define event_ops tevent_ops
1799 #define fd_event tevent_fd
1800 #define timed_event tevent_timer
1801 #define signal_event tevent_signal
1803 #define event_fd_handler_t tevent_fd_handler_t
1804 #define event_timed_handler_t tevent_timer_handler_t
1805 #define event_signal_handler_t tevent_signal_handler_t
1807 #define event_context_init(mem_ctx) \
1808 tevent_context_init(mem_ctx)
1810 #define event_context_init_byname(mem_ctx, name) \
1811 tevent_context_init_byname(mem_ctx, name)
1813 #define event_backend_list(mem_ctx) \
1814 tevent_backend_list(mem_ctx)
1816 #define event_set_default_backend(backend) \
1817 tevent_set_default_backend(backend)
1819 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
1820 tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
1822 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
1823 tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
1825 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
1826 tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
1828 #define event_loop_once(ev) \
1829 tevent_loop_once(ev)
1831 #define event_loop_wait(ev) \
1832 tevent_loop_wait(ev)
1834 #define event_get_fd_flags(fde) \
1835 tevent_fd_get_flags(fde)
1837 #define event_set_fd_flags(fde, flags) \
1838 tevent_fd_set_flags(fde, flags)
1840 #define EVENT_FD_READ TEVENT_FD_READ
1841 #define EVENT_FD_WRITE TEVENT_FD_WRITE
1843 #define EVENT_FD_WRITEABLE(fde) \
1844 TEVENT_FD_WRITEABLE(fde)
1846 #define EVENT_FD_READABLE(fde) \
1847 TEVENT_FD_READABLE(fde)
1849 #define EVENT_FD_NOT_WRITEABLE(fde) \
1850 TEVENT_FD_NOT_WRITEABLE(fde)
1852 #define EVENT_FD_NOT_READABLE(fde) \
1853 TEVENT_FD_NOT_READABLE(fde)
1855 #define ev_debug_level tevent_debug_level
1857 #define EV_DEBUG_FATAL TEVENT_DEBUG_FATAL
1858 #define EV_DEBUG_ERROR TEVENT_DEBUG_ERROR
1859 #define EV_DEBUG_WARNING TEVENT_DEBUG_WARNING
1860 #define EV_DEBUG_TRACE TEVENT_DEBUG_TRACE
1862 #define ev_set_debug(ev, debug, context) \
1863 tevent_set_debug(ev, debug, context)
1865 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
1867 #endif /* TEVENT_COMPAT_DEFINES */
1869 /* @} */
1871 #endif /* __TEVENT_H__ */