Attempt to fix the OpenChange build -- sorry for the break
[ira/wip.git] / lib / tevent / tevent.h
1 /* 
2    Unix SMB/CIFS implementation.
3
4    generalised event loop handling
5
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Stefan Metzmacher 2005-2009
8    Copyright (C) Volker Lendecke 2008
9
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
13
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.
18
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.
23
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/>.
26 */
27
28 #ifndef __TEVENT_H__
29 #define __TEVENT_H__
30
31 #include <stdint.h>
32 #include <talloc.h>
33 #include <sys/time.h>
34 #include <stdbool.h>
35
36 struct tevent_context;
37 struct tevent_ops;
38 struct tevent_fd;
39 struct tevent_timer;
40 struct tevent_signal;
41
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);
61
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);
66
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__)
78
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__)
89
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__)
101
102 int tevent_loop_once(struct tevent_context *ev);
103 int tevent_loop_wait(struct tevent_context *ev);
104
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);
110
111 /* bits for file descriptor event flags */
112 #define TEVENT_FD_READ 1
113 #define TEVENT_FD_WRITE 2
114
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)
119
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)
124
125 /* DEBUG */
126 enum tevent_debug_level {
127         TEVENT_DEBUG_FATAL,
128         TEVENT_DEBUG_ERROR,
129         TEVENT_DEBUG_WARNING,
130         TEVENT_DEBUG_TRACE
131 };
132
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);
140
141 /**
142  * An async request moves between the following 4 states:
143  */
144 enum tevent_req_state {
145         /**
146          * we are creating the request
147          */
148         TEVENT_REQ_INIT,
149         /**
150          * we are waiting the request to complete
151          */
152         TEVENT_REQ_IN_PROGRESS,
153         /**
154          * the request is finished
155          */
156         TEVENT_REQ_DONE,
157         /**
158          * A user error has occured
159          */
160         TEVENT_REQ_USER_ERROR,
161         /**
162          * Request timed out
163          */
164         TEVENT_REQ_TIMED_OUT,
165         /**
166          * No memory in between
167          */
168         TEVENT_REQ_NO_MEMORY
169 };
170
171 /**
172  * @brief An async request
173  *
174  * This represents an async request being processed by callbacks via an event
175  * context. A user can issue for example a write request to a socket, giving
176  * an implementation function the fd, the buffer and the number of bytes to
177  * transfer. The function issuing the request will immediately return without
178  * blocking most likely without having sent anything. The API user then fills
179  * in req->async.fn and req->async.private_data, functions that are called
180  * when the request is finished.
181  *
182  * It is up to the user of the async request to talloc_free it after it has
183  * finished. This can happen while the completion function is called.
184  */
185
186 struct tevent_req {
187         /**
188          * @brief What to do on completion
189          *
190          * This is used for the user of an async request, fn is called when
191          * the request completes, either successfully or with an error.
192          */
193         struct {
194                 /**
195                  * @brief Completion function
196                  * Completion function, to be filled by the API user
197                  */
198                 void (*fn)(struct tevent_req *);
199                 /**
200                  * @brief Private data for the completion function
201                  */
202                 void *private_data;
203         } async;
204
205         /**
206          * @brief Private state pointer for the actual implementation
207          *
208          * The implementation doing the work for the async request needs a
209          * current state like for example a fd event. The user of an async
210          * request should not touch this.
211          */
212         void *private_state;
213
214         /**
215          * @brief Internal state of the request
216          *
217          * Callers should only access this via functions and never directly.
218          */
219         struct {
220                 /**
221                  * @brief The talloc type of the private_state pointer
222                  *
223                  * This is filled by the tevent_req_create() macro.
224                  *
225                  * This for debugging only.
226                  */
227                 const char *private_type;
228
229                 /**
230                  * @brief The location where the request was created
231                  *
232                  * This uses the __location__ macro via the tevent_req_create()
233                  * macro.
234                  *
235                  * This for debugging only.
236                  */
237                 const char *location;
238
239                 /**
240                  * @brief The external state - will be queried by the caller
241                  *
242                  * While the async request is being processed, state will remain in
243                  * TEVENT_REQ_IN_PROGRESS. A request is finished if
244                  * req->state>=TEVENT_REQ_DONE.
245                  */
246                 enum tevent_req_state state;
247
248                 /**
249                  * @brief status code when finished
250                  *
251                  * This status can be queried in the async completion function. It
252                  * will be set to 0 when everything went fine.
253                  */
254                 uint64_t error;
255
256                 /**
257                  * @brief the timer event if tevent_req_post was used
258                  *
259                  */
260                 struct tevent_timer *trigger;
261
262                 /**
263                  * @brief the timer event if tevent_req_set_timeout was used
264                  *
265                  */
266                 struct tevent_timer *timer;
267         } internal;
268 };
269
270 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req);
271
272 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
273                                       void *pstate,
274                                       size_t state_size,
275                                       const char *type,
276                                       const char *location);
277
278 #define tevent_req_create(_mem_ctx, _pstate, _type) \
279         _tevent_req_create((_mem_ctx), (_pstate), sizeof(_type), \
280                            #_type, __location__)
281
282 bool tevent_req_set_endtime(struct tevent_req *req,
283                             struct tevent_context *ev,
284                             struct timeval endtime);
285
286 void tevent_req_done(struct tevent_req *req);
287
288 bool tevent_req_error(struct tevent_req *req,
289                       uint64_t error);
290
291 bool tevent_req_nomem(const void *p,
292                       struct tevent_req *req);
293
294 struct tevent_req *tevent_req_post(struct tevent_req *req,
295                                    struct tevent_context *ev);
296
297 bool tevent_req_is_in_progress(struct tevent_req *req);
298
299 bool tevent_req_is_error(struct tevent_req *req,
300                          enum tevent_req_state *state,
301                          uint64_t *error);
302
303 struct tevent_req *tevent_wakeup_send(TALLOC_CTX *mem_ctx,
304                                       struct tevent_context *ev,
305                                       struct timeval wakeup_time);
306 bool tevent_wakeup_recv(struct tevent_req *req);
307
308 int tevent_timeval_compare(const struct timeval *tv1,
309                            const struct timeval *tv2);
310
311 struct timeval tevent_timeval_zero(void);
312
313 struct timeval tevent_timeval_current(void);
314
315 struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs);
316
317 struct timeval tevent_timeval_until(const struct timeval *tv1,
318                                     const struct timeval *tv2);
319
320 bool tevent_timeval_is_zero(const struct timeval *tv);
321
322 struct timeval tevent_timeval_add(const struct timeval *tv, uint32_t secs,
323                                   uint32_t usecs);
324
325 struct timeval tevent_timeval_current_ofs(uint32_t secs, uint32_t usecs);
326
327 #ifdef TEVENT_COMPAT_DEFINES
328
329 #define event_context   tevent_context
330 #define event_ops       tevent_ops
331 #define fd_event        tevent_fd
332 #define timed_event     tevent_timer
333 #define signal_event    tevent_signal
334
335 #define event_fd_handler_t      tevent_fd_handler_t
336 #define event_timed_handler_t   tevent_timer_handler_t
337 #define event_signal_handler_t  tevent_signal_handler_t
338
339 #define event_context_init(mem_ctx) \
340         tevent_context_init(mem_ctx)
341
342 #define event_context_init_byname(mem_ctx, name) \
343         tevent_context_init_byname(mem_ctx, name)
344
345 #define event_backend_list(mem_ctx) \
346         tevent_backend_list(mem_ctx)
347
348 #define event_set_default_backend(backend) \
349         tevent_set_default_backend(backend)
350
351 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
352         tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
353
354 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
355         tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
356
357 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
358         tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
359
360 #define event_loop_once(ev) \
361         tevent_loop_once(ev)
362
363 #define event_loop_wait(ev) \
364         tevent_loop_wait(ev)
365
366 #define event_get_fd_flags(fde) \
367         tevent_fd_get_flags(fde)
368
369 #define event_set_fd_flags(fde, flags) \
370         tevent_fd_set_flags(fde, flags)
371
372 #define EVENT_FD_READ           TEVENT_FD_READ
373 #define EVENT_FD_WRITE          TEVENT_FD_WRITE
374
375 #define EVENT_FD_WRITEABLE(fde) \
376         TEVENT_FD_WRITEABLE(fde)
377
378 #define EVENT_FD_READABLE(fde) \
379         TEVENT_FD_READABLE(fde)
380
381 #define EVENT_FD_NOT_WRITEABLE(fde) \
382         TEVENT_FD_NOT_WRITEABLE(fde)
383
384 #define EVENT_FD_NOT_READABLE(fde) \
385         TEVENT_FD_NOT_READABLE(fde)
386
387 #define ev_debug_level          tevent_debug_level
388
389 #define EV_DEBUG_FATAL          TEVENT_DEBUG_FATAL
390 #define EV_DEBUG_ERROR          TEVENT_DEBUG_ERROR
391 #define EV_DEBUG_WARNING        TEVENT_DEBUG_WARNING
392 #define EV_DEBUG_TRACE          TEVENT_DEBUG_TRACE
393
394 #define ev_set_debug(ev, debug, context) \
395         tevent_set_debug(ev, debug, context)
396
397 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
398
399 #endif /* TEVENT_COMPAT_DEFINES */
400
401 #endif /* __TEVENT_H__ */