97f65b00 |
1 | /* -*-c-*- |
2 | * |
e32dabae |
3 | * $Id: sel.h,v 1.3 1999/05/17 20:36:36 mdw Exp $ |
97f65b00 |
4 | * |
5 | * I/O multiplexing support |
6 | * |
a3ddb778 |
7 | * (c) 1999 Straylight/Edgeware |
97f65b00 |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of the mLib utilities library. |
13 | * |
14 | * mLib is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * mLib 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 |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with mLib; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: sel.h,v $ |
e32dabae |
33 | * Revision 1.3 1999/05/17 20:36:36 mdw |
34 | * Make the selector type symbols an enumeration rather than a bunch of |
35 | * #defines. |
36 | * |
a3ddb778 |
37 | * Revision 1.2 1999/05/15 10:33:32 mdw |
38 | * Fix copyright notices. |
39 | * |
97f65b00 |
40 | * Revision 1.1 1999/05/14 21:01:15 mdw |
41 | * Integrated `select' handling bits from the background resolver project. |
42 | * |
43 | */ |
44 | |
45 | #ifndef SEL_H |
46 | #define SEL_H |
47 | |
48 | #ifdef __cplusplus |
49 | extern "C" { |
50 | #endif |
51 | |
52 | /*----- Theory lesson -----------------------------------------------------* |
53 | * |
54 | * Things which are expecting to do I/O or go off at a certain time are |
55 | * called `selectors'. There are two types of selectors: `file selectors' |
56 | * wait patiently for a file to become readable or writable; `timeout |
57 | * selectors' wait for a certain amount of time to elapse. There is also a |
58 | * `multiplexor' which copes with managing all of this stuff. |
59 | * |
60 | * Multiplexors aren't actually very interesting. You initialize them with |
61 | * @sel_init@, and then add and remove selectors as you go. When you want to |
62 | * wait for something to happen, call @sel_select@. |
63 | * |
64 | * A file selector can *either* read *or* write. It can't do both. This is |
65 | * because you quite often want to read a socket but not write it; during |
66 | * those times you don't want to write, you just don't install a write |
67 | * selector. |
68 | * |
69 | * File selectors are called when their files are available for reading or |
70 | * writing as appropriate, and given their file descriptor, the state of the |
71 | * file, and a pointer that was registered along with the selector. |
72 | * |
73 | * File selectors are set up in two phases. First, they're `initialized' |
74 | * with @sel_initfile@. An initialized file selector doesn't do anything. |
75 | * It needs to be added to a multiplexor using `sel_addfile'. It can later |
76 | * be removed using `sel_rmfile'. You can carry on adding and removing as |
77 | * you wish. Just don't try adding it twice in a row. |
78 | * |
79 | * Timeout selectors are called at a certain time. (Actually, they're called |
80 | * *after* a certain time.) There's no separate initialization step with |
81 | * timouts: you just add them and they work. If you decide you don't want a |
82 | * timeout to go off, then you just remove it. (Adding and removing the |
83 | * *same* timeout isn't something you do very often. You usually use a |
84 | * different expiry time each time.) |
85 | */ |
86 | |
87 | /*----- Header files ------------------------------------------------------*/ |
88 | |
89 | #include <sys/types.h> |
90 | #include <sys/time.h> |
91 | #include <unistd.h> |
92 | |
93 | /*----- Data structures ---------------------------------------------------*/ |
94 | |
95 | /* --- Listening for a file --- */ |
96 | |
97 | typedef struct sel_file { |
98 | struct sel_file *next; |
99 | struct sel_file *prev; |
100 | struct sel_state *s; |
101 | int fd; |
102 | unsigned mode; |
103 | void (*func)(int /*fd*/, unsigned /*mode*/, void */*p*/); |
104 | void *p; |
105 | } sel_file; |
106 | |
e32dabae |
107 | enum { |
108 | SEL_READ, |
109 | SEL_WRITE, |
110 | SEL_EXC, |
111 | SEL_MODES |
112 | }; |
97f65b00 |
113 | |
114 | /* --- Waiting for a timeout --- */ |
115 | |
116 | typedef struct sel_timer { |
117 | struct sel_timer *next; |
118 | struct sel_timer *prev; |
119 | struct timeval tv; |
120 | void (*func)(struct timeval *tv, void *p); |
121 | void *p; |
122 | } sel_timer; |
123 | |
124 | /* --- A multiplexer --- * |
125 | * |
126 | * The files are sorted in reverse order of file descriptor number; the |
127 | * timers are in normal order of occurrence. Thus, the interesting one |
128 | * is always at the front of the list. |
129 | */ |
130 | |
131 | typedef struct sel_state { |
132 | struct sel_file *files; |
133 | struct sel_timer *timers; |
134 | fd_set fd[SEL_MODES]; |
135 | struct timeval tv; |
136 | } sel_state; |
137 | |
138 | /*----- Functions provided ------------------------------------------------*/ |
139 | |
140 | /* --- @sel_init@ --- * |
141 | * |
142 | * Arguments: @sel_state *s@ = pointer to a state block to initialize |
143 | * |
144 | * Returns: --- |
145 | * |
146 | * Use: Initializes a select state block. |
147 | */ |
148 | |
149 | extern void sel_init(sel_state */*s*/); |
150 | |
151 | /* --- @sel_initfile@ --- * |
152 | * |
153 | * Arguments: @sel_state *s@ = select state to attach to |
154 | * @sel_file *f@ = pointer to a file block to initialize |
155 | * @int fd@ = the file descriptor to listen to |
156 | * @unsigned mode@ = what to listen for |
157 | * @void (*func)(int fd, unsigned mode, void *p)@ = handler |
158 | * @void *p@ = argument to pass to handler |
159 | * |
160 | * Returns: --- |
161 | * |
162 | * Use: Initializes a file block ready for use. The file block |
163 | * isn't added to the list of things to do until a call to |
164 | * @sel_addfile@. |
165 | */ |
166 | |
167 | extern void sel_initfile(sel_state */*s*/, sel_file */*f*/, |
168 | int /*fd*/, unsigned /*mode*/, |
169 | void (*/*func*/)(int /*fd*/, |
170 | unsigned /*mode*/, |
171 | void */*p*/), |
172 | void */*p*/); |
173 | |
174 | /* --- @sel_addfile@ --- * |
175 | * |
176 | * Arguments: @sel_file *f@ = pointer to a file block |
177 | * |
178 | * Returns: --- |
179 | * |
180 | * Use: Adds a file block into the list of things to listen to. |
181 | */ |
182 | |
183 | extern void sel_addfile(sel_file */*f*/); |
184 | |
185 | /* --- @sel_rmfile@ --- * |
186 | * |
187 | * Arguments: @sel_file *f@ = pointer to a file block |
188 | * |
189 | * Returns: --- |
190 | * |
191 | * Use: Removes a file block from the list of things to listen to. |
192 | */ |
193 | |
194 | extern void sel_rmfile(sel_file */*f*/); |
195 | |
196 | /* --- @sel_addtimer@ --- * |
197 | * |
198 | * Arguments: @sel_state *s@ = pointer to a state block |
199 | * @sel_timer *t@ = pointer to a timer block |
200 | * @struct timeval *tv@ = pointer to time to activate |
201 | * @void (*func)(struct timeval *tv, void *p)@ = handler |
202 | * @void *p@ = argument for handler function |
203 | * |
204 | * Returns: --- |
205 | * |
206 | * Use: Registers and sets up a timer. |
207 | */ |
208 | |
209 | extern void sel_addtimer(sel_state */*s*/, sel_timer */*t*/, |
210 | struct timeval */*tv*/, |
211 | void (*/*func*/)(struct timeval */*tv*/, |
212 | void */*p*/), |
213 | void */*p*/); |
214 | |
215 | /* --- @sel_rmtimer@ --- * |
216 | * |
217 | * Arguments: @sel_timer *t@ = pointer to timer block |
218 | * |
219 | * Returns: --- |
220 | * |
221 | * Use: Removes a timer from the list of timers. |
222 | */ |
223 | |
224 | extern void sel_rmtimer(sel_timer */*t*/); |
225 | |
226 | /* --- @sel_select@ --- * |
227 | * |
228 | * Arguments: @sel_state *s@ = pointer to state block |
229 | * |
230 | * Returns: Zero if all OK, -1 on error. |
231 | * |
232 | * Use: Does a @select@ call (or equivalent @poll@). |
233 | */ |
234 | |
235 | extern int sel_select(sel_state */*s*/); |
236 | |
237 | /*----- That's all, folks -------------------------------------------------*/ |
238 | |
239 | #ifdef __cplusplus |
240 | } |
241 | #endif |
242 | |
243 | #endif |