758bdb4628b44483dd6df0f3de0975e4087453c8
[kai/samba-autobuild/.git] / lib / tevent / tevent_internal.h
1 /* 
2    Unix SMB/CIFS implementation.
3
4    generalised event loop handling
5
6    Internal structs
7
8    Copyright (C) Stefan Metzmacher 2005-2009
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 struct tevent_ops {
29         /* conntext init */
30         int (*context_init)(struct tevent_context *ev);
31
32         /* fd_event functions */
33         struct tevent_fd *(*add_fd)(struct tevent_context *ev,
34                                     TALLOC_CTX *mem_ctx,
35                                     int fd, uint16_t flags,
36                                     tevent_fd_handler_t handler,
37                                     void *private_data,
38                                     const char *handler_name,
39                                     const char *location);
40         void (*set_fd_close_fn)(struct tevent_fd *fde,
41                                 tevent_fd_close_fn_t close_fn);
42         uint16_t (*get_fd_flags)(struct tevent_fd *fde);
43         void (*set_fd_flags)(struct tevent_fd *fde, uint16_t flags);
44
45         /* timed_event functions */
46         struct tevent_timer *(*add_timer)(struct tevent_context *ev,
47                                           TALLOC_CTX *mem_ctx,
48                                           struct timeval next_event,
49                                           tevent_timer_handler_t handler,
50                                           void *private_data,
51                                           const char *handler_name,
52                                           const char *location);
53         /* signal functions */
54         struct tevent_signal *(*add_signal)(struct tevent_context *ev,
55                                             TALLOC_CTX *mem_ctx,
56                                             int signum, int sa_flags,
57                                             tevent_signal_handler_t handler,
58                                             void *private_data,
59                                             const char *handler_name,
60                                             const char *location);
61
62         /* loop functions */
63         int (*loop_once)(struct tevent_context *ev);
64         int (*loop_wait)(struct tevent_context *ev);
65 };
66
67 struct tevent_fd {
68         struct tevent_fd *prev, *next;
69         struct tevent_context *event_ctx;
70         int fd;
71         uint16_t flags; /* see TEVENT_FD_* flags */
72         tevent_fd_handler_t handler;
73         tevent_fd_close_fn_t close_fn;
74         /* this is private for the specific handler */
75         void *private_data;
76         /* this is for debugging only! */
77         const char *handler_name;
78         const char *location;
79         /* this is private for the events_ops implementation */
80         uint16_t additional_flags;
81         void *additional_data;
82 };
83
84 struct tevent_timer {
85         struct tevent_timer *prev, *next;
86         struct tevent_context *event_ctx;
87         struct timeval next_event;
88         tevent_timer_handler_t handler;
89         /* this is private for the specific handler */
90         void *private_data;
91         /* this is for debugging only! */
92         const char *handler_name;
93         const char *location;
94         /* this is private for the events_ops implementation */
95         void *additional_data;
96 };
97
98 struct tevent_signal {
99         struct tevent_signal *prev, *next;
100         struct tevent_context *event_ctx;
101         int signum;
102         int sa_flags;
103         tevent_signal_handler_t handler;
104         /* this is private for the specific handler */
105         void *private_data;
106         /* this is for debugging only! */
107         const char *handler_name;
108         const char *location;
109         /* this is private for the events_ops implementation */
110         void *additional_data;
111 };
112
113 struct tevent_debug_ops {
114         void (*debug)(void *context, enum tevent_debug_level level,
115                       const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
116         void *context;
117 };
118
119 void tevent_debug(struct tevent_context *ev, enum tevent_debug_level level,
120                   const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
121
122 struct tevent_context {
123         /* the specific events implementation */
124         const struct tevent_ops *ops;
125
126         /* list of fd events - used by common code */
127         struct tevent_fd *fd_events;
128
129         /* list of timed events - used by common code */
130         struct tevent_timer *timer_events;
131
132         /* list of signal events - used by common code */
133         struct tevent_signal *signal_events;
134
135         /* this is private for the events_ops implementation */
136         void *additional_data;
137
138         /* pipe hack used with signal handlers */
139         struct tevent_fd *pipe_fde;
140
141         /* debugging operations */
142         struct tevent_debug_ops debug_ops;
143 };
144
145
146 bool tevent_register_backend(const char *name, const struct tevent_ops *ops);
147
148 int tevent_common_context_destructor(struct tevent_context *ev);
149
150 int tevent_common_fd_destructor(struct tevent_fd *fde);
151 struct tevent_fd *tevent_common_add_fd(struct tevent_context *ev,
152                                        TALLOC_CTX *mem_ctx,
153                                        int fd,
154                                        uint16_t flags,
155                                        tevent_fd_handler_t handler,
156                                        void *private_data,
157                                        const char *handler_name,
158                                        const char *location);
159 void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
160                                    tevent_fd_close_fn_t close_fn);
161 uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde);
162 void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
163
164 struct tevent_timer *tevent_common_add_timer(struct tevent_context *ev,
165                                              TALLOC_CTX *mem_ctx,
166                                              struct timeval next_event,
167                                              tevent_timer_handler_t handler,
168                                              void *private_data,
169                                              const char *handler_name,
170                                              const char *location);
171 struct timeval tevent_common_loop_timer_delay(struct tevent_context *);
172
173 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
174                                                TALLOC_CTX *mem_ctx,
175                                                int signum,
176                                                int sa_flags,
177                                                tevent_signal_handler_t handler,
178                                                void *private_data,
179                                                const char *handler_name,
180                                                const char *location);
181 int tevent_common_check_signal(struct tevent_context *ev);
182
183 bool tevent_standard_init(void);
184 bool tevent_select_init(void);
185 #ifdef HAVE_EPOLL
186 bool tevent_epoll_init(void);
187 #endif