3 * $Id: sel.c,v 1.13 2004/04/08 01:36:13 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 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
45 /*----- Data structures ---------------------------------------------------*/
47 typedef struct sel_pendfile
{
48 struct sel_pendfile
*next
;
52 typedef struct sel_pendtimer
{
53 struct sel_pendtimer
*next
;
57 /*----- Main code ---------------------------------------------------------*/
59 /* --- @sel_init@ --- *
61 * Arguments: @sel_state *s@ = pointer to a state block to initialize
65 * Use: Initializes a select state block.
68 void sel_init(sel_state
*s
)
72 for (i
= 0; i
< SEL_MODES
; i
++) {
81 /* --- @sel_initfile@ --- *
83 * Arguments: @sel_state *s@ = select state to attach to
84 * @sel_file *f@ = pointer to a file block to initialize
85 * @int fd@ = the file descriptor to listen to
86 * @unsigned mode@ = what to listen for
87 * @void (*func)(int fd, unsigned mode, void *p)@ = handler
88 * @void *p@ = argument to pass to handler
92 * Use: Initializes a file block ready for use. The file block
93 * isn't added to the list of things to do until a call to
97 void sel_initfile(sel_state
*s
, sel_file
*f
,
98 int fd
, unsigned mode
,
99 void (*func
)(int /*fd*/, unsigned /*mode*/, void */
*p*/
),
110 /* --- @sel_addfile@ --- *
112 * Arguments: @sel_file *f@ = pointer to a file block
116 * Use: Adds a file block into the list of things to listen to.
119 void sel_addfile(sel_file
*f
)
121 sel_file
**ff
= &f
->s
->files
[f
->mode
];
123 /* --- This little dance looks like line-noise, but it does the job --- */
125 while (*ff
&& (*ff
)->fd
> f
->fd
)
130 (*ff
)->prev
= &f
->next
;
132 FD_SET(f
->fd
, f
->s
->fd
+ f
->mode
);
135 /* --- @sel_force@ --- *
137 * Arguments: @sel_file *f@ = pointer to file selector
141 * Use: Forces a file selector to be considered ready. This is only
142 * useful during a call to @sel_select@. Of particular use is
143 * forcing a write selector when there's something interesting
147 void sel_force(sel_file
*f
)
150 FD_SET(f
->fd
, &f
->s
->args
->fd
[f
->mode
]);
153 /* --- @sel_rmfile@ --- *
155 * Arguments: @sel_file *f@ = pointer to a file block
159 * Use: Removes a file block from the list of things to listen to.
162 void sel_rmfile(sel_file
*f
)
166 f
->next
->prev
= f
->prev
;
167 FD_CLR(f
->fd
, f
->s
->fd
+ f
->mode
);
174 /* --- @sel_addtimer@ --- *
176 * Arguments: @sel_state *s@ = pointer to a state block
177 * @sel_timer *t@ = pointer to a timer block
178 * @struct timeval *tv@ = pointer to time to activate
179 * @void (*func)(struct timeval *tv, void *p)@ = handler
180 * @void *p@ = argument for handler function
184 * Use: Registers and sets up a timer.
187 void sel_addtimer(sel_state
*s
, sel_timer
*t
,
189 void (*func
)(struct timeval */
*tv*/
, void */
*p*/
),
192 sel_timer
**tt
= &s
->timers
;
193 { sel_timer
*q
; for (q
= s
->timers
; q
; q
= q
->next
) assert(q
!= t
); }
195 /* --- Set up the timer block --- */
202 /* --- More line noise --- */
204 while (*tt
&& TV_CMP(&(*tt
)->tv
, <, tv
))
209 (*tt
)->prev
= &t
->next
;
213 /* --- @sel_rmtimer@ --- *
215 * Arguments: @sel_timer *t@ = pointer to timer block
219 * Use: Removes a timer from the list of timers.
222 void sel_rmtimer(sel_timer
*t
)
230 t
->next
->prev
= t
->prev
;
234 /* --- @sel_addhook@ --- *
236 * Arguments: @sel_state *s@ = pointer to state block
237 * @sel_hook *h@ = pointer to hook block
238 * @sel_hookfn before, after@ = hook functions
239 * @void *p@ = pointer argument to pass to hook functions
243 * Use: Registers hook functions to be called on each select call.
246 void sel_addhook(sel_state
*s
, sel_hook
*h
,
247 sel_hookfn before
, sel_hookfn after
,
256 s
->hooks
->prev
= &h
->next
;
260 /* --- @sel_rmhook@ --- *
262 * Arguments: @sel_hook *h@ = pointer to hook block
266 * Use: Removes hook functions.
269 void sel_rmhook(sel_hook
*h
)
272 h
->next
->prev
= h
->prev
;
276 /* --- @sel_fdmerge@ --- *
278 * Arguments: @fd_set *dest@ = destination FD set
279 * @fd_set *fd@ = pointer to set to merge
280 * @int maxfd@ = highest numbered descriptor in @fd@ + 1
282 * Returns: Actual highest numbered descriptor.
284 * Use: Merges file descriptor sets, and returns an accurate @maxfd@
288 int sel_fdmerge(fd_set
*dest
, fd_set
*fd
, int maxfd
)
292 for (i
= 0; i
< maxfd
; i
++) {
293 if (FD_ISSET(i
, fd
)) {
302 /* --- @sel_select@ --- *
304 * Arguments: @sel_state *s@ = pointer to state block
306 * Returns: Zero if all OK, -1 on error.
308 * Use: Does a @select@ call (or equivalent @poll@).
311 int sel_select(sel_state
*s
)
316 /* --- Initialize the argument block --- */
321 for (i
= 0; i
< SEL_MODES
; i
++) {
322 if (s
->files
[i
] && s
->files
[i
]->fd
>= a
.maxfd
)
323 a
.maxfd
= s
->files
[i
]->fd
+ 1;
327 memcpy(a
.fd
, s
->fd
, sizeof(a
.fd
));
328 if (s
->timers
|| s
->hooks
)
329 gettimeofday(&a
.now
, 0);
333 if (TV_CMP(&s
->timers
->tv
, >, &a
.now
))
334 TV_SUB(&a
.tv
, &s
->timers
->tv
, &a
.now
);
343 /* --- Grind through the pre hooks --- */
346 sel_hook
*h
= s
->hooks
;
351 hh
->before(s
, &a
, hh
->p
);
355 /* --- Run the @select@ call --- */
357 if ((err
= select(a
.maxfd
,
358 &a
.fd
[SEL_READ
], &a
.fd
[SEL_WRITE
], &a
.fd
[SEL_EXC
],
365 gettimeofday(&a
.now
, 0);
367 /* --- Run through the hooks again --- */
370 sel_hook
*h
= s
->hooks
;
375 hh
->after(s
, &a
, hh
->p
);
379 /* --- Run through the timers --- */
382 ptimer
*pthead
, *pt
, **ptt
= &pthead
;
385 for (t
= s
->timers
; t
&& TV_CMP(&t
->tv
, <=, &a
.now
); t
= t
->next
) {
395 t
->prev
= &s
->timers
;
406 t
->func(&a
.now
, t
->p
);
412 /* --- And finally run through the files --- *
414 * Do reads first. It's quite possible that a read might prompt a write,
415 * but the other way around is less likely. Fortunately, the modes are
416 * in the right order for this.
422 for (i
= 0; i
< SEL_MODES
; i
++) {
423 pfile
*pfhead
, *pf
, **pff
= &pfhead
;
426 for (f
= s
->files
[i
]; f
; f
= f
->next
) {
427 if (!FD_ISSET(f
->fd
, &a
.fd
[i
]))
442 f
->func(f
->fd
, i
, f
->p
);
453 /*----- That's all, folks -------------------------------------------------*/