/[svn]/branches/newio/include/ioengine.h
ViewVC logotype

Contents of /branches/newio/include/ioengine.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2384 - (show annotations)
Sat Jul 6 18:10:58 2013 UTC (9 years ago) by michael
File MIME type: text/x-csrc
File size: 12453 byte(s)
- Add ioengine.(c|h)

1 #ifndef INCLUDED_ircd_events_h
2 #define INCLUDED_ircd_events_h
3 /*
4 * IRC - Internet Relay Chat, include/ircd_events.h
5 * Copyright (C) 2001 Kevin L. Mitchell <klmitch@mit.edu>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /** @file
23 * @brief Interface and public definitions for event loop.
24 * @version $Id: ioengine.h,v 1.5 2004/10/05 04:14:44 entrope Exp $
25 */
26
27 struct Event;
28
29 /** Generic callback for event activity. */
30 typedef void (*EventCallBack)(struct Event *);
31
32 /** State of a Socket structure. */
33 enum SocketState
34 {
35 SS_CONNECTING, /**< Connection in progress on socket */
36 SS_LISTENING, /**< Socket is a listening socket */
37 SS_CONNECTED, /**< Socket is a connected socket */
38 SS_DATAGRAM, /**< Socket is a datagram socket */
39 SS_CONNECTDG, /**< Socket is a connected datagram socket */
40 SS_NOTSOCK /**< Socket isn't a socket at all */
41 };
42
43 /** Type of a Timer (how its expiration is measured). */
44 enum TimerType
45 {
46 TT_ABSOLUTE, /**< Timer that runs at a specific time */
47 TT_RELATIVE, /**< Timer that runs so many seconds in the future */
48 TT_PERIODIC /**< Timer that runs periodically */
49 };
50
51 /** Type of event that generated a callback. */
52 enum EventType
53 {
54 ET_READ, /**< Readable event detected */
55 ET_WRITE, /**< Writable event detected */
56 ET_ACCEPT, /**< Connection can be accepted */
57 ET_CONNECT, /**< Connection completed */
58 ET_EOF, /**< End-of-file on connection */
59 ET_ERROR, /**< Error condition detected */
60 ET_SIGNAL, /**< A signal was received */
61 ET_EXPIRE, /**< A timer expired */
62 ET_DESTROY /**< The generator is being destroyed */
63 };
64
65 /** Common header for event generators. */
66 struct GenHeader
67 {
68 struct GenHeader* gh_next; /**< Linked list of generators */
69 struct GenHeader** gh_prev_p; /**< Previous pointer to this generator */
70 #ifdef IRCD_THREADED
71 struct GenHeader* gh_qnext; /**< Linked list of generators in queue */
72 struct GenHeader** gh_qprev_p; /**< Previous pointer to this generator */
73 struct Event* gh_head; /**< Head of event queue */
74 struct Event* gh_tail; /**< Tail of event queue */
75 #endif
76 unsigned int gh_flags; /**< Generator flags */
77 unsigned int gh_ref; /**< Reference count */
78 EventCallBack gh_call; /**< Generator callback function */
79 void* gh_data; /**< Extra data */
80
81 union
82 {
83 void* ed_ptr; /**< Engine data as pointer */
84 int ed_int; /**< Engine data as integer */
85 } gh_engdata; /**< Engine data */
86 };
87
88 #define GEN_DESTROY 0x0001 /**< Generator is to be destroyed */
89 #define GEN_MARKED 0x0002 /**< Generator is marked for destruction */
90 #define GEN_ACTIVE 0x0004 /**< Generator is active */
91 #define GEN_READD 0x0008 /**< Generator (timer) must be re-added */
92 #define GEN_ERROR 0x0010 /**< An error occurred on the generator */
93
94 /** Socket event generator.
95 * Note: The socket state overrides the socket event mask; that is, if
96 * it's an SS_CONNECTING socket, the engine selects its own definition
97 * of what that looks like and ignores s_events. s_events is meaningful
98 * only for SS_CONNECTED, SS_DATAGRAM, and SS_CONNECTDG, but may be set
99 * prior to the state transition, if desired.
100 */
101 struct Socket
102 {
103 struct GenHeader s_header; /**< generator information */
104 enum SocketState s_state; /**< state socket's in */
105 unsigned int s_events; /**< events socket is interested in */
106 int s_fd; /**< file descriptor for socket */
107 };
108
109 #define SOCK_EVENT_READABLE 0x0001 /**< interested in readable */
110 #define SOCK_EVENT_WRITABLE 0x0002 /**< interested in writable */
111
112 /** Bitmask of possible event interests for a socket. */
113 #define SOCK_EVENT_MASK (SOCK_EVENT_READABLE | SOCK_EVENT_WRITABLE)
114
115 #define SOCK_ACTION_SET 0x0000 /**< set interest set as follows */
116 #define SOCK_ACTION_ADD 0x1000 /**< add to interest set */
117 #define SOCK_ACTION_DEL 0x2000 /**< remove from interest set */
118
119 #define SOCK_ACTION_MASK 0x3000 /**< mask out the actions */
120
121 /** Retrieve state of the Socket \a sock. */
122 #define s_state(sock) ((sock)->s_state)
123 /** Retrieve interest mask of the Socket \a sock. */
124 #define s_events(sock) ((sock)->s_events)
125 /** Retrieve file descriptor of the Socket \a sock. */
126 #define s_fd(sock) ((sock)->s_fd)
127 /** Retrieve user data pointer of the Socket \a sock. */
128 #define s_data(sock) ((sock)->s_header.gh_data)
129 /** Retrieve engine data integer of the Socket \a sock. */
130 #define s_ed_int(sock) ((sock)->s_header.gh_engdata.ed_int)
131 /** Retrieve engine data pointer of the Socket \a sock. */
132 #define s_ed_ptr(sock) ((sock)->s_header.gh_engdata.ed_ptr)
133 /** Retrieve whether the Socket \a sock is active. */
134 #define s_active(sock) ((sock)->s_header.gh_flags & GEN_ACTIVE)
135
136 /** Signal event generator. */
137 struct Signal
138 {
139 struct GenHeader sig_header; /**< Generator information */
140 int sig_signal; /**< Signal number */
141 };
142
143 /** Retrieve signal number of the Signal \a sig. */
144 #define sig_signal(sig) ((sig)->sig_signal)
145 /** Retrieve user data pointer of the Signal \a sig. */
146 #define sig_data(sig) ((sig)->sig_header.gh_data)
147 /** Retrieve engine data integer of the Signal \a sig. */
148 #define sig_ed_int(sig) ((sig)->sig_header.gh_engdata.ed_int)
149 /** Retrieve engine data pointer of the Signal \a sig. */
150 #define sig_ed_ptr(sig) ((sig)->sig_header.gh_engdata.ed_ptr)
151 /** Retrieve whether the Signal \a sig is active. */
152 #define sig_active(sig) ((sig)->sig_header.gh_flags & GEN_ACTIVE)
153
154 /** Timer event generator. */
155 struct Timer
156 {
157 struct GenHeader t_header; /**< generator information */
158 enum TimerType t_type; /**< what type of timer this is */
159 time_t t_value; /**< value timer was added with */
160 time_t t_expire; /**< time at which timer expires */
161 };
162
163 /** Retrieve type of the Timer \a tim. */
164 #define t_type(tim) ((tim)->t_type)
165 /** Retrieve interval of the Timer \a tim. */
166 #define t_value(tim) ((tim)->t_value)
167 /** Retrieve expiration time of the Timer \a tim. */
168 #define t_expire(tim) ((tim)->t_expire)
169 /** Retrieve user data pointer of the Timer \a tim. */
170 #define t_data(tim) ((tim)->t_header.gh_data)
171 /** Retrieve engine data integer of the Timer \a tim. */
172 #define t_ed_int(tim) ((tim)->t_header.gh_engdata.ed_int)
173 /** Retrieve engine data pointer of the Timer \a tim. */
174 #define t_ed_ptr(tim) ((tim)->t_header.gh_engdata.ed_ptr)
175 /** Retrieve whether the Timer \a tim is active. */
176 #define t_active(tim) ((tim)->t_header.gh_flags & GEN_ACTIVE)
177 /** Retrieve whether the Timer \a tim is enqueued. */
178 #define t_onqueue(tim) ((tim)->t_header.gh_prev_p)
179
180 /** Event activity descriptor. */
181 struct Event
182 {
183 struct Event* ev_next; /**< linked list of events on queue */
184 struct Event** ev_prev_p; /**< previous pointer to this event */
185 enum EventType ev_type; /**< Event type */
186 int ev_data; /**< extra data, like errno value */
187
188 union
189 {
190 struct GenHeader* gen_header; /**< Generator header */
191 struct Socket* gen_socket; /**< Socket generating event */
192 struct Signal* gen_signal; /**< Signal generating event */
193 struct Timer* gen_timer; /**< Timer generating event */
194 } ev_gen; /**< Object generating event */
195 };
196
197 /** Retrieve the type of the Event \a ev. */
198 #define ev_type(ev) ((ev)->ev_type)
199 /** Retrieve the extra data of the Event \a ev. */
200 #define ev_data(ev) ((ev)->ev_data)
201 /** Retrieve the Socket that generated the Event \a ev. */
202 #define ev_socket(ev) ((ev)->ev_gen.gen_socket)
203 /** Retrieve the Signal that generated the Event \a ev. */
204 #define ev_signal(ev) ((ev)->ev_gen.gen_signal)
205 /** Retrieve the Timer that generated the Event \a ev. */
206 #define ev_timer(ev) ((ev)->ev_gen.gen_timer)
207
208 /** List of all event generators. */
209 struct Generators
210 {
211 struct Socket* g_socket; /**< List of socket generators */
212 struct Signal* g_signal; /**< List of signal generators */
213 struct Timer* g_timer; /**< List of timer generators */
214 };
215
216 /** Returns 1 if successfully initialized, 0 if not.
217 * @param[in] max_sockets Number of sockets to support.
218 */
219 typedef int (*EngineInit)(int);
220
221 /** Tell engine about new signal.
222 * @param[in] sig Signal event generator to add.
223 */
224 typedef void (*EngineSignal)(struct Signal *);
225
226 /** Tell engine about new socket.
227 * @param[in] sock Socket event generator to add.
228 */
229 typedef int (*EngineAdd)(struct Socket *);
230
231 /** Tell engine about socket's new_state.
232 * @param[in] sock Socket whose state is changing.
233 * @param[in] new_state New state for socket.
234 */
235 typedef void (*EngineState)(struct Socket *, enum SocketState);
236
237 /** Tell engine about socket's new event interests.
238 * @param[in] sock Socket whose interest mask is changing.
239 * @param[in] new_events New event mask to set (not SOCK_ACTION_ADD or SOCK_ACTION_DEL).
240 */
241 typedef void (*EngineEvents)(struct Socket *, unsigned int);
242
243 /** Tell engine a socket is going away.
244 * @param[in] sock Socket being destroyed.
245 */
246 typedef void (*EngineDelete)(struct Socket *);
247
248 /** The actual event loop.
249 * @param[in] gens List of event generators.
250 */
251 typedef void (*EngineLoop)(struct Generators *);
252
253 /** Structure for an event engine to describe itself. */
254 struct Engine
255 {
256 const char* eng_name; /**< a name for the engine */
257 EngineInit eng_init; /**< initialize engine */
258 EngineSignal eng_signal; /**< express interest in a signal (may be NULL) */
259 EngineAdd eng_add; /**< express interest in a socket */
260 EngineState eng_state; /**< mention a change in state to engine */
261 EngineEvents eng_events; /**< express interest in socket events */
262 EngineDelete eng_closing; /**< socket is being closed */
263 EngineLoop eng_loop; /**< actual event loop */
264 };
265
266 /** Increment the reference count of \a gen. */
267 #define gen_ref_inc(gen) (((struct GenHeader*) (gen))->gh_ref++)
268 /** Decrement the reference count of \a gen. */
269 #define gen_ref_dec(gen) \
270 do { \
271 struct GenHeader* _gen = (struct GenHeader*) (gen); \
272 if (!--_gen->gh_ref && (_gen->gh_flags & GEN_DESTROY)) { \
273 gen_dequeue(_gen); \
274 event_generate(ET_DESTROY, _gen, 0); \
275 } \
276 } while (0)
277 /** Clear the error flag for \a gen. */
278 #define gen_clear_error(gen) \
279 (((struct GenHeader*) (gen))->gh_flags &= ~GEN_ERROR)
280
281 extern void gen_dequeue(void *);
282
283 extern void event_init(int);
284 extern void event_loop(void);
285 extern void event_generate(enum EventType, void *, int);
286
287 extern struct Timer *timer_init(struct Timer *);
288 extern void timer_add(struct Timer *, EventCallBack, void *, enum TimerType, time_t);
289 extern void timer_del(struct Timer *);
290 extern void timer_chg(struct Timer *, enum TimerType, time_t);
291 extern void timer_run(void);
292 /** Retrieve the next timer's expiration time from Generators \a gen. */
293 #define timer_next(gen) ((gen)->g_timer ? (gen)->g_timer->t_expire : 0)
294
295 extern void signal_add(struct Signal *, EventCallBack, void *, int);
296 extern int socket_add(struct Socket *, EventCallBack, void *, enum SocketState, unsigned int, int);
297 extern void socket_del(struct Socket *);
298 extern void socket_state(struct Socket *, enum SocketState);
299 extern void socket_events(struct Socket *, unsigned int);
300
301 extern const char *engine_name(void);
302
303 /* These routines pretty-print names for states and types for debug printing */
304 extern const char *state_to_name(enum SocketState);
305 extern const char *timer_to_name(enum TimerType);
306 extern const char *event_to_name(enum EventType);
307 extern const char *gen_flags(unsigned int);
308 extern const char *sock_flags(unsigned int);
309
310 #endif /* INCLUDED_ircd_events_h */

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28