2 Unix SMB/CIFS implementation.
4 generalised event loop handling
6 INTERNAL STRUCTS. THERE ARE NO API GUARANTEES.
7 External users should only ever have to include this header when
8 implementing new tevent backends.
10 Copyright (C) Stefan Metzmacher 2005-2009
12 ** NOTE! The following LGPL license applies to the tevent
13 ** library. This does NOT imply that all of Samba is released
16 This library is free software; you can redistribute it and/or
17 modify it under the terms of the GNU Lesser General Public
18 License as published by the Free Software Foundation; either
19 version 3 of the License, or (at your option) any later version.
21 This library is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 Lesser General Public License for more details.
26 You should have received a copy of the GNU Lesser General Public
27 License along with this library; if not, see <http://www.gnu.org/licenses/>.
32 * @brief What to do on completion
34 * This is used for the user of an async request, fn is called when
35 * the request completes, either successfully or with an error.
39 * @brief Completion function
40 * Completion function, to be filled by the API user
44 * @brief Private data for the completion function
50 * @brief Private state pointer for the actual implementation
52 * The implementation doing the work for the async request needs to
53 * keep around current data like for example a fd event. The user of
54 * an async request should not touch this.
59 * @brief A function to overwrite the default print function
61 * The implementation doing the work may want to implement a
62 * custom function to print the text representation of the async
65 tevent_req_print_fn private_print
;
68 * @brief A function to cancel the request
70 * The implementation might want to set a function
71 * that is called when the tevent_req_cancel() function
74 tevent_req_cancel_fn private_cancel
;
77 * @brief A function to cleanup the request
79 * The implementation might want to set a function
80 * that is called before the tevent_req_done() and tevent_req_error()
81 * trigger the callers callback function.
84 tevent_req_cleanup_fn fn
;
85 enum tevent_req_state state
;
89 * @brief Internal state of the request
91 * Callers should only access this via functions and never directly.
95 * @brief The talloc type of the data pointer
97 * This is filled by the tevent_req_create() macro.
99 * This for debugging only.
101 const char *private_type
;
104 * @brief The location where the request was created
106 * This uses the __location__ macro via the tevent_req_create()
109 * This for debugging only.
111 const char *create_location
;
114 * @brief The location where the request was finished
116 * This uses the __location__ macro via the tevent_req_done(),
117 * tevent_req_error() or tevent_req_nomem() macro.
119 * This for debugging only.
121 const char *finish_location
;
124 * @brief The location where the request was canceled
126 * This uses the __location__ macro via the
127 * tevent_req_cancel() macro.
129 * This for debugging only.
131 const char *cancel_location
;
134 * @brief The external state - will be queried by the caller
136 * While the async request is being processed, state will remain in
137 * TEVENT_REQ_IN_PROGRESS. A request is finished if
138 * req->state>=TEVENT_REQ_DONE.
140 enum tevent_req_state state
;
143 * @brief status code when finished
145 * This status can be queried in the async completion function. It
146 * will be set to 0 when everything went fine.
151 * @brief the immediate event used by tevent_req_post
154 struct tevent_immediate
*trigger
;
157 * @brief An event context which will be used to
158 * defer the _tevent_req_notify_callback().
160 struct tevent_context
*defer_callback_ev
;
163 * @brief the timer event if tevent_req_set_endtime was used
166 struct tevent_timer
*timer
;
169 * @brief The place where profiling data is kept
171 struct tevent_req_profile
*profile
;
177 struct tevent_req_profile
{
178 struct tevent_req_profile
*prev
, *next
;
179 struct tevent_req_profile
*parent
;
180 const char *req_name
;
182 const char *start_location
;
183 struct timeval start_time
;
184 const char *stop_location
;
185 struct timeval stop_time
;
186 enum tevent_req_state state
;
188 struct tevent_req_profile
*subprofiles
;
192 struct tevent_fd
*prev
, *next
;
193 struct tevent_context
*event_ctx
;
194 struct tevent_wrapper_glue
*wrapper
;
198 uint16_t flags
; /* see TEVENT_FD_* flags */
199 tevent_fd_handler_t handler
;
200 tevent_fd_close_fn_t close_fn
;
201 /* this is private for the specific handler */
203 /* this is for debugging only! */
204 const char *handler_name
;
205 const char *location
;
206 /* this is private for the events_ops implementation */
207 uint64_t additional_flags
;
208 void *additional_data
;
209 /* custom tag that can be set by caller */
213 struct tevent_timer
{
214 struct tevent_timer
*prev
, *next
;
215 struct tevent_context
*event_ctx
;
216 struct tevent_wrapper_glue
*wrapper
;
219 struct timeval next_event
;
220 tevent_timer_handler_t handler
;
221 /* this is private for the specific handler */
223 /* this is for debugging only! */
224 const char *handler_name
;
225 const char *location
;
226 /* this is private for the events_ops implementation */
227 void *additional_data
;
228 /* custom tag that can be set by caller */
232 struct tevent_immediate
{
233 struct tevent_immediate
*prev
, *next
;
234 struct tevent_context
*event_ctx
;
235 struct tevent_wrapper_glue
*wrapper
;
238 struct tevent_context
*detach_ev_ctx
;
239 tevent_immediate_handler_t handler
;
240 /* this is private for the specific handler */
242 /* this is for debugging only! */
243 const char *handler_name
;
244 const char *create_location
;
245 const char *schedule_location
;
246 /* this is private for the events_ops implementation */
247 void (*cancel_fn
)(struct tevent_immediate
*im
);
248 void *additional_data
;
249 /* custom tag that can be set by caller */
253 struct tevent_signal
{
254 struct tevent_signal
*prev
, *next
;
255 struct tevent_context
*event_ctx
;
256 struct tevent_wrapper_glue
*wrapper
;
261 tevent_signal_handler_t handler
;
262 /* this is private for the specific handler */
264 /* this is for debugging only! */
265 const char *handler_name
;
266 const char *location
;
267 /* this is private for the events_ops implementation */
268 void *additional_data
;
269 /* custom tag that can be set by caller */
273 struct tevent_threaded_context
{
274 struct tevent_threaded_context
*next
, *prev
;
277 pthread_mutex_t event_ctx_mutex
;
279 struct tevent_context
*event_ctx
;
282 struct tevent_debug_ops
{
283 void (*debug
)(void *context
, enum tevent_debug_level level
,
284 const char *fmt
, va_list ap
) PRINTF_ATTRIBUTE(3,0);
288 void tevent_debug(struct tevent_context
*ev
, enum tevent_debug_level level
,
289 const char *fmt
, ...) PRINTF_ATTRIBUTE(3,4);
291 void tevent_abort(struct tevent_context
*ev
, const char *reason
);
293 void tevent_common_check_double_free(TALLOC_CTX
*ptr
, const char *reason
);
295 struct tevent_context
{
296 /* the specific events implementation */
297 const struct tevent_ops
*ops
;
300 * The following three pointers are queried on every loop_once
301 * in the order in which they appear here. Not measured, but
302 * hopefully putting them at the top together with "ops"
303 * should make tevent a *bit* more cache-friendly than before.
306 /* list of signal events - used by common code */
307 struct tevent_signal
*signal_events
;
309 /* List of threaded job indicators */
310 struct tevent_threaded_context
*threaded_contexts
;
312 /* list of immediate events - used by common code */
313 struct tevent_immediate
*immediate_events
;
315 /* list of fd events - used by common code */
316 struct tevent_fd
*fd_events
;
318 /* list of timed events - used by common code */
319 struct tevent_timer
*timer_events
;
321 /* List of scheduled immediates */
322 pthread_mutex_t scheduled_mutex
;
323 struct tevent_immediate
*scheduled_immediates
;
325 /* this is private for the events_ops implementation */
326 void *additional_data
;
328 /* pipe hack used with signal handlers */
329 struct tevent_fd
*wakeup_fde
;
330 int wakeup_fd
; /* fd to write into */
331 #ifndef HAVE_EVENT_FD
335 /* debugging operations */
336 struct tevent_debug_ops debug_ops
;
338 /* info about the nesting status */
342 tevent_nesting_hook hook_fn
;
348 tevent_trace_callback_t callback
;
353 tevent_trace_fd_callback_t callback
;
358 tevent_trace_signal_callback_t callback
;
363 tevent_trace_timer_callback_t callback
;
368 tevent_trace_immediate_callback_t callback
;
373 tevent_trace_queue_callback_t callback
;
380 * This is used on the main event context
382 struct tevent_wrapper_glue
*list
;
385 * This is used on the wrapper event context
387 struct tevent_wrapper_glue
*glue
;
391 * an optimization pointer into timer_events
392 * used by used by common code via
393 * tevent_common_add_timer_v2()
395 struct tevent_timer
*last_zero_timer
;
398 struct tevent_context
*prev
, *next
;
402 int tevent_common_context_destructor(struct tevent_context
*ev
);
403 int tevent_common_loop_wait(struct tevent_context
*ev
,
404 const char *location
);
406 int tevent_common_fd_destructor(struct tevent_fd
*fde
);
407 struct tevent_fd
*tevent_common_add_fd(struct tevent_context
*ev
,
411 tevent_fd_handler_t handler
,
413 const char *handler_name
,
414 const char *location
);
415 void tevent_common_fd_set_close_fn(struct tevent_fd
*fde
,
416 tevent_fd_close_fn_t close_fn
);
417 uint16_t tevent_common_fd_get_flags(struct tevent_fd
*fde
);
418 void tevent_common_fd_set_flags(struct tevent_fd
*fde
, uint16_t flags
);
419 int tevent_common_invoke_fd_handler(struct tevent_fd
*fde
, uint16_t flags
,
422 struct tevent_timer
*tevent_common_add_timer(struct tevent_context
*ev
,
424 struct timeval next_event
,
425 tevent_timer_handler_t handler
,
427 const char *handler_name
,
428 const char *location
);
429 struct tevent_timer
*tevent_common_add_timer_v2(struct tevent_context
*ev
,
431 struct timeval next_event
,
432 tevent_timer_handler_t handler
,
434 const char *handler_name
,
435 const char *location
);
436 struct timeval
tevent_common_loop_timer_delay(struct tevent_context
*);
437 int tevent_common_invoke_timer_handler(struct tevent_timer
*te
,
438 struct timeval current_time
,
441 void tevent_common_schedule_immediate(struct tevent_immediate
*im
,
442 struct tevent_context
*ev
,
443 tevent_immediate_handler_t handler
,
445 const char *handler_name
,
446 const char *location
);
447 int tevent_common_invoke_immediate_handler(struct tevent_immediate
*im
,
449 bool tevent_common_loop_immediate(struct tevent_context
*ev
);
450 void tevent_common_threaded_activate_immediate(struct tevent_context
*ev
);
452 bool tevent_common_have_events(struct tevent_context
*ev
);
453 int tevent_common_wakeup_init(struct tevent_context
*ev
);
454 int tevent_common_wakeup_fd(int fd
);
455 int tevent_common_wakeup(struct tevent_context
*ev
);
457 struct tevent_signal
*tevent_common_add_signal(struct tevent_context
*ev
,
461 tevent_signal_handler_t handler
,
463 const char *handler_name
,
464 const char *location
);
465 int tevent_common_check_signal(struct tevent_context
*ev
);
466 void tevent_cleanup_pending_signal_handlers(struct tevent_signal
*se
);
467 int tevent_common_invoke_signal_handler(struct tevent_signal
*se
,
468 int signum
, int count
, void *siginfo
,
471 struct tevent_context
*tevent_wrapper_main_ev(struct tevent_context
*ev
);
473 struct tevent_wrapper_ops
;
475 struct tevent_wrapper_glue
{
476 struct tevent_wrapper_glue
*prev
, *next
;
477 struct tevent_context
*wrap_ev
;
478 struct tevent_context
*main_ev
;
481 const struct tevent_wrapper_ops
*ops
;
485 void tevent_wrapper_push_use_internal(struct tevent_context
*ev
,
486 struct tevent_wrapper_glue
*wrapper
);
487 void tevent_wrapper_pop_use_internal(const struct tevent_context
*__ev_ptr
,
488 struct tevent_wrapper_glue
*wrapper
);
490 bool tevent_standard_init(void);
491 bool tevent_poll_init(void);
492 bool tevent_poll_event_add_fd_internal(struct tevent_context
*ev
,
493 struct tevent_fd
*fde
);
494 bool tevent_poll_mt_init(void);
496 bool tevent_epoll_init(void);
497 void tevent_epoll_set_panic_fallback(struct tevent_context
*ev
,
498 bool (*panic_fallback
)(struct tevent_context
*ev
,
502 void tevent_thread_call_depth_set(size_t depth
);
504 void tevent_trace_point_callback(struct tevent_context
*ev
,
505 enum tevent_trace_point
);
507 void tevent_trace_fd_callback(struct tevent_context
*ev
,
508 struct tevent_fd
*fde
,
509 enum tevent_event_trace_point
);
511 void tevent_trace_signal_callback(struct tevent_context
*ev
,
512 struct tevent_signal
*se
,
513 enum tevent_event_trace_point
);
515 void tevent_trace_timer_callback(struct tevent_context
*ev
,
516 struct tevent_timer
*te
,
517 enum tevent_event_trace_point
);
519 void tevent_trace_immediate_callback(struct tevent_context
*ev
,
520 struct tevent_immediate
*im
,
521 enum tevent_event_trace_point
);
523 void tevent_trace_queue_callback(struct tevent_context
*ev
,
524 struct tevent_queue_entry
*qe
,
525 enum tevent_event_trace_point
);