tevent: add tevent_req_received() function
[Samba/gbeck.git] / lib / tevent / tevent.h
blob8ae9eaf54579252cbc6c5387d5b6b724621e6f1c
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_signal;
42 /* event handler types */
43 typedef void (*tevent_fd_handler_t)(struct tevent_context *ev,
44 struct tevent_fd *fde,
45 uint16_t flags,
46 void *private_data);
47 typedef void (*tevent_fd_close_fn_t)(struct tevent_context *ev,
48 struct tevent_fd *fde,
49 int fd,
50 void *private_data);
51 typedef void (*tevent_timer_handler_t)(struct tevent_context *ev,
52 struct tevent_timer *te,
53 struct timeval current_time,
54 void *private_data);
55 typedef void (*tevent_signal_handler_t)(struct tevent_context *ev,
56 struct tevent_signal *se,
57 int signum,
58 int count,
59 void *siginfo,
60 void *private_data);
62 struct tevent_context *tevent_context_init(TALLOC_CTX *mem_ctx);
63 struct tevent_context *tevent_context_init_byname(TALLOC_CTX *mem_ctx, const char *name);
64 const char **tevent_backend_list(TALLOC_CTX *mem_ctx);
65 void tevent_set_default_backend(const char *backend);
67 struct tevent_fd *_tevent_add_fd(struct tevent_context *ev,
68 TALLOC_CTX *mem_ctx,
69 int fd,
70 uint16_t flags,
71 tevent_fd_handler_t handler,
72 void *private_data,
73 const char *handler_name,
74 const char *location);
75 #define tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
76 _tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data, \
77 #handler, __location__)
79 struct tevent_timer *_tevent_add_timer(struct tevent_context *ev,
80 TALLOC_CTX *mem_ctx,
81 struct timeval next_event,
82 tevent_timer_handler_t handler,
83 void *private_data,
84 const char *handler_name,
85 const char *location);
86 #define tevent_add_timer(ev, mem_ctx, next_event, handler, private_data) \
87 _tevent_add_timer(ev, mem_ctx, next_event, handler, private_data, \
88 #handler, __location__)
90 struct tevent_signal *_tevent_add_signal(struct tevent_context *ev,
91 TALLOC_CTX *mem_ctx,
92 int signum,
93 int sa_flags,
94 tevent_signal_handler_t handler,
95 void *private_data,
96 const char *handler_name,
97 const char *location);
98 #define tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
99 _tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data, \
100 #handler, __location__)
102 int tevent_loop_once(struct tevent_context *ev);
103 int tevent_loop_wait(struct tevent_context *ev);
105 void tevent_fd_set_close_fn(struct tevent_fd *fde,
106 tevent_fd_close_fn_t close_fn);
107 void tevent_fd_set_auto_close(struct tevent_fd *fde);
108 uint16_t tevent_fd_get_flags(struct tevent_fd *fde);
109 void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
111 /* bits for file descriptor event flags */
112 #define TEVENT_FD_READ 1
113 #define TEVENT_FD_WRITE 2
115 #define TEVENT_FD_WRITEABLE(fde) \
116 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_WRITE)
117 #define TEVENT_FD_READABLE(fde) \
118 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_READ)
120 #define TEVENT_FD_NOT_WRITEABLE(fde) \
121 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_WRITE)
122 #define TEVENT_FD_NOT_READABLE(fde) \
123 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_READ)
125 /* DEBUG */
126 enum tevent_debug_level {
127 TEVENT_DEBUG_FATAL,
128 TEVENT_DEBUG_ERROR,
129 TEVENT_DEBUG_WARNING,
130 TEVENT_DEBUG_TRACE
133 int tevent_set_debug(struct tevent_context *ev,
134 void (*debug)(void *context,
135 enum tevent_debug_level level,
136 const char *fmt,
137 va_list ap) PRINTF_ATTRIBUTE(3,0),
138 void *context);
139 int tevent_set_debug_stderr(struct tevent_context *ev);
142 * An async request moves between the following 4 states:
144 enum tevent_req_state {
146 * we are creating the request
148 TEVENT_REQ_INIT,
150 * we are waiting the request to complete
152 TEVENT_REQ_IN_PROGRESS,
154 * the request is finished
156 TEVENT_REQ_DONE,
158 * A user error has occured
160 TEVENT_REQ_USER_ERROR,
162 * Request timed out
164 TEVENT_REQ_TIMED_OUT,
166 * No memory in between
168 TEVENT_REQ_NO_MEMORY,
170 * the request is already received by the caller
172 TEVENT_REQ_RECEIVED
176 * @brief An async request
178 * This represents an async request being processed by callbacks via an event
179 * context. A user can issue for example a write request to a socket, giving
180 * an implementation function the fd, the buffer and the number of bytes to
181 * transfer. The function issuing the request will immediately return without
182 * blocking most likely without having sent anything. The API user then fills
183 * in req->async.fn and req->async.private_data, functions that are called
184 * when the request is finished.
186 * It is up to the user of the async request to talloc_free it after it has
187 * finished. This can happen while the completion function is called.
190 struct tevent_req;
192 typedef void (*tevent_req_fn)(struct tevent_req *);
194 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt);
195 void *_tevent_req_callback_data(struct tevent_req *req);
196 void *_tevent_req_data(struct tevent_req *req);
198 #define tevent_req_callback_data(_req, _type) \
199 talloc_get_type_abort(_tevent_req_callback_data(_req), _type)
200 #define tevent_req_data(_req, _type) \
201 talloc_get_type_abort(_tevent_req_data(_req), _type)
203 typedef char *(*tevent_req_print_fn)(struct tevent_req *, TALLOC_CTX *);
205 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn);
207 char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx);
209 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req);
211 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
212 void *pstate,
213 size_t state_size,
214 const char *type,
215 const char *location);
217 #define tevent_req_create(_mem_ctx, _pstate, _type) \
218 _tevent_req_create((_mem_ctx), (_pstate), sizeof(_type), \
219 #_type, __location__)
221 bool tevent_req_set_endtime(struct tevent_req *req,
222 struct tevent_context *ev,
223 struct timeval endtime);
225 void tevent_req_done(struct tevent_req *req);
227 bool tevent_req_error(struct tevent_req *req,
228 uint64_t error);
230 bool tevent_req_nomem(const void *p,
231 struct tevent_req *req);
233 struct tevent_req *tevent_req_post(struct tevent_req *req,
234 struct tevent_context *ev);
236 bool tevent_req_is_in_progress(struct tevent_req *req);
238 bool tevent_req_poll(struct tevent_req *req,
239 struct tevent_context *ev);
241 bool tevent_req_is_error(struct tevent_req *req,
242 enum tevent_req_state *state,
243 uint64_t *error);
245 void tevent_req_received(struct tevent_req *req);
247 struct tevent_req *tevent_wakeup_send(TALLOC_CTX *mem_ctx,
248 struct tevent_context *ev,
249 struct timeval wakeup_time);
250 bool tevent_wakeup_recv(struct tevent_req *req);
252 int tevent_timeval_compare(const struct timeval *tv1,
253 const struct timeval *tv2);
255 struct timeval tevent_timeval_zero(void);
257 struct timeval tevent_timeval_current(void);
259 struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs);
261 struct timeval tevent_timeval_until(const struct timeval *tv1,
262 const struct timeval *tv2);
264 bool tevent_timeval_is_zero(const struct timeval *tv);
266 struct timeval tevent_timeval_add(const struct timeval *tv, uint32_t secs,
267 uint32_t usecs);
269 struct timeval tevent_timeval_current_ofs(uint32_t secs, uint32_t usecs);
271 struct tevent_queue;
273 struct tevent_queue *_tevent_queue_create(TALLOC_CTX *mem_ctx,
274 const char *name,
275 const char *location);
277 #define tevent_queue_create(_mem_ctx, _name) \
278 _tevent_queue_create((_mem_ctx), (_name), __location__)
280 typedef void (*tevent_queue_trigger_fn_t)(struct tevent_req *req,
281 void *private_data);
282 bool tevent_queue_add(struct tevent_queue *queue,
283 struct tevent_context *ev,
284 struct tevent_req *req,
285 tevent_queue_trigger_fn_t trigger,
286 void *private_data);
287 bool tevent_queue_start(struct tevent_queue *queue,
288 struct tevent_context *ev);
289 void tevent_queue_stop(struct tevent_queue *queue);
291 size_t tevent_queue_length(struct tevent_queue *queue);
293 #ifdef TEVENT_COMPAT_DEFINES
295 #define event_context tevent_context
296 #define event_ops tevent_ops
297 #define fd_event tevent_fd
298 #define timed_event tevent_timer
299 #define signal_event tevent_signal
301 #define event_fd_handler_t tevent_fd_handler_t
302 #define event_timed_handler_t tevent_timer_handler_t
303 #define event_signal_handler_t tevent_signal_handler_t
305 #define event_context_init(mem_ctx) \
306 tevent_context_init(mem_ctx)
308 #define event_context_init_byname(mem_ctx, name) \
309 tevent_context_init_byname(mem_ctx, name)
311 #define event_backend_list(mem_ctx) \
312 tevent_backend_list(mem_ctx)
314 #define event_set_default_backend(backend) \
315 tevent_set_default_backend(backend)
317 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
318 tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
320 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
321 tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
323 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
324 tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
326 #define event_loop_once(ev) \
327 tevent_loop_once(ev)
329 #define event_loop_wait(ev) \
330 tevent_loop_wait(ev)
332 #define event_get_fd_flags(fde) \
333 tevent_fd_get_flags(fde)
335 #define event_set_fd_flags(fde, flags) \
336 tevent_fd_set_flags(fde, flags)
338 #define EVENT_FD_READ TEVENT_FD_READ
339 #define EVENT_FD_WRITE TEVENT_FD_WRITE
341 #define EVENT_FD_WRITEABLE(fde) \
342 TEVENT_FD_WRITEABLE(fde)
344 #define EVENT_FD_READABLE(fde) \
345 TEVENT_FD_READABLE(fde)
347 #define EVENT_FD_NOT_WRITEABLE(fde) \
348 TEVENT_FD_NOT_WRITEABLE(fde)
350 #define EVENT_FD_NOT_READABLE(fde) \
351 TEVENT_FD_NOT_READABLE(fde)
353 #define ev_debug_level tevent_debug_level
355 #define EV_DEBUG_FATAL TEVENT_DEBUG_FATAL
356 #define EV_DEBUG_ERROR TEVENT_DEBUG_ERROR
357 #define EV_DEBUG_WARNING TEVENT_DEBUG_WARNING
358 #define EV_DEBUG_TRACE TEVENT_DEBUG_TRACE
360 #define ev_set_debug(ev, debug, context) \
361 tevent_set_debug(ev, debug, context)
363 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
365 #endif /* TEVENT_COMPAT_DEFINES */
367 #endif /* __TEVENT_H__ */