tevent: add tevent_wakeup_send/recv()
[jra/samba/.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
35 struct tevent_context;
36 struct tevent_ops;
37 struct tevent_fd;
38 struct tevent_timer;
39 struct tevent_signal;
40
41 /* event handler types */
42 typedef void (*tevent_fd_handler_t)(struct tevent_context *ev,
43                                     struct tevent_fd *fde,
44                                     uint16_t flags,
45                                     void *private_data);
46 typedef void (*tevent_fd_close_fn_t)(struct tevent_context *ev,
47                                      struct tevent_fd *fde,
48                                      int fd,
49                                      void *private_data);
50 typedef void (*tevent_timer_handler_t)(struct tevent_context *ev,
51                                        struct tevent_timer *te,
52                                        struct timeval current_time,
53                                        void *private_data);
54 typedef void (*tevent_signal_handler_t)(struct tevent_context *ev,
55                                         struct tevent_signal *se,
56                                         int signum,
57                                         int count,
58                                         void *siginfo,
59                                         void *private_data);
60
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);
65
66 struct tevent_fd *_tevent_add_fd(struct tevent_context *ev,
67                                  TALLOC_CTX *mem_ctx,
68                                  int fd,
69                                  uint16_t flags,
70                                  tevent_fd_handler_t handler,
71                                  void *private_data,
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__)
77
78 struct tevent_timer *_tevent_add_timer(struct tevent_context *ev,
79                                        TALLOC_CTX *mem_ctx,
80                                        struct timeval next_event,
81                                        tevent_timer_handler_t handler,
82                                        void *private_data,
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__)
88
89 struct tevent_signal *_tevent_add_signal(struct tevent_context *ev,
90                                          TALLOC_CTX *mem_ctx,
91                                          int signum,
92                                          int sa_flags,
93                                          tevent_signal_handler_t handler,
94                                          void *private_data,
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__)
100
101 int tevent_loop_once(struct tevent_context *ev);
102 int tevent_loop_wait(struct tevent_context *ev);
103
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);
109
110 /* bits for file descriptor event flags */
111 #define TEVENT_FD_READ 1
112 #define TEVENT_FD_WRITE 2
113
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)
118
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)
123
124 /* DEBUG */
125 enum tevent_debug_level {
126         TEVENT_DEBUG_FATAL,
127         TEVENT_DEBUG_ERROR,
128         TEVENT_DEBUG_WARNING,
129         TEVENT_DEBUG_TRACE
130 };
131
132 int tevent_set_debug(struct tevent_context *ev,
133                      void (*debug)(void *context,
134                                    enum tevent_debug_level level,
135                                    const char *fmt,
136                                    va_list ap) PRINTF_ATTRIBUTE(3,0),
137                      void *context);
138 int tevent_set_debug_stderr(struct tevent_context *ev);
139
140 /**
141  * An async request moves between the following 4 states:
142  */
143 enum tevent_req_state {
144         /**
145          * we are creating the request
146          */
147         TEVENT_REQ_INIT,
148         /**
149          * we are waiting the request to complete
150          */
151         TEVENT_REQ_IN_PROGRESS,
152         /**
153          * the request is finished
154          */
155         TEVENT_REQ_DONE,
156         /**
157          * A user error has occured
158          */
159         TEVENT_REQ_USER_ERROR,
160         /**
161          * Request timed out
162          */
163         TEVENT_REQ_TIMED_OUT,
164         /**
165          * No memory in between
166          */
167         TEVENT_REQ_NO_MEMORY
168 };
169
170 /**
171  * @brief An async request
172  *
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.
180  *
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.
183  */
184
185 struct tevent_req {
186         /**
187          * @brief What to do on completion
188          *
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.
191          */
192         struct {
193                 /**
194                  * @brief Completion function
195                  * Completion function, to be filled by the API user
196                  */
197                 void (*fn)(struct tevent_req *);
198                 /**
199                  * @brief Private data for the completion function
200                  */
201                 void *private_data;
202         } async;
203
204         /**
205          * @brief Private state pointer for the actual implementation
206          *
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.
210          */
211         void *private_state;
212
213         /**
214          * @brief Internal state of the request
215          *
216          * Callers should only access this via functions and never directly.
217          */
218         struct {
219                 /**
220                  * @brief The talloc type of the private_state pointer
221                  *
222                  * This is filled by the tevent_req_create() macro.
223                  *
224                  * This for debugging only.
225                  */
226                 const char *private_type;
227
228                 /**
229                  * @brief The location where the request was created
230                  *
231                  * This uses the __location__ macro via the tevent_req_create()
232                  * macro.
233                  *
234                  * This for debugging only.
235                  */
236                 const char *location;
237
238                 /**
239                  * @brief The external state - will be queried by the caller
240                  *
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.
244                  */
245                 enum tevent_req_state state;
246
247                 /**
248                  * @brief status code when finished
249                  *
250                  * This status can be queried in the async completion function. It
251                  * will be set to 0 when everything went fine.
252                  */
253                 uint64_t error;
254
255                 /**
256                  * @brief the timer event if tevent_req_post was used
257                  *
258                  */
259                 struct tevent_timer *trigger;
260
261                 /**
262                  * @brief the timer event if tevent_req_set_timeout was used
263                  *
264                  */
265                 struct tevent_timer *timer;
266         } internal;
267 };
268
269 char *tevent_req_print(TALLOC_CTX *mem_ctx, struct tevent_req *req);
270
271 struct tevent_req *_tevent_req_create(TALLOC_CTX *mem_ctx,
272                                       void *pstate,
273                                       size_t state_size,
274                                       const char *type,
275                                       const char *location);
276
277 #define tevent_req_create(_mem_ctx, _pstate, _type) \
278         _tevent_req_create((_mem_ctx), (_pstate), sizeof(_type), \
279                            #_type, __location__)
280
281 bool tevent_req_set_endtime(struct tevent_req *req,
282                             struct tevent_context *ev,
283                             struct timeval endtime);
284
285 void tevent_req_done(struct tevent_req *req);
286
287 bool tevent_req_error(struct tevent_req *req,
288                       uint64_t error);
289
290 bool tevent_req_nomem(const void *p,
291                       struct tevent_req *req);
292
293 struct tevent_req *tevent_req_post(struct tevent_req *req,
294                                    struct tevent_context *ev);
295
296 bool tevent_req_is_in_progress(struct tevent_req *req);
297
298 bool tevent_req_is_error(struct tevent_req *req,
299                          enum tevent_req_state *state,
300                          uint64_t *error);
301
302 struct tevent_req *tevent_wakeup_send(TALLOC_CTX *mem_ctx,
303                                       struct tevent_context *ev,
304                                       struct timeval wakeup_time);
305 bool tevent_wakeup_recv(struct tevent_req *req);
306
307 int tevent_timeval_compare(const struct timeval *tv1,
308                            const struct timeval *tv2);
309
310 struct timeval tevent_timeval_zero(void);
311
312 struct timeval tevent_timeval_current(void);
313
314 struct timeval tevent_timeval_set(uint32_t secs, uint32_t usecs);
315
316 struct timeval tevent_timeval_until(const struct timeval *tv1,
317                                     const struct timeval *tv2);
318
319 bool tevent_timeval_is_zero(const struct timeval *tv);
320
321 struct timeval tevent_timeval_add(const struct timeval *tv, uint32_t secs,
322                                   uint32_t usecs);
323
324 struct timeval tevent_timeval_current_ofs(uint32_t secs, uint32_t usecs);
325
326 #ifdef TEVENT_COMPAT_DEFINES
327
328 #define event_context   tevent_context
329 #define event_ops       tevent_ops
330 #define fd_event        tevent_fd
331 #define timed_event     tevent_timer
332 #define signal_event    tevent_signal
333
334 #define event_fd_handler_t      tevent_fd_handler_t
335 #define event_timed_handler_t   tevent_timer_handler_t
336 #define event_signal_handler_t  tevent_signal_handler_t
337
338 #define event_context_init(mem_ctx) \
339         tevent_context_init(mem_ctx)
340
341 #define event_context_init_byname(mem_ctx, name) \
342         tevent_context_init_byname(mem_ctx, name)
343
344 #define event_backend_list(mem_ctx) \
345         tevent_backend_list(mem_ctx)
346
347 #define event_set_default_backend(backend) \
348         tevent_set_default_backend(backend)
349
350 #define event_add_fd(ev, mem_ctx, fd, flags, handler, private_data) \
351         tevent_add_fd(ev, mem_ctx, fd, flags, handler, private_data)
352
353 #define event_add_timed(ev, mem_ctx, next_event, handler, private_data) \
354         tevent_add_timer(ev, mem_ctx, next_event, handler, private_data)
355
356 #define event_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data) \
357         tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data)
358
359 #define event_loop_once(ev) \
360         tevent_loop_once(ev)
361
362 #define event_loop_wait(ev) \
363         tevent_loop_wait(ev)
364
365 #define event_get_fd_flags(fde) \
366         tevent_fd_get_flags(fde)
367
368 #define event_set_fd_flags(fde, flags) \
369         tevent_fd_set_flags(fde, flags)
370
371 #define EVENT_FD_READ           TEVENT_FD_READ
372 #define EVENT_FD_WRITE          TEVENT_FD_WRITE
373
374 #define EVENT_FD_WRITEABLE(fde) \
375         TEVENT_FD_WRITEABLE(fde)
376
377 #define EVENT_FD_READABLE(fde) \
378         TEVENT_FD_READABLE(fde)
379
380 #define EVENT_FD_NOT_WRITEABLE(fde) \
381         TEVENT_FD_NOT_WRITEABLE(fde)
382
383 #define EVENT_FD_NOT_READABLE(fde) \
384         TEVENT_FD_NOT_READABLE(fde)
385
386 #define ev_debug_level          tevent_debug_level
387
388 #define EV_DEBUG_FATAL          TEVENT_DEBUG_FATAL
389 #define EV_DEBUG_ERROR          TEVENT_DEBUG_ERROR
390 #define EV_DEBUG_WARNING        TEVENT_DEBUG_WARNING
391 #define EV_DEBUG_TRACE          TEVENT_DEBUG_TRACE
392
393 #define ev_set_debug(ev, debug, context) \
394         tevent_set_debug(ev, debug, context)
395
396 #define ev_set_debug_stderr(_ev) tevent_set_debug_stderr(ev)
397
398 #endif /* TEVENT_COMPAT_DEFINES */
399
400 #endif /* __TEVENT_H__ */