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
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/>.
35 struct tevent_context
;
41 /* event handler types */
42 typedef void (*tevent_fd_handler_t
)(struct tevent_context
*ev
,
43 struct tevent_fd
*fde
,
46 typedef void (*tevent_fd_close_fn_t
)(struct tevent_context
*ev
,
47 struct tevent_fd
*fde
,
50 typedef void (*tevent_timer_handler_t
)(struct tevent_context
*ev
,
51 struct tevent_timer
*te
,
52 struct timeval current_time
,
54 typedef void (*tevent_signal_handler_t
)(struct tevent_context
*ev
,
55 struct tevent_signal
*se
,
61 struct tevent_context
*tevent_context_init(TALLOC_CTX
*mem_ctx
);
62 struct tevent_context
*tevent_context_init_byname(TALLOC_CTX
*mem_ctx
, const char *name
);
63 const char **tevent_backend_list(TALLOC_CTX
*mem_ctx
);
64 void tevent_set_default_backend(const char *backend
);
66 struct tevent_fd
*_tevent_add_fd(struct tevent_context
*ev
,
70 tevent_fd_handler_t handler
,
72 const char *handler_name
,
73 const char *location
);
74 #define tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
75 _tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data, \
76 #handler, __location__)
78 struct tevent_timer
*_tevent_add_timer(struct tevent_context
*ev
,
80 struct timeval next_event
,
81 tevent_timer_handler_t handler
,
83 const char *handler_name
,
84 const char *location
);
85 #define tevent_add_timer(ev, mem_ctx, next_event, handler, private_data) \
86 _tevent_add_timer(ev, mem_ctx, next_event, handler, private_data, \
87 #handler, __location__)
89 struct tevent_signal
*_tevent_add_signal(struct tevent_context
*ev
,
93 tevent_signal_handler_t handler
,
95 const char *handler_name
,
96 const char *location
);
97 #define tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
98 _tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data, \
99 #handler, __location__)
101 int tevent_loop_once(struct tevent_context
*ev
);
102 int tevent_loop_wait(struct tevent_context
*ev
);
104 void tevent_fd_set_close_fn(struct tevent_fd
*fde
,
105 tevent_fd_close_fn_t close_fn
);
106 void tevent_fd_set_auto_close(struct tevent_fd
*fde
);
107 uint16_t tevent_fd_get_flags(struct tevent_fd
*fde
);
108 void tevent_fd_set_flags(struct tevent_fd
*fde
, uint16_t flags
);
110 /* bits for file descriptor event flags */
111 #define TEVENT_FD_READ 1
112 #define TEVENT_FD_WRITE 2
114 #define TEVENT_FD_WRITEABLE(fde) \
115 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_WRITE)
116 #define TEVENT_FD_READABLE(fde) \
117 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_READ)
119 #define TEVENT_FD_NOT_WRITEABLE(fde) \
120 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_WRITE)
121 #define TEVENT_FD_NOT_READABLE(fde) \
122 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_READ)
125 enum tevent_debug_level
{
128 TEVENT_DEBUG_WARNING
,
132 int tevent_set_debug(struct tevent_context
*ev
,
133 void (*debug
)(void *context
,
134 enum tevent_debug_level level
,
136 va_list ap
) PRINTF_ATTRIBUTE(3,0),
138 int tevent_set_debug_stderr(struct tevent_context
*ev
);
141 * An async request moves between the following 4 states:
143 enum tevent_req_state
{
145 * we are creating the request
149 * we are waiting the request to complete
151 TEVENT_REQ_IN_PROGRESS
,
153 * the request is finished
157 * A user error has occured
159 TEVENT_REQ_USER_ERROR
,
163 TEVENT_REQ_TIMED_OUT
,
165 * No memory in between
171 * @brief An async request
173 * This represents an async request being processed by callbacks via an event
174 * context. A user can issue for example a write request to a socket, giving
175 * an implementation function the fd, the buffer and the number of bytes to
176 * transfer. The function issuing the request will immediately return without
177 * blocking most likely without having sent anything. The API user then fills
178 * in req->async.fn and req->async.private_data, functions that are called
179 * when the request is finished.
181 * It is up to the user of the async request to talloc_free it after it has
182 * finished. This can happen while the completion function is called.
187 * @brief What to do on completion
189 * This is used for the user of an async request, fn is called when
190 * the request completes, either successfully or with an error.
194 * @brief Completion function
195 * Completion function, to be filled by the API user
197 void (*fn
)(struct tevent_req
*);
199 * @brief Private data for the completion function
205 * @brief Private state pointer for the actual implementation
207 * The implementation doing the work for the async request needs a
208 * current state like for example a fd event. The user of an async
209 * request should not touch this.
214 * @brief Internal state of the request
216 * Callers should only access this via functions and never directly.
220 * @brief The talloc type of the private_state pointer
222 * This is filled by the tevent_req_create() macro.
224 * This for debugging only.
226 const char *private_type
;
229 * @brief The location where the request was created
231 * This uses the __location__ macro via the tevent_req_create()
234 * This for debugging only.
236 const char *location
;
239 * @brief The external state - will be queried by the caller
241 * While the async request is being processed, state will remain in
242 * TEVENT_REQ_IN_PROGRESS. A request is finished if
243 * req->state>=TEVENT_REQ_DONE.
245 enum tevent_req_state state
;
248 * @brief status code when finished
250 * This status can be queried in the async completion function. It
251 * will be set to 0 when everything went fine.
256 * @brief the timer event if tevent_req_post was used
259 struct tevent_timer
*trigger
;
262 * @brief the timer event if tevent_req_set_timeout was used
265 struct tevent_timer
*timer
;
269 char *tevent_req_print(TALLOC_CTX
*mem_ctx
, struct tevent_req
*req
);
271 struct tevent_req
*_tevent_req_create(TALLOC_CTX
*mem_ctx
,
275 const char *location
);
277 #define tevent_req_create(_mem_ctx, _pstate, _type) \
278 _tevent_req_create((_mem_ctx), (_pstate), sizeof(_type), \
279 #_type, __location__)
281 bool tevent_req_set_timeout(struct tevent_req
*req
,
282 struct tevent_context
*ev
,
283 struct timeval endtime
);
285 void tevent_req_done(struct tevent_req
*req
);
287 bool tevent_req_error(struct tevent_req
*req
,
290 bool tevent_req_nomem(const void *p
,
291 struct tevent_req
*req
);
293 struct tevent_req
*tevent_req_post(struct tevent_req
*req
,
294 struct tevent_context
*ev
);
296 bool tevent_req_is_in_progress(struct tevent_req
*req
);
298 bool tevent_req_is_error(struct tevent_req
*req
,
299 enum tevent_req_state
*state
,
303 int tevent_timeval_compare(const struct timeval
*tv1
,
304 const struct timeval
*tv2
);
306 struct timeval
tevent_timeval_zero(void);
308 struct timeval
tevent_timeval_current(void);
310 struct timeval
tevent_timeval_set(uint32_t secs
, uint32_t usecs
);
312 struct timeval
tevent_timeval_until(const struct timeval
*tv1
,
313 const struct timeval
*tv2
);
315 bool tevent_timeval_is_zero(const struct timeval
*tv
);
317 struct timeval
tevent_timeval_add(const struct timeval
*tv
, uint32_t secs
,
320 struct timeval
tevent_timeval_current_ofs(uint32_t secs
, uint32_t usecs
);
322 #ifdef TEVENT_COMPAT_DEFINES
324 #define event_context tevent_context
325 #define event_ops tevent_ops
326 #define fd_event tevent_fd
327 #define timed_event tevent_timer
328 #define signal_event tevent_signal
330 #define event_fd_handler_t tevent_fd_handler_t
331 #define event_timed_handler_t tevent_timer_handler_t
332 #define event_signal_handler_t tevent_signal_handler_t
334 #define event_context_init(mem_ctx) \
335 tevent_context_init(mem_ctx)
337 #define event_context_init_byname(mem_ctx, name) \
338 tevent_context_init_byname(mem_ctx, name)
340 #define event_backend_list(mem_ctx) \
341 tevent_backend_list(mem_ctx)
343 #define event_set_default_backend(backend) \
344 tevent_set_default_backend(backend)
346 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
347 tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
349 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
350 tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
352 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
353 tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
355 #define event_loop_once(ev) \
358 #define event_loop_wait(ev) \
361 #define event_get_fd_flags(fde) \
362 tevent_fd_get_flags(fde)
364 #define event_set_fd_flags(fde, flags) \
365 tevent_fd_set_flags(fde, flags)
367 #define EVENT_FD_READ TEVENT_FD_READ
368 #define EVENT_FD_WRITE TEVENT_FD_WRITE
370 #define EVENT_FD_WRITEABLE(fde) \
371 TEVENT_FD_WRITEABLE(fde)
373 #define EVENT_FD_READABLE(fde) \
374 TEVENT_FD_READABLE(fde)
376 #define EVENT_FD_NOT_WRITEABLE(fde) \
377 TEVENT_FD_NOT_WRITEABLE(fde)
379 #define EVENT_FD_NOT_READABLE(fde) \
380 TEVENT_FD_NOT_READABLE(fde)
382 #define ev_debug_level tevent_debug_level
384 #define EV_DEBUG_FATAL TEVENT_DEBUG_FATAL
385 #define EV_DEBUG_ERROR TEVENT_DEBUG_ERROR
386 #define EV_DEBUG_WARNING TEVENT_DEBUG_WARNING
387 #define EV_DEBUG_TRACE TEVENT_DEBUG_TRACE
389 #define ev_set_debug(ev, debug, context) \
390 tevent_set_debug(ev, debug, context)
392 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
394 #endif /* TEVENT_COMPAT_DEFINES */
396 #endif /* __TEVENT_H__ */