tevent: add tevent_signal_support()
[Samba/gbeck.git] / lib / tevent / tevent.h
blob1f4cf2d3297218ffddc14e9c4902e3c545931a69
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 bool tevent_signal_support(struct tevent_context *ev);
113 /* bits for file descriptor event flags */
114 #define TEVENT_FD_READ 1
115 #define TEVENT_FD_WRITE 2
117 #define TEVENT_FD_WRITEABLE(fde) \
118 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_WRITE)
119 #define TEVENT_FD_READABLE(fde) \
120 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) | TEVENT_FD_READ)
122 #define TEVENT_FD_NOT_WRITEABLE(fde) \
123 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_WRITE)
124 #define TEVENT_FD_NOT_READABLE(fde) \
125 tevent_fd_set_flags(fde, tevent_fd_get_flags(fde) & ~TEVENT_FD_READ)
127 /* DEBUG */
128 enum tevent_debug_level {
129 TEVENT_DEBUG_FATAL,
130 TEVENT_DEBUG_ERROR,
131 TEVENT_DEBUG_WARNING,
132 TEVENT_DEBUG_TRACE
135 int tevent_set_debug(struct tevent_context *ev,
136 void (*debug)(void *context,
137 enum tevent_debug_level level,
138 const char *fmt,
139 va_list ap) PRINTF_ATTRIBUTE(3,0),
140 void *context);
141 int tevent_set_debug_stderr(struct tevent_context *ev);
144 * An async request moves between the following 4 states:
146 enum tevent_req_state {
148 * we are creating the request
150 TEVENT_REQ_INIT,
152 * we are waiting the request to complete
154 TEVENT_REQ_IN_PROGRESS,
156 * the request is finished
158 TEVENT_REQ_DONE,
160 * A user error has occured
162 TEVENT_REQ_USER_ERROR,
164 * Request timed out
166 TEVENT_REQ_TIMED_OUT,
168 * No memory in between
170 TEVENT_REQ_NO_MEMORY,
172 * the request is already received by the caller
174 TEVENT_REQ_RECEIVED
178 * @brief An async request
180 * This represents an async request being processed by callbacks via an event
181 * context. A user can issue for example a write request to a socket, giving
182 * an implementation function the fd, the buffer and the number of bytes to
183 * transfer. The function issuing the request will immediately return without
184 * blocking most likely without having sent anything. The API user then fills
185 * in req->async.fn and req->async.private_data, functions that are called
186 * when the request is finished.
188 * It is up to the user of the async request to talloc_free it after it has
189 * finished. This can happen while the completion function is called.
192 struct tevent_req;
194 typedef void (*tevent_req_fn)(struct tevent_req *);
196 void tevent_req_set_callback(struct tevent_req *req, tevent_req_fn fn, void *pvt);
197 void *_tevent_req_callback_data(struct tevent_req *req);
198 void *_tevent_req_data(struct tevent_req *req);
200 #define tevent_req_callback_data(_req, _type) \
201 talloc_get_type_abort(_tevent_req_callback_data(_req), _type)
202 #define tevent_req_callback_data_void(_req) \
203 _tevent_req_callback_data(_req)
204 #define tevent_req_data(_req, _type) \
205 talloc_get_type_abort(_tevent_req_data(_req), _type)
207 typedef char *(*tevent_req_print_fn)(struct tevent_req *, TALLOC_CTX *);
209 void tevent_req_set_print_fn(struct tevent_req *req, tevent_req_print_fn fn);
211 char *tevent_req_default_print(struct tevent_req *req, TALLOC_CTX *mem_ctx);
213 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req);
215 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
216 void *pstate,
217 size_t state_size,
218 const char *type,
219 const char *location);
221 #define tevent_req_create(_mem_ctx, _pstate, _type) \
222 _tevent_req_create((_mem_ctx), (_pstate), sizeof(_type), \
223 #_type, __location__)
225 bool tevent_req_set_endtime(struct tevent_req *req,
226 struct tevent_context *ev,
227 struct timeval endtime);
229 void tevent_req_done(struct tevent_req *req);
231 bool tevent_req_error(struct tevent_req *req,
232 uint64_t error);
234 bool tevent_req_nomem(const void *p,
235 struct tevent_req *req);
237 struct tevent_req *tevent_req_post(struct tevent_req *req,
238 struct tevent_context *ev);
240 bool tevent_req_is_in_progress(struct tevent_req *req);
242 bool tevent_req_poll(struct tevent_req *req,
243 struct tevent_context *ev);
245 bool tevent_req_is_error(struct tevent_req *req,
246 enum tevent_req_state *state,
247 uint64_t *error);
249 void tevent_req_received(struct tevent_req *req);
251 struct tevent_req *tevent_wakeup_send(TALLOC_CTX *mem_ctx,
252 struct tevent_context *ev,
253 struct timeval wakeup_time);
254 bool tevent_wakeup_recv(struct tevent_req *req);
256 int tevent_timeval_compare(const struct timeval *tv1,
257 const struct timeval *tv2);
259 struct timeval tevent_timeval_zero(void);
261 struct timeval tevent_timeval_current(void);
263 struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs);
265 struct timeval tevent_timeval_until(const struct timeval *tv1,
266 const struct timeval *tv2);
268 bool tevent_timeval_is_zero(const struct timeval *tv);
270 struct timeval tevent_timeval_add(const struct timeval *tv, uint32_t secs,
271 uint32_t usecs);
273 struct timeval tevent_timeval_current_ofs(uint32_t secs, uint32_t usecs);
275 struct tevent_queue;
277 struct tevent_queue *_tevent_queue_create(TALLOC_CTX *mem_ctx,
278 const char *name,
279 const char *location);
281 #define tevent_queue_create(_mem_ctx, _name) \
282 _tevent_queue_create((_mem_ctx), (_name), __location__)
284 typedef void (*tevent_queue_trigger_fn_t)(struct tevent_req *req,
285 void *private_data);
286 bool tevent_queue_add(struct tevent_queue *queue,
287 struct tevent_context *ev,
288 struct tevent_req *req,
289 tevent_queue_trigger_fn_t trigger,
290 void *private_data);
291 bool tevent_queue_start(struct tevent_queue *queue,
292 struct tevent_context *ev);
293 void tevent_queue_stop(struct tevent_queue *queue);
295 size_t tevent_queue_length(struct tevent_queue *queue);
297 #ifdef TEVENT_COMPAT_DEFINES
299 #define event_context tevent_context
300 #define event_ops tevent_ops
301 #define fd_event tevent_fd
302 #define timed_event tevent_timer
303 #define signal_event tevent_signal
305 #define event_fd_handler_t tevent_fd_handler_t
306 #define event_timed_handler_t tevent_timer_handler_t
307 #define event_signal_handler_t tevent_signal_handler_t
309 #define event_context_init(mem_ctx) \
310 tevent_context_init(mem_ctx)
312 #define event_context_init_byname(mem_ctx, name) \
313 tevent_context_init_byname(mem_ctx, name)
315 #define event_backend_list(mem_ctx) \
316 tevent_backend_list(mem_ctx)
318 #define event_set_default_backend(backend) \
319 tevent_set_default_backend(backend)
321 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
322 tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
324 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
325 tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
327 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
328 tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
330 #define event_loop_once(ev) \
331 tevent_loop_once(ev)
333 #define event_loop_wait(ev) \
334 tevent_loop_wait(ev)
336 #define event_get_fd_flags(fde) \
337 tevent_fd_get_flags(fde)
339 #define event_set_fd_flags(fde, flags) \
340 tevent_fd_set_flags(fde, flags)
342 #define EVENT_FD_READ TEVENT_FD_READ
343 #define EVENT_FD_WRITE TEVENT_FD_WRITE
345 #define EVENT_FD_WRITEABLE(fde) \
346 TEVENT_FD_WRITEABLE(fde)
348 #define EVENT_FD_READABLE(fde) \
349 TEVENT_FD_READABLE(fde)
351 #define EVENT_FD_NOT_WRITEABLE(fde) \
352 TEVENT_FD_NOT_WRITEABLE(fde)
354 #define EVENT_FD_NOT_READABLE(fde) \
355 TEVENT_FD_NOT_READABLE(fde)
357 #define ev_debug_level tevent_debug_level
359 #define EV_DEBUG_FATAL TEVENT_DEBUG_FATAL
360 #define EV_DEBUG_ERROR TEVENT_DEBUG_ERROR
361 #define EV_DEBUG_WARNING TEVENT_DEBUG_WARNING
362 #define EV_DEBUG_TRACE TEVENT_DEBUG_TRACE
364 #define ev_set_debug(ev, debug, context) \
365 tevent_set_debug(ev, debug, context)
367 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
369 #endif /* TEVENT_COMPAT_DEFINES */
371 #endif /* __TEVENT_H__ */