3 * $Id: sel.c,v 1.11 2003/05/17 10:34:04 mdw Exp $
5 * I/O multiplexing support
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.11 2003/05/17 10:34:04 mdw
34 * Tidying and bugfixing.
36 * Revision 1.10 2001/06/22 19:35:58 mdw
37 * Fix a large number of bugs.
39 * Revision 1.9 2001/02/03 19:07:08 mdw
40 * Ensure that timers set to go off in the past don't case a problem.
42 * Revision 1.8 2000/03/23 20:42:08 mdw
43 * Rearrange timeout handling to avoid list corruptions.
45 * Revision 1.7 1999/12/11 11:12:17 mdw
46 * Fix comment formatting error.
48 * Revision 1.6 1999/09/26 14:28:11 mdw
49 * (sel_select): Almost pointless efficiency tweak.
51 * Revision 1.5 1999/08/31 17:42:22 mdw
52 * New function `sel_force' to force a descriptor to be `selected'.
54 * Revision 1.4 1999/08/19 18:30:26 mdw
55 * Implement hooks for foreign select-using systems (currently not well
58 * Revision 1.3 1999/05/21 22:13:59 mdw
59 * Use new `tv' macros. Fix ordering bug for timeout selectors.
61 * Revision 1.2 1999/05/15 10:33:32 mdw
62 * Fix copyright notices.
64 * Revision 1.1 1999/05/14 21:01:14 mdw
65 * Integrated `select' handling bits from the background resolver project.
69 /*----- Header files ------------------------------------------------------*/
76 #include <sys/types.h>
84 /*----- Data structures ---------------------------------------------------*/
86 typedef struct sel_pendfile
{
87 struct sel_pendfile
*next
;
91 typedef struct sel_pendtimer
{
92 struct sel_pendtimer
*next
;
96 /*----- Main code ---------------------------------------------------------*/
98 /* --- @sel_init@ --- *
100 * Arguments: @sel_state *s@ = pointer to a state block to initialize
104 * Use: Initializes a select state block.
107 void sel_init(sel_state
*s
)
111 for (i
= 0; i
< SEL_MODES
; i
++) {
120 /* --- @sel_initfile@ --- *
122 * Arguments: @sel_state *s@ = select state to attach to
123 * @sel_file *f@ = pointer to a file block to initialize
124 * @int fd@ = the file descriptor to listen to
125 * @unsigned mode@ = what to listen for
126 * @void (*func)(int fd, unsigned mode, void *p)@ = handler
127 * @void *p@ = argument to pass to handler
131 * Use: Initializes a file block ready for use. The file block
132 * isn't added to the list of things to do until a call to
136 void sel_initfile(sel_state
*s
, sel_file
*f
,
137 int fd
, unsigned mode
,
138 void (*func
)(int /*fd*/, unsigned /*mode*/, void */
*p*/
),
149 /* --- @sel_addfile@ --- *
151 * Arguments: @sel_file *f@ = pointer to a file block
155 * Use: Adds a file block into the list of things to listen to.
158 void sel_addfile(sel_file
*f
)
160 sel_file
**ff
= &f
->s
->files
[f
->mode
];
162 /* --- This little dance looks like line-noise, but it does the job --- */
164 while (*ff
&& (*ff
)->fd
> f
->fd
)
169 (*ff
)->prev
= &f
->next
;
171 FD_SET(f
->fd
, f
->s
->fd
+ f
->mode
);
174 /* --- @sel_force@ --- *
176 * Arguments: @sel_file *f@ = pointer to file selector
180 * Use: Forces a file selector to be considered ready. This is only
181 * useful during a call to @sel_select@. Of particular use is
182 * forcing a write selector when there's something interesting
186 void sel_force(sel_file
*f
)
189 FD_SET(f
->fd
, &f
->s
->args
->fd
[f
->mode
]);
192 /* --- @sel_rmfile@ --- *
194 * Arguments: @sel_file *f@ = pointer to a file block
198 * Use: Removes a file block from the list of things to listen to.
201 void sel_rmfile(sel_file
*f
)
205 f
->next
->prev
= f
->prev
;
206 FD_CLR(f
->fd
, f
->s
->fd
+ f
->mode
);
213 /* --- @sel_addtimer@ --- *
215 * Arguments: @sel_state *s@ = pointer to a state block
216 * @sel_timer *t@ = pointer to a timer block
217 * @struct timeval *tv@ = pointer to time to activate
218 * @void (*func)(struct timeval *tv, void *p)@ = handler
219 * @void *p@ = argument for handler function
223 * Use: Registers and sets up a timer.
226 void sel_addtimer(sel_state
*s
, sel_timer
*t
,
228 void (*func
)(struct timeval */
*tv*/
, void */
*p*/
),
231 sel_timer
**tt
= &s
->timers
;
232 { sel_timer
*q
; for (q
= s
->timers
; q
; q
= q
->next
) assert(q
!= t
); }
234 /* --- Set up the timer block --- */
241 /* --- More line noise --- */
243 while (*tt
&& TV_CMP(&(*tt
)->tv
, <, tv
))
248 (*tt
)->prev
= &t
->next
;
252 /* --- @sel_rmtimer@ --- *
254 * Arguments: @sel_timer *t@ = pointer to timer block
258 * Use: Removes a timer from the list of timers.
261 void sel_rmtimer(sel_timer
*t
)
269 t
->next
->prev
= t
->prev
;
273 /* --- @sel_addhook@ --- *
275 * Arguments: @sel_state *s@ = pointer to state block
276 * @sel_hook *h@ = pointer to hook block
277 * @sel_hookfn before, after@ = hook functions
278 * @void *p@ = pointer argument to pass to hook functions
282 * Use: Registers hook functions to be called on each select call.
285 void sel_addhook(sel_state
*s
, sel_hook
*h
,
286 sel_hookfn before
, sel_hookfn after
,
295 s
->hooks
->prev
= &h
->next
;
299 /* --- @sel_rmhook@ --- *
301 * Arguments: @sel_hook *h@ = pointer to hook block
305 * Use: Removes hook functions.
308 void sel_rmhook(sel_hook
*h
)
311 h
->next
->prev
= h
->prev
;
315 /* --- @sel_fdmerge@ --- *
317 * Arguments: @fd_set *dest@ = destination FD set
318 * @fd_set *fd@ = pointer to set to merge
319 * @int maxfd@ = highest numbered descriptor in @fd@ + 1
321 * Returns: Actual highest numbered descriptor.
323 * Use: Merges file descriptor sets, and returns an accurate @maxfd@
327 int sel_fdmerge(fd_set
*dest
, fd_set
*fd
, int maxfd
)
331 for (i
= 0; i
< maxfd
; i
++) {
332 if (FD_ISSET(i
, fd
)) {
341 /* --- @sel_select@ --- *
343 * Arguments: @sel_state *s@ = pointer to state block
345 * Returns: Zero if all OK, -1 on error.
347 * Use: Does a @select@ call (or equivalent @poll@).
350 int sel_select(sel_state
*s
)
355 /* --- Initialize the argument block --- */
360 for (i
= 0; i
< SEL_MODES
; i
++) {
361 if (s
->files
[i
] && s
->files
[i
]->fd
>= a
.maxfd
)
362 a
.maxfd
= s
->files
[i
]->fd
+ 1;
366 memcpy(a
.fd
, s
->fd
, sizeof(a
.fd
));
367 if (s
->timers
|| s
->hooks
)
368 gettimeofday(&a
.now
, 0);
372 if (TV_CMP(&s
->timers
->tv
, >, &a
.now
))
373 TV_SUB(&a
.tv
, &s
->timers
->tv
, &a
.now
);
382 /* --- Grind through the pre hooks --- */
385 sel_hook
*h
= s
->hooks
;
390 hh
->before(s
, &a
, hh
->p
);
394 /* --- Run the @select@ call --- */
396 if ((err
= select(a
.maxfd
,
397 &a
.fd
[SEL_READ
], &a
.fd
[SEL_WRITE
], &a
.fd
[SEL_EXC
],
404 gettimeofday(&a
.now
, 0);
406 /* --- Run through the hooks again --- */
409 sel_hook
*h
= s
->hooks
;
414 hh
->after(s
, &a
, hh
->p
);
418 /* --- Run through the timers --- */
421 ptimer
*pthead
, *pt
, **ptt
= &pthead
;
424 for (t
= s
->timers
; t
&& TV_CMP(&t
->tv
, <=, &a
.now
); t
= t
->next
) {
434 t
->prev
= &s
->timers
;
445 t
->func(&a
.now
, t
->p
);
451 /* --- And finally run through the files --- *
453 * Do reads first. It's quite possible that a read might prompt a write,
454 * but the other way around is less likely. Fortunately, the modes are
455 * in the right order for this.
461 for (i
= 0; i
< SEL_MODES
; i
++) {
462 pfile
*pfhead
, *pf
, **pff
= &pfhead
;
465 for (f
= s
->files
[i
]; f
; f
= f
->next
) {
466 if (!FD_ISSET(f
->fd
, &a
.fd
[i
]))
481 f
->func(f
->fd
, i
, f
->p
);
491 /*----- That's all, folks -------------------------------------------------*/