3 * $Id: sel.c,v 1.3 1999/05/21 22:13:59 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.3 1999/05/21 22:13:59 mdw
34 * Use new `tv' macros. Fix ordering bug for timeout selectors.
36 * Revision 1.2 1999/05/15 10:33:32 mdw
37 * Fix copyright notices.
39 * Revision 1.1 1999/05/14 21:01:14 mdw
40 * Integrated `select' handling bits from the background resolver project.
44 /*----- Header files ------------------------------------------------------*/
50 #include <sys/types.h>
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 FD_ZERO(&s->fd[SEL_READ]);
73 FD_ZERO(&s->fd[SEL_WRITE]);
74 FD_ZERO(&s->fd[SEL_EXC]);
77 /* --- @sel_initfile@ --- *
79 * Arguments: @sel_state *s@ = select state to attach to
80 * @sel_file *f@ = pointer to a file block to initialize
81 * @int fd@ = the file descriptor to listen to
82 * @unsigned mode@ = what to listen for
83 * @void (*func)(int fd, unsigned mode, void *p)@ = handler
84 * @void *p@ = argument to pass to handler
88 * Use: Initializes a file block ready for use. The file block
89 * isn't added to the list of things to do until a call to
93 void sel_initfile(sel_state *s, sel_file *f,
94 int fd, unsigned mode,
95 void (*func)(int /*fd*/, unsigned /*mode*/, void */*p*/),
105 /* --- @sel_addfile@ --- *
107 * Arguments: @sel_file *f@ = pointer to a file block
111 * Use: Adds a file block into the list of things to listen to.
114 void sel_addfile(sel_file *f)
116 sel_file **ff = &f->s->files;
118 /* --- This little dance looks like line-noise, but it does the job --- */
120 while (*ff && (*ff)->fd > f->fd)
123 f->prev = (sel_file *)ff;
127 FD_SET(f->fd, f->s->fd + f->mode);
130 /* --- @sel_rmfile@ --- *
132 * Arguments: @sel_file *f@ = pointer to a file block
136 * Use: Removes a file block from the list of things to listen to.
139 void sel_rmfile(sel_file *f)
141 f->prev->next = f->next;
143 f->next->prev = f->prev;
144 FD_CLR(f->fd, f->s->fd + f->mode);
147 /* --- @sel_addtimer@ --- *
149 * Arguments: @sel_state *s@ = pointer to a state block
150 * @sel_timer *t@ = pointer to a timer block
151 * @struct timeval *tv@ = pointer to time to activate
152 * @void (*func)(struct timeval *tv, void *p)@ = handler
153 * @void *p@ = argument for handler function
157 * Use: Registers and sets up a timer.
160 void sel_addtimer(sel_state *s, sel_timer *t,
162 void (*func)(struct timeval */*tv*/, void */*p*/),
165 sel_timer **tt = &s->timers;
167 /* --- Set up the timer block --- */
173 /* --- More line noise --- */
175 while (*tt && TV_CMP(&(*tt)->tv, <, tv))
178 t->prev = (sel_timer *)tt;
184 /* --- @sel_rmtimer@ --- *
186 * Arguments: @sel_timer *t@ = pointer to timer block
190 * Use: Removes a timer from the list of timers.
193 void sel_rmtimer(sel_timer *t)
195 t->prev->next = t->next;
197 t->next->prev = t->prev;
200 /* --- @sel_select@ --- *
202 * Arguments: @sel_state *s@ = pointer to state block
204 * Returns: Zero if all OK, -1 on error.
206 * Use: Does a @select@ call (or equivalent @poll@).
209 int sel_select(sel_state *s)
211 fd_set fd[SEL_MODES];
215 memcpy(fd, s->fd, sizeof(s->fd));
218 gettimeofday(&now, 0);
219 TV_SUB(&tv, &s->timers->tv, &now);
220 err = select(s->files ? s->files->fd + 1 : 0,
221 fd + SEL_READ, fd + SEL_WRITE, fd + SEL_EXC,
223 gettimeofday(&tv, 0);
225 err = select(s->files ? s->files->fd + 1 : 0,
226 fd + SEL_READ, fd + SEL_WRITE, fd + SEL_EXC,
234 for (t = s->timers; t && TV_CMP(&t->tv, <=, &tv); t = tt) {
236 t->next = t->prev = t;
241 t->prev = (sel_timer *)&s->timers;
246 for (f = s->files; f; f = ff) {
248 if (FD_ISSET(f->fd, fd + f->mode))
249 f->func(f->fd, f->mode, f->p);
256 /*----- That's all, folks -------------------------------------------------*/