lib/tevent: add tevent_req infrastructure
[ira/wip.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
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 struct tevent_ops {
25         /* conntext init */
26         int (*context_init)(struct tevent_context *ev);
27
28         /* fd_event functions */
29         struct tevent_fd *(*add_fd)(struct tevent_context *ev,
30                                     TALLOC_CTX *mem_ctx,
31                                     int fd, uint16_t flags,
32                                     tevent_fd_handler_t handler,
33                                     void *private_data,
34                                     const char *handler_name,
35                                     const char *location);
36         void (*set_fd_close_fn)(struct tevent_fd *fde,
37                                 tevent_fd_close_fn_t close_fn);
38         uint16_t (*get_fd_flags)(struct tevent_fd *fde);
39         void (*set_fd_flags)(struct tevent_fd *fde, uint16_t flags);
40
41         /* timed_event functions */
42         struct tevent_timer *(*add_timer)(struct tevent_context *ev,
43                                           TALLOC_CTX *mem_ctx,
44                                           struct timeval next_event,
45                                           tevent_timer_handler_t handler,
46                                           void *private_data,
47                                           const char *handler_name,
48                                           const char *location);
49         /* signal functions */
50         struct tevent_signal *(*add_signal)(struct tevent_context *ev,
51                                             TALLOC_CTX *mem_ctx,
52                                             int signum, int sa_flags,
53                                             tevent_signal_handler_t handler,
54                                             void *private_data,
55                                             const char *handler_name,
56                                             const char *location);
57
58         /* loop functions */
59         int (*loop_once)(struct tevent_context *ev);
60         int (*loop_wait)(struct tevent_context *ev);
61 };
62
63 struct tevent_fd {
64         struct tevent_fd *prev, *next;
65         struct tevent_context *event_ctx;
66         int fd;
67         uint16_t flags; /* see TEVENT_FD_* flags */
68         tevent_fd_handler_t handler;
69         tevent_fd_close_fn_t close_fn;
70         /* this is private for the specific handler */
71         void *private_data;
72         /* this is for debugging only! */
73         const char *handler_name;
74         const char *location;
75         /* this is private for the events_ops implementation */
76         uint16_t additional_flags;
77         void *additional_data;
78 };
79
80 struct tevent_timer {
81         struct tevent_timer *prev, *next;
82         struct tevent_context *event_ctx;
83         struct timeval next_event;
84         tevent_timer_handler_t handler;
85         /* this is private for the specific handler */
86         void *private_data;
87         /* this is for debugging only! */
88         const char *handler_name;
89         const char *location;
90         /* this is private for the events_ops implementation */
91         void *additional_data;
92 };
93
94 struct tevent_signal {
95         struct tevent_signal *prev, *next;
96         struct tevent_context *event_ctx;
97         int signum;
98         int sa_flags;
99         tevent_signal_handler_t handler;
100         /* this is private for the specific handler */
101         void *private_data;
102         /* this is for debugging only! */
103         const char *handler_name;
104         const char *location;
105         /* this is private for the events_ops implementation */
106         void *additional_data;
107 };
108
109 struct tevent_debug_ops {
110         void (*debug)(void *context, enum tevent_debug_level level,
111                       const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
112         void *context;
113 };
114
115 void tevent_debug(struct tevent_context *ev, enum tevent_debug_level level,
116                   const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
117
118 struct tevent_context {
119         /* the specific events implementation */
120         const struct tevent_ops *ops;
121
122         /* list of fd events - used by common code */
123         struct tevent_fd *fd_events;
124
125         /* list of timed events - used by common code */
126         struct tevent_timer *timer_events;
127
128         /* list of signal events - used by common code */
129         struct tevent_signal *signal_events;
130
131         /* this is private for the events_ops implementation */
132         void *additional_data;
133
134         /* pipe hack used with signal handlers */
135         struct tevent_fd *pipe_fde;
136
137         /* debugging operations */
138         struct tevent_debug_ops debug_ops;
139 };
140
141
142 bool tevent_register_backend(const char *name, const struct tevent_ops *ops);
143
144 int tevent_common_context_destructor(struct tevent_context *ev);
145
146 int tevent_common_fd_destructor(struct tevent_fd *fde);
147 struct tevent_fd *tevent_common_add_fd(struct tevent_context *ev,
148                                        TALLOC_CTX *mem_ctx,
149                                        int fd,
150                                        uint16_t flags,
151                                        tevent_fd_handler_t handler,
152                                        void *private_data,
153                                        const char *handler_name,
154                                        const char *location);
155 void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
156                                    tevent_fd_close_fn_t close_fn);
157 uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde);
158 void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags);
159
160 struct timeval ev_timeval_zero(void);
161 bool ev_timeval_is_zero(const struct timeval *tv);
162 struct tevent_timer *tevent_common_add_timer(struct tevent_context *ev,
163                                              TALLOC_CTX *mem_ctx,
164                                              struct timeval next_event,
165                                              tevent_timer_handler_t handler,
166                                              void *private_data,
167                                              const char *handler_name,
168                                              const char *location);
169 struct timeval tevent_common_loop_timer_delay(struct tevent_context *);
170
171 struct tevent_signal *tevent_common_add_signal(struct tevent_context *ev,
172                                                TALLOC_CTX *mem_ctx,
173                                                int signum,
174                                                int sa_flags,
175                                                tevent_signal_handler_t handler,
176                                                void *private_data,
177                                                const char *handler_name,
178                                                const char *location);
179 int tevent_common_check_signal(struct tevent_context *ev);
180
181 bool tevent_standard_init(void);
182 bool tevent_select_init(void);
183 #ifdef HAVE_EPOLL
184 bool tevent_epoll_init(void);
185 #endif